1 17 package org.alfresco.repo.dictionary; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 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 41 public class DictionaryRepositoryBootstrap 42 { 43 44 private List <RepositoryLocation> repositoryLocations = new ArrayList <RepositoryLocation>(); 45 46 47 private DictionaryDAO dictionaryDAO = null; 48 49 50 private SearchService searchService; 51 52 53 private ContentService contentService; 54 55 56 private TransactionService transactionService; 57 58 59 private AuthenticationComponent authenticationComponent; 60 61 66 public void setDictionaryDAO(DictionaryDAO dictionaryDAO) 67 { 68 this.dictionaryDAO = dictionaryDAO; 69 } 70 71 76 public void setSearchService(SearchService searchService) 77 { 78 this.searchService = searchService; 79 } 80 81 86 public void setContentService(ContentService contentService) 87 { 88 this.contentService = contentService; 89 } 90 91 96 public void setTransactionService(TransactionService transactionService) 97 { 98 this.transactionService = transactionService; 99 } 100 101 106 public void setAuthenticationComponent( 107 AuthenticationComponent authenticationComponent) 108 { 109 this.authenticationComponent = authenticationComponent; 110 } 111 112 117 public void setRepositoryLocations( 118 List <RepositoryLocation> repositoryLocations) 119 { 120 this.repositoryLocations = repositoryLocations; 121 } 122 123 @SuppressWarnings ("unchecked") 124 public void bootstrap() 125 { 126 TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork() 127 { 128 public Object doWork() throws Exception 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 148 public void bootstrapImpl() 149 { 150 Map <String , M2Model> modelMap = new HashMap <String , M2Model>(); 151 152 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 List <String > loadedModels = new ArrayList <String >(); 183 for (Map.Entry <String , M2Model> entry : modelMap.entrySet()) 184 { 185 loadModel(modelMap, loadedModels, entry.getValue()); 186 } 187 } 188 189 196 private void loadModel(Map <String , M2Model> modelMap, List <String > loadedModels, M2Model model) 197 { 198 String 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 loadModel(modelMap, loadedModels, importedModel); 208 } 209 } 212 213 dictionaryDAO.putModel(model); 214 loadedModels.add(modelName); 215 } 216 } 217 218 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 return model; 234 } 235 236 242 public class RepositoryLocation 243 { 244 245 private String storeProtocol; 246 247 248 private String storeId; 249 250 251 private String path; 252 253 258 public void setStoreProtocol(String storeProtocol) 259 { 260 this.storeProtocol = storeProtocol; 261 } 262 263 268 public void setStoreId(String storeId) 269 { 270 this.storeId = storeId; 271 } 272 273 278 public void setPath(String path) 279 { 280 this.path = path; 281 } 282 283 288 public StoreRef getStoreRef() 289 { 290 return new StoreRef(this.storeProtocol, this.storeId); 291 } 292 293 298 public String getQueryStatement() 299 { 300 String 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 |