KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > repository > DataDictionary


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.web.bean.repository;
18
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
24 import org.alfresco.service.cmr.dictionary.DictionaryService;
25 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
26 import org.alfresco.service.cmr.dictionary.TypeDefinition;
27 import org.alfresco.service.namespace.NamespaceService;
28 import org.alfresco.service.namespace.QName;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * Lighweight client side representation of the repository data dictionary.
34  * This allows service calls to be kept to a minimum and for bean access, thus enabling JSF
35  * value binding expressions.
36  *
37  * @author gavinc
38  */

39 public final class DataDictionary
40 {
41    private static Log logger = LogFactory.getLog(DataDictionary.class);
42    private DictionaryService dictionaryService;
43    private NamespaceService namespaceService;
44    private Map JavaDoc<QName, TypeDefinition> types = new HashMap JavaDoc<QName, TypeDefinition>(11, 1.0f);
45
46    /**
47     * Constructor
48     *
49     * @param dictionaryService The dictionary service to use to retrieve the data
50     */

51    public DataDictionary(DictionaryService dictionaryService)
52    {
53       this.dictionaryService = dictionaryService;
54    }
55    
56    /**
57     * Returns the type definition for the type represented by the given qname
58     *
59     * @param type The qname of the type to lookup the definition for
60     * @return The type definition for the requested type
61     */

62    public TypeDefinition getTypeDef(QName type)
63    {
64       TypeDefinition typeDef = types.get(type);
65       
66       if (typeDef == null)
67       {
68          typeDef = this.dictionaryService.getType(type);
69          
70          if (typeDef != null)
71          {
72             types.put(type, typeDef);
73          }
74       }
75       
76       return typeDef;
77    }
78    
79    /**
80     * Returns the type definition for the type represented by the given qname
81     * and for all the given aspects
82     *
83     * @param type The type to retrieve the definition for
84     * @param optionalAspects A list of aspects to retrieve the definition for
85     * @return A unified type definition of the given type and aspects
86     */

87    public TypeDefinition getTypeDef(QName type, Collection JavaDoc<QName> optionalAspects)
88    {
89       return this.dictionaryService.getAnonymousType(type, optionalAspects);
90    }
91    
92    /**
93     * Returns the property definition for the given property on the given node
94     *
95     * @param node The node from which to get the property
96     * @param property The property to find the definition for
97     * @return The property definition or null if the property is not known
98     */

99    public PropertyDefinition getPropertyDefinition(Node node, String JavaDoc property)
100    {
101       PropertyDefinition propDef = null;
102       
103       TypeDefinition typeDef = getTypeDef(node.getType(), node.getAspects());
104       
105       if (typeDef != null)
106       {
107          Map JavaDoc<QName, PropertyDefinition> properties = typeDef.getProperties();
108          propDef = properties.get(Repository.resolveToQName(property));
109       }
110       
111       return propDef;
112    }
113    
114    /**
115     * Returns the association definition for the given association on the given node
116     *
117     * @param node The node from which to get the association
118     * @param association The association to find the definition for
119     * @return The association definition or null if the association is not known
120     */

121    public AssociationDefinition getAssociationDefinition(Node node, String JavaDoc association)
122    {
123       AssociationDefinition assocDef = null;
124       
125       TypeDefinition typeDef = getTypeDef(node.getType(), node.getAspects());
126       
127       if (typeDef != null)
128       {
129          Map JavaDoc<QName, AssociationDefinition> assocs = typeDef.getAssociations();
130          assocDef = assocs.get(Repository.resolveToQName(association));
131       }
132       
133       return assocDef;
134    }
135 }
136
Popular Tags