KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > classification > ClassificationWebService


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.webservice.classification;
18
19 import java.io.Serializable JavaDoc;
20 import java.rmi.RemoteException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
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 /**
49  * Web service implementation of the ClassificationService. The WSDL for this
50  * service can be accessed from
51  * http://localhost:8080/alfresco/wsdl/classification-service.wsdl
52  *
53  * @author gavinc
54  */

55 public class ClassificationWebService extends AbstractWebService implements
56         ClassificationServiceSoapPort
57 {
58     private static Log logger = LogFactory.getLog(ClassificationWebService.class);
59
60     /**
61      * The category service
62      */

63     private CategoryService categoryService;
64     
65     /**
66      * The dictionary service
67      */

68     private DictionaryService dictionaryService;
69     
70     /**
71      * The transaction service
72      */

73     private TransactionService transactionService;
74
75     /**
76      * Set the category service
77      *
78      * @param categoryService the category service
79      */

80     public void setCategoryService(CategoryService categoryService)
81     {
82         this.categoryService = categoryService;
83     }
84     
85     /**
86      * Set the transaction service
87      *
88      * @param transactionService the transaction service
89      */

90     public void setTransactionService(TransactionService transactionService)
91     {
92         this.transactionService = transactionService;
93     }
94     
95     /**
96      * Set the dictionary service
97      *
98      * @param dictionaryService the dictionary service
99      */

100     public void setDictionaryService(DictionaryService dictionaryService)
101     {
102         this.dictionaryService = dictionaryService;
103     }
104     
105     /**
106      * @see org.alfresco.repo.webservice.classification.ClassificationServiceSoapPort#getClassifications()
107      */

108     public Classification[] getClassifications(final Store store) throws RemoteException JavaDoc,
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 JavaDoc<Classification> classifications = new ArrayList JavaDoc<Classification>();
120                             
121                             Collection JavaDoc<QName> categoryAspects = ClassificationWebService.this.categoryService.getClassificationAspects();
122                             for (QName aspect : categoryAspects)
123                             {
124                                 // Get the title of the cateogry
125
String JavaDoc 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 JavaDoc<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                                     // TODO set the description
149
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 JavaDoc 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 JavaDoc title = (String JavaDoc)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         // TODO need to set the description
183
return category;
184     }
185
186     /**
187      * @see org.alfresco.repo.webservice.classification.ClassificationServiceSoapPort#getChildCategories(org.alfresco.repo.webservice.types.Reference)
188      */

189     public Category[] getChildCategories(final Reference parentCategory)
190             throws RemoteException JavaDoc, 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 JavaDoc<ChildAssociationRef> assocs = ClassificationWebService.this.categoryService.getChildren(
207                                     parentNodeRef,
208                                     CategoryService.Mode.SUB_CATEGORIES,
209                                     CategoryService.Depth.IMMEDIATE);
210                             
211                             List JavaDoc<Category> categories = new ArrayList JavaDoc<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 JavaDoc 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     /**
235      * @see org.alfresco.repo.webservice.classification.ClassificationServiceSoapPort#getCategories(org.alfresco.repo.webservice.types.Predicate)
236      */

237     public CategoriesResult[] getCategories(final Predicate items)
238             throws RemoteException JavaDoc, 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 JavaDoc<CategoriesResult> result = new ArrayList JavaDoc<CategoriesResult>();
249                             
250                             List JavaDoc<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 JavaDoc<AppliedCategory> appliedCategories = new ArrayList JavaDoc<AppliedCategory>();
259                                 
260                                 Set JavaDoc<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                                             // Get the category value
270
Collection JavaDoc<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                                             // Create the applied category object
284
AppliedCategory appliedCategory = new AppliedCategory();
285                                             appliedCategory.setClassification(aspect.toString());
286                                             appliedCategory.setCategories(categoryReferences);
287                                             
288                                             appliedCategories.add(appliedCategory);
289                                         }
290                                     }
291                                 }
292                                 
293                                 // Create the category result object
294
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 JavaDoc 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     /**
317      * Get the category property qname for a classifiable apsect
318      *
319      * @param aspect the aspect qname
320      * @return the property qname, null if none found
321      */

322     private QName getPropertyName(QName aspect)
323     {
324         QName categoryPropertyName = null;
325         
326         // Need to get category property
327
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                 // We have found the category property (assume there is only one)
333
categoryPropertyName = propertyDefintion.getName();
334                 break;
335             }
336         }
337         
338         return categoryPropertyName;
339     }
340
341     /**
342      * @see org.alfresco.repo.webservice.classification.ClassificationServiceSoapPort#setCategories(org.alfresco.repo.webservice.types.Predicate,
343      * org.alfresco.repo.webservice.classification.AppliedCategory[])
344      */

345     public CategoriesResult[] setCategories(final Predicate items, final AppliedCategory[] categories)
346              throws RemoteException JavaDoc, 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 JavaDoc<CategoriesResult> result = new ArrayList JavaDoc<CategoriesResult>();
357                             
358                             List JavaDoc<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 JavaDoc<AppliedCategory> appliedCategories = new ArrayList JavaDoc<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                                         // First check that the aspect has been applied to the node
375
if (ClassificationWebService.this.nodeService.hasAspect(nodeRef, aspect) == false)
376                                         {
377                                             ClassificationWebService.this.nodeService.addAspect(nodeRef, aspect, null);
378                                         }
379                                         
380                                         ArrayList JavaDoc<NodeRef> categoryNodeRefs = new ArrayList JavaDoc<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                                         // Create the applied category object
393
AppliedCategory appliedCategory = new AppliedCategory();
394                                         appliedCategory.setClassification(category.getClassification());
395                                         appliedCategory.setCategories(category.getCategories());
396                                         
397                                         appliedCategories.add(appliedCategory);
398                                     }
399                                 }
400                                 
401                                 
402                                 // Create the category result object
403
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 JavaDoc 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     /**
426      * @see org.alfresco.repo.webservice.classification.ClassificationServiceSoapPort#describeClassification(org.alfresco.repo.webservice.types.Reference)
427      */

428     public ClassDefinition describeClassification(final String JavaDoc classification)
429             throws RemoteException JavaDoc, 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 JavaDoc 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