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.commands;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.util.DateUtil;
38 import info.magnolia.context.Context;
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.module.workflow.WorkflowConstants;
41
42 import java.text.SimpleDateFormat;
43 import java.util.Calendar;
44 import java.util.Date;
45
46 import javax.jcr.RepositoryException;
47 import openwfe.org.engine.workitem.LaunchItem;
48
49 import org.apache.commons.lang.StringUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54
55
56
57
58 public class ActivationFlowCommand extends PathMappedFlowCommand {
59
60 private static final Logger log = LoggerFactory.getLogger(ActivationFlowCommand.class);
61
62 private boolean recursive;
63
64
65
66
67 public void prepareLaunchItem(Context context, LaunchItem launchItem) {
68 super.prepareLaunchItem(context, launchItem);
69
70 try {
71 Content node = MgnlContext.getSystemContext().getHierarchyManager(getRepository()).getContent(getPath());
72 updateDateAttribute(node, launchItem, WorkflowConstants.ATTRIBUTE_START_DATE);
73 updateDateAttribute(node, launchItem, WorkflowConstants.ATTRIBUTE_END_DATE);
74 }
75 catch (RepositoryException e) {
76 log.error("can't find node for path [" + getRepository() + ":" + getPath() + "]", e);
77 }
78
79 }
80
81
82
83
84
85
86
87
88
89
90 private void updateDateAttribute(Content node, LaunchItem launchItem, String attributeName) {
91 final SimpleDateFormat sdf = new SimpleDateFormat(WorkflowConstants.OPENWFE_DATE_FORMAT);
92 try {
93 if (node.hasNodeData(attributeName)) {
94 Calendar cd = node.getNodeData(attributeName).getDate();
95 Calendar now = DateUtil.getCurrentUTCCalendar();
96 if (cd.before(now) && isActivationDate(attributeName)) {
97 log.info("Ignoring past activation date:" + attributeName + " from node:" + node.getHandle());
98 }
99 else {
100 String date = sdf.format(new Date(DateUtil.getLocalCalendarFromUTC(cd).getTimeInMillis()));
101 launchItem.getAttributes().puts(attributeName, date);
102 }
103 }
104 }
105 catch (Exception e) {
106 log.warn("cannot set date:" + attributeName + " for node" + node.getHandle(), e);
107 }
108 }
109
110 private boolean isActivationDate(String attributeName) {
111 return ((attributeName.equals(WorkflowConstants.ATTRIBUTE_START_DATE)) || (attributeName.equals(WorkflowConstants.ATTRIBUTE_END_DATE)));
112 }
113
114 public String getDialogName() {
115 String dialogName = super.getDialogName();
116 if(StringUtils.isEmpty(WorkflowConstants.DEFAULT_EDIT_DIALOG)){
117 return WorkflowConstants.DEFAULT_ACTIVATION_EDIT_DIALOG;
118 }
119 return dialogName;
120 }
121
122 public boolean isRecursive() {
123 return this.recursive;
124 }
125
126 public void setRecursive(boolean recursive) {
127 this.recursive = recursive;
128 }
129
130 public String getWorkflowName() {
131 String workflowName = super.getWorkflowName();
132 if(super.getWorkflowName().equals(WorkflowConstants.DEFAULT_WORKFLOW)){
133 workflowName = WorkflowConstants.DEFAULT_ACTIVATION_WORKFLOW;
134 }
135 return workflowName;
136 }
137
138 @Override
139 public void release() {
140 super.release();
141 this.recursive = false;
142 }
143 }