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 @Override
69 public void execute(InstallContext ctx) throws TaskExecutionException {
70 InputStream stream = null;
71 InputStream stream30 = null;
72 try {
73 stream = ClasspathResourcesUtil.getStream("info/magnolia/module/workflow/default-activation-workflow.xml");
74 final String wfDef = IOUtils.toString(stream);
75 stream30 = ClasspathResourcesUtil.getStream("info/magnolia/module/workflow/setup/from30/activation-workflow-from-3.0-ee-bootstrap-file.xml");
76 final String wfDef30 = IOUtils.toString(stream30);
77 final Content cfg = ctx.getOrCreateCurrentModuleConfigNode();
78
79 final Content flows = ContentUtil.getOrCreateContent(cfg, "flows", ItemType.CONTENT);
80 final Content flowNode = ContentUtil.getOrCreateContent(flows, "activation", ItemType.CONTENTNODE);
81
82 if (flowNode.hasNodeData(WorkflowConstants.FLOW_VALUE)) {
83 final String oldWfDef = NodeDataUtil.getString(flowNode, WorkflowConstants.FLOW_VALUE);
84 if (StringUtils.deleteWhitespace(oldWfDef).equals(StringUtils.deleteWhitespace(wfDef30))) {
85 flowNode.setNodeData(WorkflowConstants.FLOW_VALUE, new StringValue(wfDef));
86 } else {
87 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.");
88 final Content flowNode35 = flows.createContent("activation35", ItemType.CONTENTNODE);
89 flowNode35.setNodeData(WorkflowConstants.FLOW_VALUE, new StringValue(wfDef));
90 }
91 } else {
92
93 flowNode.createNodeData(WorkflowConstants.FLOW_VALUE, wfDef);
94 }
95 } catch (IOException e) {
96 ctx.error("Could not read default activation workflow definition", e);
97 } catch (RepositoryException e) {
98 ctx.error("Could not store default activation workflow definition", e);
99 } finally {
100 if (stream != null) {
101 try {
102 stream.close();
103 stream30.close();
104 } catch (IOException e) {
105 }
106 }
107 }
108 }
109 }