KickJava   Java API By Example, From Geeks To Geeks.

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


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 1993-2005 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.util.AlertUtil;
17 import info.magnolia.context.Context;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import javax.jcr.RepositoryException;
24 import javax.jcr.version.Version;
25
26 import java.util.*;
27
28
29 /**
30  * Creates a version for the passed path in the website repository.
31  * @author Philipp Bracher
32  * @version $Id: VersionCommand.java 6497 2006-09-28 08:29:49Z scharles $
33  */

34 public class VersionCommand extends RuleBasedCommand {
35
36     private static Logger log = LoggerFactory.getLogger(VersionCommand.class);
37
38     private boolean recursive;
39
40     /**
41      * @see info.magnolia.commands.MgnlCommand#execute(org.apache.commons.chain.Context)
42      */

43     public boolean execute(Context ctx) {
44         try {
45             Content node;
46             if (StringUtils.isNotEmpty(getUuid())) {
47                 node = ctx.getHierarchyManager(this.getRepository()).getContentByUUID(this.getUuid());
48             } else {
49                 node = ctx.getHierarchyManager(this.getRepository()).getContent(this.getPath());
50             }
51             if (isRecursive()) {
52                 // set versionMap and version name for this node
53
List versionMap = new ArrayList();
54                 versionRecursively(node, ctx, versionMap);
55                 ctx.setAttribute(Context.ATTRIBUTE_VERSION_MAP, versionMap, Context.LOCAL_SCOPE);
56             } else {
57                 Version version = node.addVersion(getRule());
58                 ctx.setAttribute(Context.ATTRIBUTE_VERSION, version.getName(), Context.LOCAL_SCOPE);
59             }
60         }
61         catch (Exception JavaDoc e) {
62             log.error("can't version", e);
63             AlertUtil.setMessage("can't version: " + e.getMessage(), ctx);
64             return false;
65         }
66         return true;
67     }
68
69     private void versionRecursively(Content node, Context ctx, List versionMap) throws RepositoryException {
70         Version version = node.addVersion(getRule());
71         Map entry = new HashMap();
72         entry.put("version", version.getName());
73         entry.put("uuid", node.getUUID());
74         versionMap.add(entry);
75         if(StringUtils.isEmpty((String JavaDoc)ctx.getAttribute(Context.ATTRIBUTE_VERSION))){
76             ctx.setAttribute(Context.ATTRIBUTE_VERSION, version.getName(), Context.LOCAL_SCOPE);
77         }
78         Content.ContentFilter filter = new Content.ContentFilter() {
79             public boolean accept(Content content) {
80                 try {
81                     return !getRule().isAllowed(content.getNodeTypeName());
82                 }
83                 catch (RepositoryException e) {
84                     log.error("can't get nodetype", e);
85                     return false;
86                 }
87             }
88         };
89
90         Iterator children = node.getChildren(filter).iterator();
91
92         while (children.hasNext()) {
93             versionRecursively((Content) children.next(), ctx, versionMap);
94         }
95
96     }
97
98     /**
99      * @return is recursive versioning
100      */

101     public boolean isRecursive() {
102         return recursive;
103     }
104
105     /**
106      * @param recursive
107      */

108     public void setRecursive(boolean recursive) {
109         this.recursive = recursive;
110     }
111
112 }
113
Popular Tags