1 17 package org.alfresco.repo.search; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 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 <String > initCollections = null; 41 42 private Map <String , QueryCollection> collections = new HashMap <String , QueryCollection>(); 43 44 public QueryRegisterComponentImpl() 45 { 46 super(); 47 } 48 49 private synchronized void loadCollectionsOnDemand() 50 { 51 if(initCollections != null) 52 { 53 for(String 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 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 getCollectionNameforQueryDefinition(QName qName) 77 { 78 loadCollectionsOnDemand(); 79 for(String 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 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 getCollectionNameforParameterDefinition(QName qName) 106 { 107 loadCollectionsOnDemand(); 108 for(String 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 location) 120 { 121 loadCollectionsOnDemand(); 122 return collections.get(location); 123 } 124 125 public void loadQueryCollection(String location) 126 { 127 try 128 { 129 InputStream 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 e) 141 { 142 throw new AlfrescoRuntimeException("IO Error reading XML", e); 143 } 144 } 145 146 public void setCollections(List <String > 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 |