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.helper; 017 018import java.util.List; 019import java.util.stream.Collectors; 020 021import org.apache.avalon.framework.component.Component; 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025 026import org.ametys.cms.contenttype.ContentType; 027import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 028import org.ametys.cms.repository.Content; 029import org.ametys.cms.repository.ContentTypeExpression; 030import org.ametys.odf.enumeration.OdfReferenceTableHelper; 031import org.ametys.odf.program.AbstractProgram; 032import org.ametys.odf.program.ProgramFactory; 033import org.ametys.plugins.odfweb.service.search.criterion.DegreeUniversityCriterionDefinition; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.plugins.repository.RepositoryConstants; 036import org.ametys.plugins.repository.query.QueryHelper; 037import org.ametys.plugins.repository.query.expression.AndExpression; 038import org.ametys.plugins.repository.query.expression.Expression; 039import org.ametys.plugins.repository.query.expression.Expression.Operator; 040import org.ametys.plugins.repository.query.expression.NotExpression; 041import org.ametys.plugins.repository.query.expression.StringExpression; 042import org.ametys.runtime.model.ElementDefinition; 043import org.ametys.runtime.plugin.component.AbstractLogEnabled; 044import org.ametys.runtime.plugin.component.PluginAware; 045import org.ametys.web.frontoffice.search.metamodel.SearchServiceCriterionDefinition; 046import org.ametys.web.frontoffice.search.metamodel.Searchable; 047 048/** 049 * The helper for degree university 050 */ 051public class DegreeUniversityHelper extends AbstractLogEnabled implements Component, Serviceable, PluginAware 052{ 053 /** The avalon role. */ 054 public static final String ROLE = DegreeUniversityHelper.class.getName(); 055 056 /** The Ametys Object resolver */ 057 protected AmetysObjectResolver _resolver; 058 059 /** The content type extension point */ 060 protected ContentTypeExtensionPoint _contentTypeExtensionPoint; 061 062 /** The plugin name */ 063 protected String _pluginName; 064 065 @Override 066 public void service(ServiceManager manager) throws ServiceException 067 { 068 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 069 _contentTypeExtensionPoint = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE); 070 } 071 072 @Override 073 public void setPluginInfo(String pluginName, String featureName, String id) 074 { 075 _pluginName = pluginName; 076 } 077 078 /** 079 * Get all degree contents of type university or not 080 * @param isUniversity <code>true</code> to return all degree contents of type university 081 * @return the list of degree contents 082 */ 083 public List<Content> getDegrees(boolean isUniversity) 084 { 085 StringExpression duExpression = new StringExpression(DegreeUniversityCriterionDefinition.ATTRIBUTE_DEGREE_TYPE, Operator.EQ, DegreeUniversityCriterionDefinition.ATTRIBUTE_DEGREE_TYPE_UNIVERSITY_VALUE); 086 Expression expr = new AndExpression( 087 new ContentTypeExpression(Operator.EQ, OdfReferenceTableHelper.DEGREE), 088 isUniversity ? duExpression : new NotExpression(duExpression) 089 ); 090 091 return _resolver.query(QueryHelper.getXPathQuery(null, RepositoryConstants.NAMESPACE_PREFIX + ":content", expr)) 092 .stream() 093 .filter(Content.class::isInstance) 094 .map(Content.class::cast) 095 .collect(Collectors.toList()); 096 } 097 098 /** 099 * Get the degree university criterion definition 100 * @param searcheable the searcheable link to the criterion definition 101 * @return the criterion definition 102 */ 103 public SearchServiceCriterionDefinition getDegreeUniversityCriterionDefinition(Searchable searcheable) 104 { 105 ContentType programContentType = _contentTypeExtensionPoint.getExtension(ProgramFactory.PROGRAM_CONTENT_TYPE); 106 ElementDefinition degreeDefinition = (ElementDefinition) programContentType.getModelItem(AbstractProgram.DEGREE); 107 String name = DegreeUniversityCriterionDefinition.DEGREE_UNIVERSITY_CRITERION_DEFINITION_NAME; 108 109 SearchServiceCriterionDefinition criterionDefinition = new DegreeUniversityCriterionDefinition(degreeDefinition, AbstractProgram.DEGREE, programContentType); 110 111 criterionDefinition.setName(name); 112 criterionDefinition.setPluginName(_pluginName); 113 criterionDefinition.setSearchable(searcheable); 114 115 return criterionDefinition; 116 } 117}