package com.ppm.integration.agilesdk.connector.jira;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.xml.datatype.XMLGregorianCalendar;

import com.ppm.integration.agilesdk.tm.ExternalWorkItem;
import com.ppm.integration.agilesdk.tm.ExternalWorkItemEffortBreakdown;

public class JIRAExternalWorkItem extends ExternalWorkItem {

	private String name = "";
	private Double totalEffort = 0.0;
	private String errorMessage = null;

	private Map<String, Double> timeSpentSeconds = new HashMap<>();
	private XMLGregorianCalendar dateFrom;
	private XMLGregorianCalendar dateTo;

	public JIRAExternalWorkItem(String name, Double totalEffort, String errorMessage, XMLGregorianCalendar dateFrom,
			XMLGregorianCalendar dateTo, Map<String, Double> timeSpentSeconds) {
		this.name = name;
		this.totalEffort = totalEffort;
		this.errorMessage = errorMessage;
		this.dateFrom = dateFrom;
		this.dateTo = dateTo;
		this.timeSpentSeconds = timeSpentSeconds;
	}

	@Override
	public String getName() {
		return this.name;
	}

	public Double getTotalEffort() {
		return totalEffort;
	}

	public ExternalWorkItemEffortBreakdown getEffortBreakDown() {
		ExternalWorkItemEffortBreakdown eb = new ExternalWorkItemEffortBreakdown();

		Set<String> dateKeys = timeSpentSeconds.keySet();
		for (String date : dateKeys) {
			eb.addEffort(date, timeSpentSeconds.get(date));
		}
		return eb;
	}

	@Override
	public String getErrorMessage() {
		return errorMessage;
	}

}

@@ -47,16 +47,78 @@
 .jira;%0A%0A
+import java.text.SimpleDateFormat;%0Aimport java.util.Calendar;%0A
 import j
@@ -160,30 +160,8 @@
 Map;
-%0Aimport java.util.Set;
 %0A%0Aim
@@ -1124,27 +1124,20 @@
 %09return 
-totalEffort
+null
 ;%0A%09%7D%0A%0A%09p
@@ -1279,94 +1279,296 @@
 ();%0A
-%0A
 %09%09
-Set%3CString%3E dateKeys = timeSpentSeconds.keySet();%0A%09%09for (String d
+Calendar cursor = dateFrom.toGregorianCalendar();%0A%09%09SimpleDateFormat dateFormat = new SimpleDateFormat(%22yyyy-MM-dd%22);%0A%09%09while (cursor.before(dateTo.toGregorianCalendar())) %7B%0A%09%09%09String cursorD
 ate 
-:
+=
  date
-Keys
+Format.format(cursor.getTime());%0A%09%09%09if (timeSpentSeconds.containsKey(cursorDate)
 ) %7B%0A
+%09
 %09%09%09e
@@ -1579,17 +1579,23 @@
 dEffort(
-d
+cursorD
 ate, tim
@@ -1616,16 +1616,114 @@
 get(
-date)
+cursorDate));%0A%09%09%09%7D else %7B%0A%09%09%09%09eb.addEffort(cursorDate, 0);%0A%09%09%09%7D%0A%09%09%09cursor.add(Calendar.DAY_OF_MONTH, 1
 );%0A
+%0A
 %09%09%7D%0A

fix a bug in timesheet