1 17 package org.alfresco.repo.webservice.classification; 18 19 import java.io.Serializable ; 20 import java.rmi.RemoteException ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.List ; 24 import java.util.Set ; 25 26 import org.alfresco.model.ContentModel; 27 import org.alfresco.repo.transaction.TransactionUtil; 28 import org.alfresco.repo.webservice.AbstractWebService; 29 import org.alfresco.repo.webservice.Utils; 30 import org.alfresco.repo.webservice.types.Category; 31 import org.alfresco.repo.webservice.types.ClassDefinition; 32 import org.alfresco.repo.webservice.types.Classification; 33 import org.alfresco.repo.webservice.types.Predicate; 34 import org.alfresco.repo.webservice.types.Reference; 35 import org.alfresco.repo.webservice.types.Store; 36 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 37 import org.alfresco.service.cmr.dictionary.DictionaryService; 38 import org.alfresco.service.cmr.dictionary.PropertyDefinition; 39 import org.alfresco.service.cmr.repository.ChildAssociationRef; 40 import org.alfresco.service.cmr.repository.NodeRef; 41 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 42 import org.alfresco.service.cmr.search.CategoryService; 43 import org.alfresco.service.namespace.QName; 44 import org.alfresco.service.transaction.TransactionService; 45 import org.apache.commons.logging.Log; 46 import org.apache.commons.logging.LogFactory; 47 48 55 public class ClassificationWebService extends AbstractWebService implements 56 ClassificationServiceSoapPort 57 { 58 private static Log logger = LogFactory.getLog(ClassificationWebService.class); 59 60 63 private CategoryService categoryService; 64 65 68 private DictionaryService dictionaryService; 69 70 73 private TransactionService transactionService; 74 75 80 public void setCategoryService(CategoryService categoryService) 81 { 82 this.categoryService = categoryService; 83 } 84 85 90 public void setTransactionService(TransactionService transactionService) 91 { 92 this.transactionService = transactionService; 93 } 94 95 100 public void setDictionaryService(DictionaryService dictionaryService) 101 { 102 this.dictionaryService = dictionaryService; 103 } 104 105 108 public Classification[] getClassifications(final Store store) throws RemoteException , 109 ClassificationFault 110 { 111 try 112 { 113 return TransactionUtil.executeInUserTransaction( 114 this.transactionService, 115 new TransactionUtil.TransactionWork<Classification[]>() 116 { 117 public Classification[] doWork() 118 { 119 List <Classification> classifications = new ArrayList <Classification>(); 120 121 Collection <QName> categoryAspects = ClassificationWebService.this.categoryService.getClassificationAspects(); 122 for (QName aspect : categoryAspects) 123 { 124 String title = null; 126 org.alfresco.service.cmr.dictionary.ClassDefinition aspectDefinition = ClassificationWebService.this.dictionaryService.getClass(aspect); 127 if (aspectDefinition != null) 128 { 129 title = aspectDefinition.getTitle(); 130 } 131 132 if (logger.isDebugEnabled()) 133 { 134 logger.debug("Category aspect found: " + title + " (" + aspect.toString() + ")"); 135 } 136 137 Collection <ChildAssociationRef> assocs = ClassificationWebService.this.categoryService.getCategories( 138 Utils.convertToStoreRef(store), 139 aspect, 140 CategoryService.Depth.IMMEDIATE); 141 for (ChildAssociationRef assoc : assocs) 142 { 143 NodeRef categoryNodeRef = assoc.getChildRef(); 144 145 Classification classification = new Classification(); 146 classification.setClassification(aspect.toString()); 147 classification.setTitle(title); 148 classification.setRootCategory(convertToCategory(categoryNodeRef)); 150 151 classifications.add(classification); 152 } 153 } 154 155 return classifications.toArray(new Classification[classifications.size()]); 156 } 157 }); 158 } 159 catch (Throwable e) 160 { 161 if (logger.isDebugEnabled()) 162 { 163 logger.error("Unexpected error occurred", e); 164 } 165 166 throw new ClassificationFault(0, e.getMessage()); 167 } 168 } 169 170 private Category convertToCategory(NodeRef nodeRef) 171 { 172 String title = (String )this.nodeService.getProperty(nodeRef, ContentModel.PROP_NAME); 173 174 if (logger.isDebugEnabled()) 175 { 176 logger.debug("Category: " + title + "(" + nodeRef.toString() + ")"); 177 } 178 179 Category category = new Category(); 180 category.setId(Utils.convertToReference(nodeRef)); 181 category.setTitle(title); 182 return category; 184 } 185 186 189 public Category[] getChildCategories(final Reference parentCategory) 190 throws RemoteException , ClassificationFault 191 { 192 try 193 { 194 return TransactionUtil.executeInUserTransaction( 195 this.transactionService, 196 new TransactionUtil.TransactionWork<Category[]>() 197 { 198 public Category[] doWork() 199 { 200 NodeRef parentNodeRef = Utils.convertToNodeRef( 201 parentCategory, 202 ClassificationWebService.this.nodeService, 203 ClassificationWebService.this.searchService, 204 ClassificationWebService.this.namespaceService); 205 206 Collection <ChildAssociationRef> assocs = ClassificationWebService.this.categoryService.getChildren( 207 parentNodeRef, 208 CategoryService.Mode.SUB_CATEGORIES, 209 CategoryService.Depth.IMMEDIATE); 210 211 List <Category> categories = new ArrayList <Category>(assocs.size()); 212 213 for (ChildAssociationRef assoc : assocs) 214 { 215 NodeRef categoryNodeRef = assoc.getChildRef(); 216 categories.add(convertToCategory(categoryNodeRef)); 217 } 218 219 return categories.toArray(new Category[categories.size()]); 220 } 221 }); 222 } 223 catch (Throwable e) 224 { 225 if (logger.isDebugEnabled()) 226 { 227 logger.error("Unexpected error occurred", e); 228 } 229 230 throw new ClassificationFault(0, e.getMessage()); 231 } 232 } 233 234 237 public CategoriesResult[] getCategories(final Predicate items) 238 throws RemoteException , ClassificationFault 239 { 240 try 241 { 242 return TransactionUtil.executeInUserTransaction( 243 this.transactionService, 244 new TransactionUtil.TransactionWork<CategoriesResult[]>() 245 { 246 public CategoriesResult[] doWork() 247 { 248 List <CategoriesResult> result = new ArrayList <CategoriesResult>(); 249 250 List <NodeRef> nodeRefs = Utils.resolvePredicate( 251 items, 252 ClassificationWebService.this.nodeService, 253 ClassificationWebService.this.searchService, 254 ClassificationWebService.this.namespaceService); 255 256 for (NodeRef nodeRef : nodeRefs) 257 { 258 List <AppliedCategory> appliedCategories = new ArrayList <AppliedCategory>(); 259 260 Set <QName> apsects = ClassificationWebService.this.nodeService.getAspects(nodeRef); 261 for (QName aspect : apsects) 262 { 263 if (ClassificationWebService.this.dictionaryService.isSubClass(aspect, ContentModel.ASPECT_CLASSIFIABLE) == true) 264 { 265 QName categoryPropertyName = getPropertyName(aspect); 266 267 if (categoryPropertyName != null) 268 { 269 Collection <NodeRef> categoryNodeRefs = DefaultTypeConverter.INSTANCE.getCollection( 271 NodeRef.class, 272 ClassificationWebService.this.nodeService.getProperty(nodeRef, categoryPropertyName)); 273 274 Reference[] categoryReferences = new Reference[categoryNodeRefs.size()]; 275 int iIndex = 0; 276 for (NodeRef categoryNodeRef : categoryNodeRefs) 277 { 278 categoryReferences[iIndex] = Utils.convertToReference(categoryNodeRef); 279 iIndex ++; 280 } 281 282 283 AppliedCategory appliedCategory = new AppliedCategory(); 285 appliedCategory.setClassification(aspect.toString()); 286 appliedCategory.setCategories(categoryReferences); 287 288 appliedCategories.add(appliedCategory); 289 } 290 } 291 } 292 293 CategoriesResult categoryResult = new CategoriesResult(); 295 categoryResult.setNode(Utils.convertToReference(nodeRef)); 296 categoryResult.setCategories(appliedCategories.toArray(new AppliedCategory[appliedCategories.size()])); 297 298 result.add(categoryResult); 299 } 300 301 return result.toArray(new CategoriesResult[result.size()]); 302 } 303 }); 304 } 305 catch (Throwable e) 306 { 307 if (logger.isDebugEnabled()) 308 { 309 logger.error("Unexpected error occurred", e); 310 } 311 312 throw new ClassificationFault(0, e.getMessage()); 313 } 314 } 315 316 322 private QName getPropertyName(QName aspect) 323 { 324 QName categoryPropertyName = null; 325 326 org.alfresco.service.cmr.dictionary.ClassDefinition classDefinition = ClassificationWebService.this.dictionaryService.getClass(aspect); 328 for (PropertyDefinition propertyDefintion : classDefinition.getProperties().values()) 329 { 330 if (DataTypeDefinition.CATEGORY.equals(propertyDefintion.getDataType().getName()) == true) 331 { 332 categoryPropertyName = propertyDefintion.getName(); 334 break; 335 } 336 } 337 338 return categoryPropertyName; 339 } 340 341 345 public CategoriesResult[] setCategories(final Predicate items, final AppliedCategory[] categories) 346 throws RemoteException , ClassificationFault 347 { 348 try 349 { 350 return TransactionUtil.executeInUserTransaction( 351 this.transactionService, 352 new TransactionUtil.TransactionWork<CategoriesResult[]>() 353 { 354 public CategoriesResult[] doWork() 355 { 356 List <CategoriesResult> result = new ArrayList <CategoriesResult>(); 357 358 List <NodeRef> nodeRefs = Utils.resolvePredicate( 359 items, 360 ClassificationWebService.this.nodeService, 361 ClassificationWebService.this.searchService, 362 ClassificationWebService.this.namespaceService); 363 364 for (NodeRef nodeRef : nodeRefs) 365 { 366 List <AppliedCategory> appliedCategories = new ArrayList <AppliedCategory>(); 367 368 for (AppliedCategory category : categories) 369 { 370 QName aspect = QName.createQName(category.getClassification()); 371 QName propertyName = getPropertyName(aspect); 372 if (propertyName != null) 373 { 374 if (ClassificationWebService.this.nodeService.hasAspect(nodeRef, aspect) == false) 376 { 377 ClassificationWebService.this.nodeService.addAspect(nodeRef, aspect, null); 378 } 379 380 ArrayList <NodeRef> categoryNodeRefs = new ArrayList <NodeRef>(category.getCategories().length); 381 for (Reference categoryReference : category.getCategories()) 382 { 383 categoryNodeRefs.add(Utils.convertToNodeRef( 384 categoryReference, 385 ClassificationWebService.this.nodeService, 386 ClassificationWebService.this.searchService, 387 ClassificationWebService.this.namespaceService)); 388 } 389 390 ClassificationWebService.this.nodeService.setProperty(nodeRef, propertyName, categoryNodeRefs); 391 392 AppliedCategory appliedCategory = new AppliedCategory(); 394 appliedCategory.setClassification(category.getClassification()); 395 appliedCategory.setCategories(category.getCategories()); 396 397 appliedCategories.add(appliedCategory); 398 } 399 } 400 401 402 CategoriesResult categoryResult = new CategoriesResult(); 404 categoryResult.setNode(Utils.convertToReference(nodeRef)); 405 categoryResult.setCategories(appliedCategories.toArray(new AppliedCategory[appliedCategories.size()])); 406 407 result.add(categoryResult); 408 } 409 410 return result.toArray(new CategoriesResult[result.size()]); 411 } 412 }); 413 } 414 catch (Throwable e) 415 { 416 if (logger.isDebugEnabled()) 417 { 418 logger.error("Unexpected error occurred", e); 419 } 420 421 throw new ClassificationFault(0, e.getMessage()); 422 } 423 } 424 425 428 public ClassDefinition describeClassification(final String classification) 429 throws RemoteException , ClassificationFault 430 { 431 try 432 { 433 return TransactionUtil.executeInUserTransaction( 434 this.transactionService, 435 new TransactionUtil.TransactionWork<ClassDefinition>() 436 { 437 public ClassDefinition doWork() 438 { 439 org.alfresco.service.cmr.dictionary.ClassDefinition classDefinition = ClassificationWebService.this.dictionaryService.getClass(QName.createQName(classification)); 440 return Utils.setupClassDefObject(classDefinition); 441 } 442 }); 443 } 444 catch (Throwable e) 445 { 446 if (logger.isDebugEnabled()) 447 { 448 logger.error("Unexpected error occurred", e); 449 } 450 451 throw new ClassificationFault(0, e.getMessage()); 452 } 453 } 454 } 455 | Popular Tags |