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.Optional;
020import java.util.stream.Collectors;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.DefaultConfiguration;
025import org.apache.avalon.framework.context.Context;
026import org.apache.avalon.framework.context.ContextException;
027import org.apache.avalon.framework.context.Contextualizable;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031
032import org.ametys.cms.content.ContentHelper;
033import org.ametys.cms.contenttype.ContentType;
034import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
035import org.ametys.cms.repository.Content;
036import org.ametys.cms.repository.ContentTypeExpression;
037import org.ametys.cms.search.ui.model.SearchUICriterion;
038import org.ametys.cms.search.ui.model.impl.IndexingFieldSearchUICriterion;
039import org.ametys.core.util.I18nUtils;
040import org.ametys.odf.enumeration.OdfReferenceTableHelper;
041import org.ametys.odf.program.AbstractProgram;
042import org.ametys.odf.program.ProgramFactory;
043import org.ametys.plugins.odfweb.service.search.criterion.DegreeUniversityAttributeContentSearchCriterionDefinition;
044import org.ametys.plugins.repository.AmetysObjectResolver;
045import org.ametys.plugins.repository.RepositoryConstants;
046import org.ametys.plugins.repository.query.QueryHelper;
047import org.ametys.plugins.repository.query.expression.AndExpression;
048import org.ametys.plugins.repository.query.expression.Expression;
049import org.ametys.plugins.repository.query.expression.Expression.Operator;
050import org.ametys.plugins.repository.query.expression.NotExpression;
051import org.ametys.plugins.repository.query.expression.StringExpression;
052import org.ametys.runtime.model.ElementDefinition;
053import org.ametys.runtime.parameter.Validator;
054import org.ametys.runtime.plugin.component.AbstractLogEnabled;
055import org.ametys.runtime.plugin.component.PluginAware;
056import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
057import org.ametys.web.frontoffice.search.metamodel.Searchable;
058
059/**
060 * The helper for degree university
061 */
062public class DegreeUniversityHelper extends AbstractLogEnabled implements Component, Serviceable, Contextualizable, PluginAware
063{
064    /** The avalon role. */
065    public static final String ROLE = DegreeUniversityHelper.class.getName();
066
067    /** The Ametys Object resolver */
068    protected AmetysObjectResolver _resolver;
069    
070    /** The i18n utils */
071    protected I18nUtils _i18nUtils;
072    
073    /** The content type extension point */
074    protected ContentTypeExtensionPoint _cTypeEP;
075    
076    /** The content helper */
077    protected ContentHelper _contentHelper;
078    
079    /** The service manager */
080    protected ServiceManager _manager;
081    
082    /** The context */
083    protected Context _context;
084    
085    /** The plugin name */
086    protected String _pluginName;
087    
088    @Override
089    public void service(ServiceManager manager) throws ServiceException
090    {
091        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
092        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
093        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
094        _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE);
095        _manager = manager;
096    }
097    
098    @Override
099    public void contextualize(Context context) throws ContextException
100    {
101        _context = context;
102    }
103    
104    @Override
105    public void setPluginInfo(String pluginName, String featureName, String id)
106    {
107        _pluginName = pluginName;
108    }
109    
110    /**
111     * Get all degree contents of type university or not
112     * @param isUniversity <code>true</code> to return all degree contents of type university
113     * @return the list of degree contents
114     */
115    public List<Content> getDegrees(boolean isUniversity)
116    {
117        StringExpression duExpression = new StringExpression(DegreeUniversityAttributeContentSearchCriterionDefinition.ATTRIBUTE_DEGREE_TYPE, Operator.EQ, DegreeUniversityAttributeContentSearchCriterionDefinition.ATTRIBUTE_DEGREE_TYPE_UNIVERSITY_VALUE);
118        Expression expr = new AndExpression(
119            new ContentTypeExpression(Operator.EQ, OdfReferenceTableHelper.DEGREE),
120            isUniversity ? duExpression : new NotExpression(duExpression)
121        );
122        
123        return _resolver.query(QueryHelper.getXPathQuery(null, RepositoryConstants.NAMESPACE_PREFIX + ":content", expr))
124            .stream()
125            .filter(Content.class::isInstance)
126            .map(Content.class::cast)
127            .collect(Collectors.toList());
128    }
129    
130    /**
131     * Get the degree university search criterion definition
132     * @param searcheable the searcheable link to the criterion definition
133     * @return the search criterion definition
134     */
135    public DegreeUniversityAttributeContentSearchCriterionDefinition getDegreeUniversityCriterionDefinition(Searchable searcheable)
136    {
137        try
138        {
139            ThreadSafeComponentManager<SearchUICriterion> searchCriterionManager = new ThreadSafeComponentManager<>();
140            searchCriterionManager.setLogger(getLogger());
141            searchCriterionManager.contextualize(_context);
142            searchCriterionManager.service(_manager);
143            
144            Configuration criteriaConf = _getDegreeIndexingFieldCriteriaConfiguration();
145            searchCriterionManager.addComponent(_pluginName, null, AbstractProgram.DEGREE, IndexingFieldSearchUICriterion.class, criteriaConf);
146            
147            searchCriterionManager.initialize();
148            
149            ContentType programContentType = _cTypeEP.getExtension(ProgramFactory.PROGRAM_CONTENT_TYPE);
150            
151            Optional<Validator> validator = _getDegreeValidator(programContentType);
152            SearchUICriterion criterion = searchCriterionManager.lookup(AbstractProgram.DEGREE);
153            
154            return new DegreeUniversityAttributeContentSearchCriterionDefinition(
155                _pluginName, 
156                Optional.of(searcheable),
157                criterion, 
158                Optional.of(programContentType), 
159                validator, 
160                _resolver, 
161                _cTypeEP,
162                _contentHelper,
163                _i18nUtils,
164                this
165            );
166        }
167        catch (Exception e)
168        {
169            throw new RuntimeException("An error occured when retrieving IndexingFieldSearchCriterionDefinitions", e);
170        }
171    }
172    
173    private Optional<Validator> _getDegreeValidator(ContentType contentType) 
174    {
175        return Optional.of(contentType.getModelItem(AbstractProgram.DEGREE))
176                .filter(ElementDefinition.class::isInstance)
177                .map(ElementDefinition.class::cast)
178                .map(ElementDefinition::getValidator);
179    }
180    
181    private Configuration _getDegreeIndexingFieldCriteriaConfiguration()
182    {
183        DefaultConfiguration criteriaConf = new DefaultConfiguration("criteria");
184        DefaultConfiguration metaConf = new DefaultConfiguration("field");
185        criteriaConf.addChild(metaConf);
186        metaConf.setAttribute("path", AbstractProgram.DEGREE);
187        
188        DefaultConfiguration cTypesConf = new DefaultConfiguration("contentTypes");
189        criteriaConf.addChild(cTypesConf);
190        DefaultConfiguration baseTypeConf = new DefaultConfiguration("baseType");
191        baseTypeConf.setAttribute("id", ProgramFactory.PROGRAM_CONTENT_TYPE);
192        cTypesConf.addChild(baseTypeConf);
193        
194        return criteriaConf;
195    }
196}