1 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 37 public class ActivationCommand extends BaseActivationCommand { 38 39 42 private static Logger log = LoggerFactory.getLogger(ActivationCommand.class); 43 44 private boolean recursive; 45 46 private String versionNumber; 47 48 private List versionMap; 49 50 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 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 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 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 101 protected void activateRecursive(String 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 127 protected void activateRecursive(Context ctx, List versionMap) 128 throws ExchangeException, RepositoryException { 129 Iterator entries = versionMap.iterator(); 131 while (entries.hasNext()) { 132 Map entry = (Map) entries.next(); 133 String uuid = (String ) entry.get("uuid"); 134 String versionNumber = (String ) entry.get("version"); 135 if (StringUtils.equalsIgnoreCase("class", uuid)) { 136 continue; 139 } 140 try { 141 Content content = ctx.getHierarchyManager(getRepository()).getContentByUUID(uuid); 142 List orderedList = getOrderingInfo(content); 146 String parentPath = content.getParent().getHandle(); 147 content = content.getVersionedContent(versionNumber); 148 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 162 private List getOrderingInfo(Content node) { 163 List siblings = new ArrayList(); 165 Node thisNode = node.getJCRNode(); 166 try { 167 String thisNodeType = node.getNodeTypeName(); 168 String thisNodeUUID = node.getUUID(); 169 NodeIterator nodeIterator = thisNode.getParent().getNodes(); 170 while (nodeIterator.hasNext()) { Node sibling = nodeIterator.nextNode(); 172 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 log.error("Failed to get Ordering info", re); 186 } 187 return siblings; 188 } 189 190 193 public boolean isRecursive() { 194 return recursive; 195 } 196 197 200 public void setRecursive(boolean recursive) { 201 this.recursive = recursive; 202 } 203 204 207 public void setVersion(String number) { 208 this.versionNumber = number; 209 } 210 211 214 public String getVersion() { 215 return this.versionNumber; 216 } 217 218 221 public void setVersionMap(List versionMap) { 222 this.versionMap = versionMap; 223 } 224 225 228 public List getVersionMap() { 229 return this.versionMap; 230 } 231 232 233 } 234 | Popular Tags |