001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.search.solr;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.ProcessingException;
028import org.apache.cocoon.acting.ServiceableAction;
029import org.apache.cocoon.environment.ObjectModelHelper;
030import org.apache.cocoon.environment.Redirector;
031import org.apache.cocoon.environment.Request;
032import org.apache.cocoon.environment.SourceResolver;
033
034import org.ametys.cms.search.GetQueryFromJSONHelper;
035import org.ametys.cms.search.model.SearchCriterion;
036import org.ametys.cms.search.model.SearchModel;
037import org.ametys.cms.search.query.Query;
038import org.ametys.core.cocoon.JSonReader;
039import org.ametys.core.util.ServerCommHelper;
040
041/**
042 * Get a solr query string from a search tool.
043 */
044public class GetSolrQueryAction extends ServiceableAction
045{
046    /** The server comm helper */
047    protected ServerCommHelper _serverCommHelper;
048    
049    /** the helper to get query infos from JSON */
050    protected GetQueryFromJSONHelper _getQueryFromJSONHelper;
051    
052    @Override
053    public void service(ServiceManager smanager) throws ServiceException
054    {
055        super.service(smanager);
056        _serverCommHelper = (ServerCommHelper) smanager.lookup(ServerCommHelper.ROLE);
057        _getQueryFromJSONHelper = (GetQueryFromJSONHelper) smanager.lookup(GetQueryFromJSONHelper.ROLE);
058    }
059    
060    @Override
061    @SuppressWarnings ("unchecked")
062    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
063    {
064        Request request = ObjectModelHelper.getRequest(objectModel);
065        
066        Map<String, Object> queryResults = new HashMap<>();
067        request.setAttribute(JSonReader.OBJECT_TO_READ, queryResults);
068        
069        Map<String, Object> jsParameters = _serverCommHelper.getJsParameters();
070        
071        
072        Map<String, Object> contextualParameters = (Map<String, Object>) jsParameters.get("contextualParameters");
073        
074        if (contextualParameters == null)
075        {
076            contextualParameters = new HashMap<> ();
077        }
078        
079        SearchModel model = _getQueryFromJSONHelper.getSearchModel(jsParameters, "id");
080        
081        try
082        {
083            List<String> cTypesToFill = new ArrayList<>();
084            Query query = _getQueryFromJSONHelper.getQueryFromModel(model, jsParameters, cTypesToFill);
085            
086            // Set the query string into the results.
087            queryResults.put("query", query.build());
088            queryResults.put("contentTypes", cTypesToFill);
089            
090            // Get the model facets and set them into the results.
091            Map<String, ? extends SearchCriterion> facetedCriteria = model.getFacetedCriteria(contextualParameters);
092            setFacets(queryResults, facetedCriteria.values());
093            
094            // Get the model columns and set them into the results.
095            List<String> columnIds = _getQueryFromJSONHelper.getColumnsFromSearchModel(model, contextualParameters);
096            queryResults.put("columns", columnIds);
097        }
098        catch (Exception e)
099        {
100            getLogger().error("Cannot get solr query string: " + e.getMessage(), e);
101            throw new ProcessingException("Cannot get solr query string: " + e.getMessage(), e);
102        }
103        
104        return EMPTY_MAP;
105    }
106
107    /**
108     * Set the facet list into the query results.
109     * @param queryResults the query results.
110     * @param facetedCriteria the facets to put in the results.
111     */
112    protected void setFacets(Map<String, Object> queryResults, Collection<? extends SearchCriterion> facetedCriteria)
113    {
114        List<String> facets = new ArrayList<>();
115        queryResults.put("facets", facets);
116        
117        for (SearchCriterion criterion : facetedCriteria)
118        {
119            facets.add(criterion.getSearchField().getName());
120        }
121    }
122}