Find Questions…

Close ×
First time here? Check out the FAQ!

How to count worklogs with same date on JIRA?

Matti Kiviharju asked this question · 214 karma ·

How to count worklogs with same date on JIRA?

Here a code:

package com.i4ware.plugin.timesheet;

import java.io.IOException;

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import org.ofbiz.core.entity.DelegatorInterface;
import org.ofbiz.core.entity.EntityExpr;
import org.ofbiz.core.entity.EntityOperator;
import org.ofbiz.core.entity.GenericEntityException;
import org.ofbiz.core.entity.GenericValue;
import org.ofbiz.core.util.UtilMisc;
import org.apache.commons.lang.StringEscapeUtils;
import com.atlassian.crowd.embedded.api.User;
//import com.atlassian.jira.ComponentManager;

import com.atlassian.jira.issue.worklog.Worklog;
import com.atlassian.jira.issue.worklog.WorklogManager;
import com.atlassian.jira.issue.worklog.WorklogImpl;
import com.i4ware.plugin.util.WorklogUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.atlassian.jira.util.json.JSONObject;
import com.atlassian.jira.util.json.JSONException;
import com.atlassian.jira.util.json.JSONArray;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.TreeSet;
import java.lang.Long;
import java.util.Collection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.lang.Class;

public class LoadServlet extends HttpServlet
{
	
    /** value is made for JSON {"success":true} or {"success":false}. */
    private Boolean value;
    /** tasks Object. i.e tasks = arr; */
    private Object issues;
    /** limit int. i.e
     * limit = Integer.valueOf(request.getParameter("limit")).intValue();.
     */
    private int limit;
    /** pages int. variable of total count of rows i.e pages = numRows;. */
    private int pages;
    private String json;
    private JSONObject obj;
    
    private long count;
    
    private Project project;
    
    //private Issue issueIter;
	
	private IssueManager issueManager;
	private WorklogManager worklogManager;
	private Class cls;
	
	private String startDate;
	private String endDate;
	private ProjectManager projectManager;
	
	public LoadServlet(IssueManager issueManager, ProjectManager projectManager, WorklogManager worklogManager)
	{
		this.issueManager = issueManager;
		this.projectManager = projectManager;
		this.worklogManager = worklogManager;
	}
	
	@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
		resp.setContentType("application/json");		
		//resp.getWriter().write("");
        //resp.getWriter().close();
    
    }
	
	@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
		
		//issueManager.getIssueObjects();	
		
		/*EntityExpr startExpr = new EntityExpr("startdate",
                EntityOperator.GREATER_THAN_EQUAL_TO, new Timestamp(
                        startDate.getTime()));
        EntityExpr endExpr = new EntityExpr("startdate",
                EntityOperator.LESS_THAN, new Timestamp(endDate.getTime()));
        List<EntityExpr> entityExprs = UtilMisc.toList(startExpr, endExpr); */     
		
		/* HTTP Requests */
        //HttpServletRequest request = ServletActionContext.getRequest();
		
		String projectIdreq = req.getParameter("project");
		String monthReq = req.getParameter("month");
		String yearReq = req.getParameter("year");
        
        project = projectManager.getProjectObjByKey(projectIdreq);
        
        long projectId = project.getId();
        
        Calendar calendar = Calendar.getInstance(); 

        /*
         * Uncomment these if needed DateFormat dateFormat = new
         * SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date();
         */

        DateFormat monthFormat = new SimpleDateFormat("M");
        DateFormat yearFormat = new SimpleDateFormat("yyyy");
        DateFormat day = new SimpleDateFormat("EEE");
        DateFormat itter = new SimpleDateFormat("M-d-yyyy");

        /* gets a last date of current month */
        
        int m = Integer.valueOf(req.getParameter("month")).intValue();
        int y = Integer.valueOf(req.getParameter("year")).intValue();
        calendar.set(y, m - 1, 1);
        int lastDate = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        
        int ii = 1;
        int iii = 1;

        
        //getIssueIdsForProject
        
        /*String issue = "";
         * 
         * 
        */
        
        JSONArray arr = new JSONArray();
        int i = 1;
        
        try {
        	
        	Collection<Long> issueIds = issueManager.getIssueIdsForProject(projectId);
        	
            List<Issue> issuesList = issueManager.getIssueObjects(issueIds);
            
            Iterator iterator = issuesList.iterator();
            
            /*for (Iterator<Issue> iterator = issuesList.iterator();
            	iterator.hasNext();) {
            	
            	Issue issue = iterator.hasNext();
            	
            	//Issue issue = issueManager.getIssueObject("DEMO-1");
            	
            }*/
            
            while (iterator.hasNext()) {
        		//iterator.next();
        		//System.out.println(iterator.next());
        		
            	Issue issueIter = issueManager.getIssueObject(iterator.next().toString());
            	
            	User assignee = issueIter.getAssigneeUser();
            	
            	List<Worklog> worklog = worklogManager.getByIssue(issueIter);
            	
            	Iterator<Worklog> worklogIterator = worklog.iterator();           	
        		
        	    try {	
        		JSONObject obj = new JSONObject()
        		        .put("id", issueIter.getKey())
        				.put("title",issueIter.getSummary())
        				.put("user_id",issueIter.getAssigneeId())
        				.put("assignee",assignee.getDisplayName())
        				/*.put("d1","8")
        				.put("d2","8")
        				.put("d3","8")
        				.put("d4","8")
        				.put("d5","8")
        				.put("d6","8")
        				.put("d7","8")
        				.put("d8","8")
        				.put("d9","8")
        				.put("d10","8")
        				.put("d11","8")
        				.put("d12","8")
        				.put("d13","8")
        				.put("d14","8")
        				.put("d15","8")
        				.put("d16","8")
        				.put("d17","8")
        				.put("d18","8")
        				.put("d19","8")
        				.put("d20","8")
        				.put("d21","8")
        				.put("d22","8")
        				.put("d23","8")
        				.put("d24","8")
        				.put("d25","8")
        				.put("d26","8")
        				.put("d27","8")
        				.put("d28","8")
        				.put("d29","8")
                        .put("d30","8")
                        .put("d31","8")*/
                        .put("initial_estimate",issueIter.getOriginalEstimate() / 3600)
                        .put("remaining_estimate",issueIter.getEstimate() / 3600);
        		
        		
        		/*while (ii <= lastDate) {
        			
        			long issueId = issueIter.getId();
                	
                	GenericValue genericWorklog = issueManager.getIssue(issueId);
                	
                	//List<GenericValue> worklogs = ComponentManager.getComponent(DelegatorInterface.class);
                	
                	//Worklog worklogFor = new WorklogImpl(worklogManager, issueIter, gv.getLong("id"));
                	/*Timestamp startDateTS = genericWorklog.getTimestamp("startdate");
                	Timestamp createdTS = genericWorklog.getTimestamp("created");
                    Timestamp updatedTS = genericWorklog.getTimestamp("updated");
                	
                	Worklog worklogFor = new WorklogImpl(worklogManager, issueIter, genericWorklog.getLong("id"),
                			genericWorklog.getString("author"), genericWorklog.getString("body"),
                            startDateTS != null ? new Date(startDateTS.getTime()) : null,
                            genericWorklog.getString("grouplevel"), genericWorklog.getLong("rolelevel"),
                            genericWorklog.getLong("timeworked"), genericWorklog.getString("updateauthor"),
                            createdTS != null ? new Date(createdTS.getTime()) : null,
                            updatedTS != null ? new Date(updatedTS.getTime()) : null);
                	
                	//Worklog worklogFor = WorklogUtil.convertToWorklog(genericWorklog, worklogManager, issueManager);
        			
                	startDate = worklogFor.getStartDate();
     		        long timeSpent = worklogFor.getTimeSpent();
     		        //obj.put(startDate.toString(),timeSpent / 3600);*/
                	
                	//obj.put("d" + ii,"8");
                	
        			//ii++;
        		//}
        		
        		//ii = 1;
        		
        		while (worklogIterator.hasNext()) {
        			
        			   //System.out.println(worklogIterator.next().toString());
        			
        			   Worklog itr = worklogIterator.next();   

        		       startDate = itter.format(itr.getStartDate());
        		       long timeSpent = itr.getTimeSpent();
        		       
        		       /*if (startDate.equals(itter.format(itr.getStartDate()))) {
        		    	   timeSpent = timeSpent+timeSpent;
        		       }*/
        	           
        	           obj.put(startDate,timeSpent / 3600);
        		
        		}
        		
        		arr.put(obj);
                } catch (JSONException jerr) {
            	jerr.printStackTrace(); 
                }
        	
        	i++;
        		
        	}
        
        } catch (GenericEntityException e) {
        	e.printStackTrace(); 
        }
        
        issues = arr;
        pages = 1;
        value = Boolean.valueOf(!"false"
                .equalsIgnoreCase((String) "true"));
        
        
        try {
        
        json = new JSONObject()
        .put("tasks", issues)
        .put("totalCount", pages)
        .put("success", value)
        .toString();
        
        } catch (JSONException err) {
        	err.printStackTrace();
        }
        
        /*try {
        	List<GenericValue> worklogs = issueManager.getProjectIssues(project);
        	
        	Iterator iterator = worklogs.iterator();
        	
        	while (iterator.hasNext()) {
        		iterator.next();
        	}
        	
        } catch (GenericEntityException e) {
        	e.printStackTrace();        	
        }*/
        
		
		/*List<GenericValue> worklogs = ComponentManager.getComponent(DelegatorInterface.class).findByAnd(
                "Worklog", entityExprs);*/
		
		resp.setContentType("application/json");		
		resp.getWriter().write(json);
        resp.getWriter().close();
    
    }
}

328 views

Matti Kiviharju · 214 karma ·

I mean. How to count sum of worklogs with same date on JIRA?

Looking for something else?

Find Questions…

or Browse other questions tagged:

or Ask a Question