/** * Processes a velocity template so that variable references are replaced by the same properties * in the property provider and request. * * @param request the request. * @param queryTemplate the query template. * @param propertyProviderName * @return A processed query template * @throws MissingParameterException */ protected Query processQuery(SlingHttpServletRequest request, Node queryNode) throws RepositoryException, MissingParameterException, JSONException { String propertyProviderName = null; if (queryNode.hasProperty(SAKAI_PROPERTY_PROVIDER)) { propertyProviderName = queryNode.getProperty(SAKAI_PROPERTY_PROVIDER).getString(); } Map<String, String> propertiesMap = loadProperties(request, propertyProviderName, queryNode); String queryTemplate = queryNode.getProperty(SAKAI_QUERY_TEMPLATE).getString(); // process the query string before checking for missing terms to a) give processors a // chance to set things and b) catch any missing terms added by the processors. String queryString = templateService.evaluateTemplate(propertiesMap, queryTemplate); // expand home directory references to full path; eg. ~user => a:user queryString = expandHomeDirectory(queryString); // append the user principals to the query string queryString = addUserPrincipals(request, queryString); // check for any missing terms & process the query template Collection<String> missingTerms = templateService.missingTerms(propertiesMap, queryTemplate); if (!missingTerms.isEmpty()) { throw new MissingParameterException( "Your request is missing parameters for the template: " + StringUtils.join(missingTerms, ", ")); } // collect query options JSONObject queryOptions = accumulateQueryOptions(queryNode); // process the options as templates and check for missing params Map<String, String> options = processOptions(propertiesMap, queryOptions); // check the resource type and set the query type appropriately // default to using solr for queries javax.jcr.Property resourceType = queryNode.getProperty("sling:resourceType"); Query query = null; if ("sakai/sparse-search".equals(resourceType.getString())) { query = new Query(Type.SPARSE, queryString, options); } else { query = new Query(Type.SOLR, queryString, options); } return query; }
/** * @param propertiesMap * @param queryOptions * @return * @throws JSONException * @throws MissingParameterException */ private Map<String, String> processOptions( Map<String, String> propertiesMap, JSONObject queryOptions) throws JSONException, MissingParameterException { Collection<String> missingTerms; Map<String, String> options = Maps.newHashMap(); if (queryOptions != null) { Iterator<String> keys = queryOptions.keys(); while (keys.hasNext()) { String key = keys.next(); String val = queryOptions.getString(key); missingTerms = templateService.missingTerms(propertiesMap, val); if (!missingTerms.isEmpty()) { throw new MissingParameterException( "Your request is missing parameters for the template: " + StringUtils.join(missingTerms, ", ")); } String processedVal = templateService.evaluateTemplate(propertiesMap, val); options.put(key, processedVal); } } return options; }