KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > repository > RepositoryWebService


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.repository;
18
19 import java.io.Serializable JavaDoc;
20 import java.rmi.RemoteException JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.transaction.UserTransaction JavaDoc;
26
27 import org.alfresco.repo.cache.SimpleCache;
28 import org.alfresco.repo.webservice.AbstractWebService;
29 import org.alfresco.repo.webservice.CMLUtil;
30 import org.alfresco.repo.webservice.Utils;
31 import org.alfresco.repo.webservice.types.CML;
32 import org.alfresco.repo.webservice.types.ClassDefinition;
33 import org.alfresco.repo.webservice.types.NamedValue;
34 import org.alfresco.repo.webservice.types.Node;
35 import org.alfresco.repo.webservice.types.NodeDefinition;
36 import org.alfresco.repo.webservice.types.Predicate;
37 import org.alfresco.repo.webservice.types.Query;
38 import org.alfresco.repo.webservice.types.QueryLanguageEnum;
39 import org.alfresco.repo.webservice.types.Reference;
40 import org.alfresco.repo.webservice.types.Store;
41 import org.alfresco.repo.webservice.types.StoreEnum;
42 import org.alfresco.service.cmr.dictionary.AspectDefinition;
43 import org.alfresco.service.cmr.dictionary.DictionaryService;
44 import org.alfresco.service.cmr.dictionary.TypeDefinition;
45 import org.alfresco.service.cmr.repository.NodeRef;
46 import org.alfresco.service.cmr.repository.StoreRef;
47 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
48 import org.alfresco.service.namespace.QName;
49 import org.apache.axis.MessageContext;
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52
53 /**
54  * Web service implementation of the RepositoryService. The WSDL for this
55  * service can be accessed from
56  * http://localhost:8080/alfresco/wsdl/repository-service.wsdl
57  *
58  * @author gavinc
59  */

60 public class RepositoryWebService extends AbstractWebService implements
61         RepositoryServiceSoapPort
62 {
63     private static Log logger = LogFactory.getLog(RepositoryWebService.class);
64
65     private DictionaryService dictionaryService;
66
67     private CMLUtil cmlUtil;
68
69     private SimpleCache<String JavaDoc, QuerySession> querySessionCache;
70
71     /**
72      * Sets the instance of the DictionaryService to be used
73      *
74      * @param dictionaryService
75      * The DictionaryService
76      */

77     public void setDictionaryService(DictionaryService dictionaryService)
78     {
79         this.dictionaryService = dictionaryService;
80     }
81
82     /**
83      * Sets the CML Util
84      *
85      * @param cmlUtil CML util object
86      */

87     public void setCmlUtil(CMLUtil cmlUtil)
88     {
89         this.cmlUtil = cmlUtil;
90     }
91
92     /**
93      * Sets the instance of the SimpleCache to be used
94      *
95      * @param querySessionCache
96      * The SimpleCache
97      */

98     public void setQuerySessionCache(
99             SimpleCache<String JavaDoc, QuerySession> querySessionCache)
100     {
101         this.querySessionCache = querySessionCache;
102     }
103     
104     /**
105      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#createStore(org.alfresco.repo.webservice.types.StoreEnum, java.lang.String)
106      */

107     public Store createStore(StoreEnum scheme, String JavaDoc address) throws RemoteException JavaDoc, RepositoryFault
108     {
109         String JavaDoc protocol = scheme.getValue();
110         StoreRef storeRef = this.nodeService.createStore(protocol, address);
111         
112         StoreEnum storeEnum = StoreEnum.fromString(storeRef
113                 .getProtocol());
114         return new Store(storeEnum, storeRef.getIdentifier());
115     }
116
117     /**
118      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#getStores()
119      */

120     public Store[] getStores() throws RemoteException JavaDoc, RepositoryFault
121     {
122         UserTransaction JavaDoc tx = null;
123
124         try
125         {
126             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
127             tx.begin();
128
129             List JavaDoc<StoreRef> stores = this.nodeService.getStores();
130             Store[] returnStores = new Store[stores.size()];
131             for (int x = 0; x < stores.size(); x++)
132             {
133                 StoreRef storeRef = stores.get(x);
134                 
135                 if (logger.isDebugEnabled() == true)
136                 {
137                     logger.debug("Store protocol :" + storeRef.getProtocol());
138                 }
139                 
140                 StoreEnum storeEnum = StoreEnum.fromString(storeRef
141                         .getProtocol());
142                 Store store = new Store(storeEnum, storeRef.getIdentifier());
143                 returnStores[x] = store;
144             }
145
146             // commit the transaction
147
tx.commit();
148
149             return returnStores;
150         } catch (Throwable JavaDoc e)
151         {
152             // rollback the transaction
153
try
154             {
155                 if (tx != null)
156                 {
157                     tx.rollback();
158                 }
159             } catch (Exception JavaDoc ex)
160             {
161             }
162
163             if (logger.isDebugEnabled())
164             {
165                 logger.error("Unexpected error occurred", e);
166             }
167
168             throw new RepositoryFault(0, e.getMessage());
169         }
170     }
171
172     /**
173      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#query(org.alfresco.repo.webservice.types.Store,
174      * org.alfresco.repo.webservice.types.Query, boolean)
175      */

176     public QueryResult query(Store store, Query query, boolean includeMetaData)
177             throws RemoteException JavaDoc, RepositoryFault
178     {
179         QueryLanguageEnum langEnum = query.getLanguage();
180
181         if (langEnum.equals(QueryLanguageEnum.cql)
182                 || langEnum.equals(QueryLanguageEnum.xpath))
183         {
184             throw new RepositoryFault(110, "Only '"
185                     + QueryLanguageEnum.lucene.getValue()
186                     + "' queries are currently supported!");
187         }
188
189         UserTransaction JavaDoc tx = null;
190         MessageContext msgContext = MessageContext.getCurrentContext();
191
192         try
193         {
194             tx = Utils.getUserTransaction(msgContext);
195             tx.begin();
196
197             // setup a query session and get the first batch of results
198
QuerySession querySession = new ResultSetQuerySession(Utils
199                     .getBatchSize(msgContext), store, query, includeMetaData);
200             QueryResult queryResult = querySession
201                     .getNextResultsBatch(this.searchService, this.nodeService,
202                             this.namespaceService);
203
204             // add the session to the cache if there are more results to come
205
if (queryResult.getQuerySession() != null)
206             {
207                 // this.querySessionCache.putQuerySession(querySession);
208
this.querySessionCache.put(queryResult.getQuerySession(),
209                         querySession);
210             }
211
212             // commit the transaction
213
tx.commit();
214
215             return queryResult;
216         } catch (Throwable JavaDoc e)
217         {
218             // rollback the transaction
219
try
220             {
221                 if (tx != null)
222                 {
223                     tx.rollback();
224                 }
225             } catch (Exception JavaDoc ex)
226             {
227             }
228
229             if (logger.isDebugEnabled())
230             {
231                 logger.error("Unexpected error occurred", e);
232             }
233
234             e.printStackTrace();
235
236             throw new RepositoryFault(0, e.getMessage());
237         }
238     }
239
240     /**
241      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#queryChildren(org.alfresco.repo.webservice.types.Reference)
242      */

243     public QueryResult queryChildren(Reference node) throws RemoteException JavaDoc,
244             RepositoryFault
245     {
246         UserTransaction JavaDoc tx = null;
247
248         try
249         {
250             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
251             tx.begin();
252
253             // setup a query session and get the first batch of results
254
QuerySession querySession = new ChildrenQuerySession(Utils
255                     .getBatchSize(MessageContext.getCurrentContext()), node);
256             QueryResult queryResult = querySession
257                     .getNextResultsBatch(this.searchService, this.nodeService,
258                             this.namespaceService);
259
260             // add the session to the cache if there are more results to come
261
if (queryResult.getQuerySession() != null)
262             {
263                 // this.querySessionCache.putQuerySession(querySession);
264
this.querySessionCache.put(queryResult.getQuerySession(),
265                         querySession);
266             }
267             
268             if (logger.isDebugEnabled() == true)
269             {
270                 logger.debug("Method end ... queryChildren");
271             }
272
273             // commit the transaction
274
tx.commit();
275
276             return queryResult;
277         } catch (Throwable JavaDoc e)
278         {
279             // rollback the transaction
280
try
281             {
282                 if (tx != null)
283                 {
284                     tx.rollback();
285                 }
286             } catch (Exception JavaDoc ex)
287             {
288             }
289
290             if (logger.isDebugEnabled())
291             {
292                 logger.error("Unexpected error occurred", e);
293             }
294
295             throw new RepositoryFault(0, e.getMessage());
296         }
297     }
298
299     /**
300      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#queryParents(org.alfresco.repo.webservice.types.Reference)
301      */

302     public QueryResult queryParents(Reference node) throws RemoteException JavaDoc,
303             RepositoryFault
304     {
305         UserTransaction JavaDoc tx = null;
306
307         try
308         {
309             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
310             tx.begin();
311
312             // setup a query session and get the first batch of results
313
QuerySession querySession = new ParentsQuerySession(Utils
314                     .getBatchSize(MessageContext.getCurrentContext()), node);
315             QueryResult queryResult = querySession
316                     .getNextResultsBatch(this.searchService, this.nodeService,
317                             this.namespaceService);
318
319             // add the session to the cache if there are more results to come
320
if (queryResult.getQuerySession() != null)
321             {
322                 // this.querySessionCache.putQuerySession(querySession);
323
this.querySessionCache.put(queryResult.getQuerySession(),
324                         querySession);
325             }
326
327             // commit the transaction
328
tx.commit();
329
330             return queryResult;
331         } catch (Throwable JavaDoc e)
332         {
333             // rollback the transaction
334
try
335             {
336                 if (tx != null)
337                 {
338                     tx.rollback();
339                 }
340             } catch (Exception JavaDoc ex)
341             {
342             }
343
344             if (logger.isDebugEnabled())
345             {
346                 logger.error("Unexpected error occurred", e);
347             }
348
349             throw new RepositoryFault(0, e.getMessage());
350         }
351     }
352
353     /**
354      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#queryAssociated(org.alfresco.repo.webservice.types.Reference,
355      * org.alfresco.repo.webservice.repository.Association[])
356      */

357     public QueryResult queryAssociated(Reference node, Association[] association)
358             throws RemoteException JavaDoc, RepositoryFault
359     {
360         UserTransaction JavaDoc tx = null;
361
362         try
363         {
364             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
365             tx.begin();
366
367             // setup a query session and get the first batch of results
368
QuerySession querySession = new AssociatedQuerySession(Utils.getBatchSize(MessageContext.getCurrentContext()), node);
369             QueryResult queryResult = querySession
370                     .getNextResultsBatch(this.searchService, this.nodeService,
371                             this.namespaceService);
372
373             // add the session to the cache if there are more results to come
374
if (queryResult.getQuerySession() != null)
375             {
376                 // this.querySessionCache.putQuerySession(querySession);
377
this.querySessionCache.put(queryResult.getQuerySession(),
378                         querySession);
379             }
380
381             // commit the transaction
382
tx.commit();
383
384             return queryResult;
385         }
386         catch (Throwable JavaDoc e)
387         {
388             // rollback the transaction
389
try
390             {
391                 if (tx != null)
392                 {
393                     tx.rollback();
394                 }
395             } catch (Exception JavaDoc ex)
396             {
397             }
398
399             if (logger.isDebugEnabled())
400             {
401                 logger.error("Unexpected error occurred", e);
402             }
403
404             throw new RepositoryFault(0, e.getMessage());
405         }
406     }
407
408     /**
409      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#fetchMore(java.lang.String)
410      */

411     public QueryResult fetchMore(String JavaDoc querySession) throws RemoteException JavaDoc,
412             RepositoryFault
413     {
414         QueryResult queryResult = null;
415
416         UserTransaction JavaDoc tx = null;
417
418         try
419         {
420             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
421             tx.begin();
422
423             // try and get the QuerySession with the given id from the cache
424
QuerySession session = this.querySessionCache.get(querySession);
425
426             if (session == null)
427             {
428                 if (logger.isDebugEnabled())
429                     logger.debug("Invalid querySession id requested: "
430                             + querySession);
431
432                 throw new RepositoryFault(4, "querySession with id '"
433                         + querySession + "' is invalid");
434             }
435
436             // get the next batch of results
437
queryResult = session.getNextResultsBatch(this.searchService,
438                     this.nodeService, this.namespaceService);
439
440             // remove the QuerySession from the cache if there are no more
441
// results to come
442
if (queryResult.getQuerySession() == null)
443             {
444                 this.querySessionCache.remove(querySession);
445             }
446
447             // commit the transaction
448
tx.commit();
449
450             return queryResult;
451         } catch (Throwable JavaDoc e)
452         {
453             // rollback the transaction
454
try
455             {
456                 if (tx != null)
457                 {
458                     tx.rollback();
459                 }
460             } catch (Exception JavaDoc ex)
461             {
462             }
463
464             if (e instanceof RepositoryFault)
465             {
466                 throw (RepositoryFault) e;
467             } else
468             {
469                 if (logger.isDebugEnabled())
470                 {
471                     logger.error("Unexpected error occurred", e);
472                 }
473
474                 throw new RepositoryFault(0, e.getMessage());
475             }
476         }
477     }
478
479     /**
480      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#update(org.alfresco.repo.webservice.types.CML)
481      */

482     public UpdateResult[] update(CML statements) throws RemoteException JavaDoc,
483             RepositoryFault
484     {
485         UpdateResult[] result = null;
486         UserTransaction JavaDoc tx = null;
487
488         try
489         {
490             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
491             tx.begin();
492
493             result = this.cmlUtil.executeCML(statements);
494
495             // commit the transaction
496
tx.commit();
497
498             return result;
499         }
500         catch (Throwable JavaDoc e)
501         {
502             // rollback the transaction
503
try
504             {
505                 if (tx != null)
506                 {
507                     tx.rollback();
508                 }
509             } catch (Exception JavaDoc ex)
510             {
511             }
512
513             if (logger.isDebugEnabled())
514             {
515                 logger.error("Unexpected error occurred", e);
516             }
517
518             throw new RepositoryFault(0, e.getMessage());
519         }
520     }
521
522     /**
523      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#describe(org.alfresco.repo.webservice.types.Predicate)
524      */

525     public NodeDefinition[] describe(Predicate items) throws RemoteException JavaDoc,
526             RepositoryFault
527     {
528         NodeDefinition[] nodeDefs = null;
529         UserTransaction JavaDoc tx = null;
530
531         try
532         {
533             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
534             tx.begin();
535
536             List JavaDoc<NodeRef> nodes = Utils
537                     .resolvePredicate(items, this.nodeService,
538                             this.searchService, this.namespaceService);
539             nodeDefs = new NodeDefinition[nodes.size()];
540
541             for (int x = 0; x < nodes.size(); x++)
542             {
543                 nodeDefs[x] = setupNodeDefObject(nodes.get(x));
544             }
545
546             // commit the transaction
547
tx.commit();
548
549             return nodeDefs;
550         } catch (Throwable JavaDoc e)
551         {
552             // rollback the transaction
553
try
554             {
555                 if (tx != null)
556                 {
557                     tx.rollback();
558                 }
559             } catch (Exception JavaDoc ex)
560             {
561             }
562
563             if (logger.isDebugEnabled())
564             {
565                 logger.error("Unexpected error occurred", e);
566             }
567
568             throw new RepositoryFault(0, e.getMessage());
569         }
570     }
571
572     /**
573      * Creates a NodeDefinition web service type object for the given
574      * repository NodeRef instance
575      *
576      * @param nodeRef The NodeRef to generate the NodeDefinition for
577      * @return The NodeDefinition representation of nodeRef
578      */

579     private NodeDefinition setupNodeDefObject(NodeRef nodeRef)
580     {
581         if (logger.isDebugEnabled())
582             logger.debug("Building NodeDefinition for node: " + nodeRef);
583
584         TypeDefinition ddTypeDef = this.dictionaryService
585                 .getType(this.nodeService.getType(nodeRef));
586
587         // create the web service ClassDefinition type from the data dictionary TypeDefinition
588
ClassDefinition typeDef = Utils.setupClassDefObject(ddTypeDef);
589
590         // create the web service ClassDefinition types to represent the aspects
591
ClassDefinition[] aspectDefs = null;
592         List JavaDoc<AspectDefinition> aspects = ddTypeDef.getDefaultAspects();
593         if (aspects != null)
594         {
595             aspectDefs = new ClassDefinition[aspects.size()];
596             int pos = 0;
597             for (AspectDefinition ddAspectDef : aspects)
598             {
599                 aspectDefs[pos] = Utils.setupClassDefObject(ddAspectDef);
600                 pos++;
601             }
602         }
603
604         return new NodeDefinition(typeDef, aspectDefs);
605     }
606
607     /**
608      * Gets the nodes associatiated with the predicate provided. Usefull when the store and ids of the required
609      * nodes are known.
610      *
611      * @see org.alfresco.repo.webservice.repository.RepositoryServiceSoapPort#get(org.alfresco.repo.webservice.types.Predicate)
612      */

613     public Node[] get(Predicate where) throws RemoteException JavaDoc, RepositoryFault
614     {
615         Node[] nodes = null;
616         UserTransaction JavaDoc tx = null;
617
618         try
619         {
620             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
621             tx.begin();
622             
623             // Resolve the predicate to a list of node references
624
List JavaDoc<NodeRef> nodeRefs = Utils.resolvePredicate(where, this.nodeService, this.searchService, this.namespaceService);
625             nodes = new Node[nodeRefs.size()];
626             int index = 0;
627             for (NodeRef nodeRef : nodeRefs)
628             {
629                 // Get the nodes reference
630
Reference reference = Utils.convertToReference(nodeRef);
631                 
632                 // Get the nodes type
633
String JavaDoc type = this.nodeService.getType(nodeRef).toString();
634                 
635                 // Get the nodes aspects
636
Set JavaDoc<QName> aspectQNames = this.nodeService.getAspects(nodeRef);
637                 String JavaDoc[] aspects = new String JavaDoc[aspectQNames.size()];
638                 int aspectIndex = 0;
639                 for (QName aspectQName : aspectQNames)
640                 {
641                     aspects[aspectIndex] = aspectQName.toString();
642                     aspectIndex++;
643                 }
644                 
645                 // Get the nodes properties
646
Map JavaDoc<QName, Serializable JavaDoc> propertyMap = this.nodeService.getProperties(nodeRef);
647                 NamedValue[] properties = new NamedValue[propertyMap.size()];
648                 int propertyIndex = 0;
649                 for (Map.Entry JavaDoc<QName, Serializable JavaDoc> entry : propertyMap.entrySet())
650                 {
651                     String JavaDoc value = null;
652                     try
653                     {
654                         value = DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, entry.getValue());
655                     }
656                     catch (Throwable JavaDoc exception)
657                     {
658                         value = entry.getValue().toString();
659                     }
660                     properties[propertyIndex] = new NamedValue(entry.getKey().toString(), value);
661                     propertyIndex++;
662                 }
663                 
664                 // Create the node and add to the array
665
Node node = new Node(reference, type, aspects, properties);
666                 nodes[index] = node;
667                 
668                 index++;
669             }
670             
671             // commit the transaction
672
tx.commit();
673         }
674         catch (Throwable JavaDoc e)
675         {
676             // rollback the transaction
677
try
678             {
679                 if (tx != null)
680                 {
681                     tx.rollback();
682                 }
683             }
684             catch (Exception JavaDoc ex)
685             {
686                 // Ignore
687
}
688
689             if (logger.isDebugEnabled())
690             {
691                 logger.error("Unexpected error occurred", e);
692             }
693
694             throw new RepositoryFault(0, e.getMessage());
695         }
696         
697         return nodes;
698     }
699 }
700
Popular Tags