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.HashMap; 019import java.util.HashSet; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.container.ContainerUtil; 025import org.apache.avalon.framework.parameters.Parameters; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.environment.ObjectModelHelper; 029import org.apache.cocoon.environment.Redirector; 030import org.apache.cocoon.environment.Request; 031import org.apache.cocoon.environment.SourceResolver; 032import org.apache.commons.lang3.StringUtils; 033 034import org.ametys.cms.contenttype.ContentTypesHelper; 035import org.ametys.cms.search.cocoon.SearchAction; 036import org.ametys.cms.search.ui.model.SearchUIColumn; 037import org.ametys.cms.search.ui.model.SearchUIModel; 038import org.ametys.cms.search.ui.model.SearchUIModelHelper; 039import org.ametys.core.cocoon.JSonReader; 040import org.ametys.core.util.AvalonLoggerAdapter; 041 042/** 043 * Create a dynamic model with the given columns and facets and return the model columns if valid. 044 */ 045public class GetSolrSearchModelAction extends SearchAction 046{ 047 048 /** The content types helper. */ 049 protected ContentTypesHelper _contentTypesHelper; 050 051 /** The search model helper. */ 052 protected SearchUIModelHelper _searchModelHelper; 053 054 @Override 055 public void service(ServiceManager serviceManager) throws ServiceException 056 { 057 super.service(serviceManager); 058 _contentTypesHelper = (ContentTypesHelper) serviceManager.lookup(ContentTypesHelper.ROLE); 059 _searchModelHelper = (SearchUIModelHelper) serviceManager.lookup(SearchUIModelHelper.ROLE); 060 } 061 062 @SuppressWarnings("unchecked") 063 @Override 064 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 065 { 066 Request request = ObjectModelHelper.getRequest(objectModel); 067 068 Map<String, Object> results = new HashMap<>(); 069 request.setAttribute(JSonReader.OBJECT_TO_READ, results); 070 071 Map<String, Object> jsParameters = _serverCommHelper.getJsParameters(); 072 073 String modelId = (String) jsParameters.get("model"); 074 SearchUIModel model = _searchModelManager.getExtension(modelId); 075 076 Map<String, Object> values = (Map<String, Object>) jsParameters.get("values"); 077 078 String columnsStr = StringUtils.defaultString((String) values.get("columns")); 079 080 Object facetObj = values.get("facets"); 081 List<String> facets = null; 082 if (facetObj != null && facetObj instanceof List<?>) 083 { 084 facets = (List<String>) facetObj; 085 } 086 087 Object cTypesObj = values.get("contentTypes"); 088 String baseContentType = null; 089 if (cTypesObj != null && cTypesObj instanceof List<?>) 090 { 091 Set<String> contentTypes = new HashSet<>((List<String>) cTypesObj); 092 baseContentType = _contentTypesHelper.getCommonAncestor(contentTypes); 093 } 094 095 CriteriaSearchUIModelWrapper modelWrapper = new CriteriaSearchUIModelWrapper(model, manager, _context, new AvalonLoggerAdapter(getLogger())); 096 ContainerUtil.service(modelWrapper, manager); 097 098 Map<String, Object> contextualParameters = (Map<String, Object>) jsParameters.get("contextualParameters"); 099 100 boolean success = true; 101 try 102 { 103 modelWrapper.setFacetedCriteria(baseContentType, facets, contextualParameters); 104 } 105 catch (Exception e) 106 { 107 getLogger().warn("Error setting facets for solr query search.", e); 108 109 success = false; 110 results.put("error", "facet-error"); 111 results.put("message", e.getMessage()); 112 } 113 114 try 115 { 116 modelWrapper.setResultColumns(baseContentType, SolrQuerySearchAction.getColumns(columnsStr), contextualParameters); 117 } 118 catch (Exception e) 119 { 120 getLogger().warn("Error setting columns for solr query search.", e); 121 122 success = false; 123 results.put("error", "column-error"); 124 results.put("message", e.getMessage()); 125 } 126 127 results.put("success", success); 128 129 if (success) 130 { 131 Map<String, SearchUIColumn> resultFields = modelWrapper.getResultFields(contextualParameters); 132 results.put("columns", _searchModelHelper.getColumnListInfo(resultFields)); 133 } 134 135 return EMPTY_MAP; 136 } 137 138}