1 17 package org.alfresco.repo.search; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import org.alfresco.error.AlfrescoRuntimeException; 28 import org.alfresco.service.cmr.dictionary.DictionaryService; 29 import org.alfresco.service.cmr.search.NamedQueryParameterDefinition; 30 import org.alfresco.service.cmr.search.QueryParameterDefinition; 31 import org.alfresco.service.namespace.NamespacePrefixResolver; 32 import org.alfresco.service.namespace.NamespaceService; 33 import org.alfresco.service.namespace.QName; 34 import org.dom4j.Element; 35 import org.dom4j.Namespace; 36 37 public class CannedQueryDefImpl implements CannedQueryDef 38 { 39 private static final org.dom4j.QName ELEMENT_QNAME = new org.dom4j.QName("query-definition", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 40 41 private static final org.dom4j.QName QNAME = new org.dom4j.QName("qname", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 42 43 private static final org.dom4j.QName LANGUAGE = new org.dom4j.QName("language", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 44 45 private static final org.dom4j.QName QUERY = new org.dom4j.QName("query", new Namespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI)); 46 47 private QName qName; 48 49 private String language; 50 51 private Map <QName, QueryParameterDefinition> queryParameterDefs = new HashMap <QName, QueryParameterDefinition>(); 52 53 String query; 54 55 QueryCollection container; 56 57 public CannedQueryDefImpl(QName qName, String language, String query, List <QueryParameterDefinition> queryParameterDefs, QueryCollection container) 58 { 59 super(); 60 this.qName = qName; 61 this.language = language; 62 this.query = query; 63 for(QueryParameterDefinition paramDef : queryParameterDefs) 64 { 65 this.queryParameterDefs.put(paramDef.getQName(), paramDef); 66 } 67 this.container = container; 68 } 69 70 public QName getQname() 71 { 72 return qName; 73 } 74 75 public String getLanguage() 76 { 77 return language; 78 } 79 80 public Collection <QueryParameterDefinition> getQueryParameterDefs() 81 { 82 return Collections.unmodifiableCollection(queryParameterDefs.values()); 83 } 84 85 public String getQuery() 86 { 87 return query; 88 } 89 90 public NamespacePrefixResolver getNamespacePrefixResolver() 91 { 92 return container.getNamespacePrefixResolver(); 93 } 94 95 public static CannedQueryDefImpl createCannedQuery(Element element, DictionaryService dictionaryService, QueryCollection container, NamespacePrefixResolver nspr) 96 { 97 if (element.getQName().getName().equals(ELEMENT_QNAME.getName())) 98 { 99 QName qName = null; 100 Element qNameElement = element.element(QNAME.getName()); 101 if(qNameElement != null) 102 { 103 qName = QName.createQName(qNameElement.getText(), container.getNamespacePrefixResolver()); 104 } 105 106 String language = null; 107 Element languageElement = element.element(LANGUAGE.getName()); 108 if(languageElement != null) 109 { 110 language = languageElement.getText(); 111 } 112 113 String query = null; 114 Element queryElement = element.element(QUERY.getName()); 115 if(queryElement != null) 116 { 117 query = queryElement.getText(); 118 } 119 120 List <QueryParameterDefinition> queryParameterDefs = new ArrayList <QueryParameterDefinition>(); 121 122 List list = element.elements(QueryParameterDefImpl.getElementQName().getName()); 123 for(Iterator it = list.iterator(); it.hasNext(); ) 124 { 125 Element defElement = (Element) it.next(); 126 NamedQueryParameterDefinition nqpd = QueryParameterDefImpl.createParameterDefinition(defElement, dictionaryService, nspr); 127 queryParameterDefs.add(nqpd.getQueryParameterDefinition()); 128 } 129 130 list = element.elements(QueryParameterRefImpl.getElementQName().getName()); 131 for(Iterator it = list.iterator(); it.hasNext(); ) 132 { 133 Element refElement = (Element) it.next(); 134 NamedQueryParameterDefinition nqpd = QueryParameterRefImpl.createParameterReference(refElement, dictionaryService, container); 135 QueryParameterDefinition resolved = nqpd.getQueryParameterDefinition(); 136 if(resolved == null) 137 { 138 throw new AlfrescoRuntimeException("Unable to find refernce parameter : "+nqpd.getQName()); 139 } 140 queryParameterDefs.add(resolved); 141 } 142 143 return new CannedQueryDefImpl(qName, language, query, queryParameterDefs, container); 144 145 } 146 else 147 { 148 return null; 149 } 150 } 151 152 public static org.dom4j.QName getElementQName() 153 { 154 return ELEMENT_QNAME; 155 } 156 157 public Map <QName, QueryParameterDefinition> getQueryParameterMap() 158 { 159 return Collections.unmodifiableMap(queryParameterDefs); 160 } 161 162 } 163 | Popular Tags |