KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > workflow > commands > ActivationFlowCommand


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.module.workflow.commands;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.util.DateUtil;
18 import info.magnolia.context.Context;
19 import info.magnolia.module.workflow.WorkflowConstants;
20
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import javax.jcr.RepositoryException;
26
27 import openwfe.org.engine.workitem.LaunchItem;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33 /**
34  * The activation command which will launch a flow to do scheduled activation by "sleep" functionality of owfe
35  * @author jackie
36  */

37 public class ActivationFlowCommand extends FlowCommand {
38
39     private static final Logger log = LoggerFactory.getLogger(ActivationFlowCommand.class);
40
41     /**
42      * Set the start and end date for this page
43      */

44     public void prepareLaunchItem(Context context, LaunchItem launchItem) {
45         super.prepareLaunchItem(context, launchItem);
46
47         String JavaDoc repository = (String JavaDoc) context.get(Context.ATTRIBUTE_REPOSITORY);
48         String JavaDoc path = (String JavaDoc) context.get(Context.ATTRIBUTE_PATH);
49
50         try {
51             Content node = ContentRepository.getHierarchyManager(repository).getContent(path);
52             updateDateAttribute(node, launchItem, WorkflowConstants.ATTRIBUTE_START_DATE);
53             updateDateAttribute(node, launchItem, WorkflowConstants.ATTRIBUTE_END_DATE);
54         }
55         catch (RepositoryException e) {
56             log.error("can't find node for path [" + path + "]", e);
57         }
58
59     }
60
61     /**
62      * Set a date stored in the repository into the list of attributes of the launch item. Ignore past activation dates
63      * <ul>
64      * <li>get utc calendar from repository</li>
65      * <li>convert utc to local calendar</li>
66      * <li>get string time for open wfe from local calendar</li>
67      * <li>set string attribute of the launch item</li>
68      * </ul>
69      */

70     private void updateDateAttribute(Content node, LaunchItem launchItem, String JavaDoc attributeName) {
71         final SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(WorkflowConstants.OPENWFE_DATE_FORMAT);
72         try {
73             if (node.hasNodeData(attributeName)) {
74                 Calendar JavaDoc cd = node.getNodeData(attributeName).getDate(); // utc calendar from repository
75
Calendar JavaDoc now = DateUtil.getCurrentUTCCalendar();
76                 if (cd.before(now) && isActivationDate(attributeName)) {
77                     log.info("Ignoring past activation date:" + attributeName + " from node:" + node.getHandle());
78                 }
79                 else {
80                     String JavaDoc date = sdf.format(new Date JavaDoc(DateUtil.getLocalCalendarFromUTC(cd).getTimeInMillis()));
81                     launchItem.getAttributes().puts(attributeName, date);
82                 }
83             }
84         }
85         catch (Exception JavaDoc e) {
86             log.warn("cannot set date:" + attributeName + " for node" + node.getHandle(), e);
87         }
88     }
89
90     private boolean isActivationDate(String JavaDoc attributeName) {
91         return ((attributeName.equals(WorkflowConstants.ATTRIBUTE_START_DATE)) || (attributeName
92             .equals(WorkflowConstants.ATTRIBUTE_END_DATE)));
93     }
94
95 }
96
Popular Tags