001/*
002 *  Copyright 2020 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.odf;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021import java.util.Optional;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.cms.search.model.impl.AbstractStaticSearchModelCriterionDefinition;
028import org.ametys.cms.search.query.AndQuery;
029import org.ametys.cms.search.query.ContentLanguageQuery;
030import org.ametys.cms.search.query.Query;
031import org.ametys.cms.search.query.Query.Operator;
032import org.ametys.cms.search.query.StringQuery;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.runtime.model.type.ModelItemTypeConstants;
035
036/**
037 * UI criteria for program item context
038 */
039public class ProgramItemContextCriterionDefinition extends AbstractStaticSearchModelCriterionDefinition<String>
040{
041    /** The ametys object resolver. */
042    protected AmetysObjectResolver _resolver;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
049    }
050    
051    @Override
052    public Operator getOperator()
053    {
054        // Not suppose to be used
055        return Operator.EQ;
056    }
057    
058    @Override
059    protected String getTypeId()
060    {
061        return ModelItemTypeConstants.STRING_TYPE_ID;
062    }
063
064    @Override
065    public Query getQuery(Object value, Operator customOperator, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters)
066    {
067        List<Query> queries = new ArrayList<>();
068        
069        Optional<ProgramItem> program = Optional.ofNullable(contextualParameters.get("contentId"))
070            .filter(String.class::isInstance)
071            .map(String.class::cast)
072            .map(id -> _resolver.resolveById(id));
073        
074        if (program.isPresent())
075        {
076            ProgramItem programItem = program.get();
077            queries.add(new StringQuery(ProgramItem.CATALOG, programItem.getCatalog()));
078            
079            String lang = programItem.getLanguage();
080            if (StringUtils.isNotBlank(lang))
081            {
082                queries.add(new ContentLanguageQuery(lang));
083            }
084
085        }
086        
087        return new AndQuery(queries);
088    }
089}