KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > dictionary > DictionaryRepositoryBootstrap


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.dictionary;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.security.authentication.AuthenticationComponent;
26 import org.alfresco.repo.transaction.TransactionUtil;
27 import org.alfresco.service.cmr.repository.ContentReader;
28 import org.alfresco.service.cmr.repository.ContentService;
29 import org.alfresco.service.cmr.repository.NodeRef;
30 import org.alfresco.service.cmr.repository.StoreRef;
31 import org.alfresco.service.cmr.search.ResultSet;
32 import org.alfresco.service.cmr.search.SearchService;
33 import org.alfresco.service.transaction.TransactionService;
34
35
36 /**
37  * Bootstrap the dictionary from specified locations within the repository
38  *
39  * @author Roy Wetherall
40  */

41 public class DictionaryRepositoryBootstrap
42 {
43     /** Loactions in the respository fro which models should be loaded */
44     private List JavaDoc<RepositoryLocation> repositoryLocations = new ArrayList JavaDoc<RepositoryLocation>();
45
46     /** Dictionary DAO */
47     private DictionaryDAO dictionaryDAO = null;
48     
49     /** Search service */
50     private SearchService searchService;
51     
52     /** The content service */
53     private ContentService contentService;
54     
55     /** The transaction service */
56     private TransactionService transactionService;
57     
58     /** The authentication component */
59     private AuthenticationComponent authenticationComponent;
60       
61     /**
62      * Sets the Dictionary DAO
63      *
64      * @param dictionaryDAO
65      */

66     public void setDictionaryDAO(DictionaryDAO dictionaryDAO)
67     {
68         this.dictionaryDAO = dictionaryDAO;
69     }
70     
71     /**
72      * Set the search search service
73      *
74      * @param searchService the search service
75      */

76     public void setSearchService(SearchService searchService)
77     {
78         this.searchService = searchService;
79     }
80     
81     /**
82      * Set the content service
83      *
84      * @param contentService the content service
85      */

86     public void setContentService(ContentService contentService)
87     {
88         this.contentService = contentService;
89     }
90     
91     /**
92      * Set the transaction service
93      *
94      * @param transactionService the transaction service
95      */

96     public void setTransactionService(TransactionService transactionService)
97     {
98         this.transactionService = transactionService;
99     }
100     
101     /**
102      * Set the authentication service
103      *
104      * @param authenticationComponent the authentication component
105      */

106     public void setAuthenticationComponent(
107             AuthenticationComponent authenticationComponent)
108     {
109         this.authenticationComponent = authenticationComponent;
110     }
111         
112     /**
113      * Set the respository locations
114      *
115      * @param repositoryLocations list of the repository locaitons
116      */

117     public void setRepositoryLocations(
118             List JavaDoc<RepositoryLocation> repositoryLocations)
119     {
120         this.repositoryLocations = repositoryLocations;
121     }
122     
123     @SuppressWarnings JavaDoc("unchecked")
124     public void bootstrap()
125     {
126         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork()
127         {
128             public Object JavaDoc doWork() throws Exception JavaDoc
129             {
130                 DictionaryRepositoryBootstrap.this.authenticationComponent.setCurrentUser(
131                         DictionaryRepositoryBootstrap.this.authenticationComponent.getSystemUserName());
132                 try
133                 {
134                     bootstrapImpl();
135                 }
136                 finally
137                 {
138                     DictionaryRepositoryBootstrap.this.authenticationComponent.clearCurrentSecurityContext();
139                 }
140                 return null;
141             }
142         });
143     }
144     
145     /**
146      * Bootstrap the Dictionary
147      */

148     public void bootstrapImpl()
149     {
150         Map JavaDoc<String JavaDoc, M2Model> modelMap = new HashMap JavaDoc<String JavaDoc, M2Model>();
151         
152         // Register the models found in the respository
153
for (RepositoryLocation repositoryLocation : this.repositoryLocations)
154         {
155             ResultSet resultSet = null;
156             try
157             {
158                 resultSet = this.searchService.query(repositoryLocation.getStoreRef(), SearchService.LANGUAGE_LUCENE, repositoryLocation.getQueryStatement());
159             
160                 for (NodeRef dictionaryModel : resultSet.getNodeRefs())
161                 {
162                     M2Model model = createM2Model(dictionaryModel);
163                     if (model != null)
164                     {
165                         for (M2Namespace namespace : model.getNamespaces())
166                         {
167                             modelMap.put(namespace.getUri(), model);
168                         }
169                     }
170                 }
171             }
172             finally
173             {
174                 if (resultSet != null)
175                 {
176                     resultSet.close();
177                 }
178             }
179         }
180         
181         // Load the models ensuring that they are loaded in the correct order
182
List JavaDoc<String JavaDoc> loadedModels = new ArrayList JavaDoc<String JavaDoc>();
183         for (Map.Entry JavaDoc<String JavaDoc, M2Model> entry : modelMap.entrySet())
184         {
185             loadModel(modelMap, loadedModels, entry.getValue());
186         }
187     }
188     
189     /**
190      * Loads a model (and it dependants) if it does not exist in the list of loaded models.
191      *
192      * @param modelMap a map of the models to be loaded
193      * @param loadedModels the list of models already loaded
194      * @param model the model to try and load
195      */

196     private void loadModel(Map JavaDoc<String JavaDoc, M2Model> modelMap, List JavaDoc<String JavaDoc> loadedModels, M2Model model)
197     {
198         String JavaDoc modelName = model.getName();
199         if (loadedModels.contains(modelName) == false)
200         {
201             for (M2Namespace importNamespace : model.getImports())
202             {
203                 M2Model importedModel = modelMap.get(importNamespace.getUri());
204                 if (importedModel != null)
205                 {
206                     // Ensure that the imported model is loaded first
207
loadModel(modelMap, loadedModels, importedModel);
208                 }
209                 // else we can assume that the imported model is already loaded, if this not the case then
210
// an error will be raised during compilation
211
}
212             
213             dictionaryDAO.putModel(model);
214             loadedModels.add(modelName);
215         }
216     }
217
218     /**
219      * Create a M2Model from a dictionary model node
220      *
221      * @param nodeRef the dictionary model node reference
222      * @return the M2Model
223      */

224     public M2Model createM2Model(NodeRef nodeRef)
225     {
226         M2Model model = null;
227         ContentReader contentReader = this.contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
228         if (contentReader != null)
229         {
230             model = M2Model.createModel(contentReader.getContentInputStream());
231         }
232         // TODO should we inactivate the model node and put the error somewhere??
233
return model;
234     }
235
236     /**
237      * Repositotry location object, defines a location in the repository from within which dictionary models should be loaded
238      * for inclusion in the data dictionary.
239      *
240      * @author Roy Wetherall
241      */

242     public class RepositoryLocation
243     {
244         /** Store protocol */
245         private String JavaDoc storeProtocol;
246         
247         /** Store identifier */
248         private String JavaDoc storeId;
249         
250         /** Path */
251         private String JavaDoc path;
252         
253         /**
254          * Set the store protocol
255          *
256          * @param storeProtocol the store protocol
257          */

258         public void setStoreProtocol(String JavaDoc storeProtocol)
259         {
260             this.storeProtocol = storeProtocol;
261         }
262         
263         /**
264          * Set the store identifier
265          *
266          * @param storeId the store identifier
267          */

268         public void setStoreId(String JavaDoc storeId)
269         {
270             this.storeId = storeId;
271         }
272         
273         /**
274          * Set the path
275          *
276          * @param path the path
277          */

278         public void setPath(String JavaDoc path)
279         {
280             this.path = path;
281         }
282         
283         /**
284          * Get the store reference
285          *
286          * @return the store reference
287          */

288         public StoreRef getStoreRef()
289         {
290             return new StoreRef(this.storeProtocol, this.storeId);
291         }
292         
293         /**
294          * Get the query statement, based on the path
295          *
296          * @return the query statement
297          */

298         public String JavaDoc getQueryStatement()
299         {
300             String JavaDoc result = "+TYPE:\"" + ContentModel.TYPE_DICTIONARY_MODEL.toString() + "\"";
301             if (this.path != null)
302             {
303                 result += " +PATH:\"" + this.path + "\"";
304             }
305             return result;
306         }
307     }
308 }
309
Popular Tags