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.contenttype.MetadataType;
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.cms.search.ui.model.impl.AbstractCustomSearchUICriterion;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035
036/**
037 * UI criteria for program item context
038 */
039public class ProgramItemContextSearchUICriteria extends AbstractCustomSearchUICriterion
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    public String getFieldId()
052    {
053        return "program.item.context.field";
054    }
055
056    public Operator getOperator()
057    {
058        // Not suppose to be used
059        return Operator.EQ;
060    }
061    
062    @Override
063    public MetadataType getType()
064    {
065        // Not suppose to be used
066        return MetadataType.STRING;
067    }
068
069    public Query getQuery(Object value, Operator customOperator, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters)
070    {
071        List<Query> queries = new ArrayList<>();
072        
073        Optional<ProgramItem> program = Optional.ofNullable(contextualParameters.get("contentId"))
074            .filter(String.class::isInstance)
075            .map(String.class::cast)
076            .map(id -> _resolver.resolveById(id));
077        
078        if (program.isPresent())
079        {
080            ProgramItem programItem = program.get();
081            queries.add(new StringQuery(ProgramItem.CATALOG, programItem.getCatalog()));
082            
083            String lang = programItem.getLanguage();
084            if (StringUtils.isNotBlank(lang))
085            {
086                queries.add(new ContentLanguageQuery(lang));
087            }
088        }
089        
090        return new AndQuery(queries);
091    }
092}