| 1 |
/**
|
| 2 |
* This file Copyright (c) 2003-2011 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.admininterface.commands;
|
| 35 |
|
| 36 |
import info.magnolia.cms.core.Content;
|
| 37 |
import info.magnolia.cms.security.AccessDeniedException;
|
| 38 |
import info.magnolia.cms.util.AlertUtil;
|
| 39 |
import info.magnolia.cms.util.ExclusiveWrite;
|
| 40 |
import info.magnolia.context.Context;
|
| 41 |
|
| 42 |
import java.util.ArrayList;
|
| 43 |
import java.util.HashMap;
|
| 44 |
import java.util.Iterator;
|
| 45 |
import java.util.List;
|
| 46 |
import java.util.Map;
|
| 47 |
|
| 48 |
import javax.jcr.RepositoryException;
|
| 49 |
import javax.jcr.version.Version;
|
| 50 |
|
| 51 |
import org.apache.commons.lang.StringUtils;
|
| 52 |
import org.slf4j.Logger;
|
| 53 |
import org.slf4j.LoggerFactory;
|
| 54 |
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Creates a version for the passed path in the website repository.
|
| 58 |
* @author Philipp Bracher
|
| 59 |
* @version $Id$
|
| 60 |
*/
|
| 61 |
public class VersionCommand extends RuleBasedCommand {
|
| 62 |
|
| 63 |
private static Logger log = LoggerFactory.getLogger(VersionCommand.class);
|
| 64 |
|
| 65 |
private boolean recursive;
|
| 66 |
|
| 67 |
private String comment;
|
| 68 |
|
| 69 |
/**
|
| 70 |
* @see info.magnolia.commands.MgnlCommand#execute(org.apache.commons.chain.Context)
|
| 71 |
*/
|
| 72 |
@Override
|
| 73 |
public boolean execute(Context ctx) {
|
| 74 |
try {
|
| 75 |
final Content node = getNode(ctx);
|
| 76 |
if (isRecursive()) {
|
| 77 |
// set versionMap and version name for this node
|
| 78 |
List versionMap = new ArrayList();
|
| 79 |
versionRecursively(node, ctx, versionMap);
|
| 80 |
ctx.setAttribute(Context.ATTRIBUTE_VERSION_MAP, versionMap, Context.LOCAL_SCOPE);
|
| 81 |
} else {
|
| 82 |
addComment(node);
|
| 83 |
Version version = node.addVersion(getRule());
|
| 84 |
ctx.setAttribute(Context.ATTRIBUTE_VERSION, version.getName(), Context.LOCAL_SCOPE);
|
| 85 |
}
|
| 86 |
}
|
| 87 |
catch (Exception e) {
|
| 88 |
log.error("can't version", e);
|
| 89 |
AlertUtil.setMessage("can't version: " + e.getMessage(), ctx);
|
| 90 |
return false;
|
| 91 |
}
|
| 92 |
return true;
|
| 93 |
}
|
| 94 |
|
| 95 |
protected void addComment(final Content node) throws AccessDeniedException, RepositoryException {
|
| 96 |
if(getComment() != null){
|
| 97 |
synchronized (ExclusiveWrite.getInstance()) {
|
| 98 |
node.getMetaData().setProperty(Context.ATTRIBUTE_COMMENT, getComment());
|
| 99 |
node.save();
|
| 100 |
}
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
private void versionRecursively(Content node, Context ctx, List versionMap) throws RepositoryException {
|
| 105 |
addComment(node);
|
| 106 |
Version version = node.addVersion(getRule());
|
| 107 |
Map entry = new HashMap();
|
| 108 |
entry.put("version", version.getName());
|
| 109 |
entry.put("uuid", node.getUUID());
|
| 110 |
versionMap.add(entry);
|
| 111 |
if(StringUtils.isEmpty((String)ctx.getAttribute(Context.ATTRIBUTE_VERSION))){
|
| 112 |
ctx.setAttribute(Context.ATTRIBUTE_VERSION, version.getName(), Context.LOCAL_SCOPE);
|
| 113 |
}
|
| 114 |
|
| 115 |
Iterator children = node.getChildren(getFilter()).iterator();
|
| 116 |
while (children.hasNext()) {
|
| 117 |
versionRecursively((Content) children.next(), ctx, versionMap);
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
| 121 |
protected Content.ContentFilter getFilter() {
|
| 122 |
Content.ContentFilter filter = new Content.ContentFilter() {
|
| 123 |
@Override
|
| 124 |
public boolean accept(Content content) {
|
| 125 |
try {
|
| 126 |
return !getRule().isAllowed(content.getJCRNode());
|
| 127 |
}
|
| 128 |
catch (RepositoryException e) {
|
| 129 |
log.error("can't get nodetype", e);
|
| 130 |
return false;
|
| 131 |
}
|
| 132 |
}
|
| 133 |
};
|
| 134 |
return filter;
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* @return is recursive versioning
|
| 139 |
*/
|
| 140 |
public boolean isRecursive() {
|
| 141 |
return recursive;
|
| 142 |
}
|
| 143 |
|
| 144 |
/**
|
| 145 |
* @param recursive
|
| 146 |
*/
|
| 147 |
public void setRecursive(boolean recursive) {
|
| 148 |
this.recursive = recursive;
|
| 149 |
}
|
| 150 |
|
| 151 |
public String getComment() {
|
| 152 |
return comment;
|
| 153 |
}
|
| 154 |
|
| 155 |
public void setComment(String comment) {
|
| 156 |
this.comment = comment;
|
| 157 |
}
|
| 158 |
|
| 159 |
@Override
|
| 160 |
public void release() {
|
| 161 |
super.release();
|
| 162 |
this.recursive = false;
|
| 163 |
}
|
| 164 |
}
|