PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/innovalog/jmwe/plugins/functions/AssignToLastRoleMemberFunction.java

https://bitbucket.org/valentijnscholten/jmwe
Java | 187 lines | 143 code | 23 blank | 21 comment | 32 complexity | 531df251147980df1bbc13c3f0fd9819 MD5 | raw file
  1. package com.innovalog.jmwe.plugins.functions;
  2. import java.util.Collection;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.servlet.http.HttpServletRequest;
  6. import com.atlassian.crowd.embedded.api.User;
  7. import com.atlassian.jira.user.util.UserManager;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.ofbiz.core.entity.GenericValue;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import webwork.action.ServletActionContext;
  13. import com.atlassian.jira.ComponentManager;
  14. import com.atlassian.jira.issue.Issue;
  15. import com.atlassian.jira.issue.IssueFieldConstants;
  16. import com.atlassian.jira.issue.ModifiedValue;
  17. import com.atlassian.jira.issue.MutableIssue;
  18. import com.atlassian.jira.issue.changehistory.ChangeHistory;
  19. import com.atlassian.jira.issue.changehistory.ChangeHistoryManager;
  20. import com.atlassian.jira.issue.util.IssueChangeHolder;
  21. import com.atlassian.jira.security.roles.ProjectRole;
  22. import com.atlassian.jira.security.roles.ProjectRoleActors;
  23. import com.atlassian.jira.security.roles.ProjectRoleManager;
  24. import com.opensymphony.module.propertyset.PropertySet;
  25. import com.opensymphony.workflow.WorkflowException;
  26. // This post function will assign the issue to the first default user of the specified role
  27. public class AssignToLastRoleMemberFunction extends AbstractPreserveChangesPostFunction
  28. {
  29. private static final Logger log = LoggerFactory.getLogger(AssignToLastRoleMemberFunction.class);
  30. private final UserManager userManager;
  31. public AssignToLastRoleMemberFunction(UserManager userManager) {
  32. this.userManager = userManager;
  33. }
  34. protected void executeFunction(
  35. Map<String, Object> transientVars, Map<String, String> args,
  36. PropertySet ps, IssueChangeHolder holder
  37. ) throws WorkflowException
  38. {
  39. HttpServletRequest request = ServletActionContext.getRequest();
  40. if (request!=null)
  41. {
  42. String[] assigneeSelected = request.getParameterValues("assignee");
  43. if( assigneeSelected != null && !assigneeSelected[0].equals("-1") && args.get("skipIfAssignee")!=null && ((String)args.get("skipIfAssignee")).equalsIgnoreCase("yes")) {
  44. // the user explicitly selected an assignee (and not "Unassigned")
  45. return;
  46. }
  47. }
  48. Long projectRoleId= null;
  49. String rawprojectRoleId=(String)args.get("jira.projectrole.id");
  50. if(StringUtils.isBlank(rawprojectRoleId))
  51. {
  52. log.warn("AssignToLastRoleMember not configured with a valid projectroleid. (no assignment will be made)");
  53. return;
  54. }
  55. try
  56. {
  57. projectRoleId = new Long(Long.parseLong(rawprojectRoleId));
  58. }
  59. catch(NumberFormatException e)
  60. {
  61. StringBuffer sb = new StringBuffer();
  62. log.warn(sb.append("AssignToLastRoleMember not configured with a valid projectroleid, the project role id: ").append(projectRoleId).append(" can not be parsed. (no assignment will be made)").toString());
  63. return;
  64. }
  65. ProjectRoleManager projectRoleManager=(ProjectRoleManager)ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class);
  66. ProjectRole projectRole=projectRoleManager.getProjectRole(projectRoleId);
  67. if(projectRole== null)
  68. {
  69. StringBuffer sb = new StringBuffer();
  70. log.warn(sb.append("AssignToLastRoleMember is configured to assign to the last user in project role that doesn\'t exist: id is ").append(projectRoleId).append(" (no assignment will be made)").toString());
  71. return;
  72. }
  73. //Get users in this role
  74. Collection users = null;
  75. User user = null;
  76. Issue genericIssue = (Issue) transientVars.get("issue");
  77. ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(projectRole, genericIssue.getProjectObject());
  78. if (actors != null) {
  79. users = actors.getUsers();
  80. }
  81. if (users != null) {
  82. //first check current assignee
  83. if (args.get("includeCurrentAssignee") != null && ((String)args.get("includeCurrentAssignee")).equalsIgnoreCase("yes"))
  84. {
  85. MutableIssue issue = (MutableIssue) transientVars.get("issue");
  86. if (issue.getModifiedFields().containsKey(IssueFieldConstants.ASSIGNEE))
  87. {
  88. ModifiedValue mv = (ModifiedValue) issue.getModifiedFields().get(IssueFieldConstants.ASSIGNEE);
  89. if (mv != null)
  90. user = (User)mv.getOldValue();
  91. else
  92. user = issue.getAssignee();
  93. }
  94. else
  95. user = issue.getAssignee();
  96. if (user!=null && users.contains(user))
  97. {
  98. // Assign the issue
  99. issue.setAssignee(user);
  100. return;
  101. }
  102. }
  103. ChangeHistoryManager changeHistoryManager = (ChangeHistoryManager)ComponentManager.getComponentInstanceOfType(ChangeHistoryManager.class);
  104. //changeHistory represents each changeset of the issue. we get each field changed per changeset below in changeItemBeans
  105. List changeHistory = changeHistoryManager.getChangeHistoriesForUser(genericIssue, null);
  106. ChangeHistory changeHistoryItem = null;
  107. List changeItemBeans = null;
  108. for (int i = changeHistory.size() - 1; i >= 0; i--) {
  109. user = null;
  110. changeHistoryItem = (ChangeHistory) changeHistory.get(i);
  111. log.debug("history change at " + changeHistoryItem.getTimePerformed().toString());
  112. //changeItemBeans is a List of the fields that were modified in this issue change
  113. changeItemBeans = changeHistoryItem.getChangeItems();
  114. java.util.Iterator it = changeItemBeans.iterator();
  115. //loop over all the fields updated in this change and look for a change in 'assignee'
  116. while ( it.hasNext() ) {
  117. GenericValue change = (GenericValue)it.next();
  118. String changedField = change.getString("field");
  119. /*
  120. * (GenericValue)change seems to have properties matching the columns of the changeitem db table
  121. * we're interested in change items where the field == 'assignee'
  122. * The old and new data is capture in two forms: oldvalue/oldstring and newvalue/newstring
  123. * The "value" appears to be the username, while the "string" is the user's full name
  124. * Rely on username as it can't be freely changed by users so shouldn't get out of synch with user profile data
  125. */
  126. if ( changedField.equalsIgnoreCase("assignee") ) {
  127. //the assignee was changed in this changeset. grab the user object and move on to see if this user is a member of the specified role
  128. log.debug("AssignToLastRoleMember history says assignee was previously " + change.getString("oldvalue"));
  129. //get a true User object based on the username
  130. user = userManager.getUser(change.getString("oldvalue"));
  131. //break out of the loop over fields changed in this changeset. NOT the loop over all changesets
  132. break;
  133. }
  134. }
  135. //if we have a real user and it is a member of the specified role
  136. if (user != null && users.contains(user)) {
  137. log.info("AssignToLastRoleMember assigning " + genericIssue.getKey() + " to: " + user.getName());
  138. // Assign the issue
  139. MutableIssue issue = (MutableIssue) transientVars.get("issue");
  140. issue.setAssignee(user);
  141. return;
  142. }
  143. }
  144. if (args.get("includeReporter") != null && ((String)args.get("includeReporter")).equalsIgnoreCase("yes"))
  145. {
  146. MutableIssue issue = (MutableIssue) transientVars.get("issue");
  147. user = issue.getReporter();
  148. if (user!=null && users.contains(user))
  149. {
  150. // Assign the issue
  151. issue.setAssignee(user);
  152. return;
  153. }
  154. }
  155. StringBuffer sb = new StringBuffer();
  156. log.warn(sb.append("AssignToRoleMember is configured to assign to the last user in project role ").append(projectRole.getName()).append(". There are was no such user found in the change history. (no assignment will be made)").toString());
  157. return;
  158. } else {
  159. StringBuffer sb = new StringBuffer();
  160. log.warn(sb.append("AssignToRoleMember is configured to assign to the last user in project role ").append(projectRole.getName()).append(". There are no users in that role. (no assignment will be made)").toString());
  161. return;
  162. }
  163. }
  164. }