Java tutorial
/* * Dashboard * Copyright 2014 Christian Robert * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.perdian.apps.dashboard.services.jira; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.codehaus.jackson.JsonNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import de.perdian.apps.dashboard.DashboardException; import de.perdian.apps.dashboard.services.jira.dto.BurndownDto; import de.perdian.apps.dashboard.services.jira.dto.OverviewDto; import de.perdian.apps.dashboard.services.jira.util.BurndownCollector; import de.perdian.apps.dashboard.services.properties.PropertiesService; import de.perdian.apps.dashboard.support.clients.JsonClient; class JiraServiceImpl implements JiraService { private static final Logger log = LoggerFactory.getLogger(JiraServiceImpl.class); private PropertiesService propertiesService = null; @Override public OverviewDto loadOverview(String rapidViewId, String storyPointFieldName) { String jiraUsername = this.getPropertiesService().getProperty(JiraProperties.ENVIRONMENT_KEY_USERNAME); String jiraPassword = this.getPropertiesService().getProperty(JiraProperties.ENVIRONMENT_KEY_PASSWORD); try (JsonClient jsonClient = new JsonClient(jiraUsername, jiraPassword)) { return this.loadOverview(rapidViewId, storyPointFieldName, jsonClient); } } private OverviewDto loadOverview(String rapidViewId, String storyPointFieldName, JsonClient jsonClient) { StringBuilder sprintQueryRequest = new StringBuilder(); sprintQueryRequest.append(this.getPropertiesService().getProperty(JiraProperties.ENVIRONMENT_KEY_URL)); sprintQueryRequest.append("rest/greenhopper/1.0/sprintquery/").append(rapidViewId); sprintQueryRequest.append("?includeHistoricSprints=false"); sprintQueryRequest.append("&includeFutureSprints=false"); JsonNode sprintqueryResult = jsonClient.sendRequest(sprintQueryRequest); JsonNode sprintsNode = sprintqueryResult.get("sprints"); for (int i = 0; i < sprintsNode.size(); i++) { JsonNode sprintqueryChild = sprintsNode.get(i); if ("ACTIVE".equalsIgnoreCase(sprintqueryChild.get("state").asText())) { return this.loadOverview(rapidViewId, sprintqueryChild.get("id").asText(), sprintqueryChild.get("name").asText(), jsonClient); } } // No active sprint found! throw new DashboardException("No active sprint found for rapidView: " + rapidViewId); } private OverviewDto loadOverview(String rapidViewId, String sprintId, String sprintTitle, JsonClient jsonClient) { StringBuilder sprintReportRequest = new StringBuilder(); sprintReportRequest.append(this.getPropertiesService().getProperty(JiraProperties.ENVIRONMENT_KEY_URL)); sprintReportRequest.append("rest/greenhopper/1.0/rapid/charts/sprintreport?rapidViewId=") .append(rapidViewId); sprintReportRequest.append("&sprintId=").append(sprintId); JsonNode sprintReportNode = jsonClient.sendRequest(sprintReportRequest); OverviewDto overviewDto = new OverviewDto(); overviewDto.setSprintId(sprintId); overviewDto.setSprintTitle(sprintTitle); JsonNode sprintNode = sprintReportNode.get("sprint"); DateFormat sprintDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); try { overviewDto.setStartDate(sprintDateFormat.parse(sprintNode.get("startDate").asText())); overviewDto.setEndDate(sprintDateFormat.parse(sprintNode.get("endDate").asText())); overviewDto.setCurrentDate(new Date()); } catch (ParseException e) { log.warn("Invalid date format retrieved from JIRA", e); } return overviewDto; } @Override public BurndownDto loadBurndown(String rapidViewId, String burndownBasis, String storyPointFieldName) { String jiraUsername = this.getPropertiesService().getProperty(JiraProperties.ENVIRONMENT_KEY_USERNAME); String jiraPassword = this.getPropertiesService().getProperty(JiraProperties.ENVIRONMENT_KEY_PASSWORD); try (JsonClient jsonClient = new JsonClient(jiraUsername, jiraPassword)) { OverviewDto overviewDto = this.loadOverview(rapidViewId, storyPointFieldName, jsonClient); String useStoryPointFieldName = storyPointFieldName == null ? this.getPropertiesService().getProperty(JiraProperties.ENVIRONMENT_KEY_STORY_POINT_FIELD_NAME) : storyPointFieldName; StringBuilder requestUri = new StringBuilder(); requestUri.append(this.getPropertiesService().getProperty(JiraProperties.ENVIRONMENT_KEY_URL)) .append("rest/api/2/search"); requestUri.append("?jql=issuetype+IN+%28Story,Bug,Defect,%22Change+Request%22%29+AND+Sprint=") .append(overviewDto.getSprintId()); requestUri.append("&fields=summary"); BurndownCollector burndownCollector = BurndownCollector.createInstance(overviewDto, burndownBasis, useStoryPointFieldName); JsonNode responseNode = jsonClient.sendRequest(requestUri); JsonNode issuesNode = responseNode.get("issues"); for (int i = 0; i < issuesNode.size(); i++) { JsonNode issueNode = issuesNode.get(i); String issueKey = issueNode.get("key").asText(); StringBuilder issueDetailsRequestUri = new StringBuilder(); issueDetailsRequestUri .append(this.getPropertiesService().getProperty(JiraProperties.ENVIRONMENT_KEY_URL)); issueDetailsRequestUri.append("rest/api/2/issue/").append(issueKey); issueDetailsRequestUri.append("?expand=changelog"); JsonNode issueDetailsNode = jsonClient.sendRequest(issueDetailsRequestUri); burndownCollector.acceptIssueNode(issueDetailsNode); } return burndownCollector.toBurndownDto(); } } // ------------------------------------------------------------------------- // --- Property access methods --------------------------------------------- // ------------------------------------------------------------------------- public PropertiesService getPropertiesService() { return this.propertiesService; } @Autowired void setPropertiesService(PropertiesService propertiesService) { this.propertiesService = propertiesService; } }