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