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