View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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       * Logger
72       */
73      private static Logger log = LoggerFactory.getLogger(WorkflowUtilPage.class);
74  
75      protected FlowDefinitionManager fdm = WorkflowModule.getFlowDefinitionManager();
76  
77      /**
78       * @param name
79       * @param request
80       * @param response
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      * Getter for <code>flow</code>.
108      * @return Returns the flow.
109      */
110     public String getFlow() {
111         return this.flow;
112     }
113 
114     /**
115      * Setter for <code>flow</code>.
116      * @param flow The flow to set.
117      */
118     public void setFlow(String flow) {
119         this.flow = flow;
120     }
121 
122     /**
123      * Getter for <code>flowName</code>.
124      * @return Returns the flowName.
125      */
126     public String getFlowName() {
127         return this.flowName;
128     }
129 
130     /**
131      * Setter for <code>flowName</code>.
132      * @param flowName The flowName to set.
133      */
134     public void setFlowName(String flowName) {
135         this.flowName = flowName;
136     }
137 
138     /**
139      * Getter for <code>path</code>.
140      * @return Returns the path.
141      */
142     public String getPath() {
143         return this.path;
144     }
145 
146     /**
147      * Setter for <code>path</code>.
148      * @param path The path to set.
149      */
150     public void setPath(String path) {
151         this.path = path;
152     }
153 
154     /**
155      * Getter for <code>query</code>.
156      * @return Returns the query.
157      */
158     public String getQuery() {
159         return this.query;
160     }
161 
162     /**
163      * Setter for <code>query</code>.
164      * @param query The query to set.
165      */
166     public void setQuery(String query) {
167         this.query = query;
168     }
169 
170     /**
171      * Getter for <code>repository</code>.
172      * @return Returns the repository.
173      */
174     public String getRepository() {
175         return this.repository;
176     }
177 
178     /**
179      * Setter for <code>repository</code>.
180      * @param repository The repository to set.
181      */
182     public void setRepository(String repository) {
183         this.repository = repository;
184     }
185 
186     /**
187      * Getter for <code>startDate</code>.
188      * @return Returns the startDate.
189      */
190     public String getStartDate() {
191         return this.startDate;
192     }
193 
194     /**
195      * Setter for <code>startDate</code>.
196      * @param startDate The startDate to set.
197      */
198     public void setStartDate(String startDate) {
199         this.startDate = startDate;
200     }
201 
202     /**
203      * Getter for <code>stopDate</code>.
204      * @return Returns the stopDate.
205      */
206     public String getStopDate() {
207         return this.stopDate;
208     }
209 
210     /**
211      * Setter for <code>stopDate</code>.
212      * @param stopDate The stopDate to set.
213      */
214     public void setStopDate(String stopDate) {
215         this.stopDate = stopDate;
216     }
217 
218     /**
219      * Getter for <code>result</code>.
220      * @return Returns the result.
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         // get flow by name
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         // add start date and end date
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 }