KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > item > property > PropertyResolver


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.jcr.item.property;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.jcr.PathNotFoundException;
26
27 import org.alfresco.jcr.item.NodeImpl;
28 import org.alfresco.jcr.item.PropertyImpl;
29 import org.alfresco.model.ContentModel;
30 import org.alfresco.service.cmr.repository.NodeService;
31 import org.alfresco.service.namespace.QName;
32 import org.alfresco.service.namespace.QNamePattern;
33
34
35 /**
36  * Responsible for resolving properties on Nodes
37  *
38  * @author David Caruana
39  */

40 public class PropertyResolver
41 {
42
43     private static Map JavaDoc<QName, QName> virtualProperties = new HashMap JavaDoc<QName, QName>();
44     static
45     {
46         virtualProperties.put(JCRUUIDProperty.PROPERTY_NAME, null);
47         virtualProperties.put(JCRPrimaryTypeProperty.PROPERTY_NAME, null);
48         virtualProperties.put(JCRMixinTypesProperty.PROPERTY_NAME, null);
49         virtualProperties.put(JCRLockOwnerProperty.PROPERTY_NAME, ContentModel.ASPECT_LOCKABLE);
50         virtualProperties.put(JCRLockIsDeepProperty.PROPERTY_NAME, ContentModel.ASPECT_LOCKABLE);
51
52         // TODO: mix:versionable
53
}
54     
55     
56     /**
57      * Create Property List for all properties of this node
58      *
59      * @return list of properties (null properties are filtered)
60      */

61     public static List JavaDoc<PropertyImpl> createProperties(NodeImpl node, QNamePattern pattern)
62     {
63         // Create list of properties from node itself
64
NodeService nodeService = node.getSessionImpl().getRepositoryImpl().getServiceRegistry().getNodeService();
65         Map JavaDoc<QName, Serializable JavaDoc> properties = nodeService.getProperties(node.getNodeRef());
66         List JavaDoc<PropertyImpl> propertyList = new ArrayList JavaDoc<PropertyImpl>(properties.size());
67         for (Map.Entry JavaDoc<QName, Serializable JavaDoc> entry : properties.entrySet())
68         {
69             QName propertyName = entry.getKey();
70             if (pattern == null || pattern.isMatch(propertyName))
71             {
72                 Serializable JavaDoc value = entry.getValue();
73                 if (value != null)
74                 {
75                     PropertyImpl property = new PropertyImpl(node, propertyName);
76                     propertyList.add(property);
77                 }
78             }
79         }
80         
81         // Add JCR properties
82
for (Map.Entry JavaDoc<QName, QName> virtualProperty : virtualProperties.entrySet())
83         {
84             boolean addJCRProperty = false;
85             if (virtualProperty.getValue() == null)
86             {
87                 addJCRProperty = true;
88             }
89             else
90             {
91                 addJCRProperty = nodeService.hasAspect(node.getNodeRef(), virtualProperty.getValue());
92             }
93             
94             if (addJCRProperty && (pattern == null || pattern.isMatch(virtualProperty.getKey())))
95             {
96                 propertyList.add(createVirtualProperty(node, virtualProperty.getKey()));
97             }
98         }
99         
100         return propertyList;
101     }
102
103
104     /**
105      * Create property for the given named property
106      *
107      * @param node
108      * @param propertyName
109      * @return
110      * @throws PathNotFoundException
111      */

112     public static PropertyImpl createProperty(NodeImpl node, QName propertyName)
113         throws PathNotFoundException
114     {
115         // has a JCR property been requested that is not persisted in Alfresco repository?
116
if (hasVirtualProperty(node, propertyName))
117         {
118             return createVirtualProperty(node, propertyName);
119         }
120         
121         // has a property been requested that actually exists?
122
NodeService nodeService = node.getSessionImpl().getRepositoryImpl().getServiceRegistry().getNodeService();
123         Serializable JavaDoc value = nodeService.getProperty(node.getNodeRef(), propertyName);
124         if (value == null)
125         {
126             throw new PathNotFoundException("Property path " + propertyName + " not found from node " + node.getNodeRef());
127         }
128         
129         // construct property wrapper
130
PropertyImpl propertyImpl = new PropertyImpl(node, propertyName);
131         return propertyImpl;
132     }
133     
134     
135     private static PropertyImpl createVirtualProperty(NodeImpl node, QName propertyName)
136     {
137         if (propertyName.equals(JCRUUIDProperty.PROPERTY_NAME))
138         {
139             return new JCRUUIDProperty(node);
140         }
141         if (propertyName.equals(JCRPrimaryTypeProperty.PROPERTY_NAME))
142         {
143             return new JCRPrimaryTypeProperty(node);
144         }
145         if (propertyName.equals(JCRMixinTypesProperty.PROPERTY_NAME))
146         {
147             return new JCRMixinTypesProperty(node);
148         }
149         if (propertyName.equals(JCRLockOwnerProperty.PROPERTY_NAME))
150         {
151             return new JCRLockOwnerProperty(node);
152         }
153         if (propertyName.equals(JCRLockIsDeepProperty.PROPERTY_NAME))
154         {
155             return new JCRLockIsDeepProperty(node);
156         }
157         
158         return null;
159     }
160     
161     
162     /**
163      * Check for existence of Property on specified Node
164      *
165      * @param node
166      * @param propertyName
167      * @return
168      */

169     public static boolean hasProperty(NodeImpl node, QName propertyName)
170     {
171         if (hasVirtualProperty(node, propertyName))
172         {
173             return true;
174         }
175
176         NodeService nodeService = node.getSessionImpl().getRepositoryImpl().getServiceRegistry().getNodeService();
177         Serializable JavaDoc value = nodeService.getProperty(node.getNodeRef(), propertyName);
178         return value != null;
179     }
180  
181     
182     private static boolean hasVirtualProperty(NodeImpl node, QName propertyName)
183     {
184         // is this a virtual property
185
if (virtualProperties.containsKey(propertyName))
186         {
187             // is this a virtual property attached to a specific aspect
188
QName aspect = virtualProperties.get(propertyName);
189             if (aspect == null)
190             {
191                 // it's supported on all types
192
return true;
193             }
194             
195             // is the aspect attached to the node
196
NodeService nodeService = node.getSessionImpl().getRepositoryImpl().getServiceRegistry().getNodeService();
197             return nodeService.hasAspect(node.getNodeRef(), aspect);
198         }
199         
200         // no, it's not even a virtual property
201
return false;
202     }
203         
204 }
205
Popular Tags