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.core.Content;
37  import info.magnolia.cms.util.DateUtil;
38  import info.magnolia.context.Context;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.module.workflow.WorkflowConstants;
41  
42  import java.text.SimpleDateFormat;
43  import java.util.Calendar;
44  import java.util.Date;
45  
46  import javax.jcr.RepositoryException;
47  import openwfe.org.engine.workitem.LaunchItem;
48  
49  import org.apache.commons.lang.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  
54  /**
55   * The activation command which will launch a flow to do scheduled activation by "sleep" functionality of owfe
56   * @author jackie
57   */
58  public class ActivationFlowCommand extends PathMappedFlowCommand {
59  
60      private static final Logger log = LoggerFactory.getLogger(ActivationFlowCommand.class);
61  
62      private boolean recursive;
63  
64      /**
65       * Set the start and end date for this page
66       */
67      public void prepareLaunchItem(Context context, LaunchItem launchItem) {
68          super.prepareLaunchItem(context, launchItem);
69  
70          try {
71              Content node = MgnlContext.getSystemContext().getHierarchyManager(getRepository()).getContent(getPath());
72              updateDateAttribute(node, launchItem, WorkflowConstants.ATTRIBUTE_START_DATE);
73              updateDateAttribute(node, launchItem, WorkflowConstants.ATTRIBUTE_END_DATE);
74          }
75          catch (RepositoryException e) {
76              log.error("can't find node for path [" + getRepository() + ":" + getPath() + "]", e);
77          }
78  
79      }
80  
81      /**
82       * Set a date stored in the repository into the list of attributes of the launch item. Ignore past activation dates
83       * <ul>
84       * <li>get utc calendar from repository</li>
85       * <li>convert utc to local calendar</li>
86       * <li>get string time for open wfe from local calendar</li>
87       * <li>set string attribute of the launch item</li>
88       * </ul>
89       */
90      private void updateDateAttribute(Content node, LaunchItem launchItem, String attributeName) {
91          final SimpleDateFormat sdf = new SimpleDateFormat(WorkflowConstants.OPENWFE_DATE_FORMAT);
92          try {
93              if (node.hasNodeData(attributeName)) {
94                  Calendar cd = node.getNodeData(attributeName).getDate(); // utc calendar from repository
95                  Calendar now = DateUtil.getCurrentUTCCalendar();
96                  if (cd.before(now) && isActivationDate(attributeName)) {
97                      log.info("Ignoring past activation date:" + attributeName + " from node:" + node.getHandle());
98                  }
99                  else {
100                     String date = sdf.format(new Date(DateUtil.getLocalCalendarFromUTC(cd).getTimeInMillis()));
101                     launchItem.getAttributes().puts(attributeName, date);
102                 }
103             }
104         }
105         catch (Exception e) {
106             log.warn("cannot set date:" + attributeName + " for node" + node.getHandle(), e);
107         }
108     }
109 
110     private boolean isActivationDate(String attributeName) {
111         return ((attributeName.equals(WorkflowConstants.ATTRIBUTE_START_DATE)) || (attributeName.equals(WorkflowConstants.ATTRIBUTE_END_DATE)));
112     }
113 
114     public String getDialogName() {
115         String dialogName = super.getDialogName();
116         if(StringUtils.isEmpty(WorkflowConstants.DEFAULT_EDIT_DIALOG)){
117             return WorkflowConstants.DEFAULT_ACTIVATION_EDIT_DIALOG;
118         }
119         return dialogName;
120     }
121 
122     public boolean isRecursive() {
123         return this.recursive;
124     }
125 
126     public void setRecursive(boolean recursive) {
127         this.recursive = recursive;
128     }
129 
130     public String getWorkflowName() {
131         String workflowName = super.getWorkflowName();
132         if(super.getWorkflowName().equals(WorkflowConstants.DEFAULT_WORKFLOW)){
133             workflowName = WorkflowConstants.DEFAULT_ACTIVATION_WORKFLOW;
134         }
135         return workflowName;
136     }
137 
138     @Override
139     public void release() {
140         super.release();
141         this.recursive = false;
142     }
143 }