KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > commands > ActivationCommand


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.admininterface.commands;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.exchange.ExchangeException;
17 import info.magnolia.cms.i18n.MessagesManager;
18 import info.magnolia.cms.util.AlertUtil;
19 import info.magnolia.context.Context;
20
21 import java.util.*;
22
23 import javax.jcr.RepositoryException;
24 import javax.jcr.Node;
25 import javax.jcr.NodeIterator;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32 /**
33  * the activation command which do real activation
34  * @author jackie
35  * $Id: ActivationCommand.java 7655 2006-11-27 10:32:10Z scharles $
36  */

37 public class ActivationCommand extends BaseActivationCommand {
38
39     /**
40      * Log
41      */

42     private static Logger log = LoggerFactory.getLogger(ActivationCommand.class);
43
44     private boolean recursive;
45
46     private String JavaDoc versionNumber;
47
48     private List versionMap;
49
50     /**
51      * Execute activation
52      */

53     public boolean execute(Context ctx) {
54         try {
55             Content thisState;
56             if (StringUtils.isNotEmpty(getUuid())) {
57                 thisState = ctx.getHierarchyManager(getRepository()).getContentByUUID(getUuid());
58             } else {
59                 thisState = ctx.getHierarchyManager(getRepository()).getContent(getPath());
60             }
61             String JavaDoc parentPath = StringUtils.substringBeforeLast(thisState.getHandle(), "/");
62             if (StringUtils.isEmpty(parentPath)) {
63                 parentPath = "/";
64             }
65             if (StringUtils.isNotEmpty(getVersion())) {
66                 try {
67                     thisState = thisState.getVersionedContent(getVersion());
68                 } catch (RepositoryException re) {
69                     log.error("Failed to get version "+getVersion()+" for "+thisState.getHandle(), re);
70                 }
71             }
72             // make multiple activations instead of a big bulp
73
if (recursive) {
74                 List versionMap = getVersionMap();
75                 if (versionMap == null) {
76                     activateRecursive(parentPath, thisState, ctx);
77                 } else {
78                     activateRecursive(ctx, versionMap);
79                 }
80             }
81             else {
82                 getSyndicator().activate(parentPath, thisState, getOrderingInfo(thisState));
83             }
84         }
85         catch (Exception JavaDoc e) {
86             log.error("can't activate", e);
87             AlertUtil.setException(MessagesManager.get("tree.error.activate"), e, ctx);
88             return false;
89         }
90         log.info("exec successfully.");
91         return true;
92     }
93
94     /**
95      * Activate recursively. This is done one by one to send only small peaces (memory friendly).
96      * @param parentPath
97      * @param node
98      * @throws ExchangeException
99      * @throws RepositoryException
100      */

101     protected void activateRecursive(String JavaDoc parentPath, Content node, Context ctx)
102             throws ExchangeException, RepositoryException {
103
104         getSyndicator().activate(parentPath, node, getOrderingInfo(node));
105
106         Iterator children = node.getChildren(new Content.ContentFilter() {
107             public boolean accept(Content content) {
108                 try {
109                     return !getRule().isAllowed(content.getNodeTypeName());
110                 }
111                 catch (RepositoryException e) {
112                     log.error("can't get nodetype", e);
113                     return false;
114                 }
115             }
116         }).iterator();
117
118         while (children.hasNext()) {
119             this.activateRecursive(node.getHandle(), ((Content) children.next()), ctx);
120         }
121     }
122
123     /**
124      * @param ctx
125      * @param versionMap
126      * */

127     protected void activateRecursive(Context ctx, List versionMap)
128             throws ExchangeException, RepositoryException {
129         // activate all uuid's present in versionMap
130
Iterator entries = versionMap.iterator();
131         while (entries.hasNext()) {
132             Map entry = (Map) entries.next();
133             String JavaDoc uuid = (String JavaDoc) entry.get("uuid");
134             String JavaDoc versionNumber = (String JavaDoc) entry.get("version");
135             if (StringUtils.equalsIgnoreCase("class", uuid)) {
136                 // todo , this should not happen in between the serialized list, somewhere a bug
137
// for the moment simply ignore it
138
continue;
139             }
140             try {
141                 Content content = ctx.getHierarchyManager(getRepository()).getContentByUUID(uuid);
142                 // NOTE : on activation of the version it uses current hierarchy to order
143
// since admin interface does not protect the state of the hierarchy if its in workflow
144
// we have to use the current state
145
List orderedList = getOrderingInfo(content);
146                 String JavaDoc parentPath = content.getParent().getHandle();
147                 content = content.getVersionedContent(versionNumber);
148                 // add order info for the first node as it represents the parent in a tree
149
getSyndicator().activate(parentPath, content, orderedList);
150             } catch (RepositoryException re) {
151                 log.error("Failed to activate node with UUID : "+uuid);
152                 log.error(re.getMessage());
153             }
154         }
155     }
156
157     /**
158      * collect node UUID of the siblings in the exact order as it should be written on
159      * subscribers
160      * @param node
161      * */

162     private List getOrderingInfo(Content node) {
163         //do not use magnolia Content class since these objects are only meant for a single use to read UUID
164
List siblings = new ArrayList();
165         Node thisNode = node.getJCRNode();
166         try {
167             String JavaDoc thisNodeType = node.getNodeTypeName();
168             String JavaDoc thisNodeUUID = node.getUUID();
169             NodeIterator nodeIterator = thisNode.getParent().getNodes();
170             while (nodeIterator.hasNext()) { // only collect elements after this node
171
Node sibling = nodeIterator.nextNode();
172                 // skip till the actual position
173
if (sibling.isNodeType(thisNodeType)) {
174                     if (thisNodeUUID.equalsIgnoreCase(sibling.getUUID())) break;
175                 }
176             }
177             while (nodeIterator.hasNext()) {
178                 Node sibling = nodeIterator.nextNode();
179                 if (sibling.isNodeType(thisNodeType)) {
180                     siblings.add(sibling.getUUID());
181                 }
182             }
183         } catch (RepositoryException re) {
184             // do not throw this exception, if it fails simply do not add any ordering info
185
log.error("Failed to get Ordering info", re);
186         }
187         return siblings;
188     }
189
190     /**
191      * @return the recursive
192      */

193     public boolean isRecursive() {
194         return recursive;
195     }
196
197     /**
198      * @param recursive the recursive to set
199      */

200     public void setRecursive(boolean recursive) {
201         this.recursive = recursive;
202     }
203
204     /**
205      * @param number version number to be set for activation
206      * */

207     public void setVersion(String JavaDoc number) {
208         this.versionNumber = number;
209     }
210
211     /**
212      * @return version number
213      * */

214     public String JavaDoc getVersion() {
215         return this.versionNumber;
216     }
217
218     /**
219      * @param versionMap version map to be set for activation
220      * */

221     public void setVersionMap(List versionMap) {
222         this.versionMap = versionMap;
223     }
224
225     /**
226      * @return version map
227      * */

228     public List getVersionMap() {
229         return this.versionMap;
230     }
231
232
233 }
234
Popular Tags