Java tutorial
package com.fdu.jira.plugin.gadget.timesheet; import com.atlassian.crowd.embedded.api.User; import com.atlassian.jira.ComponentManager; import com.atlassian.jira.bc.filter.SearchRequestService; import com.atlassian.jira.bc.issue.util.VisibilityValidator; import com.atlassian.jira.config.properties.ApplicationProperties; import com.atlassian.jira.datetime.DateTimeFormatterFactory; import com.atlassian.jira.datetime.DateTimeStyle; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.search.SearchProvider; import com.atlassian.jira.issue.worklog.WorklogManager; import com.atlassian.jira.project.Project; import com.atlassian.jira.project.ProjectManager; import com.atlassian.jira.security.JiraAuthenticationContext; import com.atlassian.jira.security.PermissionManager; import com.atlassian.jira.security.Permissions; import com.atlassian.jira.timezone.TimeZoneManager; import com.atlassian.jira.user.util.UserUtil; import com.atlassian.jira.util.velocity.DefaultVelocityRequestContextFactory; import com.atlassian.jira.util.velocity.VelocityRequestContext; import com.atlassian.jira.web.FieldVisibilityManager; import com.atlassian.jira.web.bean.I18nBean; import com.atlassian.plugins.rest.common.security.AnonymousAllowed; import com.atlassian.upm.license.storage.lib.ThirdPartyPluginLicenseStorageManager; import com.atlassian.velocity.VelocityManager; import com.fdu.jira.util.CalendarUtil; import com.fdu.jira.util.LicenseUtil; import com.fdu.jira.util.ServletUtil; import com.fdu.jira.util.TextUtil; import com.opensymphony.util.TextUtils; import org.apache.commons.lang.StringEscapeUtils; import org.apache.log4j.Logger; import org.apache.velocity.exception.VelocityException; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.Calendar; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TimeZone; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.CacheControl; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; /** * Generate a summary of worked hours for current week. */ @Path("/group-timesheet") public class GroupTimeSheetResource { private static final Logger log = Logger.getLogger(GroupTimeSheetResource.class); // References to managers required for this portlet private WorklogManager worklogManager; private IssueManager issueManager; private VisibilityValidator visibilityValidator; private JiraAuthenticationContext authenticationContext; private PermissionManager permissionManager; private SearchProvider searchProvider; private ApplicationProperties applicationProperties; private FieldVisibilityManager fieldVisibilityManager; private ProjectManager projectManager; private UserUtil userUtil; private SearchRequestService searchRequestService; private DateTimeFormatterFactory dateTimeFormatterFactory; private TimeZoneManager timeZoneManager; private final ThirdPartyPluginLicenseStorageManager licenseManager; public GroupTimeSheetResource(JiraAuthenticationContext authenticationContext, PermissionManager permissionManager, ApplicationProperties applicationProperties, DateTimeFormatterFactory dateTimeFormatterFactory, WorklogManager worklogManager, IssueManager issueManager, SearchProvider searchProvider, VisibilityValidator visibilityValidator, FieldVisibilityManager fieldVisibilityManager, ProjectManager projectManager, UserUtil userUtil, SearchRequestService searchRequestService, TimeZoneManager timeZoneManager, ThirdPartyPluginLicenseStorageManager licenseManager) { this.authenticationContext = authenticationContext; this.permissionManager = permissionManager; this.applicationProperties = applicationProperties; this.dateTimeFormatterFactory = dateTimeFormatterFactory; this.worklogManager = worklogManager; this.issueManager = issueManager; this.searchProvider = searchProvider; this.visibilityValidator = visibilityValidator; this.fieldVisibilityManager = fieldVisibilityManager; this.projectManager = projectManager; this.userUtil = userUtil; this.searchRequestService = searchRequestService; this.timeZoneManager = timeZoneManager; this.licenseManager = licenseManager; } @GET @AnonymousAllowed @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Response getTimesheet(@Context HttpServletRequest request, @QueryParam("targetGroup") String targetGroupName, @QueryParam("project") String project) { if (targetGroupName != null) try { // TIME-158 workaround: this must be handled by application server instead or this may be bug in Jira gadgets targetGroupName = new String(StringEscapeUtils.unescapeHtml(targetGroupName).getBytes("ISO-8859-1"), applicationProperties.getEncoding()); } catch (UnsupportedEncodingException e) { // safe to ignore } // Retrieve projectId Long projectId = null; try { projectId = Long.parseLong(project.substring(project.indexOf("-") + 1)); } catch (NumberFormatException e) { // ignore, no project selected } // Retrieve the number of weeks to display in summary int numOfWeeks = ServletUtil.getIntParam(request, "numOfWeeks", 1); // Retrieve the week day that is to be a first day int reportingDay = ServletUtil.getIntParam(request, "reportingDay", Calendar.MONDAY); VelocityManager vm = ComponentManager.getInstance().getVelocityManager(); try { return Response.ok(new TimeSheetRepresentation( vm.getBody("templates/grouptimesheet/", "group-timesheet.vm", getVelocityParams(request, numOfWeeks, reportingDay, targetGroupName, projectId)), /* projiectOrFilterName */ null)).cacheControl(getNoCacheControl()).build(); } catch (VelocityException e) { e.printStackTrace(); return Response.serverError().build(); } } // Pass the data required for the portlet display to the view template protected Map<String, Object> getVelocityParams(HttpServletRequest request, int numOfWeeks, int reportingDay, String groupName, Long projectId) { Map<String, Object> params = getVelocityParams(numOfWeeks, reportingDay, groupName, projectId); params.put("i18n", authenticationContext.getI18nHelper()); params.put("textutils", new TextUtils()); params.put("req", request); final VelocityRequestContext velocityRequestContext = new DefaultVelocityRequestContextFactory( applicationProperties).getJiraVelocityRequestContext(); params.put("baseurl", velocityRequestContext.getBaseUrl()); params.put("requestContext", velocityRequestContext); return params; } protected Map<String, Object> getVelocityParams(int numOfWeeks, int reportingDay, String targetGroupName, Long projectId) { Map<String, Object> params = new HashMap<String, Object>(); final User user = authenticationContext.getLoggedInUser(); TimeZone timezone = timeZoneManager.getLoggedInUserTimeZone(); params.put("loggedin", user != null); params.put("license", LicenseUtil.getStatus(licenseManager)); /* anonymous access or gadget not configured */ { if (user == null || targetGroupName == null || targetGroupName.length() == 0) return params; } final I18nBean i18nBean = new I18nBean(user); final Calendar[] dates = CalendarUtil.getDatesRange(reportingDay, numOfWeeks, timezone); final Calendar startDate = dates[0], endDate = dates[1]; try { Set<User> users = null; if (permissionManager.hasPermission(Permissions.USER_PICKER, user)) { users = userUtil.getAllUsersInGroupNames(Arrays.asList(new String[] { targetGroupName })); } // for better diagnosability params.put("targetGroup", targetGroupName); if (users == null || users.size() == 0) { return params; } // get time spents com.fdu.jira.plugin.report.timesheet.TimeSheet ts = new com.fdu.jira.plugin.report.timesheet.TimeSheet( dateTimeFormatterFactory, applicationProperties, permissionManager, worklogManager, issueManager, searchProvider, visibilityValidator, fieldVisibilityManager, userUtil, searchRequestService, authenticationContext, timeZoneManager, licenseManager); ts.getTimeSpents(user, startDate.getTime(), endDate.getTime(), null, false, null, new String[] { targetGroupName }, projectId, null, null, null, null); // pass parameters if (projectId != null) { Project project = projectManager.getProjectObj(projectId); params.put("project", project); } params.put("users", users); params.put("fieldVisibility", fieldVisibilityManager); String dpStartDate = CalendarUtil.toDatePickerString(startDate, dateTimeFormatterFactory, authenticationContext); params.put("startDate", dpStartDate); endDate.add(Calendar.DAY_OF_YEAR, -1); // timeshet report will add 1 day String dpEndDate = CalendarUtil.toDatePickerString(endDate, dateTimeFormatterFactory, authenticationContext); params.put("endDate", dpEndDate); params.put("textUtil", new TextUtil(i18nBean, timezone)); params.put("outlookDate", dateTimeFormatterFactory.formatter().withStyle(DateTimeStyle.DATE).forLoggedInUser()); params.put("weekDays", ts.getWeekDays()); params.put("userWorkLog", ts.getUserWorkLog()); params.put("weekTotalTimeSpents", ts.getWeekTotalTimeSpents()); } catch (Exception e) { e.printStackTrace(); } return params; } private CacheControl getNoCacheControl() { CacheControl noCache = new CacheControl(); noCache.setNoCache(true); return noCache; } }