PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/webs/kaleo-web/docroot/WEB-INF/src/com/liferay/portal/workflow/kaleo/parser/XMLWorkflowModelParser.java

https://github.com/l15k4/liferay-plugins
Java | 669 lines | 452 code | 199 blank | 18 comment | 59 complexity | 0d001d1f331aada1e31f49340617da77 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.workflow.kaleo.parser;
  15. import com.liferay.portal.kernel.util.GetterUtil;
  16. import com.liferay.portal.kernel.util.Validator;
  17. import com.liferay.portal.kernel.workflow.WorkflowException;
  18. import com.liferay.portal.kernel.xml.Document;
  19. import com.liferay.portal.kernel.xml.Element;
  20. import com.liferay.portal.kernel.xml.SAXReaderUtil;
  21. import com.liferay.portal.workflow.kaleo.definition.Action;
  22. import com.liferay.portal.workflow.kaleo.definition.ActionAware;
  23. import com.liferay.portal.workflow.kaleo.definition.AddressRecipient;
  24. import com.liferay.portal.workflow.kaleo.definition.Assignment;
  25. import com.liferay.portal.workflow.kaleo.definition.Condition;
  26. import com.liferay.portal.workflow.kaleo.definition.Definition;
  27. import com.liferay.portal.workflow.kaleo.definition.DelayDuration;
  28. import com.liferay.portal.workflow.kaleo.definition.DurationScale;
  29. import com.liferay.portal.workflow.kaleo.definition.Fork;
  30. import com.liferay.portal.workflow.kaleo.definition.Join;
  31. import com.liferay.portal.workflow.kaleo.definition.Node;
  32. import com.liferay.portal.workflow.kaleo.definition.Notification;
  33. import com.liferay.portal.workflow.kaleo.definition.NotificationAware;
  34. import com.liferay.portal.workflow.kaleo.definition.ResourceActionAssignment;
  35. import com.liferay.portal.workflow.kaleo.definition.RoleAssignment;
  36. import com.liferay.portal.workflow.kaleo.definition.RoleRecipient;
  37. import com.liferay.portal.workflow.kaleo.definition.ScriptAssignment;
  38. import com.liferay.portal.workflow.kaleo.definition.State;
  39. import com.liferay.portal.workflow.kaleo.definition.Task;
  40. import com.liferay.portal.workflow.kaleo.definition.Timer;
  41. import com.liferay.portal.workflow.kaleo.definition.Transition;
  42. import com.liferay.portal.workflow.kaleo.definition.UserAssignment;
  43. import com.liferay.portal.workflow.kaleo.definition.UserRecipient;
  44. import java.io.InputStream;
  45. import java.util.Collections;
  46. import java.util.HashSet;
  47. import java.util.List;
  48. import java.util.Set;
  49. /**
  50. * @author Michael C. Han
  51. * @author Marcellus Tavares
  52. * @author Eduardo Lundgren
  53. */
  54. public class XMLWorkflowModelParser implements WorkflowModelParser {
  55. public Definition parse(InputStream inputStream) throws WorkflowException {
  56. try {
  57. return doParse(inputStream);
  58. }
  59. catch (Exception e) {
  60. throw new WorkflowException("Unable to parse definition", e);
  61. }
  62. }
  63. public void setValidate(boolean validate) {
  64. _validate = validate;
  65. }
  66. protected Definition doParse(InputStream inputStream) throws Exception {
  67. Document document = SAXReaderUtil.read(inputStream, _validate);
  68. Element rootElement = document.getRootElement();
  69. String name = rootElement.elementText("name");
  70. String description = rootElement.elementText("description");
  71. int version = GetterUtil.getInteger(rootElement.elementText("version"));
  72. Definition definition = new Definition(
  73. name, description, document.formattedString(), version);
  74. List<Element> conditionElements = rootElement.elements("condition");
  75. for (Element conditionElement : conditionElements) {
  76. Condition condition = parseCondition(conditionElement);
  77. definition.addNode(condition);
  78. }
  79. List<Element> forkElements = rootElement.elements("fork");
  80. for (Element forkElement : forkElements) {
  81. Fork fork = parseFork(forkElement);
  82. definition.addNode(fork);
  83. }
  84. List<Element> joinElements = rootElement.elements("join");
  85. for (Element joinElement : joinElements) {
  86. Join join = parseJoin(joinElement);
  87. definition.addNode(join);
  88. }
  89. List<Element> stateElements = rootElement.elements("state");
  90. for (Element stateElement : stateElements) {
  91. State state = parseState(stateElement);
  92. definition.addNode(state);
  93. }
  94. List<Element> taskElements = rootElement.elements("task");
  95. for (Element taskElement : taskElements) {
  96. Task task = parseTask(taskElement);
  97. definition.addNode(task);
  98. }
  99. parseTransitions(
  100. definition, conditionElements, forkElements,
  101. joinElements, stateElements, taskElements);
  102. return definition;
  103. }
  104. protected void parseActionElements(
  105. List<Element> actionElements, ActionAware actionAware) {
  106. if (actionElements.isEmpty()) {
  107. return;
  108. }
  109. Set<Action> actions = new HashSet<Action>(actionElements.size());
  110. for (Element actionElement : actionElements) {
  111. String name = actionElement.elementText("name");
  112. String description = actionElement.elementText("description");
  113. String executionType = actionElement.elementText("execution-type");
  114. String script = actionElement.elementText("script");
  115. String language = actionElement.elementText("script-language");
  116. int priority = GetterUtil.getInteger(
  117. actionElement.elementText("priority"));
  118. Action action = new Action(
  119. name, description, executionType, script, language, priority);
  120. actions.add(action);
  121. }
  122. actionAware.setActions(actions);
  123. }
  124. protected void parseActionsElement(Element actionsElement, Node node) {
  125. if (actionsElement == null) {
  126. return;
  127. }
  128. List<Element> actionElements = actionsElement.elements("action");
  129. parseActionElements(actionElements, node);
  130. List<Element> notificationElements = actionsElement.elements(
  131. "notification");
  132. parseNotificationElements(notificationElements, node);
  133. }
  134. protected Set<Assignment> parseAssignments(Element assignmentsElement) {
  135. if (assignmentsElement == null) {
  136. return Collections.emptySet();
  137. }
  138. Set<Assignment> assignments = new HashSet<Assignment>();
  139. Element resourceActionsElement = assignmentsElement.element(
  140. "resource-actions");
  141. if (resourceActionsElement != null) {
  142. List<Element> resourceActionElements =
  143. resourceActionsElement.elements("resource-action");
  144. for (Element resourceActionElement : resourceActionElements) {
  145. String actionId = resourceActionElement.getText();
  146. if (Validator.isNotNull(actionId)) {
  147. ResourceActionAssignment resourceActionAssignment =
  148. new ResourceActionAssignment(actionId);
  149. assignments.add(resourceActionAssignment);
  150. }
  151. }
  152. }
  153. Element rolesElement = assignmentsElement.element("roles");
  154. if (rolesElement != null) {
  155. List<Element> roleAssignmentElements = rolesElement.elements(
  156. "role");
  157. for (Element roleAssignmentElement : roleAssignmentElements) {
  158. long roleId = GetterUtil.getLong(
  159. roleAssignmentElement.elementText("role-id"));
  160. String roleType = roleAssignmentElement.elementText(
  161. "role-type");
  162. String name = roleAssignmentElement.elementText("name");
  163. RoleAssignment roleAssignment = null;
  164. if (Validator.isNotNull(name)) {
  165. roleAssignment = new RoleAssignment(name, roleType);
  166. boolean autoCreate = GetterUtil.getBoolean(
  167. roleAssignmentElement.elementText("auto-create"), true);
  168. roleAssignment.setAutoCreate(autoCreate);
  169. }
  170. else {
  171. roleAssignment = new RoleAssignment(roleId, roleType);
  172. }
  173. assignments.add(roleAssignment);
  174. }
  175. }
  176. List<Element> scriptedAssignmentElements = assignmentsElement.elements(
  177. "scripted-assignment");
  178. for (Element scriptedAssignmentElement : scriptedAssignmentElements) {
  179. String script = scriptedAssignmentElement.elementText("script");
  180. String scriptLanguage = scriptedAssignmentElement.elementText(
  181. "script-language");
  182. ScriptAssignment scriptAssignment = new ScriptAssignment(
  183. script, scriptLanguage);
  184. assignments.add(scriptAssignment);
  185. }
  186. List<Element> userAssignmentElements = assignmentsElement.elements(
  187. "user");
  188. for (Element userAssignmentElement : userAssignmentElements) {
  189. long userId = GetterUtil.getLong(
  190. userAssignmentElement.elementText("user-id"));
  191. String screenName = userAssignmentElement.elementText(
  192. "screen-name");
  193. String emailAddress = userAssignmentElement.elementText(
  194. "email-address");
  195. UserAssignment userAssignment = new UserAssignment(
  196. userId, screenName, emailAddress);
  197. assignments.add(userAssignment);
  198. }
  199. return assignments;
  200. }
  201. protected Condition parseCondition(Element conditionElement) {
  202. String name = conditionElement.elementText("name");
  203. String description = conditionElement.elementText("description");
  204. String script = conditionElement.elementText("script");
  205. String scriptLanguage = conditionElement.elementText("script-language");
  206. Condition condition = new Condition(
  207. name, description, script, scriptLanguage);
  208. String metadata = conditionElement.elementText("metadata");
  209. condition.setMetadata(metadata);
  210. Element actionsElement = conditionElement.element("actions");
  211. parseActionsElement(actionsElement, condition);
  212. Element timersElement = conditionElement.element("timers");
  213. parseTimerElements(timersElement, condition);
  214. return condition;
  215. }
  216. protected DelayDuration parseDelay(Element delayElement) {
  217. if (delayElement == null) {
  218. return null;
  219. }
  220. double duration = GetterUtil.getDouble(
  221. delayElement.elementText("duration"));
  222. DurationScale durationScale = DurationScale.parse(
  223. delayElement.elementText("scale"));
  224. return new DelayDuration(duration, durationScale);
  225. }
  226. protected Fork parseFork(Element forkElement) {
  227. String name = forkElement.elementText("name");
  228. String description = forkElement.elementText("description");
  229. Fork fork = new Fork(name, description);
  230. String metadata = forkElement.elementText("metadata");
  231. fork.setMetadata(metadata);
  232. Element actionsElement = forkElement.element("actions");
  233. parseActionsElement(actionsElement, fork);
  234. Element timersElement = forkElement.element("timers");
  235. parseTimerElements(timersElement, fork);
  236. return fork;
  237. }
  238. protected Join parseJoin(Element joinElement) {
  239. String name = joinElement.elementText("name");
  240. String description = joinElement.elementText("description");
  241. Join join = new Join(name, description);
  242. String metadata = joinElement.elementText("metadata");
  243. join.setMetadata(metadata);
  244. Element actionsElement = joinElement.element("actions");
  245. parseActionsElement(actionsElement, join);
  246. Element timersElement = joinElement.element("timers");
  247. parseTimerElements(timersElement, join);
  248. return join;
  249. }
  250. protected void parseNotificationElements(
  251. List<Element> notificationElements,
  252. NotificationAware notificationAware) {
  253. if (notificationElements.isEmpty()) {
  254. return;
  255. }
  256. Set<Notification> notifications = new HashSet<Notification>(
  257. notificationElements.size());
  258. for (Element notificationElement : notificationElements) {
  259. String name = notificationElement.elementText("name");
  260. String description = notificationElement.elementText("description");
  261. String executionType = notificationElement.elementText(
  262. "execution-type");
  263. String template = notificationElement.elementText("template");
  264. String templateLanguage = notificationElement.elementText(
  265. "template-language");
  266. Notification notification = new Notification(
  267. name, description, executionType, template, templateLanguage);
  268. List<Element> notificationTypeElements =
  269. notificationElement.elements("notification-type");
  270. for (Element notificationTypeElement : notificationTypeElements) {
  271. notification.addNotificationType(
  272. notificationTypeElement.getText());
  273. }
  274. Element recipientsElement = notificationElement.element(
  275. "recipients");
  276. parseRecipients(recipientsElement, notification);
  277. notifications.add(notification);
  278. }
  279. notificationAware.setNotifications(notifications);
  280. }
  281. protected void parseRecipients(
  282. Element recipientsElement, Notification notification) {
  283. if (recipientsElement == null) {
  284. return;
  285. }
  286. List<Element> addressRecipientElements = recipientsElement.elements(
  287. "address");
  288. for (Element addressRecipientElement : addressRecipientElements) {
  289. AddressRecipient addressRecipient = new AddressRecipient(
  290. addressRecipientElement.getText());
  291. notification.addRecipients(addressRecipient);
  292. }
  293. List<Element> roleReceipientElements = recipientsElement.elements(
  294. "role");
  295. for (Element roleAssignmentElement : roleReceipientElements) {
  296. long roleId = GetterUtil.getLong(
  297. roleAssignmentElement.elementText("role-id"));
  298. String roleType = roleAssignmentElement.elementText("role-type");
  299. String name = roleAssignmentElement.elementText("name");
  300. RoleRecipient roleRecipient = null;
  301. if (roleId > 0) {
  302. roleRecipient = new RoleRecipient(roleId, roleType);
  303. }
  304. else {
  305. roleRecipient = new RoleRecipient(name, roleType);
  306. boolean autoCreate = GetterUtil.getBoolean(
  307. roleAssignmentElement.elementText("auto-create"), true);
  308. roleRecipient.setAutoCreate(autoCreate);
  309. }
  310. notification.addRecipients(roleRecipient);
  311. }
  312. List<Element> userRecipientElements = recipientsElement.elements(
  313. "user");
  314. for (Element userRecipientElement : userRecipientElements) {
  315. long userId = GetterUtil.getLong(
  316. userRecipientElement.elementText("user-id"));
  317. String screenName = userRecipientElement.elementText("screen-name");
  318. String emailAddress = userRecipientElement.elementText(
  319. "email-address");
  320. UserRecipient userRecipient = new UserRecipient(
  321. userId, screenName, emailAddress);
  322. notification.addRecipients(userRecipient);
  323. }
  324. }
  325. protected State parseState(Element stateElement) {
  326. String name = stateElement.elementText("name");
  327. String description = stateElement.elementText("description");
  328. boolean initial = GetterUtil.getBoolean(
  329. stateElement.elementText("initial"), false);
  330. State state = new State(name, description, initial);
  331. String metadata = stateElement.elementText("metadata");
  332. state.setMetadata(metadata);
  333. Element actionsElement = stateElement.element("actions");
  334. parseActionsElement(actionsElement, state);
  335. Element timersElement = stateElement.element("timers");
  336. parseTimerElements(timersElement, state);
  337. return state;
  338. }
  339. protected Task parseTask(Element taskElement) {
  340. String name = taskElement.elementText("name");
  341. String description = taskElement.elementText("description");
  342. Task task = new Task(name, description);
  343. String metadata = taskElement.elementText("metadata");
  344. task.setMetadata(metadata);
  345. Element actionsElement = taskElement.element("actions");
  346. parseActionsElement(actionsElement, task);
  347. Element assignmentsElement = taskElement.element("assignments");
  348. if (assignmentsElement != null) {
  349. Set<Assignment> assignments = parseAssignments(assignmentsElement);
  350. task.setAssignments(assignments);
  351. }
  352. Element timersElement = taskElement.element("task-timers");
  353. parseTaskTimerElements(timersElement, task);
  354. return task;
  355. }
  356. protected void parseTaskTimerElements(
  357. Element taskTimersElement, Node node) {
  358. if (taskTimersElement == null) {
  359. return;
  360. }
  361. List<Element> taskTimerElements = taskTimersElement.elements(
  362. "task-timer");
  363. if (taskTimerElements.isEmpty()) {
  364. return;
  365. }
  366. Set<Timer> timers = new HashSet<Timer>(taskTimerElements.size());
  367. for (Element timerElement : taskTimerElements) {
  368. Timer timer = parseTimerElement(timerElement, true);
  369. timers.add(timer);
  370. }
  371. node.setTimers(timers);
  372. }
  373. protected void parseTimerActions(Element timersElement, Timer timer) {
  374. if (timersElement == null) {
  375. return;
  376. }
  377. List<Element> timerActionElements = timersElement.elements(
  378. "timer-action");
  379. parseActionElements(timerActionElements, timer);
  380. List<Element> timerNotificationElements = timersElement.elements(
  381. "timer-notification");
  382. parseNotificationElements(timerNotificationElements, timer);
  383. Element reassignmentsElement = timersElement.element("reassignments");
  384. if (reassignmentsElement != null) {
  385. Set<Assignment> assignments = parseAssignments(
  386. reassignmentsElement);
  387. timer.setReassignments(assignments);
  388. }
  389. }
  390. protected Timer parseTimerElement(
  391. Element timerElement, boolean isTaskTimer) {
  392. String name = timerElement.elementText("name");
  393. String description = timerElement.elementText("description");
  394. boolean blocking = GetterUtil.getBoolean(
  395. timerElement.elementText("blocking"), !isTaskTimer);
  396. Timer timer = new Timer(name, description, blocking);
  397. Element delayElement = timerElement.element("delay");
  398. DelayDuration delayDuration = parseDelay(delayElement);
  399. timer.setDelayDuration(delayDuration);
  400. if (!blocking) {
  401. Element recurrenceElement = timerElement.element("recurrence");
  402. DelayDuration recurrence = parseDelay(recurrenceElement);
  403. timer.setRecurrence(recurrence);
  404. }
  405. Element timerActions = timerElement.element("timer-actions");
  406. parseTimerActions(timerActions, timer);
  407. return timer;
  408. }
  409. protected void parseTimerElements(Element timersElement, Node node) {
  410. if (timersElement == null) {
  411. return;
  412. }
  413. List<Element> timerElements = timersElement.elements("timer");
  414. if (timerElements.isEmpty()) {
  415. return;
  416. }
  417. Set<Timer> timers = new HashSet<Timer>(timerElements.size());
  418. for (Element timerElement : timerElements) {
  419. Timer timer = parseTimerElement(timerElement, false);
  420. timers.add(timer);
  421. }
  422. node.setTimers(timers);
  423. }
  424. protected void parseTransition(Definition definition, Element nodeElement) {
  425. String sourceName = nodeElement.elementText("name");
  426. Node sourceNode = definition.getNode(sourceName);
  427. Element transitionsElement = nodeElement.element("transitions");
  428. if (transitionsElement == null) {
  429. return;
  430. }
  431. List<Element> transitionElements = transitionsElement.elements(
  432. "transition");
  433. for (Element transitionElement : transitionElements) {
  434. String transitionName = transitionElement.elementText("name");
  435. String targetName = transitionElement.elementText("target");
  436. Node targetNode = definition.getNode(targetName);
  437. boolean defaultValue = GetterUtil.getBoolean(
  438. transitionElement.elementText("default"), true);
  439. Transition transition = new Transition(
  440. transitionName, sourceNode, targetNode, defaultValue);
  441. Element timerElement = transitionElement.element("timer");
  442. if (timerElement != null) {
  443. Timer timer = parseTimerElement(timerElement, false);
  444. transition.setTimers(timer);
  445. }
  446. sourceNode.addTransition(transition);
  447. }
  448. }
  449. protected void parseTransitions(
  450. Definition definition, List<Element> conditionElements,
  451. List<Element> forkElements, List<Element> joinElements,
  452. List<Element> stateElements, List<Element> taskElements) {
  453. for (Element conditionElement : conditionElements) {
  454. parseTransition(definition, conditionElement);
  455. }
  456. for (Element forkElement : forkElements) {
  457. parseTransition(definition, forkElement);
  458. }
  459. for (Element joinElement : joinElements) {
  460. parseTransition(definition, joinElement);
  461. }
  462. for (Element stateElement : stateElements) {
  463. parseTransition(definition, stateElement);
  464. }
  465. for (Element taskElement : taskElements) {
  466. parseTransition(definition, taskElement);
  467. }
  468. }
  469. private boolean _validate;
  470. }