PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-core/src/main/java/com/atlassian/jira/web/action/admin/eventtype/ListEventTypes.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 308 lines | 209 code | 55 blank | 44 comment | 38 complexity | 1c7f3af96cf51cd1aa731042e5e83504 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.web.action.admin.eventtype;
  2. import com.atlassian.jira.component.ComponentAccessor;
  3. import com.atlassian.jira.event.type.DefaultEventTypeManager;
  4. import com.atlassian.jira.event.type.EventType;
  5. import com.atlassian.jira.event.type.EventTypeManager;
  6. import com.atlassian.jira.scheme.Scheme;
  7. import com.atlassian.jira.scheme.SchemeEntity;
  8. import com.atlassian.jira.security.xsrf.RequiresXsrfCheck;
  9. import com.atlassian.jira.template.TemplateManager;
  10. import com.atlassian.jira.web.action.JiraWebActionSupport;
  11. import com.atlassian.jira.workflow.JiraWorkflow;
  12. import com.atlassian.jira.workflow.WorkflowManager;
  13. import com.atlassian.sal.api.websudo.WebSudoRequired;
  14. import com.opensymphony.util.TextUtils;
  15. import com.opensymphony.workflow.loader.ActionDescriptor;
  16. import com.opensymphony.workflow.loader.FunctionDescriptor;
  17. import org.apache.commons.collections.MultiHashMap;
  18. import org.apache.commons.collections.MultiMap;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. @WebSudoRequired
  25. public class ListEventTypes extends JiraWebActionSupport {
  26. private static final int SHORT_LIST_COUNT = 3;
  27. private final EventTypeManager eventTypeManager;
  28. private final WorkflowManager workflowManager;
  29. private final TemplateManager templateManager;
  30. private String name;
  31. private String description;
  32. private Long templateId;
  33. private String type;
  34. private Long eventTypeId;
  35. // Used to confirm edit and delete actions
  36. private boolean confirmed;
  37. private List<Scheme> allNotificationSchemes;
  38. public ListEventTypes(EventTypeManager eventTypeManager, WorkflowManager workflowManager, TemplateManager templateManager) {
  39. this.eventTypeManager = eventTypeManager;
  40. this.workflowManager = workflowManager;
  41. this.templateManager = templateManager;
  42. }
  43. // ---- Webwork actions --------------------------------------------------------------------------------------------
  44. protected String doExecute() throws Exception {
  45. return SUCCESS;
  46. }
  47. @RequiresXsrfCheck
  48. public String doAddEventType() {
  49. if (!TextUtils.stringSet(name)) {
  50. addError("name", getText("admin.event.types.errors.specify.name"));
  51. } else if (eventTypeManager.isEventTypeExists(name)) {
  52. addError("name", getText("admin.event.types.errors.not.unique"));
  53. }
  54. if (templateId == null || templateId == -1) {
  55. addError("templateId", getText("admin.event.types.errors.select.template"));
  56. }
  57. if (invalidInput())
  58. return INPUT;
  59. EventType newEventType = new EventType(name, description, templateId);
  60. eventTypeManager.addEventType(newEventType);
  61. // reset "Add New Event" fields
  62. setTemplateId(-1L);
  63. setName(null);
  64. setDescription(null);
  65. return getRedirect("ListEventTypes.jspa");
  66. }
  67. @RequiresXsrfCheck
  68. public String doDeleteEventType() {
  69. EventType eventType = eventTypeManager.getEventType(eventTypeId);
  70. if (!eventTypeManager.isActive(eventType)) {
  71. if (confirmed) {
  72. eventTypeManager.deleteEventType(eventTypeId);
  73. if (invalidInput())
  74. return ERROR;
  75. return getRedirect("ListEventTypes.jspa");
  76. } else {
  77. return INPUT;
  78. }
  79. } else {
  80. addErrorMessage(getText("admin.event.types.errors.delete.active"));
  81. return INPUT;
  82. }
  83. }
  84. @RequiresXsrfCheck
  85. public String doEditEventType() {
  86. if (confirmed) {
  87. // Validate name
  88. if (TextUtils.stringSet(name)) {
  89. // Check if name is unique or if name is not changed
  90. if (eventTypeManager.isEventTypeExists(name) && !name.equals(eventTypeManager.getEventType(eventTypeId).getName())) {
  91. addError("name", getText("admin.event.types.errors.not.unique"));
  92. }
  93. } else {
  94. addError("name", getText("admin.event.types.errors.specify.name"));
  95. }
  96. // Validate template selection
  97. if (templateId == null || templateId == -1) {
  98. addError("templateId", getText("admin.event.types.errors.select.template"));
  99. }
  100. if (invalidInput()) {
  101. return INPUT;
  102. } else {
  103. // Commit the edit
  104. eventTypeManager.editEventType(eventTypeId, name, description, templateId);
  105. // Reset the name and description so as 'AddEventType' fields are blank when returning to list view
  106. setName(null);
  107. setDescription(null);
  108. setTemplateId(-1L);
  109. return getRedirect("ListEventTypes.jspa");
  110. }
  111. } else {
  112. return INPUT;
  113. }
  114. }
  115. /**
  116. * Determine a suitable stepId for the *ViewWorkflowTransition* link in the event type list.
  117. * It is only necessary to retireve the first step id for the link as the screen for the transition is the same for
  118. * each step.
  119. * <p>
  120. * The initial step *Create Issue* does not have a related step id and is not needed for the link - so return null
  121. * in this case.
  122. *
  123. * @param workflowName
  124. * @param actionDescriptorId
  125. */
  126. public String getStepId(String workflowName, long actionDescriptorId) {
  127. return workflowManager.getStepId(actionDescriptorId, workflowName);
  128. }
  129. /**
  130. * Creates a short list of the workflow transitions limited to {@link ListEventTypes#SHORT_LIST_COUNT}
  131. *
  132. * @param transitions a collection of workflow transitions
  133. * @return List a list of workflow transitions limited to {@link ListEventTypes#SHORT_LIST_COUNT} in size
  134. */
  135. public List getShortList(Collection<ActionDescriptor> transitions) {
  136. int count = 0;
  137. List<ActionDescriptor> shortList = new ArrayList<ActionDescriptor>();
  138. for (ActionDescriptor transition : transitions) {
  139. shortList.add(transition);
  140. count++;
  141. if (count >= SHORT_LIST_COUNT) {
  142. break;
  143. }
  144. }
  145. return shortList;
  146. }
  147. public EventTypeManager getEventTypeManager() {
  148. return eventTypeManager;
  149. }
  150. /**
  151. * Determines which workflows and transitions are associated with the specified eventType.
  152. * <p>
  153. * The event type can be associated with a workflow through a post function on any of the workflow transitions.
  154. *
  155. * @param eventType event type
  156. * @return MultiMap {@link com.atlassian.jira.web.action.issue.bulkedit.WorkflowTransitionKey}s -> transitions
  157. */
  158. public MultiMap getAssociatedWorkflows(EventType eventType) {
  159. // JRA-25880 For performance we do this work in the Action, so we can re-use the results of workflowManager.getWorkflows()
  160. Collection<JiraWorkflow> workflows = getWorkflows();
  161. Long eventTypeId = eventType.getId();
  162. MultiMap workflowTransitionMap = new MultiHashMap();
  163. for (final JiraWorkflow workflow : workflows) {
  164. Map<ActionDescriptor, Collection<FunctionDescriptor>> transitionPostFunctionMap = workflowManager.getPostFunctionsForWorkflow(workflow);
  165. Collection<ActionDescriptor> keys = transitionPostFunctionMap.keySet();
  166. for (final ActionDescriptor actionDescriptor : keys) {
  167. Collection<FunctionDescriptor> postFunctions = transitionPostFunctionMap.get(actionDescriptor);
  168. for (final FunctionDescriptor functionDescriptor : postFunctions) {
  169. if (functionDescriptor.getArgs().containsKey(DefaultEventTypeManager.EVENT_TYPE_ID) &&
  170. eventTypeId.equals(new Long((String) functionDescriptor.getArgs().get(DefaultEventTypeManager.EVENT_TYPE_ID)))) {
  171. workflowTransitionMap.put(workflow.getName(), actionDescriptor);
  172. }
  173. }
  174. }
  175. }
  176. return workflowTransitionMap;
  177. }
  178. Collection<JiraWorkflow> workflows = null;
  179. private Collection<JiraWorkflow> getWorkflows() {
  180. if (workflows == null) {
  181. // Expensive call
  182. workflows = workflowManager.getWorkflows();
  183. }
  184. return workflows;
  185. }
  186. /**
  187. * Retrieve a map of scheme ids to scheme names that are associsated with this EventType
  188. *
  189. * @param eventType the EventType
  190. * @return Map of scheme ids -> scheme names
  191. */
  192. public Map<Long, String> getAssociatedNotificationSchemes(EventType eventType) {
  193. Long eventTypeId = eventType.getId();
  194. // JRA-25880 For performance we do this work in the Action, so we can re-use the results of NotificationSchemeManager.getSchemeObjects()
  195. final List<Scheme> notificationSchemes = getAllNotificationSchemes();
  196. Map<Long, String> associatedSchemes = new HashMap<Long, String>();
  197. for (Scheme notificationScheme : notificationSchemes) {
  198. for (SchemeEntity schemeEntity : notificationScheme.getEntities()) {
  199. if (eventTypeId.equals(schemeEntity.getEntityTypeId())) {
  200. associatedSchemes.put(notificationScheme.getId().longValue(), notificationScheme.getName());
  201. }
  202. }
  203. }
  204. return associatedSchemes;
  205. }
  206. private List<Scheme> getAllNotificationSchemes() {
  207. if (allNotificationSchemes == null) {
  208. // Expensive call
  209. allNotificationSchemes = ComponentAccessor.getNotificationSchemeManager().getSchemeObjects();
  210. }
  211. return allNotificationSchemes;
  212. }
  213. public TemplateManager getTemplateManager() {
  214. return templateManager;
  215. }
  216. // ---- Getters & Setters ------------------------------------------------------------------------------------------
  217. public String getName() {
  218. return name;
  219. }
  220. public void setName(String name) {
  221. this.name = name;
  222. }
  223. public String getDescription() {
  224. return description;
  225. }
  226. public void setDescription(String description) {
  227. this.description = description;
  228. }
  229. public boolean isConfirmed() {
  230. return confirmed;
  231. }
  232. public void setConfirmed(boolean confirmed) {
  233. this.confirmed = confirmed;
  234. }
  235. public Long getEventTypeId() {
  236. return eventTypeId;
  237. }
  238. public void setEventTypeId(Long eventTypeId) {
  239. this.eventTypeId = eventTypeId;
  240. }
  241. public String getType() {
  242. return type;
  243. }
  244. public void setType(String type) {
  245. this.type = type;
  246. }
  247. public Long getTemplateId() {
  248. return templateId;
  249. }
  250. public void setTemplateId(Long templateId) {
  251. this.templateId = templateId;
  252. }
  253. }