KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > QueryCollectionImpl


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.search;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.service.cmr.dictionary.DictionaryService;
25 import org.alfresco.service.cmr.search.QueryParameterDefinition;
26 import org.alfresco.service.namespace.DynamicNamespacePrefixResolver;
27 import org.alfresco.service.namespace.NamespacePrefixResolver;
28 import org.alfresco.service.namespace.NamespaceService;
29 import org.alfresco.service.namespace.QName;
30 import org.dom4j.Element;
31 import org.dom4j.Namespace;
32
33 public class QueryCollectionImpl implements QueryCollection
34 {
35     private static final org.dom4j.QName ELEMENT_QNAME = new org.dom4j.QName("query-register", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
36
37     private static final org.dom4j.QName NAME = new org.dom4j.QName("name", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
38
39     private static final org.dom4j.QName NAMESPACES = new org.dom4j.QName("namespaces", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
40
41     private static final org.dom4j.QName NAMESPACE = new org.dom4j.QName("namespace", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
42
43     private static final org.dom4j.QName PREFIX = new org.dom4j.QName("prefix", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
44
45     private static final org.dom4j.QName URI = new org.dom4j.QName("uri", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
46     
47     private String JavaDoc name;
48     
49     private Map JavaDoc<QName, QueryParameterDefinition> parameters = new HashMap JavaDoc<QName, QueryParameterDefinition>();
50     
51     private Map JavaDoc<QName, CannedQueryDef> queries = new HashMap JavaDoc<QName, CannedQueryDef>();
52     
53     NamespacePrefixResolver namespacePrefixResolver;
54
55     public QueryCollectionImpl(String JavaDoc name, Map JavaDoc<QName, QueryParameterDefinition> parameters, NamespacePrefixResolver namespacePrefixResolver)
56     {
57         super();
58         this.name = name;
59         this.parameters = parameters;
60         this.namespacePrefixResolver = namespacePrefixResolver;
61     }
62
63     public String JavaDoc getName()
64     {
65        return name;
66     }
67
68     public boolean containsQueryDefinition(QName qName)
69     {
70         return queries.containsKey(qName);
71     }
72
73     private void addQueryDefinition(CannedQueryDef queryDefinition)
74     {
75         queries.put(queryDefinition.getQname(), queryDefinition);
76     }
77     
78     public CannedQueryDef getQueryDefinition(QName qName)
79     {
80         return queries.get(qName);
81     }
82
83     public boolean containsParameterDefinition(QName qName)
84     {
85         return parameters.containsKey(qName);
86     }
87
88     public QueryParameterDefinition getParameterDefinition(QName qName)
89     {
90         return parameters.get(qName);
91     }
92
93     public NamespacePrefixResolver getNamespacePrefixResolver()
94     {
95         return namespacePrefixResolver;
96     }
97
98     
99     public static QueryCollection createQueryCollection(Element element, DictionaryService dictionaryService, NamespacePrefixResolver nspr)
100     {
101         DynamicNamespacePrefixResolver dnpr = new DynamicNamespacePrefixResolver(nspr);
102         if (element.getName().equals(ELEMENT_QNAME.getName()))
103         {
104             String JavaDoc name = null;
105             Element nameElement = element.element(NAME.getName());
106             if(nameElement != null)
107             {
108                name = nameElement.getText();
109             }
110             
111             Element nameSpaces = element.element(NAMESPACES.getName());
112             if(nameSpaces != null)
113             {
114                 List JavaDoc ns = nameSpaces.elements(NAMESPACE.getName());
115                 for(Iterator JavaDoc it = ns.iterator(); it.hasNext(); /**/)
116                 {
117                     Element nsElement = (Element)it.next();
118                     Element prefixElement = nsElement.element(PREFIX.getName());
119                     Element uriElement = nsElement.element(URI.getName());
120                     if((prefixElement != null) && (nsElement != null))
121                     {
122                         dnpr.registerNamespace(prefixElement.getText(), uriElement.getText());
123                     }
124                 }
125             }
126             
127             // Do property definitions so they are available to query defintions
128

129             Map JavaDoc<QName, QueryParameterDefinition> parameters = new HashMap JavaDoc<QName, QueryParameterDefinition>();
130             List JavaDoc list = element.elements(QueryParameterDefImpl.getElementQName().getName());
131             for(Iterator JavaDoc it = list.iterator(); it.hasNext(); /**/)
132             {
133                 Element defElement = (Element) it.next();
134                 QueryParameterDefinition paramDef = QueryParameterDefImpl.createParameterDefinition(defElement, dictionaryService, nspr);
135                 parameters.put(paramDef.getQName(), paramDef);
136             }
137             
138             QueryCollectionImpl collection = new QueryCollectionImpl(name, parameters, dnpr);
139             
140             list = element.elements(CannedQueryDefImpl.getElementQName().getName());
141             for(Iterator JavaDoc it = list.iterator(); it.hasNext(); /**/)
142             {
143                 Element defElement = (Element) it.next();
144                 CannedQueryDefImpl queryDef = CannedQueryDefImpl.createCannedQuery(defElement, dictionaryService, collection, nspr);
145                 collection.addQueryDefinition(queryDef);
146             }
147             
148             return collection;
149         }
150         else
151         {
152             return null;
153         }
154     }
155 }
156
Popular Tags