KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > version > NodeServiceImpl


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.version;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.alfresco.model.ContentModel;
30 import org.alfresco.service.cmr.dictionary.DictionaryService;
31 import org.alfresco.service.cmr.dictionary.InvalidAspectException;
32 import org.alfresco.service.cmr.repository.AssociationExistsException;
33 import org.alfresco.service.cmr.repository.AssociationRef;
34 import org.alfresco.service.cmr.repository.ChildAssociationRef;
35 import org.alfresco.service.cmr.repository.InvalidChildAssociationRefException;
36 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
37 import org.alfresco.service.cmr.repository.NodeRef;
38 import org.alfresco.service.cmr.repository.NodeService;
39 import org.alfresco.service.cmr.repository.Path;
40 import org.alfresco.service.cmr.repository.StoreRef;
41 import org.alfresco.service.cmr.repository.NodeRef.Status;
42 import org.alfresco.service.cmr.search.QueryParameterDefinition;
43 import org.alfresco.service.cmr.search.SearchService;
44 import org.alfresco.service.namespace.NamespacePrefixResolver;
45 import org.alfresco.service.namespace.QName;
46 import org.alfresco.service.namespace.QNamePattern;
47 import org.alfresco.service.namespace.RegexQNamePattern;
48
49
50 /**
51  * The light weight version store node service implementation.
52  *
53  * @author Roy Wetherall
54  */

55 public class NodeServiceImpl implements NodeService, VersionModel
56 {
57     /**
58      * Error messages
59      */

60     private final static String JavaDoc MSG_UNSUPPORTED =
61         "This operation is not supported by a version store implementation of the node service.";
62     
63     /**
64      * The name of the spoofed root association
65      */

66     private static final QName rootAssocName = QName.createQName(VersionModel.NAMESPACE_URI, "versionedState");
67     
68     /**
69      * The db node service, used as the version store implementation
70      */

71     protected NodeService dbNodeService;
72
73     /**
74      * The repository searcher
75      */

76     @SuppressWarnings JavaDoc("unused")
77     private SearchService searcher;
78     
79     /**
80      * The dictionary service
81      */

82     protected DictionaryService dicitionaryService;
83     
84     
85     /**
86      * Sets the db node service, used as the version store implementation
87      *
88      * @param nodeService the node service
89      */

90     public void setDbNodeService(NodeService nodeService)
91     {
92         this.dbNodeService = nodeService;
93     }
94
95     /**
96      * Sets the searcher
97      *
98      * @param searcher the searcher
99      */

100     public void setSearcher(SearchService searcher)
101     {
102         this.searcher = searcher;
103     }
104     
105     /**
106      * Sets the dictionary service
107      *
108      * @param dictionaryService the dictionary service
109      */

110     public void setDictionaryService(DictionaryService dictionaryService)
111     {
112         this.dicitionaryService = dictionaryService;
113     }
114     
115     /**
116      * Delegates to the <code>NodeService</code> used as the version store implementation
117      */

118     public List JavaDoc<StoreRef> getStores()
119     {
120         return dbNodeService.getStores();
121     }
122     
123     /**
124      * Delegates to the <code>NodeService</code> used as the version store implementation
125      */

126     public StoreRef createStore(String JavaDoc protocol, String JavaDoc identifier)
127     {
128         return dbNodeService.createStore(protocol, identifier);
129     }
130
131     /**
132      * Delegates to the <code>NodeService</code> used as the version store implementation
133      */

134     public boolean exists(StoreRef storeRef)
135     {
136         return dbNodeService.exists(storeRef);
137     }
138
139     /**
140      * Delegates to the <code>NodeService</code> used as the version store implementation
141      */

142     public boolean exists(NodeRef nodeRef)
143     {
144         return dbNodeService.exists(convertNodeRef(nodeRef));
145     }
146     
147     /**
148      * Delegates to the <code>NodeService</code> used as the version store implementation
149      */

150     public Status getNodeStatus(NodeRef nodeRef)
151     {
152         return dbNodeService.getNodeStatus(nodeRef);
153     }
154
155     /**
156      * Convert the incomming node ref (with the version store protocol specified)
157      * to the internal representation with the workspace protocol.
158      *
159      * @param nodeRef the incomming verison protocol node reference
160      * @return the internal version node reference
161      */

162     private NodeRef convertNodeRef(NodeRef nodeRef)
163     {
164         return new NodeRef(new StoreRef(StoreRef.PROTOCOL_WORKSPACE, STORE_ID), nodeRef.getId());
165     }
166
167     /**
168      * Delegates to the <code>NodeService</code> used as the version store implementation
169      */

170     public NodeRef getRootNode(StoreRef storeRef)
171     {
172         return dbNodeService.getRootNode(storeRef);
173     }
174
175     /**
176      * @throws UnsupportedOperationException always
177      */

178     public ChildAssociationRef createNode(
179             NodeRef parentRef,
180             QName assocTypeQName,
181             QName assocQName,
182             QName nodeTypeQName) throws InvalidNodeRefException
183     {
184         // This operation is not supported for a verion store
185
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
186     }
187     
188     /**
189      * @throws UnsupportedOperationException always
190      */

191     public ChildAssociationRef createNode(
192             NodeRef parentRef,
193             QName assocTypeQName,
194             QName assocQName,
195             QName nodeTypeQName,
196             Map JavaDoc<QName, Serializable JavaDoc> properties) throws InvalidNodeRefException
197     {
198         // This operation is not supported for a verion store
199
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
200     }
201     
202     /**
203      * @throws UnsupportedOperationException always
204      */

205     public void deleteNode(NodeRef nodeRef) throws InvalidNodeRefException
206     {
207         // This operation is not supported for a verion store
208
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
209     }
210     
211     /**
212      * @throws UnsupportedOperationException always
213      */

214     public ChildAssociationRef addChild(NodeRef parentRef,
215             NodeRef childRef,
216             QName assocTypeQName,
217             QName qname) throws InvalidNodeRefException
218     {
219         // This operation is not supported for a verion store
220
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
221     }
222     
223     /**
224      * @throws UnsupportedOperationException always
225      */

226     public void removeChild(NodeRef parentRef, NodeRef childRef) throws InvalidNodeRefException
227     {
228         // This operation is not supported for a verion store
229
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
230     }
231
232     /**
233      * @throws UnsupportedOperationException always
234      */

235     public ChildAssociationRef moveNode(NodeRef nodeToMoveRef, NodeRef newParentRef, QName assocTypeQName, QName assocQName) throws InvalidNodeRefException
236     {
237         throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
238     }
239
240     /**
241      * @throws UnsupportedOperationException always
242      */

243     public void setChildAssociationIndex(ChildAssociationRef childAssocRef, int index) throws InvalidChildAssociationRefException
244     {
245         throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
246     }
247
248     /**
249      * Type translation for version store
250      */

251     public QName getType(NodeRef nodeRef) throws InvalidNodeRefException
252     {
253         return (QName)this.dbNodeService.getProperty(convertNodeRef(nodeRef), PROP_QNAME_FROZEN_NODE_TYPE);
254     }
255     
256     /**
257      * @see org.alfresco.service.cmr.repository.NodeService#setType(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
258      */

259     public void setType(NodeRef nodeRef, QName typeQName) throws InvalidNodeRefException
260     {
261         // This operation is not supported for a version store
262
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
263     }
264     
265     /**
266      * @throws UnsupportedOperationException always
267      */

268     public void addAspect(NodeRef nodeRef, QName aspectRef, Map JavaDoc<QName, Serializable JavaDoc> aspectProperties) throws InvalidNodeRefException, InvalidAspectException
269     {
270         // This operation is not supported for a verion store
271
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
272     }
273
274     /**
275      * Translation for version store
276      */

277     public boolean hasAspect(NodeRef nodeRef, QName aspectRef) throws InvalidNodeRefException, InvalidAspectException
278     {
279         return getAspects(nodeRef).contains(aspectRef);
280     }
281
282     /**
283      * @throws UnsupportedOperationException always
284      */

285     public void removeAspect(NodeRef nodeRef, QName aspectRef) throws InvalidNodeRefException, InvalidAspectException
286     {
287         // This operation is not supported for a verion store
288
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
289     }
290
291     /**
292      * Translation for version store
293      */

294     public Set JavaDoc<QName> getAspects(NodeRef nodeRef) throws InvalidNodeRefException
295     {
296         return new HashSet JavaDoc<QName>(
297                 (ArrayList JavaDoc<QName>)this.dbNodeService.getProperty(convertNodeRef(nodeRef), PROP_QNAME_FROZEN_ASPECTS));
298     }
299
300     /**
301      * Property translation for version store
302      */

303     public Map JavaDoc<QName, Serializable JavaDoc> getProperties(NodeRef nodeRef) throws InvalidNodeRefException
304     {
305         Map JavaDoc<QName, Serializable JavaDoc> result = new HashMap JavaDoc<QName, Serializable JavaDoc>();
306         
307         // TODO should be doing this using a path query ..
308

309         Collection JavaDoc<ChildAssociationRef> children = this.dbNodeService.getChildAssocs(convertNodeRef(nodeRef));
310         for (ChildAssociationRef child : children)
311         {
312             if (child.getQName().equals(CHILD_QNAME_VERSIONED_ATTRIBUTES))
313             {
314                 NodeRef versionedAttribute = child.getChildRef();
315
316                 // Get the QName and the value
317
Serializable JavaDoc value = null;
318                 QName qName = (QName)this.dbNodeService.getProperty(versionedAttribute, PROP_QNAME_QNAME);
319                 Boolean JavaDoc isMultiValue = (Boolean JavaDoc)this.dbNodeService.getProperty(versionedAttribute, PROP_QNAME_IS_MULTI_VALUE);
320                 if (isMultiValue.booleanValue() == false)
321                 {
322                     value = this.dbNodeService.getProperty(versionedAttribute, PROP_QNAME_VALUE);
323                 }
324                 else
325                 {
326                     value = this.dbNodeService.getProperty(versionedAttribute, PROP_QNAME_MULTI_VALUE);
327                 }
328                 
329                 result.put(qName, value);
330             }
331         }
332         
333         return result;
334     }
335     
336     /**
337      * Property translation for version store
338      */

339     public Serializable JavaDoc getProperty(NodeRef nodeRef, QName qname) throws InvalidNodeRefException
340     {
341         // TODO should be doing this with a search ...
342

343         Map JavaDoc<QName, Serializable JavaDoc> properties = getProperties(convertNodeRef(nodeRef));
344         return properties.get(qname);
345     }
346     
347     /**
348      * @throws UnsupportedOperationException always
349      */

350     public void setProperties(NodeRef nodeRef, Map JavaDoc<QName, Serializable JavaDoc> properties) throws InvalidNodeRefException
351     {
352         // This operation is not supported for a verion store
353
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
354     }
355     
356     /**
357      * @throws UnsupportedOperationException always
358      */

359     public void setProperty(NodeRef nodeRef, QName qame, Serializable JavaDoc value) throws InvalidNodeRefException
360     {
361         // This operation is not supported for a verion store
362
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
363     }
364     
365     /**
366      * The node will appear to be attached to the root of the version store
367      *
368      * @see NodeService#getParentAssocs(NodeRef)
369      */

370     public List JavaDoc<ChildAssociationRef> getParentAssocs(NodeRef nodeRef)
371     {
372         return getParentAssocs(nodeRef, RegexQNamePattern.MATCH_ALL, RegexQNamePattern.MATCH_ALL);
373     }
374     
375     /**
376      * The node will apprear to be attached to the root of the version store
377      *
378      * @see NodeService#getParentAssocs(NodeRef, QNamePattern, QNamePattern)
379      */

380     public List JavaDoc<ChildAssociationRef> getParentAssocs(NodeRef nodeRef, QNamePattern typeQNamePattern, QNamePattern qnamePattern)
381     {
382         List JavaDoc<ChildAssociationRef> result = new ArrayList JavaDoc<ChildAssociationRef>();
383         if (qnamePattern.isMatch(rootAssocName) == true)
384         {
385             result.add(new ChildAssociationRef(
386                     ContentModel.ASSOC_CHILDREN,
387                     dbNodeService.getRootNode(new StoreRef(StoreRef.PROTOCOL_WORKSPACE, STORE_ID)),
388                     rootAssocName,
389                     nodeRef));
390         }
391         return result;
392     }
393
394     /**
395      * @see RegexQNamePattern#MATCH_ALL
396      * @see #getChildAssocs(NodeRef, QNamePattern, QNamePattern)
397      */

398     public List JavaDoc<ChildAssociationRef> getChildAssocs(NodeRef nodeRef) throws InvalidNodeRefException
399     {
400         return getChildAssocs(convertNodeRef(nodeRef), RegexQNamePattern.MATCH_ALL, RegexQNamePattern.MATCH_ALL);
401     }
402
403     /**
404      * Performs conversion from version store properties to <i>real</i> associations
405      */

406     public List JavaDoc<ChildAssociationRef> getChildAssocs(NodeRef nodeRef, QNamePattern typeQNamePattern, QNamePattern qnamePattern) throws InvalidNodeRefException
407     {
408         // Get the child assocs from the version store
409
List JavaDoc<ChildAssociationRef> childAssocRefs = this.dbNodeService.getChildAssocs(
410                 convertNodeRef(nodeRef),
411                 RegexQNamePattern.MATCH_ALL, CHILD_QNAME_VERSIONED_CHILD_ASSOCS);
412         List JavaDoc<ChildAssociationRef> result = new ArrayList JavaDoc<ChildAssociationRef>(childAssocRefs.size());
413         for (ChildAssociationRef childAssocRef : childAssocRefs)
414         {
415             // Get the child reference
416
NodeRef childRef = childAssocRef.getChildRef();
417             NodeRef referencedNode = (NodeRef)this.dbNodeService.getProperty(childRef, ContentModel.PROP_REFERENCE);
418             
419             // get the qualified name of the frozen child association and filter out unwanted names
420
QName qName = (QName)this.dbNodeService.getProperty(childRef, PROP_QNAME_ASSOC_QNAME);
421             
422             if (qnamePattern.isMatch(qName) == true)
423             {
424                 // Retrieve the isPrimary and nthSibling values of the forzen child association
425
QName assocType = (QName)this.dbNodeService.getProperty(childRef, PROP_QNAME_ASSOC_TYPE_QNAME);
426                 boolean isPrimary = ((Boolean JavaDoc)this.dbNodeService.getProperty(childRef, PROP_QNAME_IS_PRIMARY)).booleanValue();
427                 int nthSibling = ((Integer JavaDoc)this.dbNodeService.getProperty(childRef, PROP_QNAME_NTH_SIBLING)).intValue();
428                 
429                 // Build a child assoc ref to add to the returned list
430
ChildAssociationRef newChildAssocRef = new ChildAssociationRef(
431                         assocType,
432                         nodeRef,
433                         qName,
434                         referencedNode,
435                         isPrimary,
436                         nthSibling);
437                 result.add(newChildAssocRef);
438             }
439         }
440         
441         // sort the results so that the order appears to be exactly as it was originally
442
Collections.sort(result);
443         
444         return result;
445     }
446     
447     /**
448      * Simulates the node begin attached ot the root node of the version store.
449      */

450     public ChildAssociationRef getPrimaryParent(NodeRef nodeRef) throws InvalidNodeRefException
451     {
452         return new ChildAssociationRef(
453                 ContentModel.ASSOC_CHILDREN,
454                 dbNodeService.getRootNode(new StoreRef(StoreRef.PROTOCOL_WORKSPACE, STORE_ID)),
455                 rootAssocName,
456                 nodeRef);
457     }
458     
459     /**
460      * @throws UnsupportedOperationException always
461      */

462     public AssociationRef createAssociation(NodeRef sourceRef, NodeRef targetRef, QName assocTypeQName)
463             throws InvalidNodeRefException, AssociationExistsException
464     {
465         // This operation is not supported for a verion store
466
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
467     }
468     
469     /**
470      * @throws UnsupportedOperationException always
471      */

472     public void removeAssociation(NodeRef sourceRef, NodeRef targetRef, QName assocTypeQName)
473     {
474         // This operation is not supported for a verion store
475
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
476     }
477     
478     /**
479      * @throws UnsupportedOperationException always
480      */

481     public List JavaDoc<AssociationRef> getTargetAssocs(NodeRef sourceRef, QNamePattern qnamePattern)
482     {
483         // Get the child assocs from the version store
484
List JavaDoc<ChildAssociationRef> childAssocRefs = this.dbNodeService.getChildAssocs(
485                 convertNodeRef(sourceRef),
486                 RegexQNamePattern.MATCH_ALL, CHILD_QNAME_VERSIONED_ASSOCS);
487         List JavaDoc<AssociationRef> result = new ArrayList JavaDoc<AssociationRef>(childAssocRefs.size());
488         for (ChildAssociationRef childAssocRef : childAssocRefs)
489         {
490             // Get the assoc reference
491
NodeRef childRef = childAssocRef.getChildRef();
492             NodeRef referencedNode = (NodeRef)this.dbNodeService.getProperty(childRef, ContentModel.PROP_REFERENCE);
493             
494             // get the qualified type name of the frozen child association and filter out unwanted names
495
QName qName = (QName)this.dbNodeService.getProperty(childRef, PROP_QNAME_ASSOC_TYPE_QNAME);
496             
497             if (qnamePattern.isMatch(qName) == true)
498             {
499                 AssociationRef newAssocRef = new AssociationRef(sourceRef, qName, referencedNode);
500                 result.add(newAssocRef);
501             }
502         }
503         
504         return result;
505     }
506     
507     /**
508      * @throws UnsupportedOperationException always
509      */

510     public List JavaDoc<AssociationRef> getSourceAssocs(NodeRef sourceRef, QNamePattern qnamePattern)
511     {
512         // This operation is not supported for a verion store
513
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
514     }
515     
516     /**
517      * @throws UnsupportedOperationException always
518      */

519     public Path getPath(NodeRef nodeRef) throws InvalidNodeRefException
520     {
521         ChildAssociationRef childAssocRef = getPrimaryParent(nodeRef);
522         Path path = new Path();
523         path.append(new Path.ChildAssocElement(childAssocRef));
524         return path;
525     }
526     
527     /**
528      * @throws UnsupportedOperationException always
529      */

530     public List JavaDoc<Path> getPaths(NodeRef nodeRef, boolean primaryOnly) throws InvalidNodeRefException
531     {
532         List JavaDoc<Path> paths = new ArrayList JavaDoc<Path>(1);
533         paths.add(getPath(nodeRef));
534         return paths;
535     }
536
537     public List JavaDoc<NodeRef> selectNodes(NodeRef contextNode, String JavaDoc XPath, QueryParameterDefinition[] parameters, NamespacePrefixResolver namespacePrefixResolver, boolean followAllParentLinks)
538     {
539         // TODO Auto-generated method stub
540
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
541     }
542
543     public List JavaDoc<Serializable JavaDoc> selectProperties(NodeRef contextNode, String JavaDoc XPath, QueryParameterDefinition[] parameters, NamespacePrefixResolver namespacePrefixResolver, boolean followAllParentLinks)
544     {
545         // TODO Auto-generated method stub
546
throw new UnsupportedOperationException JavaDoc(MSG_UNSUPPORTED);
547     }
548
549     public boolean contains(NodeRef nodeRef, QName property, String JavaDoc sqlLikePattern)
550     {
551         // TODO Auto-generated method stub
552
throw new UnsupportedOperationException JavaDoc();
553     }
554
555     public boolean like(NodeRef nodeRef, QName property, String JavaDoc sqlLikePattern, boolean includeFTS)
556     {
557         // TODO Auto-generated method stub
558
throw new UnsupportedOperationException JavaDoc();
559     }
560     
561     
562 }
563
Popular Tags