001/*
002 *  Copyright 2017 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.cms.content.referencetable.search;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.solr.client.solrj.util.ClientUtils;
024
025import org.ametys.cms.content.referencetable.HierarchicalReferenceTablesHelper;
026import org.ametys.cms.contenttype.MetadataType;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.search.SearchField;
029import org.ametys.cms.search.model.SystemProperty;
030import org.ametys.cms.search.query.Query;
031import org.ametys.cms.search.query.Query.Operator;
032import org.ametys.cms.search.systemprop.AbstractSystemProperty;
033import org.ametys.runtime.parameter.ParameterHelper;
034import org.ametys.runtime.parameter.ParameterHelper.ParameterType;
035
036/**
037 * {@link SystemProperty} which represents all parents (parent, parent of the parent, etc.) of a simple content.
038 */
039public class ParentContentSystemProperty extends AbstractSystemProperty
040{
041    /** The helper component for hierarchical simple contents */
042    protected HierarchicalReferenceTablesHelper _hierarchicalSimpleContentsHelper;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048        _hierarchicalSimpleContentsHelper = (HierarchicalReferenceTablesHelper) manager.lookup(HierarchicalReferenceTablesHelper.ROLE);
049    }
050    
051    @Override
052    public MetadataType getType()
053    {
054        return MetadataType.CONTENT;
055    }
056
057    @Override
058    public boolean isMultiple()
059    {
060        return true;
061    }
062    
063    @Override
064    public boolean isSortable()
065    {
066        return false;
067    }
068    
069    @Override
070    public String getRenderer()
071    {
072        return "Ametys.plugins.cms.search.SearchGridHelper.renderContent";
073    }
074    
075    @Override
076    public String getWidget()
077    {
078        return "edition.hidden";
079        // return "edition.select-referencetable-content";
080    }
081    
082    @Override
083    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
084    {
085        if (value instanceof String)
086        {
087            String stringValue = (String) ParameterHelper.castValue((String) value, ParameterType.STRING);
088            return () -> ParentContentSearchField.NAME + ":" + ClientUtils.escapeQueryChars(stringValue);
089        }
090        else
091        {
092            return null;
093        }
094    }
095
096    @Override
097    public SearchField getSearchField()
098    {
099        return new ParentContentSearchField();
100    }
101
102    @Override
103    public Object getValue(Content content)
104    {
105        List<String> parents = _hierarchicalSimpleContentsHelper.getAllParents(content);
106        return parents.toArray(new String[parents.size()]);
107    }
108}