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.criterion; 017 018import java.util.Comparator; 019import java.util.List; 020import java.util.Locale; 021import java.util.Map; 022import java.util.Optional; 023import java.util.stream.Collectors; 024 025import org.apache.commons.lang3.tuple.Pair; 026 027import org.ametys.cms.content.ContentHelper; 028import org.ametys.cms.contenttype.ContentType; 029import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 030import org.ametys.cms.search.ui.model.SearchUICriterion; 031import org.ametys.core.util.I18nUtils; 032import org.ametys.core.util.LambdaUtils; 033import org.ametys.odf.program.AbstractProgram; 034import org.ametys.plugins.odfweb.service.search.ProgramSearchable; 035import org.ametys.plugins.odfweb.service.search.helper.DegreeUniversityHelper; 036import org.ametys.plugins.repository.AmetysObjectResolver; 037import org.ametys.runtime.i18n.I18nizableText; 038import org.ametys.runtime.parameter.Validator; 039import org.ametys.web.frontoffice.search.metamodel.Searchable; 040import org.ametys.web.frontoffice.search.metamodel.impl.ContentAttributeContentSearchCriterionDefinition; 041import org.ametys.web.frontoffice.search.metamodel.impl.ContentSearchCriterionDefinition; 042 043/** 044 * {@link ContentSearchCriterionDefinition} for a degree content attribute. 045 */ 046public class DegreeUniversityAttributeContentSearchCriterionDefinition extends ContentAttributeContentSearchCriterionDefinition 047{ 048 /** The attribute for degree type */ 049 public static final String ATTRIBUTE_DEGREE_TYPE = "type"; 050 051 /** The value for university for degree attribute */ 052 public static final String ATTRIBUTE_DEGREE_TYPE_UNIVERSITY_VALUE = "UNIVERSITY"; 053 054 /** The id of the degree search criterion */ 055 public static final String DEGREE_SEARCH_CRITERION_ID = ProgramSearchable.PROGRAM_SEARCHEABLE_INDEXING_FIELD_PREFIX + AbstractProgram.DEGREE; 056 057 /** The id of the degree university search criterion */ 058 public static final String DEGREE_UNIVERSITY_SEARCH_CRITERION_ID = DEGREE_SEARCH_CRITERION_ID + "$University"; 059 060 /** The value for university for degree attribute */ 061 public static final String SEARCH_CRITERION_ALL_DU_VALUE = "_Ametys_Degree_All_University"; 062 063 /** The i18n utils */ 064 protected I18nUtils _i18nUtils; 065 066 /** The degree university helper */ 067 protected DegreeUniversityHelper _degreeUniversityHelper; 068 069 /** 070 * Default constructor 071 * @param pluginName The plugin name 072 * @param searchable The {@link Searchable} 073 * @param criterion The linked {@link SearchUICriterion} 074 * @param contentType The content type on which this criterion definition applies. Can be empty if it applies to all types of contents. 075 * @param validator The validator 076 * @param resolver The resolver 077 * @param contentTypeEP The extension point for content types 078 * @param contentHelper the content helper 079 * @param i18nUtils The i18n utils 080 * @param degreeUniversityHelper The degree university helper 081 */ 082 public DegreeUniversityAttributeContentSearchCriterionDefinition( 083 String pluginName, 084 Optional<Searchable> searchable, 085 SearchUICriterion criterion, 086 Optional<ContentType> contentType, 087 Optional<Validator> validator, 088 AmetysObjectResolver resolver, 089 ContentTypeExtensionPoint contentTypeEP, 090 ContentHelper contentHelper, 091 I18nUtils i18nUtils, 092 DegreeUniversityHelper degreeUniversityHelper) 093 { 094 super(DEGREE_UNIVERSITY_SEARCH_CRITERION_ID, pluginName, searchable, criterion, contentType, validator, resolver, contentTypeEP, contentHelper); 095 096 _i18nUtils = i18nUtils; 097 _degreeUniversityHelper = degreeUniversityHelper; 098 setLabel(new I18nizableText("plugin.odf-web", "PLUGINS_ODFWEB_SEARCH_CRITERION_DEGREE_UNIVERSITY_LABEL")); 099 } 100 101 @Override 102 public Map<Object, I18nizableText> getEntries(String language) 103 { 104 List<Pair<String, String>> contentsAsPair = _degreeUniversityHelper.getDegrees(false) 105 .stream() 106 .map(content -> Pair.of(content.getId(), content.getTitle(new Locale(language)))) 107 .collect(Collectors.toList()); 108 109 if (!_degreeUniversityHelper.getDegrees(true).isEmpty()) 110 { 111 I18nizableText criterionAllDULabel = new I18nizableText("plugin.odf-web", "PLUGINS_ODFWEB_SEARCH_CRITERION_DEGREE_UNIVERSITY_FIELD_ALL"); 112 contentsAsPair.add(Pair.of(SEARCH_CRITERION_ALL_DU_VALUE, _i18nUtils.translate(criterionAllDULabel, language))); 113 } 114 115 return contentsAsPair.stream() 116 .sorted(Comparator.comparing(Pair::getRight)) // sort by title 117 .collect(LambdaUtils.Collectors.toLinkedHashMap(Pair::getLeft, pair -> new I18nizableText(pair.getRight()))); 118 } 119 120 @Override 121 public Long getOrder(String contentId) 122 { 123 if (SEARCH_CRITERION_ALL_DU_VALUE.equals(contentId)) 124 { 125 return _degreeUniversityHelper.getDegrees(true) 126 .stream() 127 .map(c -> c.<Long>getValue("order")) 128 .filter(l -> l != null) 129 .min(Long::compare) 130 .orElse(null); 131 } 132 else 133 { 134 return super.getOrder(contentId); 135 } 136 } 137} 138