Java tutorial
/* * * Copyright (c) 2015 SERENA Software, Inc. All Rights Reserved. * * This software is proprietary information of SERENA Software, Inc. * Use is subject to license terms. * * @author Kevin Lee */ package com.serena.rlc.provider.jira; import com.serena.rlc.provider.BaseRequestProvider; import com.serena.rlc.provider.annotations.*; import com.serena.rlc.provider.domain.*; import com.serena.rlc.provider.exceptions.ProviderException; import com.serena.rlc.provider.jira.client.JiraClient; import com.serena.rlc.provider.jira.domain.Issue; import com.serena.rlc.provider.jira.domain.JiraObject; import com.serena.rlc.provider.jira.domain.Project; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import com.serena.rlc.provider.jira.exception.JiraClientException; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class JiraRequestProvider extends BaseRequestProvider { final static Logger logger = LoggerFactory.getLogger(JiraRequestProvider.class); final static String PROJECT = "project"; final static String STATUS_FILTERS = "statusFilters"; final static String TITLE_FILTER = "titleFilter"; @Autowired private JiraClient jiraClient; private Integer resultLimit; //================================================================================ // Configuration Properties // ------------------------------------------------------------------------------- // The configuration properties are marked with the @ConfigProperty annotaion // and will be displayed in the provider administration page when creating a // configuration of this plugin for use. //================================================================================ @ConfigProperty(name = "request_provider_name", displayName = "Request Provider Name", description = "provider name", defaultValue = "Jira Provider", dataType = DataType.TEXT) private String providerName; @ConfigProperty(name = "request_provider_description", displayName = "Request Provider Description", description = "provider description", defaultValue = "", dataType = DataType.TEXT) private String providerDescription; @ConfigProperty(name = "jira_url", displayName = "JIRA URL", description = "JIRA Server URL.", defaultValue = "http://<servername>:8080/jira/", dataType = DataType.TEXT) private String jiraUrl; @ConfigProperty(name = "jira_serviceuser", displayName = "User Name", description = "JIRA service username.", defaultValue = "", dataType = DataType.TEXT) private String serviceUser; @ConfigProperty(name = "jira_servicepassword", displayName = "Password", description = "JIRA service password", defaultValue = "", dataType = DataType.PASSWORD) private String servicePassword; @ConfigProperty(name = "request_status_filters", displayName = "Status Filters", description = "status filters separated by commas", defaultValue = "To Do,In Progress,In Review,Done", dataType = DataType.TEXT) private String statusFilters; @ConfigProperty(name = "request_result_limit", displayName = "Result Limit", description = "Result limit for find requests action", defaultValue = "300", dataType = DataType.TEXT) private String requestResultLimit; @Override public String getProviderName() { return this.providerName; } @Autowired(required = false) @Override public void setProviderName(String providerName) { if (StringUtils.isNotEmpty(providerName)) { providerName = providerName.trim(); } this.providerName = providerName; } @Override public String getProviderDescription() { return this.providerDescription; } @Autowired(required = false) @Override public void setProviderDescription(String providerDescription) { if (StringUtils.isNotEmpty(providerDescription)) { providerDescription = providerDescription.trim(); } this.providerDescription = providerDescription; } public String getStatusFilters() { return statusFilters; } @Autowired(required = false) public void setStatusFilters(String statusFilters) { if (!StringUtils.isEmpty(statusFilters)) { statusFilters = statusFilters.trim(); } this.statusFilters = statusFilters; } public String getRequestResultLimit() { return requestResultLimit; } @Autowired(required = false) public void setRequestResultLimit(String requestResultLimit) { this.requestResultLimit = requestResultLimit; } public String getJiraUrl() { return jiraUrl; } @Autowired(required = false) public void setJiraUrl(String jiraUrl) { if (StringUtils.isNotEmpty(jiraUrl)) { this.jiraUrl = jiraUrl.replaceAll("^\\s+", ""); } else { this.jiraUrl = "http://localhost:8080/jira"; } } public String getServiceUser() { return serviceUser; } @Autowired(required = false) public void setServiceUser(String serviceUser) { if (!StringUtils.isEmpty(serviceUser)) { this.serviceUser = serviceUser.replaceAll("^\\s+", ""); } } public String getServicePassword() { return servicePassword; } @Autowired(required = false) public void setServicePassword(String servicePassword) { if (!StringUtils.isEmpty(servicePassword)) { this.servicePassword = servicePassword.replaceAll("^\\s+", ""); } } //================================================================================ // IRequestProvider Overrides //================================================================================ @Override @Service(name = FIND_REQUESTS, displayName = "Find Issues", description = "Find JIRA issues.") @Params(params = { @Param(fieldName = PROJECT, displayName = "Project", description = "JIRA project name", required = true, dataType = DataType.SELECT), @Param(fieldName = STATUS_FILTERS, displayName = "Status Filters", description = "JIRA Status filters.", dataType = DataType.MULTI_SELECT, required = false), @Param(fieldName = TITLE_FILTER, displayName = "Title Filter", description = "JIRA title filter."), }) public ProviderInfoResult findRequests(List<Field> properties, Long startIndex, Long resultCount) throws ProviderException { Field field = Field.getFieldByName(properties, PROJECT); if (field == null) { throw new ProviderException("Missing required property: " + PROJECT); } String projectId = field.getValue(); logger.debug("Filtering on project: " + projectId); String titleFilter = null; field = Field.getFieldByName(properties, TITLE_FILTER); if (field != null) { titleFilter = field.getValue(); } List<String> requestStatusFilters = null; List<Field> fields = Field.getFieldsByName(properties, STATUS_FILTERS); if (fields != null && fields.size() > 0) { requestStatusFilters = new ArrayList<>(); for (Field fieldFilter : fields) { requestStatusFilters.add(fieldFilter.getValue()); } } List<ProviderInfo> list = new ArrayList<>(); setJiraClientConnectionDetails(); try { List<Issue> requests = jiraClient.getIssues(projectId, requestStatusFilters, titleFilter, getResultLimit()); if (requests != null) { ProviderInfo pReqInfo; for (Issue request : requests) { pReqInfo = new ProviderInfo(request.getId(), request.getName(), request.getType(), request.getName()); if (StringUtils.isEmpty(request.getId())) { pReqInfo.setId(request.getName()); } pReqInfo.setDescription(request.getDescription()); pReqInfo.setUrl(this.getJiraUrl() + "browse/" + request.getUrl()); fields = new ArrayList<>(); addField(fields, "project", "Project", request.getProject()); addField(fields, "owner", "Owner", request.getOwner()); addField(fields, "status", "Status", request.getStatus()); addField(fields, "priority", "Priority", request.getPriority()); addField(fields, "creator", "Creator", request.getCreator()); addField(fields, "dateCreated", "Date Created", request.getDateCreated()); addField(fields, "lastUpdated", "Last Update", request.getLastUpdated()); pReqInfo.setProperties(fields); list.add(pReqInfo); } } } catch (JiraClientException e) { logger.debug(e.getLocalizedMessage()); throw new ProviderException(e.getLocalizedMessage()); } return new ProviderInfoResult(0, list.size(), list.toArray(new ProviderInfo[list.size()])); } @Override @Service(name = GET_REQUEST, displayName = "Get Request", description = "Get JIRA issue information.") @Params(params = { @Param(fieldName = REQUEST_ID, displayName = "Issue Id", description = "JIRA issue identifier", required = true, deployUnit = false, dataType = DataType.SELECT) }) public ProviderInfo getRequest(Field property) throws ProviderException { if (StringUtils.isEmpty(property.getValue())) { throw new ProviderException("Missing required field: " + REQUEST_ID); } setJiraClientConnectionDetails(); try { Issue request = jiraClient.getIssue(property.getValue()); if (request == null) { throw new ProviderException("Unable to find request: " + property.getValue()); } ProviderInfo pReqInfo = new ProviderInfo(request.getId(), request.getName(), request.getType(), request.getName()); if (StringUtils.isEmpty(request.getId())) { pReqInfo.setId(request.getName()); } pReqInfo.setDescription(request.getDescription()); pReqInfo.setUrl(this.getJiraUrl() + "browse/" + request.getUrl()); List<Field> fields = new ArrayList<>(); addField(fields, "project", "Project", request.getProject()); addField(fields, "owner", "Owner", request.getOwner()); addField(fields, "status", "Status", request.getStatus()); addField(fields, "priority", "Priority", request.getPriority()); addField(fields, "creator", "Creator", request.getCreator()); addField(fields, "dateCreated", "Date Created", request.getDateCreated()); addField(fields, "lastUpdated", "Last Update", request.getLastUpdated()); pReqInfo.setProperties(fields); return pReqInfo; } catch (JiraClientException e) { logger.debug(e.getLocalizedMessage()); throw new ProviderException(e.getLocalizedMessage()); } } @Override public FieldInfo getFieldValues(String fieldName, List<Field> properties) throws ProviderException { if (fieldName.equalsIgnoreCase(PROJECT)) { return getProjectFieldValues(fieldName); } else if (fieldName.equalsIgnoreCase(STATUS_FILTERS)) { return getStatusFiltersFieldValues(fieldName); } throw new ProviderException("Unsupported get values for field name: " + fieldName); } //================================================================================ // Getter Methods // ------------------------------------------------------------------------------- // These methods are used to get the field values. The @Getter annotation is used // by the system to generate a user interface and pass the correct parameters to // to the provider //================================================================================ @Getter(name = PROJECT, displayName = "Project", description = "Get JIRA project field values.") public FieldInfo getProjectFieldValues(String fieldName) throws ProviderException { FieldInfo fieldInfo = new FieldInfo(fieldName); setJiraClientConnectionDetails(); try { List<Project> jiraProjects = jiraClient.getProjects(); if (jiraProjects == null || jiraProjects.size() < 1) { return null; } List<FieldValueInfo> values = new ArrayList<>(); FieldValueInfo value; for (JiraObject jro : jiraProjects) { value = new FieldValueInfo(jro.getId(), jro.getName()); if (StringUtils.isEmpty(jro.getId())) { value.setId(jro.getId()); } value.setDescription(jro.getName()); values.add(value); } fieldInfo.setValues(values); return fieldInfo; } catch (JiraClientException e) { throw new ProviderException(e.getLocalizedMessage()); } } @Getter(name = STATUS_FILTERS, displayName = "Issue Status Filters", description = "Get JIRA status filters field values.") public FieldInfo getStatusFiltersFieldValues(String fieldName) throws ProviderException { if (StringUtils.isEmpty(statusFilters)) { return null; } StringTokenizer st = new StringTokenizer(statusFilters, ",;"); FieldInfo fieldInfo = new FieldInfo(fieldName); List<FieldValueInfo> values = new ArrayList<>(); FieldValueInfo value; String status; while (st.hasMoreElements()) { status = (String) st.nextElement(); status = status.trim(); value = new FieldValueInfo(status, status); values.add(value); } fieldInfo.setValues(values); return fieldInfo; } //================================================================================ // Private Methods //================================================================================ private void addField(List<Field> fieldCollection, String fieldName, String fieldDisplayName, String fieldValue) { if (StringUtils.isNotEmpty(fieldValue)) { Field field = new Field(fieldName, fieldDisplayName); field.setValue(fieldValue); fieldCollection.add(field); } } private void setJiraClientConnectionDetails() { jiraClient.createConnection(getSession(), getJiraUrl(), getServiceUser(), getServicePassword()); } private int getResultLimit() { if (resultLimit == null) { resultLimit = 300; if (StringUtils.isNotBlank(requestResultLimit)) { try { resultLimit = Integer.parseInt(requestResultLimit); } catch (Throwable e) { logger.warn(e.getMessage(), e); } } } return resultLimit; } }