001/* 002 * Copyright 2021 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.plugins.odfweb.service.search; 017 018import java.util.List; 019import java.util.Map; 020import java.util.Optional; 021import java.util.stream.Collectors; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.xml.XMLUtils; 026import org.xml.sax.ContentHandler; 027import org.xml.sax.SAXException; 028 029import org.ametys.cms.repository.Content; 030import org.ametys.cms.search.SearchResults; 031import org.ametys.plugins.odfweb.service.search.criterion.DegreeUniversityAttributeContentSearchCriterionDefinition; 032import org.ametys.plugins.odfweb.service.search.helper.DegreeUniversityHelper; 033import org.ametys.plugins.repository.AmetysObject; 034import org.ametys.web.frontoffice.search.instance.SearchServiceInstance; 035import org.ametys.web.frontoffice.search.instance.model.FOSearchCriterion; 036import org.ametys.web.frontoffice.search.metamodel.FacetDefinition; 037import org.ametys.web.frontoffice.search.requesttime.SearchComponent; 038import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments; 039import org.ametys.web.frontoffice.search.requesttime.impl.SaxEnumeratedCriteriaComponent; 040import org.ametys.web.frontoffice.search.requesttime.impl.SearchComponentHelper; 041 042/** 043 * {@link SearchComponent} for saxing number of results for degree university criteria 044 */ 045public class SaxDegreeUniversityEnumeratedCriteriaComponent extends SaxEnumeratedCriteriaComponent 046{ 047 /** The degree university helper */ 048 protected DegreeUniversityHelper _degreeUniversityHelper; 049 050 @Override 051 public void service(ServiceManager manager) throws ServiceException 052 { 053 super.service(manager); 054 _degreeUniversityHelper = (DegreeUniversityHelper) manager.lookup(DegreeUniversityHelper.ROLE); 055 } 056 057 @Override 058 public int priority() 059 { 060 return super.priority() + 1; 061 } 062 063 @Override 064 public void execute(SearchComponentArguments args) throws Exception 065 { 066 Optional<SearchResults<AmetysObject>> enumeratedResults = args.enumeratedResults(); 067 // Search for enumerated criteria is alreay done, just sax the search results for degree university criterion 068 if (enumeratedResults.isPresent()) 069 { 070 ContentHandler contentHandler = args.contentHandler(); 071 XMLUtils.startElement(contentHandler, "enumerated-criteria"); 072 073 Map<String, Object> contextualParameters = SearchComponentHelper.getSearchComponentContextualParameters(args); 074 Map<FacetDefinition, FOSearchCriterion> serviceFacets = _getFacetDefinitions(args.serviceInstance()); 075 _saxCountEnumeratedCriteria(contentHandler, args.generatorParameters(), serviceFacets, enumeratedResults.get(), contextualParameters); 076 077 XMLUtils.endElement(contentHandler, "enumerated-criteria"); 078 } 079 else 080 { 081 super.execute(args); 082 } 083 } 084 085 @Override 086 protected Map<FacetDefinition, FOSearchCriterion> _getFacetDefinitions(SearchServiceInstance serviceInstance) 087 { 088 // Get only facet for DU 089 return super._getFacetDefinitions(serviceInstance) 090 .entrySet() 091 .stream() 092 .filter(entry -> entry.getKey() 093 .getId() 094 .equals("ContentReturnable$" + DegreeUniversityAttributeContentSearchCriterionDefinition.DEGREE_UNIVERSITY_SEARCH_CRITERION_ID)) 095 .collect(Collectors.toMap( 096 Map.Entry::getKey, 097 Map.Entry::getValue)); 098 } 099 100 @Override 101 protected void _saxFacetItemsWithCount(ContentHandler contentHandler, FacetDefinition facet, Map<String, Integer> valuesForCurrentFacetDef, Map<String, Object> contextualParameters) throws SAXException 102 { 103 // Get DU ids 104 List<String> dUIds = _degreeUniversityHelper.getDegrees(true) 105 .stream() 106 .map(Content::getId) 107 .collect(Collectors.toList()); 108 109 // Sum all facet count for DU values 110 int allCount = 0; 111 for (String value : valuesForCurrentFacetDef.keySet()) 112 { 113 if (dUIds.contains(value)) 114 { 115 allCount += valuesForCurrentFacetDef.get(value); 116 } 117 } 118 119 // Sax facet for "ALL DU" value 120 Map<String, Integer> valuesForAllDU = Map.of(DegreeUniversityAttributeContentSearchCriterionDefinition.SEARCH_CRITERION_ALL_DU_VALUE, allCount); 121 super._saxFacetItemsWithCount(contentHandler, facet, valuesForAllDU, contextualParameters); 122 } 123}