001/*
002 *  Copyright 2018 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.web.frontoffice.search.metamodel.impl;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021
022import org.ametys.cms.data.type.indexing.IndexableElementType;
023import org.ametys.cms.search.query.NotQuery;
024import org.ametys.cms.search.query.Query;
025import org.ametys.cms.search.query.Query.Operator;
026import org.ametys.runtime.model.type.ModelItemTypeConstants;
027import org.ametys.web.frontoffice.search.metamodel.SearchServiceCriterionDefinition;
028import org.ametys.web.search.misc.SiteQueryHelper;
029import org.ametys.web.search.query.SiteQuery;
030
031/**
032 * {@link SearchServiceCriterionDefinition} proposing a criterion definition on the site of the indexed document.
033 */
034public class SiteCriterionDefinition extends AbstractSearchServiceCriterionDefinition<String>
035{
036    private SiteQueryHelper _siteQueryHelper;
037
038    @SuppressWarnings("unchecked")
039    @Override
040    public IndexableElementType<String> getType()
041    {
042        String typeId = ModelItemTypeConstants.STRING_TYPE_ID;
043        return (IndexableElementType<String>) _getCriterionTypeExtensionPoint().getExtension(typeId);
044    }
045    
046    @Override
047    public String getWidget()
048    {
049        return "edition.select-sites-criterion";
050    }
051    
052    @Override
053    public Object convertQueryValue(Object value, Map<String, Object> contextualParameters)
054    {
055        return _getSiteQueryHelper().convertQueryValue(value, contextualParameters, getName());
056    }
057    
058    @SuppressWarnings("unchecked")
059    @Override
060    public Query getQuery(Object value, Operator operator, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters)
061    {
062        return _getSiteQueryHelper().getQuery((Map<String, Object>) value, operator, contextualParameters);
063    }
064    
065    @Override
066    public Query getEmptyValueQuery(String language, Map<String, Object> contextualParameters)
067    {
068        Query existQuery = new SiteQuery();
069        return new NotQuery(existQuery);
070    }
071    
072    /**
073     * Retrieves the {@link SiteQueryHelper}
074     * @return the {@link SiteQueryHelper}
075     */
076    protected SiteQueryHelper _getSiteQueryHelper()
077    {
078        if (_siteQueryHelper == null)
079        {
080            try
081            {
082                _siteQueryHelper = (SiteQueryHelper) __serviceManager.lookup(SiteQueryHelper.ROLE);
083            }
084            catch (ServiceException e)
085            {
086                throw new RuntimeException("Unable to lookup after the site query helper", e);
087            }
088        }
089        
090        return _siteQueryHelper;
091    }
092}