1 17 package org.alfresco.repo.search; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.alfresco.service.cmr.dictionary.DictionaryService; 25 import org.alfresco.service.cmr.search.QueryParameterDefinition; 26 import org.alfresco.service.namespace.DynamicNamespacePrefixResolver; 27 import org.alfresco.service.namespace.NamespacePrefixResolver; 28 import org.alfresco.service.namespace.NamespaceService; 29 import org.alfresco.service.namespace.QName; 30 import org.dom4j.Element; 31 import org.dom4j.Namespace; 32 33 public class QueryCollectionImpl implements QueryCollection 34 { 35 private static final org.dom4j.QName ELEMENT_QNAME = new org.dom4j.QName("query-register", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 36 37 private static final org.dom4j.QName NAME = new org.dom4j.QName("name", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 38 39 private static final org.dom4j.QName NAMESPACES = new org.dom4j.QName("namespaces", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 40 41 private static final org.dom4j.QName NAMESPACE = new org.dom4j.QName("namespace", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 42 43 private static final org.dom4j.QName PREFIX = new org.dom4j.QName("prefix", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 44 45 private static final org.dom4j.QName URI = new org.dom4j.QName("uri", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 46 47 private String name; 48 49 private Map <QName, QueryParameterDefinition> parameters = new HashMap <QName, QueryParameterDefinition>(); 50 51 private Map <QName, CannedQueryDef> queries = new HashMap <QName, CannedQueryDef>(); 52 53 NamespacePrefixResolver namespacePrefixResolver; 54 55 public QueryCollectionImpl(String name, Map <QName, QueryParameterDefinition> parameters, NamespacePrefixResolver namespacePrefixResolver) 56 { 57 super(); 58 this.name = name; 59 this.parameters = parameters; 60 this.namespacePrefixResolver = namespacePrefixResolver; 61 } 62 63 public String getName() 64 { 65 return name; 66 } 67 68 public boolean containsQueryDefinition(QName qName) 69 { 70 return queries.containsKey(qName); 71 } 72 73 private void addQueryDefinition(CannedQueryDef queryDefinition) 74 { 75 queries.put(queryDefinition.getQname(), queryDefinition); 76 } 77 78 public CannedQueryDef getQueryDefinition(QName qName) 79 { 80 return queries.get(qName); 81 } 82 83 public boolean containsParameterDefinition(QName qName) 84 { 85 return parameters.containsKey(qName); 86 } 87 88 public QueryParameterDefinition getParameterDefinition(QName qName) 89 { 90 return parameters.get(qName); 91 } 92 93 public NamespacePrefixResolver getNamespacePrefixResolver() 94 { 95 return namespacePrefixResolver; 96 } 97 98 99 public static QueryCollection createQueryCollection(Element element, DictionaryService dictionaryService, NamespacePrefixResolver nspr) 100 { 101 DynamicNamespacePrefixResolver dnpr = new DynamicNamespacePrefixResolver(nspr); 102 if (element.getName().equals(ELEMENT_QNAME.getName())) 103 { 104 String name = null; 105 Element nameElement = element.element(NAME.getName()); 106 if(nameElement != null) 107 { 108 name = nameElement.getText(); 109 } 110 111 Element nameSpaces = element.element(NAMESPACES.getName()); 112 if(nameSpaces != null) 113 { 114 List ns = nameSpaces.elements(NAMESPACE.getName()); 115 for(Iterator it = ns.iterator(); it.hasNext(); ) 116 { 117 Element nsElement = (Element)it.next(); 118 Element prefixElement = nsElement.element(PREFIX.getName()); 119 Element uriElement = nsElement.element(URI.getName()); 120 if((prefixElement != null) && (nsElement != null)) 121 { 122 dnpr.registerNamespace(prefixElement.getText(), uriElement.getText()); 123 } 124 } 125 } 126 127 129 Map <QName, QueryParameterDefinition> parameters = new HashMap <QName, QueryParameterDefinition>(); 130 List list = element.elements(QueryParameterDefImpl.getElementQName().getName()); 131 for(Iterator it = list.iterator(); it.hasNext(); ) 132 { 133 Element defElement = (Element) it.next(); 134 QueryParameterDefinition paramDef = QueryParameterDefImpl.createParameterDefinition(defElement, dictionaryService, nspr); 135 parameters.put(paramDef.getQName(), paramDef); 136 } 137 138 QueryCollectionImpl collection = new QueryCollectionImpl(name, parameters, dnpr); 139 140 list = element.elements(CannedQueryDefImpl.getElementQName().getName()); 141 for(Iterator it = list.iterator(); it.hasNext(); ) 142 { 143 Element defElement = (Element) it.next(); 144 CannedQueryDefImpl queryDef = CannedQueryDefImpl.createCannedQuery(defElement, dictionaryService, collection, nspr); 145 collection.addQueryDefinition(queryDef); 146 } 147 148 return collection; 149 } 150 else 151 { 152 return null; 153 } 154 } 155 } 156 | Popular Tags |