/*
 * Copyright 2015 Tomas Rohovsky
 *
 * 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 com.github.trohovsky.jira.analyzer;

import org.joda.time.DateTime;
import org.joda.time.Months;

import com.atlassian.jira.rest.client.api.SearchRestClient;
import com.atlassian.jira.rest.client.api.domain.Issue;
import com.atlassian.jira.rest.client.api.domain.SearchResult;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * It queries a JIRA and reports the results. The results are in this format:
 * query_parameter+ number_of_issues creation_date_of_first_issue issues_per_month
 *
 * issues_per_month is computed from creation_date_of_first_issue
 *
 * @author Tomas Rohovsky
 */
public class IssuesPerMonthStrategy implements AnalyzerStrategy {

	private SearchRestClient searchRestClient;

	public IssuesPerMonthStrategy(SearchRestClient searchRestClient) {
		this.searchRestClient = searchRestClient;
	}

	@Override
	public void analyze(String jqlQueryTemplate, List<String> queryParameters) {
		final String jqlQuery = String.format(jqlQueryTemplate, queryParameters.toArray());
		// BUG in JiraRestClient - setting of fields makes troubles
		final SearchResult searchResult = searchRestClient.searchJql(jqlQuery, 1, 0).claim();

		DateTime firstIssueCreationDate = null;
		if (searchResult.getIssues().iterator().hasNext()) {
			final Issue firstIssue = searchResult.getIssues().iterator().next();
			firstIssueCreationDate = firstIssue.getCreationDate();
		}
		int monthDiff = 0;
		if (firstIssueCreationDate != null) {
			final DateTime today = new DateTime();
			final Months mt = Months.monthsBetween(firstIssueCreationDate, today);
			monthDiff = mt.getMonths();
		}
		final float issuesPerMonth = (float) searchResult.getTotal() / monthDiff;
		final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		final String firstIssueCreationDateString = formatter.format(firstIssueCreationDate.toDate());
		System.out.println(String.format("%s %s %s %s", String.join(" ", queryParameters), searchResult.getTotal(),
				firstIssueCreationDateString, issuesPerMonth));
	}
}

@@ -1522,22 +1522,16 @@
 rs) %7B%0A%09%09
-final 
 String j
@@ -1600,16 +1600,289 @@
 ray());%0A
+%09%09// adjust the query to correctly order the results, it's required due to creation date%0A%09%09int indexOfOrderBy = jqlQuery.indexOf(%22ORDER BY%22);%0A%09%09if (indexOfOrderBy != -1) %7B%0A%09%09%09jqlQuery = jqlQuery.substring(0, indexOfOrderBy - 1);%0A%09%09%7D%0A%09%09jqlQuery += %22 ORDER BY created ASC%22;%0A%0A
 %09%09// BUG
@@ -2606,14 +2606,14 @@
 at(%22
-dd-MM-
+MM/dd/
 yyyy
@@ -2622,21 +2622,95 @@
 ;%0A%09%09
-final 
 String 
+firstIssueCreationDateString = null;%0A%09%09if (firstIssueCreationDate != null) %7B%0A%09%09%09
 firs
@@ -2787,16 +2787,73 @@
 ate());%0A
+%09%09%7D else %7B%0A%09%09%09firstIssueCreationDateString = %22None%22;%0A%09%09%7D%0A
 %09%09System

Fix date related issues in IssuesPerMonthStrategy