View Javadoc

1   /**
2    * This file Copyright (c) 2003-2012 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.core.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.module.admininterface.TemplatedMVCHandler;
40  import info.magnolia.module.workflow.WorkflowConstants;
41  import info.magnolia.module.workflow.WorkflowModule;
42  import info.magnolia.module.workflow.WorkflowUtil;
43  import info.magnolia.module.workflow.flows.FlowDefinitionException;
44  import info.magnolia.module.workflow.flows.FlowDefinitionManager;
45  import info.magnolia.repository.RepositoryConstants;
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.StringEscapeUtils;
56  import org.apache.commons.lang.StringUtils;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  
61  /**
62   * A {@link TemplatedMVCHandler} providing utility/test functions for workflows.
63   */
64  public class WorkflowUtilPage extends TemplatedMVCHandler {
65  
66      private static final long serialVersionUID = 222L;
67  
68      private static final Logger log = LoggerFactory.getLogger(WorkflowUtilPage.class);
69  
70      protected FlowDefinitionManager fdm = WorkflowModule.getFlowDefinitionManager();
71  
72      public WorkflowUtilPage(String name, HttpServletRequest request, HttpServletResponse response) {
73          super(name, request, response);
74      }
75  
76      public String getCurrentTimeStamp() {
77          return new Timestamp(System.currentTimeMillis()).toString();
78      }
79  
80      private String flowName;
81  
82      private String query;
83  
84      private String path;
85  
86      private String startDate;
87  
88      private String stopDate;
89  
90      private String result;
91  
92      private String repository;
93  
94      private String flow = "<!-- the name of the definition will get used as the upload name-->";
95  
96      public String getFlow() {
97          return this.flow;
98      }
99  
100     public void setFlow(String flow) {
101         this.flow = flow;
102     }
103 
104     public String getFlowName() {
105         return this.flowName;
106     }
107 
108     public void setFlowName(String flowName) {
109         this.flowName = StringEscapeUtils.escapeHtml(flowName); //escape to prevent XSS attack
110     }
111 
112     @Override
113     public void setCommand(String command) {
114         super.setCommand(StringEscapeUtils.escapeHtml(command)); //escape to prevent XSS attack
115     }
116 
117     public String getPath() {
118         return this.path;
119     }
120 
121     public void setPath(String path) {
122         this.path = path;
123     }
124 
125     public String getQuery() {
126         return this.query;
127     }
128 
129     public void setQuery(String query) {
130         this.query = query;
131     }
132 
133     public String getRepository() {
134         return this.repository;
135     }
136 
137     public void setRepository(String repository) {
138         this.repository = repository;
139     }
140 
141     public String getStartDate() {
142         return this.startDate;
143     }
144 
145     public void setStartDate(String startDate) {
146         this.startDate = startDate;
147     }
148 
149     public String getStopDate() {
150         return this.stopDate;
151     }
152 
153     public void setStopDate(String stopDate) {
154         this.stopDate = stopDate;
155     }
156 
157     public String getResult() {
158         return this.result;
159     }
160 
161     public List getFlowList() throws FlowDefinitionException {
162         return fdm.getDefinitionNames();
163     }
164 
165     public String showFlow() throws FlowDefinitionException {
166         String flow;
167         flow = fdm.readDefinition(flowName);
168 
169         // get flow by name
170         if (StringUtils.isEmpty(flow)) {
171             log.error("can not find flow definition for {}", flowName);
172             result = "can not find flow definition for " + flowName;
173         }
174         setFlow(flow);
175         return VIEW_SHOW;
176     }
177 
178     public String doQuery() throws Exception {
179         if (query != null && query.length() > 0) {
180             result = WorkflowUtil.getWorkItemStore().doQuery(query).toString();
181         }
182         return VIEW_SHOW;
183     }
184 
185     public String updateDate() throws Exception {
186         result = setDate(path, startDate, stopDate);
187         return VIEW_SHOW;
188     }
189 
190     public String launchFlow() throws Exception {
191 
192         WorkflowUtil.launchFlow(repository, path, flowName);
193 
194         return VIEW_SHOW;
195     }
196 
197     public String upload() throws Exception {
198 
199         if (flow == null) {
200             return null;
201         }
202         flow = flow.trim();
203 
204         fdm.saveDefinition(flow);
205 
206         result = "Workflow was uploaded successfully.";
207         return VIEW_SHOW;
208     }
209 
210     private String setDate(String pathSelected, String start, String stop) throws Exception {
211         // add start date and end date
212         HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(RepositoryConstants.WEBSITE);
213         Content ct;
214         try {
215             ct = hm.getContent(pathSelected);
216             Calendar start_c = Calendar.getInstance();
217             Calendar stop_c = Calendar.getInstance();
218             start_c.setTime(new Date(Timestamp.valueOf(start).getTime()));
219             ct.getNodeData(WorkflowConstants.ATTRIBUTE_START_DATE).setValue(start_c);
220             stop_c.setTime(new Date(Timestamp.valueOf(stop).getTime()));
221             ct.getNodeData(WorkflowConstants.ATTRIBUTE_END_DATE).setValue(stop_c);
222             hm.save();
223             return "set date ok. path " + pathSelected + ", start date " + start + ", stop date " + stop;
224         }
225         catch (Exception e) {
226             log.error("can not get content node for path " + pathSelected, e);
227         }
228         return "set date failed";
229     }
230 
231 }