1 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 34 public class VersionCommand extends RuleBasedCommand { 35 36 private static Logger log = LoggerFactory.getLogger(VersionCommand.class); 37 38 private boolean recursive; 39 40 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 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 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 )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 101 public boolean isRecursive() { 102 return recursive; 103 } 104 105 108 public void setRecursive(boolean recursive) { 109 this.recursive = recursive; 110 } 111 112 } 113 | Popular Tags |