KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.alfresco.error.AlfrescoRuntimeException;
26 import org.alfresco.service.cmr.dictionary.DictionaryService;
27 import org.alfresco.service.cmr.search.QueryParameterDefinition;
28 import org.alfresco.service.namespace.NamespacePrefixResolver;
29 import org.alfresco.service.namespace.QName;
30 import org.dom4j.Document;
31 import org.dom4j.DocumentException;
32 import org.dom4j.io.SAXReader;
33
34 public class QueryRegisterComponentImpl implements QueryRegisterComponent
35 {
36     private DictionaryService dictionaryService;
37
38     private NamespacePrefixResolver namespaceService;
39     
40     private List JavaDoc<String JavaDoc> initCollections = null;
41
42     private Map JavaDoc<String JavaDoc, QueryCollection> collections = new HashMap JavaDoc<String JavaDoc, QueryCollection>();
43
44     public QueryRegisterComponentImpl()
45     {
46         super();
47     }
48
49     private synchronized void loadCollectionsOnDemand()
50     {
51         if(initCollections != null)
52         {
53             for(String JavaDoc location: initCollections)
54             {
55                 loadQueryCollection(location);
56             }
57         }
58         initCollections = null;
59     }
60     
61     public CannedQueryDef getQueryDefinition(QName qName)
62     {
63         loadCollectionsOnDemand();
64         for(String JavaDoc key: collections.keySet())
65         {
66             QueryCollection collection = collections.get(key);
67             CannedQueryDef def = collection.getQueryDefinition(qName);
68             if(def != null)
69             {
70                 return def;
71             }
72         }
73         return null;
74     }
75
76     public String JavaDoc getCollectionNameforQueryDefinition(QName qName)
77     {
78         loadCollectionsOnDemand();
79         for(String JavaDoc key: collections.keySet())
80         {
81             QueryCollection collection = collections.get(key);
82             if(collection.containsQueryDefinition(qName))
83             {
84                 return key;
85             }
86         }
87         return null;
88     }
89
90     public QueryParameterDefinition getParameterDefinition(QName qName)
91     {
92         loadCollectionsOnDemand();
93         for(String JavaDoc key: collections.keySet())
94         {
95             QueryCollection collection = collections.get(key);
96             QueryParameterDefinition def = collection.getParameterDefinition(qName);
97             if(def != null)
98             {
99                 return def;
100             }
101         }
102         return null;
103     }
104
105     public String JavaDoc getCollectionNameforParameterDefinition(QName qName)
106     {
107         loadCollectionsOnDemand();
108         for(String JavaDoc key: collections.keySet())
109         {
110             QueryCollection collection = collections.get(key);
111             if(collection.containsParameterDefinition(qName))
112             {
113                 return key;
114             }
115         }
116         return null;
117     }
118
119     public QueryCollection getQueryCollection(String JavaDoc location)
120     {
121         loadCollectionsOnDemand();
122         return collections.get(location);
123     }
124
125     public void loadQueryCollection(String JavaDoc location)
126     {
127         try
128         {
129             InputStream JavaDoc is = this.getClass().getClassLoader().getResourceAsStream(location);
130             SAXReader reader = new SAXReader();
131             Document document = reader.read(is);
132             is.close();
133             QueryCollection collection = QueryCollectionImpl.createQueryCollection(document.getRootElement(), dictionaryService, namespaceService);
134             collections.put(location, collection);
135         }
136         catch (DocumentException de)
137         {
138             throw new AlfrescoRuntimeException("Error reading XML", de);
139         }
140         catch (IOException JavaDoc e)
141         {
142             throw new AlfrescoRuntimeException("IO Error reading XML", e);
143         }
144     }
145     
146     public void setCollections(List JavaDoc<String JavaDoc> collections)
147     {
148         this.initCollections = collections;
149     }
150
151     public void setDictionaryService(DictionaryService dictionaryService)
152     {
153         this.dictionaryService = dictionaryService;
154     }
155
156     public void setNamespaceService(NamespacePrefixResolver namespaceService)
157     {
158         this.namespaceService = namespaceService;
159     }
160 }
161
Popular Tags