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