KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > node > AbstractNodeServiceImpl


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.node;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.alfresco.error.AlfrescoRuntimeException;
28 import org.alfresco.model.ContentModel;
29 import org.alfresco.repo.domain.PropertyValue;
30 import org.alfresco.repo.node.NodeServicePolicies.BeforeAddAspectPolicy;
31 import org.alfresco.repo.node.NodeServicePolicies.BeforeCreateChildAssociationPolicy;
32 import org.alfresco.repo.node.NodeServicePolicies.BeforeCreateNodePolicy;
33 import org.alfresco.repo.node.NodeServicePolicies.BeforeCreateStorePolicy;
34 import org.alfresco.repo.node.NodeServicePolicies.BeforeDeleteChildAssociationPolicy;
35 import org.alfresco.repo.node.NodeServicePolicies.BeforeDeleteNodePolicy;
36 import org.alfresco.repo.node.NodeServicePolicies.BeforeRemoveAspectPolicy;
37 import org.alfresco.repo.node.NodeServicePolicies.BeforeUpdateNodePolicy;
38 import org.alfresco.repo.node.NodeServicePolicies.OnAddAspectPolicy;
39 import org.alfresco.repo.node.NodeServicePolicies.OnCreateAssociationPolicy;
40 import org.alfresco.repo.node.NodeServicePolicies.OnCreateChildAssociationPolicy;
41 import org.alfresco.repo.node.NodeServicePolicies.OnCreateNodePolicy;
42 import org.alfresco.repo.node.NodeServicePolicies.OnCreateStorePolicy;
43 import org.alfresco.repo.node.NodeServicePolicies.OnDeleteAssociationPolicy;
44 import org.alfresco.repo.node.NodeServicePolicies.OnDeleteChildAssociationPolicy;
45 import org.alfresco.repo.node.NodeServicePolicies.OnDeleteNodePolicy;
46 import org.alfresco.repo.node.NodeServicePolicies.OnRemoveAspectPolicy;
47 import org.alfresco.repo.node.NodeServicePolicies.OnUpdateNodePolicy;
48 import org.alfresco.repo.node.NodeServicePolicies.OnUpdatePropertiesPolicy;
49 import org.alfresco.repo.policy.AssociationPolicyDelegate;
50 import org.alfresco.repo.policy.ClassPolicyDelegate;
51 import org.alfresco.repo.policy.PolicyComponent;
52 import org.alfresco.repo.search.Indexer;
53 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
54 import org.alfresco.service.cmr.dictionary.DictionaryException;
55 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
56 import org.alfresco.service.cmr.repository.AssociationRef;
57 import org.alfresco.service.cmr.repository.ChildAssociationRef;
58 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
59 import org.alfresco.service.cmr.repository.NodeRef;
60 import org.alfresco.service.cmr.repository.NodeService;
61 import org.alfresco.service.cmr.repository.StoreRef;
62 import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
63 import org.alfresco.service.namespace.QName;
64 import org.alfresco.service.namespace.QNamePattern;
65 import org.alfresco.service.namespace.RegexQNamePattern;
66 import org.alfresco.util.GUID;
67
68 /**
69  * Provides common functionality for
70  * {@link org.alfresco.service.cmr.repository.NodeService} implementations.
71  * <p>
72  * Some of the overloaded simpler versions of methods are implemented by passing
73  * through the defaults as required.
74  * <p>
75  * The callback handling is also provided as a convenience for implementations.
76  *
77  * @author Derek Hulley
78  */

79 public abstract class AbstractNodeServiceImpl implements NodeService
80 {
81     /** a uuid identifying this unique instance */
82     private String JavaDoc uuid;
83     /** controls policy delegates */
84     private PolicyComponent policyComponent;
85
86     /*
87      * Policy delegates
88      */

89     private ClassPolicyDelegate<BeforeCreateStorePolicy> beforeCreateStoreDelegate;
90     private ClassPolicyDelegate<OnCreateStorePolicy> onCreateStoreDelegate;
91     private ClassPolicyDelegate<BeforeCreateNodePolicy> beforeCreateNodeDelegate;
92     private ClassPolicyDelegate<OnCreateNodePolicy> onCreateNodeDelegate;
93     private ClassPolicyDelegate<BeforeUpdateNodePolicy> beforeUpdateNodeDelegate;
94     private ClassPolicyDelegate<OnUpdateNodePolicy> onUpdateNodeDelegate;
95     private ClassPolicyDelegate<OnUpdatePropertiesPolicy> onUpdatePropertiesDelegate;
96     private ClassPolicyDelegate<BeforeDeleteNodePolicy> beforeDeleteNodeDelegate;
97     private ClassPolicyDelegate<OnDeleteNodePolicy> onDeleteNodeDelegate;
98     private ClassPolicyDelegate<BeforeAddAspectPolicy> beforeAddAspectDelegate;
99     private ClassPolicyDelegate<OnAddAspectPolicy> onAddAspectDelegate;
100     private ClassPolicyDelegate<BeforeRemoveAspectPolicy> beforeRemoveAspectDelegate;
101     private ClassPolicyDelegate<OnRemoveAspectPolicy> onRemoveAspectDelegate;
102     private AssociationPolicyDelegate<BeforeCreateChildAssociationPolicy> beforeCreateChildAssociationDelegate;
103     private AssociationPolicyDelegate<OnCreateChildAssociationPolicy> onCreateChildAssociationDelegate;
104     private AssociationPolicyDelegate<BeforeDeleteChildAssociationPolicy> beforeDeleteChildAssociationDelegate;
105     private AssociationPolicyDelegate<OnDeleteChildAssociationPolicy> onDeleteChildAssociationDelegate;
106     private AssociationPolicyDelegate<OnCreateAssociationPolicy> onCreateAssociationDelegate;
107     private AssociationPolicyDelegate<OnDeleteAssociationPolicy> onDeleteAssociationDelegate;
108
109     /**
110      * @param policyComponent the component with which to register class policies and behaviour
111      * @param dictionaryService
112      * used to check that node operations conform to the model
113      */

114     protected AbstractNodeServiceImpl(PolicyComponent policyComponent)
115     {
116         this.uuid = GUID.generate();
117         this.policyComponent = policyComponent;
118     }
119
120     /**
121      * Checks equality by type and uuid
122      */

123     public boolean equals(Object JavaDoc obj)
124     {
125         if (obj == null)
126         {
127             return false;
128         }
129         else if (!(obj instanceof AbstractNodeServiceImpl))
130         {
131             return false;
132         }
133         AbstractNodeServiceImpl that = (AbstractNodeServiceImpl) obj;
134         return this.uuid.equals(that.uuid);
135     }
136     
137     /**
138      * @see #uuid
139      */

140     public int hashCode()
141     {
142         return uuid.hashCode();
143     }
144
145     /**
146      * Registers the node policies as well as node indexing behaviour if the
147      * {@link #setIndexer(Indexer) indexer} is present.
148      */

149     public void init()
150     {
151         // Register the various policies
152
beforeCreateStoreDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeCreateStorePolicy.class);
153         onCreateStoreDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnCreateStorePolicy.class);
154         beforeCreateNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeCreateNodePolicy.class);
155         onCreateNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnCreateNodePolicy.class);
156         beforeUpdateNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeUpdateNodePolicy.class);
157         onUpdateNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnUpdateNodePolicy.class);
158         onUpdatePropertiesDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnUpdatePropertiesPolicy.class);
159         beforeDeleteNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeDeleteNodePolicy.class);
160         onDeleteNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnDeleteNodePolicy.class);
161
162         beforeAddAspectDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeAddAspectPolicy.class);
163         onAddAspectDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnAddAspectPolicy.class);
164         beforeRemoveAspectDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeRemoveAspectPolicy.class);
165         onRemoveAspectDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnRemoveAspectPolicy.class);
166
167         beforeCreateChildAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.BeforeCreateChildAssociationPolicy.class);
168         onCreateChildAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.OnCreateChildAssociationPolicy.class);
169         beforeDeleteChildAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.BeforeDeleteChildAssociationPolicy.class);
170         onDeleteChildAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.OnDeleteChildAssociationPolicy.class);
171
172         onCreateAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.OnCreateAssociationPolicy.class);
173         onDeleteAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.OnDeleteAssociationPolicy.class);
174     }
175
176     /**
177      * @see NodeServicePolicies.BeforeCreateStorePolicy#beforeCreateStore(QName,
178      * StoreRef)
179      */

180     protected void invokeBeforeCreateStore(QName nodeTypeQName, StoreRef storeRef)
181     {
182         NodeServicePolicies.BeforeCreateStorePolicy policy = this.beforeCreateStoreDelegate.get(nodeTypeQName);
183         policy.beforeCreateStore(nodeTypeQName, storeRef);
184     }
185
186     /**
187      * @see NodeServicePolicies.OnCreateStorePolicy#onCreateStore(NodeRef)
188      */

189     protected void invokeOnCreateStore(NodeRef rootNodeRef)
190     {
191         // get qnames to invoke against
192
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(rootNodeRef);
193         // execute policy for node type and aspects
194
NodeServicePolicies.OnCreateStorePolicy policy = onCreateStoreDelegate.get(qnames);
195         policy.onCreateStore(rootNodeRef);
196     }
197
198     /**
199      * @see NodeServicePolicies.BeforeCreateNodePolicy#beforeCreateNode(NodeRef,
200      * QName, QName, QName)
201      */

202     protected void invokeBeforeCreateNode(NodeRef parentNodeRef, QName assocTypeQName, QName assocQName, QName childNodeTypeQName)
203     {
204         // execute policy for node type
205
NodeServicePolicies.BeforeCreateNodePolicy policy = beforeCreateNodeDelegate.get(parentNodeRef, childNodeTypeQName);
206         policy.beforeCreateNode(parentNodeRef, assocTypeQName, assocQName, childNodeTypeQName);
207     }
208
209     /**
210      * @see NodeServicePolicies.OnCreateNodePolicy#onCreateNode(ChildAssociationRef)
211      */

212     protected void invokeOnCreateNode(ChildAssociationRef childAssocRef)
213     {
214         NodeRef childNodeRef = childAssocRef.getChildRef();
215         // get qnames to invoke against
216
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(childNodeRef);
217         // execute policy for node type and aspects
218
NodeServicePolicies.OnCreateNodePolicy policy = onCreateNodeDelegate.get(childNodeRef, qnames);
219         policy.onCreateNode(childAssocRef);
220     }
221
222     /**
223      * @see NodeServicePolicies.BeforeUpdateNodePolicy#beforeUpdateNode(NodeRef)
224      */

225     protected void invokeBeforeUpdateNode(NodeRef nodeRef)
226     {
227         // get qnames to invoke against
228
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(nodeRef);
229         // execute policy for node type and aspects
230
NodeServicePolicies.BeforeUpdateNodePolicy policy = beforeUpdateNodeDelegate.get(nodeRef, qnames);
231         policy.beforeUpdateNode(nodeRef);
232     }
233
234     /**
235      * @see NodeServicePolicies.OnUpdateNodePolicy#onUpdateNode(NodeRef)
236      */

237     protected void invokeOnUpdateNode(NodeRef nodeRef)
238     {
239         // get qnames to invoke against
240
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(nodeRef);
241         // execute policy for node type and aspects
242
NodeServicePolicies.OnUpdateNodePolicy policy = onUpdateNodeDelegate.get(nodeRef, qnames);
243         policy.onUpdateNode(nodeRef);
244     }
245     
246     /**
247      * @see NodeServicePolicies.OnUpdateProperties#onUpdatePropertiesPolicy(NodeRef, Map, Map)
248      */

249     protected void invokeOnUpdateProperties(
250             NodeRef nodeRef,
251             Map JavaDoc<QName, Serializable JavaDoc> before,
252             Map JavaDoc<QName, Serializable JavaDoc> after)
253     {
254         // get qnames to invoke against
255
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(nodeRef);
256         // execute policy for node type and aspects
257
NodeServicePolicies.OnUpdatePropertiesPolicy policy = onUpdatePropertiesDelegate.get(nodeRef, qnames);
258         policy.onUpdateProperties(nodeRef, before, after);
259     }
260
261     /**
262      * @see NodeServicePolicies.BeforeDeleteNodePolicy#beforeDeleteNode(NodeRef)
263      */

264     protected void invokeBeforeDeleteNode(NodeRef nodeRef)
265     {
266         // get qnames to invoke against
267
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(nodeRef);
268         // execute policy for node type and aspects
269
NodeServicePolicies.BeforeDeleteNodePolicy policy = beforeDeleteNodeDelegate.get(nodeRef, qnames);
270         policy.beforeDeleteNode(nodeRef);
271     }
272
273     /**
274      * @see NodeServicePolicies.OnDeleteNodePolicy#onDeleteNode(ChildAssociationRef)
275      */

276     protected void invokeOnDeleteNode(ChildAssociationRef childAssocRef, QName childNodeTypeQName, Set JavaDoc<QName> childAspectQnames)
277     {
278         // get qnames to invoke against
279
Set JavaDoc<QName> qnames = new HashSet JavaDoc<QName>(childAspectQnames.size() + 1);
280         qnames.addAll(childAspectQnames);
281         qnames.add(childNodeTypeQName);
282         
283         // execute policy for node type and aspects
284
NodeServicePolicies.OnDeleteNodePolicy policy = onDeleteNodeDelegate.get(childAssocRef.getChildRef(), qnames);
285         policy.onDeleteNode(childAssocRef);
286     }
287
288     /**
289      * @see NodeServicePolicies.BeforeAddAspectPolicy#beforeAddAspect(NodeRef,
290      * QName)
291      */

292     protected void invokeBeforeAddAspect(NodeRef nodeRef, QName aspectTypeQName)
293     {
294         NodeServicePolicies.BeforeAddAspectPolicy policy = beforeAddAspectDelegate.get(nodeRef, aspectTypeQName);
295         policy.beforeAddAspect(nodeRef, aspectTypeQName);
296     }
297
298     /**
299      * @see NodeServicePolicies.OnAddAspectPolicy#onAddAspect(NodeRef, QName)
300      */

301     protected void invokeOnAddAspect(NodeRef nodeRef, QName aspectTypeQName)
302     {
303         NodeServicePolicies.OnAddAspectPolicy policy = onAddAspectDelegate.get(nodeRef, aspectTypeQName);
304         policy.onAddAspect(nodeRef, aspectTypeQName);
305     }
306
307     /**
308      * @see NodeServicePolicies.BeforeRemoveAspectPolicy#BeforeRemoveAspect(NodeRef,
309      * QName)
310      */

311     protected void invokeBeforeRemoveAspect(NodeRef nodeRef, QName aspectTypeQName)
312     {
313         NodeServicePolicies.BeforeRemoveAspectPolicy policy = beforeRemoveAspectDelegate.get(nodeRef, aspectTypeQName);
314         policy.beforeRemoveAspect(nodeRef, aspectTypeQName);
315     }
316
317     /**
318      * @see NodeServicePolicies.OnRemoveAspectPolicy#onRemoveAspect(NodeRef,
319      * QName)
320      */

321     protected void invokeOnRemoveAspect(NodeRef nodeRef, QName aspectTypeQName)
322     {
323         NodeServicePolicies.OnRemoveAspectPolicy policy = onRemoveAspectDelegate.get(nodeRef, aspectTypeQName);
324         policy.onRemoveAspect(nodeRef, aspectTypeQName);
325     }
326
327     /**
328      * @see NodeServicePolicies.BeforeCreateChildAssociationPolicy#beforeCreateChildAssociation(NodeRef,
329      * NodeRef, QName, QName)
330      */

331     protected void invokeBeforeCreateChildAssociation(NodeRef parentNodeRef, NodeRef childNodeRef, QName assocTypeQName, QName assocQName)
332     {
333         // get qnames to invoke against
334
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(parentNodeRef);
335         // execute policy for node type
336
NodeServicePolicies.BeforeCreateChildAssociationPolicy policy = beforeCreateChildAssociationDelegate.get(parentNodeRef, qnames, assocTypeQName);
337         policy.beforeCreateChildAssociation(parentNodeRef, childNodeRef, assocTypeQName, assocQName);
338     }
339
340     /**
341      * @see NodeServicePolicies.OnCreateChildAssociationPolicy#onCreateChildAssociation(ChildAssociationRef)
342      */

343     protected void invokeOnCreateChildAssociation(ChildAssociationRef childAssocRef)
344     {
345         // Get the parent reference and the assoc type qName
346
NodeRef parentNodeRef = childAssocRef.getParentRef();
347         QName assocTypeQName = childAssocRef.getTypeQName();
348         // get qnames to invoke against
349
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(parentNodeRef);
350         // execute policy for node type and aspects
351
NodeServicePolicies.OnCreateChildAssociationPolicy policy = onCreateChildAssociationDelegate.get(parentNodeRef, qnames, assocTypeQName);
352         policy.onCreateChildAssociation(childAssocRef);
353     }
354
355     /**
356      * @see NodeServicePolicies.BeforeDeleteChildAssociationPolicy#beforeDeleteChildAssociation(ChildAssociationRef)
357      */

358     protected void invokeBeforeDeleteChildAssociation(ChildAssociationRef childAssocRef)
359     {
360         NodeRef parentNodeRef = childAssocRef.getParentRef();
361         QName assocTypeQName = childAssocRef.getTypeQName();
362         // get qnames to invoke against
363
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(parentNodeRef);
364         // execute policy for node type and aspects
365
NodeServicePolicies.BeforeDeleteChildAssociationPolicy policy = beforeDeleteChildAssociationDelegate.get(parentNodeRef, qnames, assocTypeQName);
366         policy.beforeDeleteChildAssociation(childAssocRef);
367     }
368
369     /**
370      * @see NodeServicePolicies.OnDeleteChildAssociationPolicy#onDeleteChildAssociation(ChildAssociationRef)
371      */

372     protected void invokeOnDeleteChildAssociation(ChildAssociationRef childAssocRef)
373     {
374         NodeRef parentNodeRef = childAssocRef.getParentRef();
375         QName assocTypeQName = childAssocRef.getTypeQName();
376         // get qnames to invoke against
377
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(parentNodeRef);
378         // execute policy for node type and aspects
379
NodeServicePolicies.OnDeleteChildAssociationPolicy policy = onDeleteChildAssociationDelegate.get(parentNodeRef, qnames, assocTypeQName);
380         policy.onDeleteChildAssociation(childAssocRef);
381     }
382
383     /**
384      * @see NodeServicePolicies.OnCreateAssociationPolicy#onCreateAssociation(NodeRef, NodeRef, QName)
385      */

386     protected void invokeOnCreateAssociation(AssociationRef nodeAssocRef)
387     {
388         NodeRef sourceNodeRef = nodeAssocRef.getSourceRef();
389         QName assocTypeQName = nodeAssocRef.getTypeQName();
390         // get qnames to invoke against
391
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(sourceNodeRef);
392         // execute policy for node type and aspects
393
NodeServicePolicies.OnCreateAssociationPolicy policy = onCreateAssociationDelegate.get(sourceNodeRef, qnames, assocTypeQName);
394         policy.onCreateAssociation(nodeAssocRef);
395     }
396
397     /**
398      * @see NodeServicePolicies.OnDeleteAssociationPolicy#onDeleteAssociation(AssociationRef)
399      */

400     protected void invokeOnDeleteAssociation(AssociationRef nodeAssocRef)
401     {
402         NodeRef sourceNodeRef = nodeAssocRef.getSourceRef();
403         QName assocTypeQName = nodeAssocRef.getTypeQName();
404         // get qnames to invoke against
405
Set JavaDoc<QName> qnames = getTypeAndAspectQNames(sourceNodeRef);
406         // execute policy for node type and aspects
407
NodeServicePolicies.OnDeleteAssociationPolicy policy = onDeleteAssociationDelegate.get(sourceNodeRef, qnames, assocTypeQName);
408         policy.onDeleteAssociation(nodeAssocRef);
409     }
410
411     /**
412      * Get all aspect and node type qualified names
413      *
414      * @param nodeRef
415      * the node we are interested in
416      * @return Returns a set of qualified names containing the node type and all
417      * the node aspects, or null if the node no longer exists
418      */

419     protected Set JavaDoc<QName> getTypeAndAspectQNames(NodeRef nodeRef)
420     {
421         Set JavaDoc<QName> qnames = null;
422         try
423         {
424             Set JavaDoc<QName> aspectQNames = getAspects(nodeRef);
425             QName typeQName = getType(nodeRef);
426             qnames = new HashSet JavaDoc<QName>(aspectQNames.size() + 1);
427             qnames.addAll(aspectQNames);
428             qnames.add(typeQName);
429         }
430         catch (InvalidNodeRefException e)
431         {
432             qnames = Collections.emptySet();
433         }
434         // done
435
return qnames;
436     }
437
438     /**
439      * Generates a GUID for the node using either the creation properties or just by
440      * generating a value randomly.
441      *
442      * @param preCreationProperties the properties that will be applied to the node
443      * @return Returns the ID to create the node with
444      */

445     protected String JavaDoc generateGuid(Map JavaDoc<QName, Serializable JavaDoc> preCreationProperties)
446     {
447         String JavaDoc uuid = (String JavaDoc) preCreationProperties.get(ContentModel.PROP_NODE_UUID);
448         if (uuid == null)
449         {
450             uuid = GUID.generate();
451         }
452         else
453         {
454             // remove the property as we don't want to persist it
455
preCreationProperties.remove(ContentModel.PROP_NODE_UUID);
456         }
457         // done
458
return uuid;
459     }
460     
461     /**
462      * Remove all properties used by the
463      * {@link ContentModel#ASPECT_REFERENCEABLE referencable aspect}.
464      * <p>
465      * This method can be used to ensure that the information already stored
466      * by the node key is not duplicated by the properties.
467      *
468      * @param properties properties to change
469      */

470     protected void removeReferencableProperties(Map JavaDoc<QName, Serializable JavaDoc> properties)
471     {
472         properties.remove(ContentModel.PROP_STORE_PROTOCOL);
473         properties.remove(ContentModel.PROP_STORE_IDENTIFIER);
474         properties.remove(ContentModel.PROP_NODE_UUID);
475     }
476     
477     /**
478      * Adds all properties used by the
479      * {@link ContentModel#ASPECT_REFERENCEABLE referencable aspect}.
480      * <p>
481      * This method can be used to ensure that the values used by the aspect
482      * are present as node properties.
483      * <p>
484      * This method also ensures that the {@link ContentModel#PROP_NAME name property}
485      * is always present as a property on a node.
486      *
487      * @param nodeRef the node reference containing the values required
488      * @param properties the node properties
489      */

490     protected void addReferencableProperties(NodeRef nodeRef, Map JavaDoc<QName, Serializable JavaDoc> properties)
491     {
492         properties.put(ContentModel.PROP_STORE_PROTOCOL, nodeRef.getStoreRef().getProtocol());
493         properties.put(ContentModel.PROP_STORE_IDENTIFIER, nodeRef.getStoreRef().getIdentifier());
494         properties.put(ContentModel.PROP_NODE_UUID, nodeRef.getId());
495         // add the ID as the name, if required
496
if (properties.get(ContentModel.PROP_NAME) == null)
497         {
498             properties.put(ContentModel.PROP_NAME, nodeRef.getId());
499         }
500     }
501     
502     /**
503      * Defers to the pattern matching overload
504      *
505      * @see RegexQNamePattern#MATCH_ALL
506      * @see NodeService#getParentAssocs(NodeRef, QNamePattern, QNamePattern)
507      */

508     public List JavaDoc<ChildAssociationRef> getParentAssocs(NodeRef nodeRef) throws InvalidNodeRefException
509     {
510         return getParentAssocs(nodeRef, RegexQNamePattern.MATCH_ALL, RegexQNamePattern.MATCH_ALL);
511     }
512
513     /**
514      * Defers to the pattern matching overload
515      *
516      * @see RegexQNamePattern#MATCH_ALL
517      * @see NodeService#getChildAssocs(NodeRef, QNamePattern, QNamePattern)
518      */

519     public final List JavaDoc<ChildAssociationRef> getChildAssocs(NodeRef nodeRef) throws InvalidNodeRefException
520     {
521         return getChildAssocs(nodeRef, RegexQNamePattern.MATCH_ALL, RegexQNamePattern.MATCH_ALL);
522     }
523
524     /**
525      * Helper method to convert the <code>Serializable</code> value into a full,
526      * persistable {@link PropertyValue}.
527      * <p>
528      * Where the property definition is null, the value will take on the
529      * {@link DataTypeDefinition#ANY generic ANY} value.
530      * <p>
531      * Where the property definition specifies a multi-valued property but the
532      * value provided is not a collection, the value will be wrapped in a collection.
533      *
534      * @param propertyDef the property dictionary definition, may be null
535      * @param value the value, which will be converted according to the definition -
536      * may be null
537      * @return Returns the persistable property value
538      */

539     protected PropertyValue makePropertyValue(PropertyDefinition propertyDef, Serializable JavaDoc value)
540     {
541         // get property attributes
542
QName propertyTypeQName = null;
543         if (propertyDef == null) // property not recognised
544
{
545             // allow it for now - persisting excess properties can be useful sometimes
546
propertyTypeQName = DataTypeDefinition.ANY;
547         }
548         else
549         {
550             propertyTypeQName = propertyDef.getDataType().getName();
551             // check that multi-valued properties are allowed
552
boolean isMultiValued = propertyDef.isMultiValued();
553             if (isMultiValued && !(value instanceof Collection JavaDoc))
554             {
555                 if (value != null)
556                 {
557                     // put the value into a collection
558
// the implementation gives back a Serializable list
559
value = (Serializable JavaDoc) Collections.singletonList(value);
560                 }
561             }
562             else if (!isMultiValued && (value instanceof Collection JavaDoc))
563             {
564                 throw new DictionaryException("A single-valued property may not be a collection: \n" +
565                         " Property: " + propertyDef + "\n" +
566                         " Value: " + value);
567             }
568         }
569         try
570         {
571             PropertyValue propertyValue = new PropertyValue(propertyTypeQName, value);
572             // done
573
return propertyValue;
574         }
575         catch (TypeConversionException e)
576         {
577             throw new TypeConversionException(
578                     "The property value is not compatible with the type defined for the property: \n" +
579                     " property: " + (propertyDef == null ? "unknown" : propertyDef) + "\n" +
580                     " value: " + value + "\n" +
581                     " value type: " + value.getClass(),
582                     e);
583         }
584     }
585     
586     /**
587      * Extracts the externally-visible property from the {@link PropertyValue propertyValue}.
588      *
589      * @param propertyDef
590      * @param propertyValue
591      * @return Returns the value of the property in the format dictated by the property
592      * definition, or null if the property value is null
593      */

594     protected Serializable JavaDoc makeSerializableValue(PropertyDefinition propertyDef, PropertyValue propertyValue)
595     {
596         if (propertyValue == null)
597         {
598             return null;
599         }
600         // get property attributes
601
QName propertyTypeQName = null;
602         if (propertyDef == null)
603         {
604             // allow this for now
605
propertyTypeQName = DataTypeDefinition.ANY;
606         }
607         else
608         {
609             propertyTypeQName = propertyDef.getDataType().getName();
610         }
611         try
612         {
613             Serializable JavaDoc value = propertyValue.getValue(propertyTypeQName);
614             // done
615
return value;
616         }
617         catch (TypeConversionException e)
618         {
619             throw new TypeConversionException(
620                     "The property value is not compatible with the type defined for the property: \n" +
621                     " property: " + (propertyDef == null ? "unknown" : propertyDef) + "\n" +
622                     " property value: " + propertyValue,
623                     e);
624         }
625     }
626 }
627
Popular Tags