1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.workflow.setup.for3_5;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.ItemType;
38 import info.magnolia.cms.util.ClasspathResourcesUtil;
39 import info.magnolia.cms.util.ContentUtil;
40 import info.magnolia.cms.util.NodeDataUtil;
41 import info.magnolia.module.InstallContext;
42 import info.magnolia.module.delta.AbstractTask;
43 import info.magnolia.module.delta.TaskExecutionException;
44 import info.magnolia.module.workflow.WorkflowConstants;
45
46 import java.io.IOException;
47 import java.io.InputStream;
48
49 import javax.jcr.RepositoryException;
50
51 import org.apache.commons.io.IOUtils;
52 import org.apache.commons.lang.StringUtils;
53 import org.apache.jackrabbit.value.StringValue;
54
55
56
57
58
59
60
61
62 public class CheckAndUpdateDefaultWorkflowDefinition extends AbstractTask {
63
64 public CheckAndUpdateDefaultWorkflowDefinition() {
65 super("Update the default activation workflow definition", "Checks and updates the default activation workflow definition under the /modules/workflow/config/flows/activation config node.");
66 }
67
68 public void execute(InstallContext ctx) throws TaskExecutionException {
69 InputStream stream = null;
70 InputStream stream30 = null;
71 try {
72 stream = ClasspathResourcesUtil.getStream("info/magnolia/module/workflow/default-activation-workflow.xml");
73 final String wfDef = IOUtils.toString(stream);
74 stream30 = ClasspathResourcesUtil.getStream("info/magnolia/module/workflow/setup/from30/activation-workflow-from-3.0-ee-bootstrap-file.xml");
75 final String wfDef30 = IOUtils.toString(stream30);
76 final Content cfg = ctx.getOrCreateCurrentModuleConfigNode();
77
78 final Content flows = ContentUtil.getOrCreateContent(cfg, "flows", ItemType.CONTENT);
79 final Content flowNode = ContentUtil.getOrCreateContent(flows, "activation", ItemType.CONTENTNODE);
80
81 if (flowNode.hasNodeData(WorkflowConstants.FLOW_VALUE)) {
82 final String oldWfDef = NodeDataUtil.getString(flowNode, WorkflowConstants.FLOW_VALUE);
83 if (StringUtils.deleteWhitespace(oldWfDef).equals(StringUtils.deleteWhitespace(wfDef30))) {
84 flowNode.setNodeData(WorkflowConstants.FLOW_VALUE, new StringValue(wfDef));
85 } else {
86 ctx.warn("The activation workflow definition has been modified. Magnolia will not update it but instead install the new workflow definition for 3.5 in /modules/workflow/config/flows/activation35. Please review the changes manually.");
87 final Content flowNode35 = flows.createContent("activation35", ItemType.CONTENTNODE);
88 flowNode35.setNodeData(WorkflowConstants.FLOW_VALUE, new StringValue(wfDef));
89 }
90 } else {
91
92 flowNode.createNodeData(WorkflowConstants.FLOW_VALUE, wfDef);
93 }
94 } catch (IOException e) {
95 ctx.error("Could not read default activation workflow definition", e);
96 } catch (RepositoryException e) {
97 ctx.error("Could not store default activation workflow definition", e);
98 } finally {
99 if (stream != null) {
100 try {
101 stream.close();
102 stream30.close();
103 } catch (IOException e) {
104 }
105 }
106 }
107 }
108 }