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            null,
121            isUniversity ? duExpression : new NotExpression(duExpression)
122        );
123        
124        return _resolver.query(QueryHelper.getXPathQuery(null, RepositoryConstants.NAMESPACE_PREFIX + ":content", expr))
125            .stream()
126            .filter(Content.class::isInstance)
127            .map(Content.class::cast)
128            .collect(Collectors.toList());
129    }
130    
131    /**
132     * Get the degree university search criterion definition
133     * @param searcheable the searcheable link to the criterion definition
134     * @return the search criterion definition
135     */
136    public DegreeUniversityAttributeContentSearchCriterionDefinition getDegreeUniversityCriterionDefinition(Searchable searcheable)
137    {
138        try
139        {
140            ThreadSafeComponentManager<SearchUICriterion> searchCriterionManager = new ThreadSafeComponentManager<>();
141            searchCriterionManager.setLogger(getLogger());
142            searchCriterionManager.contextualize(_context);
143            searchCriterionManager.service(_manager);
144            
145            Configuration criteriaConf = _getDegreeIndexingFieldCriteriaConfiguration();
146            searchCriterionManager.addComponent(_pluginName, null, AbstractProgram.DEGREE, IndexingFieldSearchUICriterion.class, criteriaConf);
147            
148            searchCriterionManager.initialize();
149            
150            ContentType programContentType = _cTypeEP.getExtension(ProgramFactory.PROGRAM_CONTENT_TYPE);
151            
152            Optional<Validator> validator = _getDegreeValidator(programContentType);
153            SearchUICriterion criterion = searchCriterionManager.lookup(AbstractProgram.DEGREE);
154            
155            return new DegreeUniversityAttributeContentSearchCriterionDefinition(
156                _pluginName, 
157                Optional.of(searcheable),
158                criterion, 
159                Optional.of(programContentType), 
160                validator, 
161                _resolver, 
162                _cTypeEP,
163                _contentHelper,
164                _i18nUtils,
165                this
166            );
167        }
168        catch (Exception e)
169        {
170            throw new RuntimeException("An error occured when retrieving IndexingFieldSearchCriterionDefinitions", e);
171        }
172    }
173    
174    private Optional<Validator> _getDegreeValidator(ContentType contentType) 
175    {
176        return Optional.of(contentType.getModelItem(AbstractProgram.DEGREE))
177                .filter(ElementDefinition.class::isInstance)
178                .map(ElementDefinition.class::cast)
179                .map(ElementDefinition::getValidator);
180    }
181    
182    private Configuration _getDegreeIndexingFieldCriteriaConfiguration()
183    {
184        DefaultConfiguration criteriaConf = new DefaultConfiguration("criteria");
185        DefaultConfiguration metaConf = new DefaultConfiguration("field");
186        criteriaConf.addChild(metaConf);
187        metaConf.setAttribute("path", AbstractProgram.DEGREE);
188        
189        DefaultConfiguration cTypesConf = new DefaultConfiguration("contentTypes");
190        criteriaConf.addChild(cTypesConf);
191        DefaultConfiguration baseTypeConf = new DefaultConfiguration("baseType");
192        baseTypeConf.setAttribute("id", ProgramFactory.PROGRAM_CONTENT_TYPE);
193        cTypesConf.addChild(baseTypeConf);
194        
195        return criteriaConf;
196    }
197}