001/* 002 * Copyright 2015 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.cocoon; 017 018import java.util.List; 019import java.util.Map; 020import java.util.Optional; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.cocoon.environment.Request; 025 026import org.ametys.cms.contenttype.ContentTypesHelper; 027import org.ametys.cms.search.model.SearchModel; 028import org.ametys.cms.search.model.impl.ReferencingCriterionDefinition; 029import org.ametys.cms.search.model.impl.ReferencingSearchModelCriterionDefinition; 030import org.ametys.plugins.repository.AmetysObjectResolver; 031 032/** 033 * Specific search action for the SelectContent widget. 034 */ 035public class SelectContentSearchAction extends SearchAction 036{ 037 /** The Ametys object resolver. */ 038 protected AmetysObjectResolver _resolver; 039 040 /** The content types helper */ 041 protected ContentTypesHelper _contentTypesHelper; 042 043 @Override 044 public void service(ServiceManager serviceManager) throws ServiceException 045 { 046 super.service(serviceManager); 047 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 048 _contentTypesHelper = (ContentTypesHelper) serviceManager.lookup(ContentTypesHelper.ROLE); 049 } 050 051 @Override 052 protected void doSearch(Request request, SearchModel model, int offset, int maxResults, Map<String, Object> jsParameters, Map<String, Object> contextualParameters) throws Exception 053 { 054 // If the id provided is the none value, we can't search it as a content (not a valid ID), therefore do a standard search 055 if (jsParameters.get("id") != null && !(jsParameters.get("id") instanceof List ids && ids.size() == 1 && ReferencingCriterionDefinition.NONE_VALUE.equals(ids.get(0)))) 056 { 057 // Don't do a standard search, use the provided ID list. 058 @SuppressWarnings("unchecked") 059 List<String> contentIds = (List<String>) jsParameters.get("id"); 060 request.setAttribute(SEARCH_CONTENTS, contentIds); 061 } 062 else 063 { 064 Optional<String> isArchivedContentSearch = model.getContentTypes(contextualParameters) 065 .stream() 066 .filter(id -> _contentTypesHelper.isArchivedContentType(id)) 067 .findAny(); 068 069 if (isArchivedContentSearch.isPresent()) 070 { 071 @SuppressWarnings("unchecked") 072 Map<String, Object> values = (Map<String, Object>) jsParameters.get("values"); 073 values.put(ReferencingSearchModelCriterionDefinition.CRITERION_DEFINITION_PREFIX + "archived-eq", false); 074 } 075 076 super.doSearch(request, model, offset, maxResults, jsParameters, contextualParameters); 077 } 078 } 079}