1 17 package org.alfresco.repo.coci; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.Map ; 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 49 public class CheckOutCheckInServiceImpl implements CheckOutCheckInService 50 { 51 54 private static final String MSG_ERR_BAD_COPY = "coci_service.err_bad_copy"; 55 private static final String MSG_WORKING_COPY_LABEL = "coci_service.working_copy_label"; 56 private static final String MSG_ERR_NOT_OWNER = "coci_service.err_not_owner"; 57 private static final String MSG_ERR_ALREADY_WORKING_COPY = "coci_service.err_workingcopy_checkout"; 58 private static final String MSG_ERR_NOT_AUTHENTICATED = "coci_service.err_not_authenticated"; 59 private static final String MSG_ERR_WORKINGCOPY_HAS_NO_MIMETYPE = "coci_service.err_workingcopy_has_no_mimetype"; 60 61 64 private static final String EXTENSION_CHARACTER = "."; 65 66 69 private NodeService nodeService; 70 71 74 private VersionService versionService; 75 76 79 private LockService lockService; 80 81 84 private CopyService copyService; 85 86 89 private SearchService searchService; 90 91 94 private AuthenticationService authenticationService; 95 96 99 private VersionableAspect versionableAspect; 100 101 106 public void setNodeService(NodeService nodeService) 107 { 108 this.nodeService = nodeService; 109 } 110 111 116 public void setVersionService(VersionService versionService) 117 { 118 this.versionService = versionService; 119 } 120 121 126 public void setLockService(LockService lockService) 127 { 128 this.lockService = lockService; 129 } 130 131 136 public void setCopyService( 137 CopyService copyService) 138 { 139 this.copyService = copyService; 140 } 141 142 147 public void setAuthenticationService( 148 AuthenticationService authenticationService) 149 { 150 this.authenticationService = authenticationService; 151 } 152 153 158 public void setSearchService(SearchService searchService) 159 { 160 this.searchService = searchService; 161 } 162 163 168 public void setVersionableAspect(VersionableAspect versionableAspect) 169 { 170 this.versionableAspect = versionableAspect; 171 } 172 173 178 public String getWorkingCopyLabel() 179 { 180 return I18NUtil.getMessage(MSG_WORKING_COPY_LABEL); 181 } 182 183 186 public NodeRef checkout( 187 NodeRef nodeRef, 188 NodeRef destinationParentNodeRef, 189 QName destinationAssocTypeQName, 190 QName destinationAssocQName) 191 { 192 if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WORKING_COPY) == true) 194 { 195 throw new CheckOutCheckInServiceException(MSG_ERR_ALREADY_WORKING_COPY); 196 } 197 198 if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE) == false) 200 { 201 this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_LOCKABLE, null); 202 } 203 204 String copyName = (String )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 copyName = copyName.substring(0, index) + " " + getWorkingCopyLabel() + copyName.substring(index); 215 } 216 else 217 { 218 copyName = copyName + " " + getWorkingCopyLabel(); 220 } 221 } 222 else 223 { 224 copyName = getWorkingCopyLabel(); 225 } 226 } 227 228 destinationAssocQName = QName.createQName(destinationAssocQName.getNamespaceURI(), QName.createValidLocalName(copyName)); 230 NodeRef workingCopy = this.copyService.copy( 231 nodeRef, 232 destinationParentNodeRef, 233 destinationAssocTypeQName, 234 destinationAssocQName); 235 236 this.nodeService.setProperty(workingCopy, ContentModel.PROP_NAME, copyName); 238 239 String userName = getUserName(); 241 242 Map <QName, Serializable > workingCopyProperties = new HashMap <QName, Serializable >(1); 244 workingCopyProperties.put(ContentModel.PROP_WORKING_COPY_OWNER, userName); 245 this.nodeService.addAspect(workingCopy, ContentModel.ASPECT_WORKING_COPY, workingCopyProperties); 246 247 this.lockService.lock(nodeRef, LockType.READ_ONLY_LOCK); 249 250 return workingCopy; 252 } 253 254 259 private String getUserName() 260 { 261 String 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 275 public NodeRef checkout(NodeRef nodeRef) 276 { 277 ChildAssociationRef childAssocRef = this.nodeService.getPrimaryParent(nodeRef); 279 280 return checkout(nodeRef, childAssocRef.getParentRef(), childAssocRef.getTypeQName(), childAssocRef.getQName()); 282 } 283 284 287 public NodeRef checkin( 288 NodeRef workingCopyNodeRef, 289 Map <String ,Serializable > versionProperties, 290 String contentUrl, 291 boolean keepCheckedOut) 292 { 293 NodeRef nodeRef = null; 294 295 if (this.nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_WORKING_COPY) == false) 297 { 298 throw new AspectMissingException(ContentModel.ASPECT_WORKING_COPY, workingCopyNodeRef); 300 } 301 302 if (this.nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_COPIEDFROM) == true) 304 { 305 this.versionableAspect.disableAutoVersion(); 307 try 308 { 309 Map <QName, Serializable > workingCopyProperties = nodeService.getProperties(workingCopyNodeRef); 310 nodeRef = (NodeRef) workingCopyProperties.get(ContentModel.PROP_COPY_REFERENCE); 312 if(nodeRef == null) 313 { 314 throw new CheckOutCheckInServiceException(MSG_ERR_BAD_COPY); 316 } 317 318 try 319 { 320 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 []{workingCopyNodeRef}); 334 } 335 else 336 { 337 contentData = new ContentData( 338 contentUrl, 339 contentData.getMimetype(), 340 contentData.getSize(), 341 contentData.getEncoding()); 342 } 343 this.nodeService.setProperty( 345 workingCopyNodeRef, 346 ContentModel.PROP_CONTENT, 347 contentData); 348 } 349 350 this.copyService.copy(workingCopyNodeRef, nodeRef); 352 353 if (versionProperties != null && this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE) == true) 354 { 355 this.versionService.createVersion(nodeRef, versionProperties); 357 } 358 359 if (keepCheckedOut == false) 360 { 361 this.nodeService.removeAspect(workingCopyNodeRef, ContentModel.ASPECT_WORKING_COPY); 363 this.nodeService.deleteNode(workingCopyNodeRef); 364 } 365 else 366 { 367 this.lockService.lock(nodeRef, LockType.READ_ONLY_LOCK); 369 } 370 } 371 finally 372 { 373 this.versionableAspect.enableAutoVersion(); 374 } 375 376 } 377 else 378 { 379 throw new AspectMissingException(ContentModel.ASPECT_COPIEDFROM, workingCopyNodeRef); 381 } 382 383 return nodeRef; 384 } 385 386 389 public NodeRef checkin( 390 NodeRef workingCopyNodeRef, 391 Map <String , Serializable > versionProperties, 392 String contentUrl) 393 { 394 return checkin(workingCopyNodeRef, versionProperties, contentUrl, false); 395 } 396 397 400 public NodeRef checkin( 401 NodeRef workingCopyNodeRef, 402 Map <String , Serializable > versionProperties) 403 { 404 return checkin(workingCopyNodeRef, versionProperties, null, false); 405 } 406 407 410 public NodeRef cancelCheckout(NodeRef workingCopyNodeRef) 411 { 412 NodeRef nodeRef = null; 413 414 if (this.nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_WORKING_COPY) == false) 416 { 417 throw new AspectMissingException(ContentModel.ASPECT_WORKING_COPY, workingCopyNodeRef); 419 } 420 421 if (this.nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_COPIEDFROM) == true) 423 { 424 nodeRef = (NodeRef)this.nodeService.getProperty(workingCopyNodeRef, ContentModel.PROP_COPY_REFERENCE); 426 if (nodeRef == null) 427 { 428 throw new CheckOutCheckInServiceException(MSG_ERR_BAD_COPY); 430 } 431 432 this.lockService.unlock(nodeRef); 434 435 this.nodeService.removeAspect(workingCopyNodeRef, ContentModel.ASPECT_WORKING_COPY); 437 this.nodeService.deleteNode(workingCopyNodeRef); 438 } 439 else 440 { 441 throw new AspectMissingException(ContentModel.ASPECT_COPIEDFROM, workingCopyNodeRef); 443 } 444 445 return nodeRef; 446 } 447 448 451 public NodeRef getWorkingCopy(NodeRef nodeRef) 452 { 453 NodeRef workingCopy = null; 454 455 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 |