Zalmoxix

Untitled

Dec 1st, 2020
644
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.atsistemas.jira.addons.createLinkedIssue.workflow;
  2.  
  3. import java.util.Collection;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9.  
  10.  
  11. import com.atlassian.jira.component.ComponentAccessor;
  12. import com.atlassian.jira.config.IssueTypeManager;
  13. import com.atlassian.jira.event.type.EventDispatchOption;
  14. import com.atlassian.jira.exception.CreateException;
  15. import com.atlassian.jira.issue.AttachmentManager;
  16. import com.atlassian.jira.issue.CustomFieldManager;
  17. import com.atlassian.jira.issue.IssueFactory;
  18. import com.atlassian.jira.issue.IssueManager;
  19. import com.atlassian.jira.issue.MutableIssue;
  20. import com.atlassian.jira.issue.attachment.Attachment;
  21. import com.atlassian.jira.issue.comments.CommentManager;
  22. import com.atlassian.jira.issue.fields.CustomField;
  23. import com.atlassian.jira.issue.link.IssueLinkManager;
  24. import com.atlassian.jira.project.Project;
  25. import com.atlassian.jira.project.ProjectManager;
  26. import com.atlassian.jira.security.JiraAuthenticationContext;
  27. import com.atlassian.jira.user.util.UserManager;
  28. import com.atlassian.jira.workflow.function.issue.AbstractJiraFunctionProvider;
  29. import com.atlassian.pocketknife.api.commons.error.AnError;
  30. import com.atlassian.servicedesk.api.requesttype.RequestType;
  31. import com.atlassian.servicedesk.api.requesttype.RequestTypeQuery;
  32. import com.atlassian.servicedesk.api.requesttype.RequestTypeService;
  33. import com.atlassian.servicedesk.api.util.paging.PagedResponse;
  34. import com.opensymphony.module.propertyset.PropertySet;
  35. import com.opensymphony.workflow.WorkflowException;
  36. /**
  37.  * This is the post-function class that gets executed at the end of the
  38.  * transition. Any parameters that were saved in your factory class will be
  39.  * available in the transientVars Map.
  40.  */
  41. public class CreateLinkedIssuePostFunction extends AbstractJiraFunctionProvider {
  42.     private static final Logger log = LoggerFactory.getLogger(CreateLinkedIssuePostFunction.class);
  43.     public static final String SELECTED_ORIGIN_PROJECT = "originProjectSelect";
  44.     public static final String SELECTED_PROJECT = "projectSelect";
  45.     public static final String SELECTED_LINKTYPE = "linkTypeSelect";
  46.     public static final String SELECTED_ORIGIN_ISSUETYPE = "originIssueTypeSelect";
  47.     public static final String SELECTED_ISSUETYPE = "issueTypeSelect";
  48.     public static final String SELECTED_USER = "userSelect";
  49.     public static final String SELECTED_CF_CUSTOMER = "cfCustomerSelect";
  50.     public static final String SELECTED_CF_REQTYPE = "cfReqtypeSelect";
  51.  
  52.     private final ProjectManager projectManager;
  53.     private final IssueFactory issueFactory;
  54.     private final IssueManager issueManager;
  55.     private final IssueLinkManager issueLinkManager;
  56.     private final IssueTypeManager issueTypeManager;
  57.     private final AttachmentManager attachmentManager;
  58.     private final UserManager userManager;
  59.     private final CustomFieldManager customFieldManager;
  60.     private final CommentManager commentManager;
  61.     private final JiraAuthenticationContext jiraAuthenticationContext;
  62.  
  63.     public CreateLinkedIssuePostFunction(ProjectManager projectManager, IssueFactory issueFactory,
  64.             IssueManager issueManager, IssueLinkManager issueLinkManager, IssueTypeManager issueTypeManager,
  65.             AttachmentManager attachmentManager, UserManager userManager, CustomFieldManager customFieldManager,
  66.             CommentManager commentManager, JiraAuthenticationContext jiraAuthenticationContext) {
  67.         this.projectManager = projectManager;
  68.         this.issueFactory = issueFactory;
  69.         this.issueManager = issueManager;
  70.         this.issueLinkManager = issueLinkManager;
  71.         this.issueTypeManager = issueTypeManager;
  72.         this.attachmentManager = attachmentManager;
  73.         this.userManager = userManager;
  74.         this.customFieldManager = customFieldManager;
  75.         this.commentManager = commentManager;
  76.         this.jiraAuthenticationContext = jiraAuthenticationContext;
  77.     }
  78.  
  79.     public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException {
  80.  
  81.         MutableIssue issue = getIssue(transientVars);
  82.         String originProjectId = (String) args.get(SELECTED_ORIGIN_PROJECT);
  83.         String projectId = (String) args.get(SELECTED_PROJECT);
  84.         String linkTypeId = (String) args.get(SELECTED_LINKTYPE);
  85.         String originIssueTypeId = (String) args.get(SELECTED_ORIGIN_ISSUETYPE);
  86.         String issueTypeId = (String) args.get(SELECTED_ISSUETYPE);
  87.         String userKey = (String) args.get(SELECTED_USER);
  88.         String cfCustomer = (String) args.get(SELECTED_CF_CUSTOMER);
  89.         String cfReqtype = (String) args.get(SELECTED_CF_REQTYPE);
  90.        
  91.         if (issue.getProjectId().toString().equals(originProjectId)
  92.                 && issue.getIssueTypeId().equals(originIssueTypeId)) {
  93.  
  94.             final Project project = projectManager.getProjectObj(new Long(projectId));
  95.  
  96.             final MutableIssue linkedIssue = issueFactory.getIssue();
  97.             linkedIssue.setProjectObject(project);
  98.             linkedIssue.setSummary(issue.getSummary());
  99.             linkedIssue.setIssueType(issueTypeManager.getIssueType(issueTypeId));
  100.             linkedIssue.setDescription(issue.getDescription());
  101.             linkedIssue.setPriority(issue.getPriority());
  102.             linkedIssue.setComponent(issue.getComponents());
  103.             linkedIssue.setLabels(issue.getLabels());
  104.             linkedIssue.setDueDate(issue.getDueDate());
  105.             linkedIssue.setReporter(userManager.getUserByKey(userKey));
  106.  
  107.             final List<CustomField> customfields = customFieldManager.getCustomFieldObjects();
  108.             for (CustomField cs : customfields) {
  109.                 linkedIssue.setCustomFieldValue(cs, issue.getCustomFieldValue(cs));
  110.             }
  111.  
  112.             if(cfCustomer != null) {
  113.                 CustomField cf = customFieldManager.getCustomFieldObject(cfCustomer);
  114.                 if(cf != null) {
  115.                     linkedIssue.setCustomFieldValue(cf, issue.getReporterUser().getDisplayName()+" ("+issue.getReporterUser().getUsername()+")");
  116.                 }
  117.             }
  118.            
  119.             if(cfReqtype != null) {
  120.                 CustomField cf = customFieldManager.getCustomFieldObject(cfReqtype);
  121.                 if(cf != null) {
  122.                     RequestTypeService requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService.class) ;
  123.                     if(requestTypeService != null) {
  124.                         RequestTypeQuery issueQuery = requestTypeService.newQueryBuilder().issue(issue.getId()).build();
  125.                         PagedResponse<RequestType> requestTypeEither =  requestTypeService.getRequestTypes(
  126.                             jiraAuthenticationContext.getLoggedInUser(),
  127.                             issueQuery
  128.                         );
  129.                         if(requestTypeEither.getResults()==null) {
  130.                             log.error("REQUEST TYPE ERROR >>>>>>>>>>>>>>>>");
  131.                             log.error(requestTypeEither.toString());
  132.                         }else {
  133.                             if(requestTypeEither.getResults().size() > 0) {
  134.                                 RequestType requestType = requestTypeEither.getResults().get(0);
  135.                                 String reqGroup = "";
  136.                                 if(requestType.getGroups().size() > 0) {
  137.                                     reqGroup = requestType.getGroups().get(0).getName();
  138.                                 }
  139.                                 String requestTypeValue = reqGroup+" / "+requestType.getName();
  140.                                 linkedIssue.setCustomFieldValue(cf, requestTypeValue);
  141.                             }
  142.                         }
  143.                     }
  144.                 }
  145.             }
  146.  
  147.             try {
  148.                 issueManager.createIssueObject(userManager.getUserByKey(userKey), linkedIssue);
  149.                 issueLinkManager.createIssueLink(issue.getId(), linkedIssue.getId(), new Long(linkTypeId), null,
  150.                         userManager.getUserByKey(userKey));
  151.                 attachmentManager.copyAttachments(issue, userManager.getUserByKey(userKey), linkedIssue.getKey());
  152.                 issueManager.updateIssue(userManager.getUserByKey(userKey), linkedIssue,
  153.                         EventDispatchOption.ISSUE_UPDATED, false);
  154.  
  155.             } catch (CreateException e) {
  156.                 log.error("Error creating linked issue: " + e.getMessage());
  157.             }
  158.  
  159.             Collection<Attachment> attachments = issue.getAttachments();
  160.             String bodyComment = "";
  161.             if (!attachments.isEmpty()) {
  162.                 for (Attachment at : attachments) {
  163.                     if (at.getMimetype().contains("image")) {
  164.                         bodyComment = bodyComment + "\n\n!" + at.getFilename() + "|thumbnail!";
  165.                     } else {
  166.                         bodyComment = bodyComment + "\n\n[^" + at.getFilename() + "] _(" + Math.round(at.getFilesize()/1024.0) + " kB)_";
  167.                     }
  168.                 }
  169.                 commentManager.create(linkedIssue, jiraAuthenticationContext.getLoggedInUser(), bodyComment, false);
  170.             }
  171.         }
  172.     }
  173. }
RAW Paste Data