View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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.commands;
35  
36  import info.magnolia.cms.util.DateUtil;
37  import info.magnolia.commands.MgnlCommand;
38  import info.magnolia.context.Context;
39  import info.magnolia.module.workflow.WorkflowConstants;
40  import info.magnolia.module.workflow.WorkflowUtil;
41  import info.magnolia.module.workflow.flows.FlowDefinitionException;
42  
43  import java.io.Serializable;
44  import java.text.ParseException;
45  import java.util.Calendar;
46  import java.util.Date;
47  import java.util.HashMap;
48  import java.util.Iterator;
49  import java.util.Map;
50  
51  import openwfe.org.engine.workitem.AttributeUtils;
52  import openwfe.org.engine.workitem.LaunchItem;
53  import openwfe.org.engine.workitem.StringMapAttribute;
54  
55  import org.apache.commons.lang.time.DateFormatUtils;
56  import org.apache.commons.lang.time.DateUtils;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  
61  /**
62   * Base command class for starting workflows.
63   */
64  public class FlowCommand extends MgnlCommand {
65      private static final Logger log = LoggerFactory.getLogger(FlowCommand.class);
66  
67      /**
68       * The name of the workflow to start.
69       */
70      private String workflowName = WorkflowConstants.DEFAULT_WORKFLOW;
71  
72      /**
73       * The dialog used in the inbox.
74       */
75      private String dialogName = WorkflowConstants.DEFAULT_EDIT_DIALOG;
76  
77      @Override
78      public boolean execute(Context ctx) throws FlowDefinitionException {
79          // Get the references
80          LaunchItem li = new LaunchItem();
81          prepareLaunchItem(ctx, li);
82          WorkflowUtil.launchFlow(li, getWorkflowName());
83          return true;
84      }
85  
86      /**
87       * The default implementation puts all the contexts attributes which are in the request scope into the work item.
88       * @param context
89       * @param launchItem
90       */
91      public void prepareLaunchItem(Context context, LaunchItem launchItem) {
92          Map map = context.getAttributes(Context.LOCAL_SCOPE);
93          // create map for workflowItem with all serializable entries from the context
94          Map serializableMap = new HashMap();
95          for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
96              Object key = iter.next();
97              Object val = map.get(key);
98              if (val instanceof Serializable) {
99                  // transform dates
100                 if(val instanceof String){
101                     try{
102                         Date date = DateUtils.parseDate((String)val, new String[]{DateUtil.YYYY_MM_DD_T_HH_MM_SS, DateUtil.YYYY_MM_DD});
103                         Calendar cal = Calendar.getInstance();
104                         cal.setTime(date);
105                         val = DateFormatUtils.format(cal, WorkflowConstants.OPENWFE_DATE_FORMAT);
106                     }
107                     catch(ParseException e){
108                         // its not a date string
109                     }
110                 }
111                 serializableMap.put(key, val);
112             }
113         }
114         serializableMap.put(WorkflowConstants.ATTRIBUTE_USERNAME, context.getUser().getName());
115         StringMapAttribute attrs = AttributeUtils.java2attributes(serializableMap);
116         launchItem.setAttributes(attrs);
117         // set the dialog to use in the inbox
118         launchItem.getAttributes().sput(WorkflowConstants.ATTRIBUTE_EDIT_DIALOG, getDialogName());
119     }
120 
121     public String getWorkflowName() {
122         return workflowName;
123     }
124 
125     public void setWorkflowName(String flowName) {
126         this.workflowName = flowName;
127     }
128 
129 
130     public String getDialogName() {
131         return this.dialogName;
132     }
133 
134 
135     public void setDialogName(String dialogName) {
136         this.dialogName = dialogName;
137     }
138 
139     @Override
140     public void release() {
141         super.release();
142         workflowName = WorkflowConstants.DEFAULT_WORKFLOW;
143         dialogName = WorkflowConstants.DEFAULT_EDIT_DIALOG;
144     }
145 }