1 16 package org.outerj.daisy.repository.clientimpl.query; 17 18 import org.outerj.daisy.repository.clientimpl.infrastructure.AbstractRemoteStrategy; 19 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient; 20 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager; 21 import org.outerj.daisy.repository.query.*; 22 import org.outerj.daisy.repository.RepositoryException; 23 import org.outerj.daisy.repository.LocaleHelper; 24 import org.outerj.daisy.repository.VariantKey; 25 import org.outerj.daisy.repository.VariantKeys; 26 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 27 import org.apache.commons.httpclient.NameValuePair; 28 import org.apache.commons.httpclient.methods.GetMethod; 29 import org.apache.commons.httpclient.methods.PostMethod; 30 import org.outerx.daisy.x10.*; 31 32 import java.util.*; 33 34 public class RemoteQueryManager extends AbstractRemoteStrategy implements QueryManager { 35 private AuthenticatedUser user; 36 37 public RemoteQueryManager(RemoteRepositoryManager.Context context, AuthenticatedUser user) { 38 super(context); 39 this.user = user; 40 } 41 42 public SearchResultDocument performQuery(String query, Locale locale) throws RepositoryException { 43 return performQuery(query, null, null, locale); 44 } 45 46 public SearchResultDocument performQuery(String query, Locale locale, EvaluationContext evaluationContext) throws RepositoryException { 47 throw new RepositoryException("Specifying an evaluationContext is not possible in the remote repository API implementation."); 48 } 49 50 public VariantKey[] performQueryReturnKeys(String query, Locale locale) throws RepositoryException { 51 return performQueryReturnKeys(query, null, null, locale); 52 } 53 54 public VariantKey[] performQueryReturnKeys(String query, Locale locale, EvaluationContext evaluationContext) throws RepositoryException { 55 throw new RepositoryException("Specifying an evaluationContext is not possible in the remote repository API implementation."); 56 } 57 58 public VariantKey[] performQueryReturnKeys(String query, String extraCond, Locale locale) throws RepositoryException { 59 return performQueryReturnKeys(query, extraCond, null, locale); 60 } 61 62 public VariantKey[] performQueryReturnKeys(String query, String extraCond, Map queryOptions, Locale locale) throws RepositoryException { 63 DaisyHttpClient httpClient = getClient(user); 64 GetMethod method = new GetMethod("/repository/query"); 65 66 List queryString = new ArrayList(4); 67 queryString.add(new NameValuePair("q", query)); 68 queryString.add(new NameValuePair("locale", LocaleHelper.getString(locale))); 69 queryString.add(new NameValuePair("returnKeys", "true")); 70 if (extraCond != null) 71 queryString.add(new NameValuePair("extraCondition", extraCond)); 72 addQueryOptions(queryString, queryOptions); 73 74 method.setQueryString((NameValuePair[])queryString.toArray(new NameValuePair[queryString.size()])); 75 76 VariantKeysDocument variantKeysDocument = (VariantKeysDocument)httpClient.executeMethod(method, VariantKeysDocument.class, true); 77 return VariantKeys.fromXml(variantKeysDocument).getArray(); 78 } 79 80 public VariantKey[] performQueryReturnKeys(String query, String extraCond, Map queryOptions, Locale locale, EvaluationContext evaluationContext) throws RepositoryException { 81 throw new RepositoryException("Specifying an evaluationContext is not possible in the remote repository API implementation."); 82 } 83 84 public VariantKey[] performQueryReturnKeys(String query, String extraCond, Locale locale, EvaluationContext evaluationContext) throws RepositoryException { 85 throw new RepositoryException("Specifying an evaluationContext is not possible in the remote repository API implementation."); 86 } 87 88 public SearchResultDocument performQuery(String query, String extraCond, Locale locale) throws RepositoryException { 89 return performQuery(query, extraCond, null, locale); 90 } 91 92 public SearchResultDocument performQuery(String query, String extraCond, Map queryOptions, Locale locale) throws RepositoryException { 93 DaisyHttpClient httpClient = getClient(user); 94 GetMethod method = new GetMethod("/repository/query"); 95 96 List queryString = new ArrayList(4); 97 queryString.add(new NameValuePair("q", query)); 98 queryString.add(new NameValuePair("locale", LocaleHelper.getString(locale))); 99 if (extraCond != null) 100 queryString.add(new NameValuePair("extraCondition", extraCond)); 101 addQueryOptions(queryString, queryOptions); 102 103 method.setQueryString((NameValuePair[])queryString.toArray(new NameValuePair[queryString.size()])); 104 105 SearchResultDocument searchResultDocument = (SearchResultDocument)httpClient.executeMethod(method, SearchResultDocument.class, true); 106 return searchResultDocument; 107 } 108 109 private void addQueryOptions(List queryString, Map queryOptions) { 110 if (queryOptions != null) { 111 Iterator queryOptionsIt = queryOptions.entrySet().iterator(); 112 while (queryOptionsIt.hasNext()) { 113 Map.Entry entry = (Map.Entry)queryOptionsIt.next(); 114 queryString.add(new NameValuePair("queryOption", entry.getKey() + "=" + entry.getValue())); 115 } 116 } 117 } 118 119 public SearchResultDocument performQuery(String query, String extraCond, Map queryOptions, Locale locale, EvaluationContext evaluationContext) throws RepositoryException { 120 throw new RepositoryException("Specifying an evaluationContext is not possible in the remote repository API implementation."); 121 } 122 123 public SearchResultDocument performQuery(String query, String extraCond, Locale locale, EvaluationContext evaluationContext) throws RepositoryException { 124 throw new RepositoryException("Specifying an evaluationContext is not possible in the remote repository API implementation."); 125 } 126 127 public FacetedQueryResultDocument performFacetedQuery(String query, FacetConf[] facetConfs, int chunkOffset, int chunkLength, Locale locale) throws RepositoryException { 128 FacetedQueryRequestDocument facetedQueryRequestDocument = FacetedQueryRequestDocument.Factory.newInstance(); 129 FacetedQueryRequestDocument.FacetedQueryRequest facetedQueryRequest = facetedQueryRequestDocument.addNewFacetedQueryRequest(); 130 facetedQueryRequest.setQuery(query); 131 FacetedQueryRequestDocument.FacetedQueryRequest.FacetConfs facetConfsXml = facetedQueryRequest.addNewFacetConfs(); 132 for (int i = 0; i < facetConfs.length; i++) { 133 FacetConf facetConf = facetConfs[i]; 134 FacetedQueryRequestDocument.FacetedQueryRequest.FacetConfs.FacetConf facetConfXml = facetConfsXml.addNewFacetConf(); 135 facetConfXml.setIsFacet(facetConf.isFacet()); 136 facetConfXml.setMaxValues(facetConf.getMaxValues()); 137 facetConfXml.setSortAscending(facetConf.getSortAscending()); 138 facetConfXml.setSortOnValue(facetConf.getSortOnValue());; 139 } 140 FacetedQueryRequestDocument.FacetedQueryRequest.Chunk chunk = facetedQueryRequest.addNewChunk(); 141 chunk.setOffset(chunkOffset); 142 chunk.setLength(chunkLength); 143 facetedQueryRequest.setLocale(LocaleHelper.getString(locale)); 144 145 DaisyHttpClient httpClient = getClient(user); 146 PostMethod method = new PostMethod("/repository/facetedQuery"); 147 method.setRequestBody(facetedQueryRequestDocument.newInputStream()); 148 149 FacetedQueryResultDocument resultDocument = (FacetedQueryResultDocument)httpClient.executeMethod(method, FacetedQueryResultDocument.class, true); 150 return resultDocument; 151 } 152 153 public DistinctSearchResultDocument performDistinctQuery(String query, SortOrder sortOrder, Locale locale) throws RepositoryException { 154 return performDistinctQuery(query, null, sortOrder, locale); 155 } 156 157 public DistinctSearchResultDocument performDistinctQuery(String query, String extraCond, SortOrder sortOrder, Locale locale) throws RepositoryException { 158 DaisyHttpClient httpClient = getClient(user); 159 GetMethod method = new GetMethod("/repository/distinctquery"); 160 161 NameValuePair[] queryString = new NameValuePair[extraCond == null ? 3 : 4]; 162 queryString[0] = new NameValuePair("q", query); 163 queryString[1] = new NameValuePair("locale", LocaleHelper.getString(locale)); 164 queryString[2] = new NameValuePair("sortOrder", sortOrder.toString()); 165 if (extraCond != null) 166 queryString[3] = new NameValuePair("extraCondition", extraCond); 167 168 method.setQueryString(queryString); 169 170 DistinctSearchResultDocument resultDocument = (DistinctSearchResultDocument)httpClient.executeMethod(method, DistinctSearchResultDocument.class, true); 171 return resultDocument; 172 } 173 174 public PredicateExpression parsePredicateExpression(String expression) throws QueryException { 175 throw new QueryException("This method is not available in the remote repository API implementation."); 176 } 177 } 178 | Popular Tags |