KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > coci > CheckOutCheckInServiceImpl


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 package org.alfresco.repo.coci;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.alfresco.error.AlfrescoRuntimeException;
24 import org.alfresco.i18n.I18NUtil;
25 import org.alfresco.model.ContentModel;
26 import org.alfresco.repo.version.VersionableAspect;
27 import org.alfresco.service.cmr.coci.CheckOutCheckInService;
28 import org.alfresco.service.cmr.coci.CheckOutCheckInServiceException;
29 import org.alfresco.service.cmr.lock.LockService;
30 import org.alfresco.service.cmr.lock.LockType;
31 import org.alfresco.service.cmr.lock.UnableToReleaseLockException;
32 import org.alfresco.service.cmr.repository.AspectMissingException;
33 import org.alfresco.service.cmr.repository.ChildAssociationRef;
34 import org.alfresco.service.cmr.repository.ContentData;
35 import org.alfresco.service.cmr.repository.CopyService;
36 import org.alfresco.service.cmr.repository.NodeRef;
37 import org.alfresco.service.cmr.repository.NodeService;
38 import org.alfresco.service.cmr.search.ResultSet;
39 import org.alfresco.service.cmr.search.SearchService;
40 import org.alfresco.service.cmr.security.AuthenticationService;
41 import org.alfresco.service.cmr.version.VersionService;
42 import org.alfresco.service.namespace.QName;
43
44 /**
45  * Version opertaions service implementation
46  *
47  * @author Roy Wetherall
48  */

49 public class CheckOutCheckInServiceImpl implements CheckOutCheckInService
50 {
51     /**
52      * I18N labels
53      */

54     private static final String JavaDoc MSG_ERR_BAD_COPY = "coci_service.err_bad_copy";
55     private static final String JavaDoc MSG_WORKING_COPY_LABEL = "coci_service.working_copy_label";
56     private static final String JavaDoc MSG_ERR_NOT_OWNER = "coci_service.err_not_owner";
57     private static final String JavaDoc MSG_ERR_ALREADY_WORKING_COPY = "coci_service.err_workingcopy_checkout";
58     private static final String JavaDoc MSG_ERR_NOT_AUTHENTICATED = "coci_service.err_not_authenticated";
59     private static final String JavaDoc MSG_ERR_WORKINGCOPY_HAS_NO_MIMETYPE = "coci_service.err_workingcopy_has_no_mimetype";
60
61     /**
62      * Extension character, used to recalculate the working copy names
63      */

64     private static final String JavaDoc EXTENSION_CHARACTER = ".";
65     
66     /**
67      * The node service
68      */

69     private NodeService nodeService;
70     
71     /**
72      * The version service
73      */

74     private VersionService versionService;
75     
76     /**
77      * The lock service
78      */

79     private LockService lockService;
80     
81     /**
82      * The copy service
83      */

84     private CopyService copyService;
85     
86     /**
87      * The search service
88      */

89     private SearchService searchService;
90     
91     /**
92      * The authentication service
93      */

94     private AuthenticationService authenticationService;
95     
96     /**
97      * The versionable aspect behaviour implementation
98      */

99     private VersionableAspect versionableAspect;
100     
101     /**
102      * Set the node service
103      *
104      * @param nodeService the node service
105      */

106     public void setNodeService(NodeService nodeService)
107     {
108         this.nodeService = nodeService;
109     }
110     
111     /**
112      * Set the version service
113      *
114      * @param versionService the version service
115      */

116     public void setVersionService(VersionService versionService)
117     {
118         this.versionService = versionService;
119     }
120     
121     /**
122      * Sets the lock service
123      *
124      * @param lockService the lock service
125      */

126     public void setLockService(LockService lockService)
127     {
128         this.lockService = lockService;
129     }
130     
131     /**
132      * Sets the copy service
133      *
134      * @param copyService the copy service
135      */

136     public void setCopyService(
137             CopyService copyService)
138     {
139         this.copyService = copyService;
140     }
141     
142     /**
143      * Sets the authenticatin service
144      *
145      * @param authenticationService the authentication service
146      */

147     public void setAuthenticationService(
148             AuthenticationService authenticationService)
149     {
150         this.authenticationService = authenticationService;
151     }
152     
153     /**
154      * Set the search service
155      *
156      * @param searchService the search service
157      */

158     public void setSearchService(SearchService searchService)
159     {
160         this.searchService = searchService;
161     }
162     
163     /**
164      * Sets the versionable aspect behaviour implementation
165      *
166      * @param versionableAspect the versionable aspect behaviour implementation
167      */

168     public void setVersionableAspect(VersionableAspect versionableAspect)
169     {
170         this.versionableAspect = versionableAspect;
171     }
172     
173     /**
174      * Get the working copy label.
175      *
176      * @return the working copy label
177      */

178     public String JavaDoc getWorkingCopyLabel()
179     {
180         return I18NUtil.getMessage(MSG_WORKING_COPY_LABEL);
181     }
182     
183     /**
184      * @see org.alfresco.service.cmr.coci.CheckOutCheckInService#checkout(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, org.alfresco.service.namespace.QName)
185      */

186     public NodeRef checkout(
187             NodeRef nodeRef,
188             NodeRef destinationParentNodeRef,
189             QName destinationAssocTypeQName,
190             QName destinationAssocQName)
191     {
192         // Make sure we are no checking out a working copy node
193
if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WORKING_COPY) == true)
194         {
195             throw new CheckOutCheckInServiceException(MSG_ERR_ALREADY_WORKING_COPY);
196         }
197         
198         // Apply the lock aspect if required
199
if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE) == false)
200         {
201             this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_LOCKABLE, null);
202         }
203         
204         // Rename the working copy
205
String JavaDoc copyName = (String JavaDoc)this.nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
206         if (this.getWorkingCopyLabel() != null && this.getWorkingCopyLabel().length() != 0)
207         {
208             if (copyName != null && copyName.length() != 0)
209             {
210                 int index = copyName.lastIndexOf(EXTENSION_CHARACTER);
211                 if (index > 0)
212                 {
213                     // Insert the working copy label before the file extension
214
copyName = copyName.substring(0, index) + " " + getWorkingCopyLabel() + copyName.substring(index);
215                 }
216                 else
217                 {
218                     // Simply append the working copy label onto the end of the existing name
219
copyName = copyName + " " + getWorkingCopyLabel();
220                 }
221             }
222             else
223             {
224                 copyName = getWorkingCopyLabel();
225             }
226         }
227
228         // Make the working copy
229
destinationAssocQName = QName.createQName(destinationAssocQName.getNamespaceURI(), QName.createValidLocalName(copyName));
230         NodeRef workingCopy = this.copyService.copy(
231                 nodeRef,
232                 destinationParentNodeRef,
233                 destinationAssocTypeQName,
234                 destinationAssocQName);
235         
236         // Update the working copy name
237
this.nodeService.setProperty(workingCopy, ContentModel.PROP_NAME, copyName);
238
239         // Get the user
240
String JavaDoc userName = getUserName();
241         
242         // Apply the working copy aspect to the working copy
243
Map JavaDoc<QName, Serializable JavaDoc> workingCopyProperties = new HashMap JavaDoc<QName, Serializable JavaDoc>(1);
244         workingCopyProperties.put(ContentModel.PROP_WORKING_COPY_OWNER, userName);
245         this.nodeService.addAspect(workingCopy, ContentModel.ASPECT_WORKING_COPY, workingCopyProperties);
246         
247         // Lock the origional node
248
this.lockService.lock(nodeRef, LockType.READ_ONLY_LOCK);
249         
250         // Return the working copy
251
return workingCopy;
252     }
253     
254     /**
255      * Gets the authenticated users node reference
256      *
257      * @return the users node reference
258      */

259     private String JavaDoc getUserName()
260     {
261         String JavaDoc un = this.authenticationService.getCurrentUserName();
262         if (un != null)
263         {
264            return un;
265         }
266         else
267         {
268             throw new CheckOutCheckInServiceException(MSG_ERR_NOT_AUTHENTICATED);
269         }
270     }
271
272     /**
273      * @see org.alfresco.service.cmr.coci.CheckOutCheckInService#checkout(org.alfresco.service.cmr.repository.NodeRef)
274      */

275     public NodeRef checkout(NodeRef nodeRef)
276     {
277         // Find the primary parent in order to determine where to put the copy
278
ChildAssociationRef childAssocRef = this.nodeService.getPrimaryParent(nodeRef);
279         
280         // Checkout the working copy to the same destination
281
return checkout(nodeRef, childAssocRef.getParentRef(), childAssocRef.getTypeQName(), childAssocRef.getQName());
282     }
283
284     /**
285      * @see org.alfresco.repo.version.operations.VersionOperationsService#checkin(org.alfresco.repo.ref.NodeRef, Map<String,Serializable>, java.lang.String, boolean)
286      */

287     public NodeRef checkin(
288             NodeRef workingCopyNodeRef,
289             Map JavaDoc<String JavaDoc,Serializable JavaDoc> versionProperties,
290             String JavaDoc contentUrl,
291             boolean keepCheckedOut)
292     {
293         NodeRef nodeRef = null;
294         
295         // Check that we have been handed a working copy
296
if (this.nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_WORKING_COPY) == false)
297         {
298             // Error since we have not been passed a working copy
299
throw new AspectMissingException(ContentModel.ASPECT_WORKING_COPY, workingCopyNodeRef);
300         }
301         
302         // Check that the working node still has the copy aspect applied
303
if (this.nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_COPIEDFROM) == true)
304         {
305             // Disable versionable behaviours since we don't want the auto version policy behaviour to execute when we check-in
306
this.versionableAspect.disableAutoVersion();
307             try
308             {
309                 Map JavaDoc<QName, Serializable JavaDoc> workingCopyProperties = nodeService.getProperties(workingCopyNodeRef);
310                 // Try and get the origional node reference
311
nodeRef = (NodeRef) workingCopyProperties.get(ContentModel.PROP_COPY_REFERENCE);
312                 if(nodeRef == null)
313                 {
314                     // Error since the origional node can not be found
315
throw new CheckOutCheckInServiceException(MSG_ERR_BAD_COPY);
316                 }
317                 
318                 try
319                 {
320                     // Release the lock
321
this.lockService.unlock(nodeRef);
322                 }
323                 catch (UnableToReleaseLockException exception)
324                 {
325                     throw new CheckOutCheckInServiceException(MSG_ERR_NOT_OWNER, exception);
326                 }
327                 
328                 if (contentUrl != null)
329                 {
330                     ContentData contentData = (ContentData) workingCopyProperties.get(ContentModel.PROP_CONTENT);
331                     if (contentData == null)
332                     {
333                         throw new AlfrescoRuntimeException(MSG_ERR_WORKINGCOPY_HAS_NO_MIMETYPE, new Object JavaDoc[]{workingCopyNodeRef});
334                     }
335                     else
336                     {
337                         contentData = new ContentData(
338                                 contentUrl,
339                                 contentData.getMimetype(),
340                                 contentData.getSize(),
341                                 contentData.getEncoding());
342                     }
343                     // Set the content url value onto the working copy
344
this.nodeService.setProperty(
345                             workingCopyNodeRef,
346                             ContentModel.PROP_CONTENT,
347                             contentData);
348                 }
349                 
350                 // Copy the contents of the working copy onto the origional
351
this.copyService.copy(workingCopyNodeRef, nodeRef);
352                 
353                 if (versionProperties != null && this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE) == true)
354                 {
355                     // Create the new version
356
this.versionService.createVersion(nodeRef, versionProperties);
357                 }
358                 
359                 if (keepCheckedOut == false)
360                 {
361                     // Delete the working copy
362
this.nodeService.removeAspect(workingCopyNodeRef, ContentModel.ASPECT_WORKING_COPY);
363                     this.nodeService.deleteNode(workingCopyNodeRef);
364                 }
365                 else
366                 {
367                     // Re-lock the origional node
368
this.lockService.lock(nodeRef, LockType.READ_ONLY_LOCK);
369                 }
370             }
371             finally
372             {
373                 this.versionableAspect.enableAutoVersion();
374             }
375             
376         }
377         else
378         {
379             // Error since the copy aspect is missing
380
throw new AspectMissingException(ContentModel.ASPECT_COPIEDFROM, workingCopyNodeRef);
381         }
382         
383         return nodeRef;
384     }
385
386     /**
387      * @see org.alfresco.service.cmr.coci.CheckOutCheckInService#checkin(org.alfresco.service.cmr.repository.NodeRef, Map, java.lang.String)
388      */

389     public NodeRef checkin(
390             NodeRef workingCopyNodeRef,
391             Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties,
392             String JavaDoc contentUrl)
393     {
394         return checkin(workingCopyNodeRef, versionProperties, contentUrl, false);
395     }
396
397     /**
398      * @see org.alfresco.service.cmr.coci.CheckOutCheckInService#checkin(org.alfresco.service.cmr.repository.NodeRef, Map)
399      */

400     public NodeRef checkin(
401             NodeRef workingCopyNodeRef,
402             Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties)
403     {
404         return checkin(workingCopyNodeRef, versionProperties, null, false);
405     }
406
407     /**
408      * @see org.alfresco.service.cmr.coci.CheckOutCheckInService#cancelCheckout(org.alfresco.service.cmr.repository.NodeRef)
409      */

410     public NodeRef cancelCheckout(NodeRef workingCopyNodeRef)
411     {
412         NodeRef nodeRef = null;
413         
414         // Check that we have been handed a working copy
415
if (this.nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_WORKING_COPY) == false)
416         {
417             // Error since we have not been passed a working copy
418
throw new AspectMissingException(ContentModel.ASPECT_WORKING_COPY, workingCopyNodeRef);
419         }
420         
421         // Ensure that the node has the copy aspect
422
if (this.nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_COPIEDFROM) == true)
423         {
424             // Get the origional node
425
nodeRef = (NodeRef)this.nodeService.getProperty(workingCopyNodeRef, ContentModel.PROP_COPY_REFERENCE);
426             if (nodeRef == null)
427             {
428                 // Error since the origional node can not be found
429
throw new CheckOutCheckInServiceException(MSG_ERR_BAD_COPY);
430             }
431             
432             // Release the lock on the origional node
433
this.lockService.unlock(nodeRef);
434             
435             // Delete the working copy
436
this.nodeService.removeAspect(workingCopyNodeRef, ContentModel.ASPECT_WORKING_COPY);
437             this.nodeService.deleteNode(workingCopyNodeRef);
438         }
439         else
440         {
441             // Error since the copy aspect is missing
442
throw new AspectMissingException(ContentModel.ASPECT_COPIEDFROM, workingCopyNodeRef);
443         }
444         
445         return nodeRef;
446     }
447     
448     /**
449      * @see org.alfresco.service.cmr.coci.CheckOutCheckInService#getWorkingCopy(org.alfresco.service.cmr.repository.NodeRef)
450      */

451     public NodeRef getWorkingCopy(NodeRef nodeRef)
452     {
453         NodeRef workingCopy = null;
454         
455         // Do a search to find the origional document
456
ResultSet resultSet = null;
457         try
458         {
459             resultSet = this.searchService.query(
460                     nodeRef.getStoreRef(),
461                     SearchService.LANGUAGE_LUCENE,
462                     "ASPECT:\"" + ContentModel.ASPECT_WORKING_COPY.toString() + "\" +@\\{http\\://www.alfresco.org/model/content/1.0\\}" + ContentModel.PROP_COPY_REFERENCE.getLocalName() + ":\"" + nodeRef.toString() + "\"");
463             if (resultSet.getNodeRefs().size() != 0)
464             {
465                 workingCopy = resultSet.getNodeRef(0);
466             }
467         }
468         finally
469         {
470             if (resultSet != null)
471             {
472                 resultSet.close();
473             }
474         }
475         
476         return workingCopy;
477     }
478 }
479
Popular Tags