KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.alfresco.error.AlfrescoRuntimeException;
28 import org.alfresco.service.cmr.dictionary.DictionaryService;
29 import org.alfresco.service.cmr.search.NamedQueryParameterDefinition;
30 import org.alfresco.service.cmr.search.QueryParameterDefinition;
31 import org.alfresco.service.namespace.NamespacePrefixResolver;
32 import org.alfresco.service.namespace.NamespaceService;
33 import org.alfresco.service.namespace.QName;
34 import org.dom4j.Element;
35 import org.dom4j.Namespace;
36
37 public class CannedQueryDefImpl implements CannedQueryDef
38 {
39     private static final org.dom4j.QName ELEMENT_QNAME = new org.dom4j.QName("query-definition", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
40
41     private static final org.dom4j.QName QNAME = new org.dom4j.QName("qname", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
42
43     private static final org.dom4j.QName LANGUAGE = new org.dom4j.QName("language", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
44
45     private static final org.dom4j.QName QUERY = new org.dom4j.QName("query", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI));
46
47     private QName qName;
48
49     private String JavaDoc language;
50
51     private Map JavaDoc<QName, QueryParameterDefinition> queryParameterDefs = new HashMap JavaDoc<QName, QueryParameterDefinition>();
52
53     String JavaDoc query;
54
55     QueryCollection container;
56
57     public CannedQueryDefImpl(QName qName, String JavaDoc language, String JavaDoc query, List JavaDoc<QueryParameterDefinition> queryParameterDefs, QueryCollection container)
58     {
59         super();
60         this.qName = qName;
61         this.language = language;
62         this.query = query;
63         for(QueryParameterDefinition paramDef : queryParameterDefs)
64         {
65             this.queryParameterDefs.put(paramDef.getQName(), paramDef);
66         }
67         this.container = container;
68     }
69
70     public QName getQname()
71     {
72         return qName;
73     }
74
75     public String JavaDoc getLanguage()
76     {
77         return language;
78     }
79
80     public Collection JavaDoc<QueryParameterDefinition> getQueryParameterDefs()
81     {
82         return Collections.unmodifiableCollection(queryParameterDefs.values());
83     }
84
85     public String JavaDoc getQuery()
86     {
87         return query;
88     }
89
90     public NamespacePrefixResolver getNamespacePrefixResolver()
91     {
92         return container.getNamespacePrefixResolver();
93     }
94
95     public static CannedQueryDefImpl createCannedQuery(Element element, DictionaryService dictionaryService, QueryCollection container, NamespacePrefixResolver nspr)
96     {
97         if (element.getQName().getName().equals(ELEMENT_QNAME.getName()))
98         {
99             QName qName = null;
100             Element qNameElement = element.element(QNAME.getName());
101             if(qNameElement != null)
102             {
103                qName = QName.createQName(qNameElement.getText(), container.getNamespacePrefixResolver());
104             }
105             
106             String JavaDoc language = null;
107             Element languageElement = element.element(LANGUAGE.getName());
108             if(languageElement != null)
109             {
110                language = languageElement.getText();
111             }
112             
113             String JavaDoc query = null;
114             Element queryElement = element.element(QUERY.getName());
115             if(queryElement != null)
116             {
117                query = queryElement.getText();
118             }
119             
120             List JavaDoc<QueryParameterDefinition> queryParameterDefs = new ArrayList JavaDoc<QueryParameterDefinition>();
121             
122             List JavaDoc list = element.elements(QueryParameterDefImpl.getElementQName().getName());
123             for(Iterator JavaDoc it = list.iterator(); it.hasNext(); /**/)
124             {
125                 Element defElement = (Element) it.next();
126                 NamedQueryParameterDefinition nqpd = QueryParameterDefImpl.createParameterDefinition(defElement, dictionaryService, nspr);
127                 queryParameterDefs.add(nqpd.getQueryParameterDefinition());
128             }
129             
130             list = element.elements(QueryParameterRefImpl.getElementQName().getName());
131             for(Iterator JavaDoc it = list.iterator(); it.hasNext(); /**/)
132             {
133                 Element refElement = (Element) it.next();
134                 NamedQueryParameterDefinition nqpd = QueryParameterRefImpl.createParameterReference(refElement, dictionaryService, container);
135                 QueryParameterDefinition resolved = nqpd.getQueryParameterDefinition();
136                 if(resolved == null)
137                 {
138                     throw new AlfrescoRuntimeException("Unable to find refernce parameter : "+nqpd.getQName());
139                 }
140                 queryParameterDefs.add(resolved);
141             }
142             
143             return new CannedQueryDefImpl(qName, language, query, queryParameterDefs, container);
144             
145         }
146         else
147         {
148             return null;
149         }
150     }
151     
152     public static org.dom4j.QName getElementQName()
153     {
154         return ELEMENT_QNAME;
155     }
156
157     public Map JavaDoc<QName, QueryParameterDefinition> getQueryParameterMap()
158     {
159         return Collections.unmodifiableMap(queryParameterDefs);
160     }
161
162 }
163
Popular Tags