1 17 18 package org.alfresco.repo.forum; 19 20 import java.io.Serializable ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.alfresco.i18n.I18NUtil; 27 import org.alfresco.model.ContentModel; 28 import org.alfresco.model.ForumModel; 29 import org.alfresco.repo.policy.JavaBehaviour; 30 import org.alfresco.repo.policy.PolicyComponent; 31 import org.alfresco.repo.policy.PolicyScope; 32 import org.alfresco.service.cmr.coci.CheckOutCheckInServiceException; 33 import org.alfresco.service.cmr.model.FileExistsException; 34 import org.alfresco.service.cmr.model.FileFolderService; 35 import org.alfresco.service.cmr.model.FileNotFoundException; 36 import org.alfresco.service.cmr.repository.ChildAssociationRef; 37 import org.alfresco.service.cmr.repository.NodeRef; 38 import org.alfresco.service.cmr.repository.NodeService; 39 import org.alfresco.service.cmr.repository.StoreRef; 40 import org.alfresco.service.namespace.NamespaceService; 41 import org.alfresco.service.namespace.QName; 42 import org.alfresco.service.namespace.RegexQNamePattern; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 46 public class DiscussableAspect 47 { 48 private static final Log logger = LogFactory.getLog(DiscussableAspect.class); 49 50 53 private PolicyComponent policyComponent; 54 55 58 private NodeService nodeService; 59 60 63 private FileFolderService fileFolderService; 64 65 70 public void setPolicyComponent(PolicyComponent policyComponent) 71 { 72 this.policyComponent = policyComponent; 73 } 74 75 80 public void setNodeService(NodeService nodeService) 81 { 82 this.nodeService = nodeService; 83 } 84 85 90 public void setFileFolderService(FileFolderService fileFolderService) 91 { 92 this.fileFolderService = fileFolderService; 93 } 94 95 98 public void init() 99 { 100 this.policyComponent.bindClassBehaviour( 102 QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyNode"), 103 ForumModel.ASPECT_DISCUSSABLE, 104 new JavaBehaviour(this, "onCopy")); 105 106 this.policyComponent.bindClassBehaviour( 107 QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyComplete"), 108 ForumModel.ASPECT_DISCUSSABLE, 109 new JavaBehaviour(this, "onCopyComplete")); 110 } 111 112 117 public void onCopy( 118 QName sourceClassRef, 119 NodeRef sourceNodeRef, 120 StoreRef destinationStoreRef, 121 boolean copyToNewNode, 122 PolicyScope copyDetails) 123 { 124 } 127 128 public void onCopyComplete( 129 QName classRef, 130 NodeRef sourceNodeRef, 131 NodeRef destinationRef, 132 boolean copyNewNode, 133 Map <NodeRef, NodeRef> copyMap) 134 { 135 if (copyNewNode == false) 139 { 140 List <ChildAssociationRef> sourceChildren = this.nodeService.getChildAssocs(sourceNodeRef, 141 ForumModel.ASSOC_DISCUSSION, RegexQNamePattern.MATCH_ALL); 142 143 if (sourceChildren.size() != 1) 144 { 145 throw new CheckOutCheckInServiceException( 146 "The source node has the discussable aspect but does not have 1 child, it has " + 147 sourceChildren.size() + " children!"); 148 } 149 150 NodeRef sourceForum = sourceChildren.get(0).getChildRef(); 151 152 NodeRef destinationForum = getDestinationForum(destinationRef); 154 155 int copied = 0; 157 List <ChildAssociationRef> sourceForums = this.nodeService.getChildAssocs(sourceForum); 158 for (ChildAssociationRef childRef : sourceForums) 159 { 160 String topicName = null; 161 NodeRef childNode = childRef.getChildRef(); 162 if (this.nodeService.getType(childNode).equals(ForumModel.TYPE_TOPIC)) 163 { 164 try 165 { 166 String childName = this.nodeService.getProperty(childNode, 168 ContentModel.PROP_NAME).toString(); 169 Serializable labelProp = this.nodeService.getProperty(destinationRef, 170 ContentModel.PROP_VERSION_LABEL); 171 if (labelProp == null) 172 { 173 topicName = childName + " - " + new Date (); 174 } 175 else 176 { 177 topicName = childName + " (" + labelProp.toString() + ")"; 178 } 179 180 this.fileFolderService.copy(childNode, destinationForum, topicName); 181 copied++; 182 } 183 catch (FileNotFoundException fnfe) 184 { 185 throw new CheckOutCheckInServiceException( 186 "Failed to copy topic from working copy to checked out content", fnfe); 187 } 188 catch (FileExistsException fee) 189 { 190 throw new CheckOutCheckInServiceException("Failed to checkin content as a topic called " + 191 topicName + " already exists on the checked out content", fee); 192 } 193 } 194 } 195 196 if (logger.isDebugEnabled()) 197 logger.debug("Copied " + copied + " topics from the working copy to the checked out content"); 198 } 199 } 200 201 207 private NodeRef getDestinationForum(NodeRef destNodeRef) 208 { 209 NodeRef destinationForum = null; 210 211 if (this.nodeService.hasAspect(destNodeRef, ForumModel.ASPECT_DISCUSSABLE)) 212 { 213 List <ChildAssociationRef> destChildren = this.nodeService.getChildAssocs(destNodeRef, 214 ForumModel.ASSOC_DISCUSSION, RegexQNamePattern.MATCH_ALL); 215 216 if (destChildren.size() != 1) 217 { 218 throw new IllegalStateException ("Locked node has the discussable aspect but does not have 1 child, it has " + 219 destChildren.size() + " children!"); 220 } 221 222 destinationForum = destChildren.get(0).getChildRef(); 223 } 224 else 225 { 226 229 this.nodeService.addAspect(destNodeRef, ForumModel.ASPECT_DISCUSSABLE, null); 231 232 String name = (String )this.nodeService.getProperty(destNodeRef, 235 ContentModel.PROP_NAME); 236 String forumName = I18NUtil.getMessage("coci_service.discussion_for", new Object [] {name}); 237 238 Map <QName, Serializable > forumProps = new HashMap <QName, Serializable >(1); 239 forumProps.put(ContentModel.PROP_NAME, forumName); 240 241 ChildAssociationRef childRef = this.nodeService.createNode(destNodeRef, 242 ForumModel.ASSOC_DISCUSSION, 243 QName.createQName(ForumModel.FORUMS_MODEL_URI, "discussion"), 244 ForumModel.TYPE_FORUM, forumProps); 245 246 destinationForum = childRef.getChildRef(); 247 248 Map <QName, Serializable > uiFacetsProps = new HashMap <QName, Serializable >(5); 250 uiFacetsProps.put(ContentModel.PROP_ICON, "forum_large"); 251 this.nodeService.addAspect(destinationForum, ContentModel.ASPECT_UIFACETS, uiFacetsProps); 252 } 253 254 return destinationForum; 255 } 256 } 257 | Popular Tags |