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.Collection; 019import java.util.HashMap; 020import java.util.HashSet; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024 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.model.DefaultSearchModel; 037import org.ametys.cms.search.ui.model.ColumnHelper; 038import org.ametys.cms.search.ui.model.ColumnHelper.Column; 039import org.ametys.cms.search.ui.model.impl.DefaultSearchUIModel; 040import org.ametys.cms.search.ui.model.SearchUIModel; 041import org.ametys.core.cocoon.JSonReader; 042import org.ametys.runtime.model.ViewItemContainer; 043 044/** 045 * Create a dynamic model with the given columns and facets and return the model columns if valid. 046 */ 047public class GetSolrSearchModelAction extends SearchAction 048{ 049 /** The content types helper */ 050 protected ContentTypesHelper _contentTypesHelper; 051 052 /** The helper for columns */ 053 protected ColumnHelper _columnHelper; 054 055 @Override 056 public void service(ServiceManager serviceManager) throws ServiceException 057 { 058 super.service(serviceManager); 059 060 _contentTypesHelper = (ContentTypesHelper) serviceManager.lookup(ContentTypesHelper.ROLE); 061 _columnHelper = (ColumnHelper) serviceManager.lookup(ColumnHelper.ROLE); 062 } 063 064 @SuppressWarnings("unchecked") 065 @Override 066 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 067 { 068 Request request = ObjectModelHelper.getRequest(objectModel); 069 070 Map<String, Object> results = new HashMap<>(); 071 request.setAttribute(JSonReader.OBJECT_TO_READ, results); 072 073 Map<String, Object> jsParameters = _serverCommHelper.getJsParameters(); 074 075 String modelId = (String) jsParameters.get("model"); 076 SearchUIModel model = _searchModelManager.getExtension(modelId); 077 078 Map<String, Object> values = (Map<String, Object>) jsParameters.get("values"); 079 080 String columnsStr = StringUtils.defaultString((String) values.get("columns")); 081 082 Object cTypesObj = values.get("contentTypes"); 083 Set<String> commonContentTypes = null; 084 if (cTypesObj != null && cTypesObj instanceof List<?>) 085 { 086 Set<String> contentTypes = new HashSet<>((List<String>) cTypesObj); 087 commonContentTypes = _contentTypesHelper.getCommonAncestors(contentTypes); 088 } 089 090 Map<String, Object> contextualParameters = (Map<String, Object>) jsParameters.get("contextualParameters"); 091 092 DefaultSearchModel modelCopy = model instanceof SearchUIModel uiModel 093 ? new DefaultSearchUIModel(uiModel, contextualParameters) 094 : new DefaultSearchModel(model, contextualParameters); 095 modelCopy.setContentTypes(commonContentTypes); 096 097 boolean success = true; 098 try 099 { 100 Collection<Column> columns = _getColumns(columnsStr, commonContentTypes); 101 if (columns != null && !columns.isEmpty()) 102 { 103 ViewItemContainer resultItems = _columnHelper.createViewFromColumns(commonContentTypes, columns, false); 104 modelCopy.setResultItems(resultItems); 105 } 106 } 107 catch (Exception e) 108 { 109 getLogger().warn("Error setting columns for solr query search.", e); 110 111 success = false; 112 results.put("error", "column-error"); 113 results.put("message", e.getMessage()); 114 } 115 116 results.put("success", success); 117 118 if (success) 119 { 120 results.put("columns", modelCopy.resultItemsToJSON(contextualParameters)); 121 } 122 123 return EMPTY_MAP; 124 } 125 126 private Collection<Column> _getColumns(String columnsStr, Set<String> baseContentTypes) 127 { 128 return _columnHelper.getColumns(columnsStr, baseContentTypes); 129 } 130 131}