KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > forum > DiscussableAspect


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17
18 package org.alfresco.repo.forum;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
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     /**
51      * Policy component
52      */

53     private PolicyComponent policyComponent;
54     
55     /**
56      * The node service
57      */

58     private NodeService nodeService;
59     
60     /**
61      * The file folder service
62      */

63     private FileFolderService fileFolderService;
64     
65     /**
66      * Sets the policy component
67      *
68      * @param policyComponent the policy component
69      */

70     public void setPolicyComponent(PolicyComponent policyComponent)
71     {
72         this.policyComponent = policyComponent;
73     }
74     
75     /**
76      * Set the node service
77      *
78      * @param nodeService the node service
79      */

80     public void setNodeService(NodeService nodeService)
81     {
82         this.nodeService = nodeService;
83     }
84     
85     /**
86      * Set the file folder service
87      *
88      * @param fileFolderService the file folder service
89      */

90     public void setFileFolderService(FileFolderService fileFolderService)
91     {
92         this.fileFolderService = fileFolderService;
93     }
94     
95     /**
96      * Initialise method
97      */

98     public void init()
99     {
100         // Register copy behaviour for the discussable aspect
101
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     /**
113      * onCopy policy behaviour
114      *
115      * @see org.alfresco.repo.copy.CopyServicePolicies.OnCopyNodePolicy#onCopyNode(QName, NodeRef, StoreRef, boolean, PolicyScope)
116      */

117     public void onCopy(
118             QName sourceClassRef,
119             NodeRef sourceNodeRef,
120             StoreRef destinationStoreRef,
121             boolean copyToNewNode,
122             PolicyScope copyDetails)
123     {
124         // NOTE: we intentionally don't do anything in here, this stops the discussable
125
// aspect from being added to the new copied node - the behaviour we want.
126
}
127     
128     public void onCopyComplete(
129          QName classRef,
130          NodeRef sourceNodeRef,
131          NodeRef destinationRef,
132          boolean copyNewNode,
133          Map JavaDoc<NodeRef, NodeRef> copyMap)
134     {
135         // if the copy is not a new node it is a checkin, we therefore
136
// need to copy any discussions from the working copy document
137
// to the document being checked in
138
if (copyNewNode == false)
139         {
140             List JavaDoc<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             // get the forum for the destination node, it's created if necessary
153
NodeRef destinationForum = getDestinationForum(destinationRef);
154               
155             // copy any topics from the source forum to the destination forum
156
int copied = 0;
157             List JavaDoc<ChildAssociationRef> sourceForums = this.nodeService.getChildAssocs(sourceForum);
158             for (ChildAssociationRef childRef : sourceForums)
159             {
160                 String JavaDoc topicName = null;
161                 NodeRef childNode = childRef.getChildRef();
162                 if (this.nodeService.getType(childNode).equals(ForumModel.TYPE_TOPIC))
163                 {
164                     try
165                     {
166                         // work out the name for the copied topic
167
String JavaDoc childName = this.nodeService.getProperty(childNode,
168                               ContentModel.PROP_NAME).toString();
169                         Serializable JavaDoc labelProp = this.nodeService.getProperty(destinationRef,
170                               ContentModel.PROP_VERSION_LABEL);
171                         if (labelProp == null)
172                         {
173                             topicName = childName + " - " + new Date JavaDoc();
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     /**
202      * Retrieves or creates the forum node for the given destination node
203      *
204      * @param destNodeRef The node to get the forum for
205      * @return NodeRef representing the forum
206      */

207     private NodeRef getDestinationForum(NodeRef destNodeRef)
208     {
209         NodeRef destinationForum = null;
210         
211         if (this.nodeService.hasAspect(destNodeRef, ForumModel.ASPECT_DISCUSSABLE))
212         {
213             List JavaDoc<ChildAssociationRef> destChildren = this.nodeService.getChildAssocs(destNodeRef,
214                 ForumModel.ASSOC_DISCUSSION, RegexQNamePattern.MATCH_ALL);
215             
216             if (destChildren.size() != 1)
217             {
218                 throw new IllegalStateException JavaDoc("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            // create the forum - TODO: Move this to a repo discussion service so that it can
227
// be shared between here and the discussion wizard
228

229            // add the discussable aspect
230
this.nodeService.addAspect(destNodeRef, ForumModel.ASPECT_DISCUSSABLE, null);
231          
232            // create a child forum space using the child association just introduced by
233
// adding the discussable aspect
234
String JavaDoc name = (String JavaDoc)this.nodeService.getProperty(destNodeRef,
235                ContentModel.PROP_NAME);
236            String JavaDoc forumName = I18NUtil.getMessage("coci_service.discussion_for", new Object JavaDoc[] {name});
237          
238            Map JavaDoc<QName, Serializable JavaDoc> forumProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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            // apply the uifacets aspect
249
Map JavaDoc<QName, Serializable JavaDoc> uiFacetsProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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