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.pages;
35
36 import info.magnolia.cms.beans.config.ContentRepository;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.module.admininterface.TemplatedMVCHandler;
39 import info.magnolia.module.workflow.WorkflowConstants;
40 import info.magnolia.module.workflow.WorkflowModule;
41 import info.magnolia.module.workflow.WorkflowUtil;
42 import info.magnolia.module.workflow.flows.FlowDefinitionException;
43 import info.magnolia.module.workflow.flows.FlowDefinitionManager;
44 import info.magnolia.cms.core.HierarchyManager;
45 import info.magnolia.context.MgnlContext;
46
47 import java.sql.Date;
48 import java.sql.Timestamp;
49 import java.util.Calendar;
50 import java.util.List;
51
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54
55 import org.apache.commons.lang.StringUtils;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59
60
61
62
63 public class WorkflowUtilPage extends TemplatedMVCHandler {
64
65
66
67
68 private static final long serialVersionUID = 222L;
69
70
71
72
73 private static Logger log = LoggerFactory.getLogger(WorkflowUtilPage.class);
74
75 protected FlowDefinitionManager fdm = WorkflowModule.getFlowDefinitionManager();
76
77
78
79
80
81
82 public WorkflowUtilPage(String name, HttpServletRequest request, HttpServletResponse response) {
83 super(name, request, response);
84 }
85
86 public String getCurrentTimeStamp() {
87 return new Timestamp(System.currentTimeMillis()).toString();
88 }
89
90 private String flowName;
91
92 private String query;
93
94 private String path;
95
96 private String startDate;
97
98 private String stopDate;
99
100 private String result;
101
102 private String repository;
103
104 private String flow = "<!-- the name of the definition will get used as the upload name-->";
105
106
107
108
109
110 public String getFlow() {
111 return this.flow;
112 }
113
114
115
116
117
118 public void setFlow(String flow) {
119 this.flow = flow;
120 }
121
122
123
124
125
126 public String getFlowName() {
127 return this.flowName;
128 }
129
130
131
132
133
134 public void setFlowName(String flowName) {
135 this.flowName = flowName;
136 }
137
138
139
140
141
142 public String getPath() {
143 return this.path;
144 }
145
146
147
148
149
150 public void setPath(String path) {
151 this.path = path;
152 }
153
154
155
156
157
158 public String getQuery() {
159 return this.query;
160 }
161
162
163
164
165
166 public void setQuery(String query) {
167 this.query = query;
168 }
169
170
171
172
173
174 public String getRepository() {
175 return this.repository;
176 }
177
178
179
180
181
182 public void setRepository(String repository) {
183 this.repository = repository;
184 }
185
186
187
188
189
190 public String getStartDate() {
191 return this.startDate;
192 }
193
194
195
196
197
198 public void setStartDate(String startDate) {
199 this.startDate = startDate;
200 }
201
202
203
204
205
206 public String getStopDate() {
207 return this.stopDate;
208 }
209
210
211
212
213
214 public void setStopDate(String stopDate) {
215 this.stopDate = stopDate;
216 }
217
218
219
220
221
222 public String getResult() {
223 return this.result;
224 }
225
226 public List getFlowList() throws FlowDefinitionException {
227 return fdm.getDefinitionNames();
228 }
229
230 public String showFlow() throws FlowDefinitionException {
231 String flow;
232 flow = fdm.readDefinition(flowName);
233
234
235 if (StringUtils.isEmpty(flow)) {
236 log.error("can not find flow definition for {}", flowName);
237 result = "can not find flow definition for " + flowName;
238 }
239 setFlow(flow);
240 return VIEW_SHOW;
241 }
242
243 public String doQuery() throws Exception {
244 if (query != null && query.length() > 0) {
245 result = WorkflowUtil.getWorkItemStore().doQuery(query).toString();
246 }
247 return VIEW_SHOW;
248 }
249
250 public String updateDate() throws Exception {
251 result = setDate(path, startDate, stopDate);
252 return VIEW_SHOW;
253 }
254
255 public String launchFlow() throws Exception {
256
257 WorkflowUtil.launchFlow(repository, path, flowName);
258
259 return VIEW_SHOW;
260 }
261
262 public String upload() throws Exception {
263
264 if (flow == null) {
265 return null;
266 }
267 flow = flow.trim();
268
269 fdm.saveDefinition(flow);
270
271 result = "Workflow was uploaded successfully.";
272 return VIEW_SHOW;
273 }
274
275 private String setDate(String pathSelected, String start, String stop) throws Exception {
276
277 HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.WEBSITE);
278 Content ct;
279 try {
280 ct = hm.getContent(pathSelected);
281 Calendar start_c = Calendar.getInstance();
282 Calendar stop_c = Calendar.getInstance();
283 start_c.setTime(new Date(Timestamp.valueOf(start).getTime()));
284 ct.getNodeData(WorkflowConstants.ATTRIBUTE_START_DATE).setValue(start_c);
285 stop_c.setTime(new Date(Timestamp.valueOf(stop).getTime()));
286 ct.getNodeData(WorkflowConstants.ATTRIBUTE_END_DATE).setValue(stop_c);
287 hm.save();
288 return "set date ok. path " + pathSelected + ", start date " + start + ", stop date " + stop;
289 }
290 catch (Exception e) {
291 log.error("can not get content node for path " + pathSelected, e);
292 }
293 return "set date failed";
294 }
295
296 }