Make code refactoring (Java 8): import com.fasterxml.jackson.databind.ObjectMapper;
import com.hannat.dashboard.dal.domain.incident.Incident;
import com.hannat.dashboard.dal.ims.JiraAdditionParams;
import com.hannat.dashboard.ims.jira.data.JiraDbSettings;
import com.hannat.dashboard.ims.jira.data.JiraDestSettings;
import com.hannat.dashboard.dal.ims.JiraUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.http.;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.;

@Slf4j
public class JiraRestConnector {

private final RestTemplate restTemplate;
private final ObjectMapper mapper;

public JiraRestConnector (ObjectMapper mapper) {
this(new RestTemplate(), mapper);
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName(“UTF-8”)));
}

public JiraRestConnector(RestTemplate restTemplate, ObjectMapper mapper) {
this.restTemplate = restTemplate;
this.mapper = mapper;
}

public String testConnect(JiraDestSettings dest) {
String url = createURL(dest.getHost(), dest.getPort(), “/rest/api/latest/serverInfo”);
HashMap<String, Object> map = restTemplate.getForObject(url, HashMap.class);
return String.valueOf(map.get(“serverTitle”));
}

public Object checkUserRights(JiraDbSettings db, JiraUser user) {
List<String> permissions = getUserPermission(db, user);
List<String> references = getReferencePermission();

List<String> unavailablePermissions = new ArrayList<>();

for(String reference : references) {
if (!permissions.contains(reference)) {
unavailablePermissions.add(reference);
}
}
String info;

if (unavailablePermissions.isEmpty()) {
info = “User has all rights”;
} else {
info = “User has not all rights”;
}

HashMap<String, Object> rights = new HashMap<>();

rights.put(“info”, info);
rights.put(“unavailablePermissions”, unavailablePermissions);

return rights;

}

public Object getData(JiraDbSettings db, JiraUser user) {
List<HashMap<String, Object>> list = getListFromResponse(db, user, “/rest/api/latest/project”);
String[] fields = {“key”, “name”};
List<HashMap<String, Object>> projects = setResponseFields(list, fields);
for (HashMap<String, Object> project : projects) {
project.put(“assignee”, getAssignee(db, user, (String) project.get(“key”)));
project.put(“issues”, getIssues(db, user, (String) project.get(“key”)));
}
return projects;
}

public String createTask(JiraDbSettings db, JiraUser user, Incident incident, JiraAdditionParams params) {
String url = createURL(db.getHost(), db.getPort(), “/rest/api/2/issue”);
HttpHeaders auth = createPostHeaders(user);

HashMap<String, Object> fields = new HashMap<>();

fields.put(“project”, objectForMap(“key”, params.getProject()));
fields.put(“summary”, incident.getTitle());
fields.put(“issuetype”, objectForMap(“id”, params.getIssueType()));

if (incident.getDescription() != null) {
fields.put(“description”, incident.getDescription());
}

if (params.getAssignee() != null) {
fields.put(“assignee”, objectForMap(“name”, params.getAssignee()));
}

if (incident.getDueDate() != null && checkDueDate(db, user, params.getProject(), params.getIssueType())) {
DateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);
String date = formatter.format(incident.getDueDate());
fields.put(“duedate”, date);
}

if (params.getIssueType().equals(“10000”)) {
fields.put(“customfield_10103”, “Platform”);
}

HashMap<String, Object> request = new HashMap<>();
request.put(“fields”, fields);

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, auth);
HashMap<String, String> response = restTemplate.exchange(url, HttpMethod.POST, entity, HashMap.class).getBody();

return response.get(“key”);
}

public void updateTask(JiraDbSettings db, JiraUser user, Incident incident, String taskId) {
String url = createURL(db.getHost(), db.getPort(), “/rest/api/2/issue/” + taskId);
HttpHeaders auth = createPostHeaders(user);

HashMap<String, Object> update = new HashMap<>();
update.put(“summary”, getSetObject(incident.getTitle()));

if (incident.getDescription() != null) {
update.put(“description”, getSetObject(incident.getDescription()));
}

if (incident.getDueDate() != null && checkDueDate(db, user, taskId)) {
DateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);
String date = formatter.format(incident.getDueDate());
update.put(“duedate”, getSetObject(date));
}

HashMap<String, Object> request = new HashMap<>();
request.put(“update”, update);

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, auth);
restTemplate.exchange(url, HttpMethod.PUT, entity, Void.class);
}

public void sendComment(JiraDbSettings db, JiraUser user, String taskId, String comment) {
String url = createURL(db.getHost(), db.getPort(), “/rest/api/2/issue/”+ taskId + “/comment”);
HttpHeaders auth = createPostHeaders(user);

HashMap<String, Object> request = new HashMap<>();
request.put(“body”, comment);

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, auth);
restTemplate.exchange(url, HttpMethod.POST, entity, Void.class);
}

public void sendReportFile(JiraDbSettings db, JiraUser user, String taskId, Resource resource) {
String url = createURL(db.getHost(), db.getPort(), “/rest/api/2/issue/”+ taskId + “/attachments”);
HttpHeaders auth = createUploadHeaders(user);

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add(“file”, resource);
body.add(“filename”, “report”);

HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, auth);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

HttpStatus statusCode = responseEntity.getStatusCode();

if (statusCode == HttpStatus.OK) {
log.info(“File uploaded successfully”);
log.info("Response: " + responseEntity.getBody());
} else {
log.info("Failed to upload file: " + statusCode + " - " + responseEntity.getBody());
}
}

private List<HashMap<String, Object>> getIssues (JiraDbSettings db, JiraUser user, String projectKey) {
String path = “/rest/api/latest/issue/createmeta/” + projectKey + “/issuetypes”;
List<HashMap<String, Object>> values = (List<HashMap<String, Object>>) getMapFromResponse(db, user, path).get(“values”);
String[] fields = {“id”, “name”};
List<HashMap<String, Object>> issues = setResponseFields(values, fields);
for (HashMap<String, Object> issue : issues) {
issue.put(“fields”, getIssueTypes(db, user, projectKey, (String) issue.get(“id”)));
}
return issues;
}

private List<HashMap<String, Object>> getAssignee(JiraDbSettings db, JiraUser user, String projectKey) {
String path = “/rest/api/latest/user/assignable/search?project=” + projectKey;
List<HashMap<String, Object>> list = getListFromResponse(db, user, path);
String[] fields = {“name”, “displayName”};
return setResponseFields(list, fields);
}

private List<HashMap<String, Object>> getIssueTypes(JiraDbSettings db, JiraUser user, String projectKey, String issueId) {
String path = “/rest/api/latest/issue/createmeta/” + projectKey + “/issuetypes/” + issueId;
List<HashMap<String, Object>> values = (List<HashMap<String, Object>>) getMapFromResponse(db, user, path).get(“values”);

List<HashMap<String, Object>> issueTypes = new ArrayList<>();

for (HashMap<String, Object> issueType : values) {
HashMap<String, Object> element = new HashMap<>();

element.put(“required”, issueType.get(“required”));
element.put(“name”, issueType.get(“name”));
element.put(“fieldId”, issueType.get(“fieldId”));

if (issueType.get(“allowedValues”) != null) {
List<HashMap<String, Object>> valuesFromResponse =
(List<HashMap<String, Object>>) issueType.get(“allowedValues”);
String[] fields = {“name”, “id”};
element.put(“allowedValues”, setResponseFields(valuesFromResponse, fields));
} else {
element.put(“allowedValues”, new ArrayList<>());
}
issueTypes.add(element);
}
return issueTypes;
}

private HttpHeaders createGetHeaders(String credential){
return new HttpHeaders() {{
String authHeader = "Basic " + credential;
set(HttpHeaders.AUTHORIZATION, authHeader);
}};
}

private HttpHeaders createPostHeaders(JiraUser user) {
return new HttpHeaders() {{
String authHeader = “Basic " + user.getCredential();
set(HttpHeaders.AUTHORIZATION, authHeader);
set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
}};
}

private HttpHeaders createUploadHeaders(JiraUser user) {
return new HttpHeaders() {{
String authHeader = “Basic " + user.getCredential();
set(HttpHeaders.AUTHORIZATION, authHeader);
set(“X-Atlassian-Token”, “no-check”);
setContentType(MediaType.MULTIPART_FORM_DATA);
}};
}

private String createURL(String host, Integer port, String path) {
return “http://” + host + “:” + port.toString() + path;
}

private HashMap<String, Object> getMapFromResponse(JiraDbSettings db, JiraUser user, String path) {
String url = createURL(db.getHost(), db.getPort(), path);
HttpHeaders auth = createGetHeaders(user.getCredential());
return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(auth), HashMap.class).getBody();
}

private List<HashMap<String, Object>> getListFromResponse(JiraDbSettings db, JiraUser user, String path) {
String url = createURL(db.getHost(), db.getPort(), path);
HttpHeaders auth = createGetHeaders(user.getCredential());
return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(auth), List.class).getBody();
}

private List<String> getUserPermission(JiraDbSettings db, JiraUser user) {
HashMap<String, Object> map = getMapFromResponse(db, user, “/rest/api/latest/mypermissions”);
return getKeys(map);
}

private List<String> getReferencePermission() {
List<String> list = new ArrayList<>();

list.add(“BROWSE_PROJECTS”);
list.add(“CREATE_ISSUES”);
list.add(“ASSIGN_ISSUES”);
list.add(“EDIT_ISSUES”);
list.add(“SCHEDULE_ISSUES”);
list.add(“ADD_COMMENTS”);
list.add(“CREATE_ATTACHMENTS”);

return list;
}

private List<String> getKeys(HashMap<String, Object> response) {
HashMap<String, Object> permissions = (HashMap<String, Object>) response.get(“permissions”);

List<String> keys = new ArrayList<>();
for (String key : permissions.keySet()) {
keys.add(key);
}
return keys;
}

private List<HashMap<String, Object>> setResponseFields(List<HashMap<String, Object>> values, String[] fields) {
List<HashMap<String, Object>> response = new ArrayList<>();
for (HashMap<String, Object> value : values) {
HashMap<String, Object> element = new HashMap<>();
for (String field : fields) {
element.put(field, value.get(field));
}
if (value.get(“name”).equals(“Sub-task”)) {
continue;
} else {
response.add(element);
}
}
return response;
}

private Boolean checkDueDate(JiraDbSettings db, JiraUser user, String projectKey, String issueId) {
List<HashMap<String, Object>> issueTypes = getIssueTypes(db, user, projectKey, issueId);
for (HashMap<String, Object> issue : issueTypes) {
if (issue.get(“fieldId”).equals(“duedate”)) {
return true;
}
}
return false;
}

private Boolean checkDueDate(JiraDbSettings db, JiraUser user, String taskId) {
String projectId = splitTask(taskId);
List<HashMap<String, Object>> issues = getIssues(db, user, projectId);
for (HashMap<String, Object> issue : issues) {
return checkDueDate(db, user, projectId, issue.get(“id”).toString());
}
return false;
}

private HashMap<String, String> objectForMap(String key, String value) {
HashMap<String, String> map = new HashMap<>();
map.put(key, value);
return map;
}

private List<HashMap<String,String>> getSetObject(String value) {
List<HashMap<String,String>> list = new ArrayList<>();
HashMap<String,String> map = new HashMap<>();
map.put(“set”, value);
list.add(map);
return list;
}

private String splitTask(String taskId) {
String[] words = taskId.split(”-”);
return words[0];
}
}