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;
033
034/**
035 * {@link SystemProperty} which represents all parents (parent, parent of the parent, etc.) of a simple content.
036 */
037public class ParentContentSystemProperty extends AbstractSystemProperty
038{
039    /** The helper component for hierarchical simple contents */
040    protected HierarchicalReferenceTablesHelper _hierarchicalSimpleContentsHelper;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _hierarchicalSimpleContentsHelper = (HierarchicalReferenceTablesHelper) manager.lookup(HierarchicalReferenceTablesHelper.ROLE);
047    }
048    
049    @Override
050    public MetadataType getType()
051    {
052        return MetadataType.CONTENT;
053    }
054
055    @Override
056    public boolean isMultiple()
057    {
058        return true;
059    }
060    
061    @Override
062    public boolean isSortable()
063    {
064        return false;
065    }
066    
067    @Override
068    public String getRenderer()
069    {
070        return "Ametys.plugins.cms.search.SearchGridHelper.renderContent";
071    }
072    
073    @Override
074    public String getWidget()
075    {
076        return "edition.hidden";
077        // return "edition.select-referencetable-content";
078    }
079    
080    @Override
081    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
082    {
083        if (value instanceof String)
084        {
085            return () -> ParentContentSearchField.NAME + ":" + ClientUtils.escapeQueryChars((String) value);
086        }
087        else
088        {
089            return null;
090        }
091    }
092
093    @Override
094    public SearchField getSearchField()
095    {
096        return new ParentContentSearchField();
097    }
098
099    @Override
100    public Object getValue(Content content)
101    {
102        List<String> parentsAndMe = _hierarchicalSimpleContentsHelper.getAllParents(content);
103        parentsAndMe.add(content.getId());
104        return parentsAndMe.toArray(new String[parentsAndMe.size()]);
105    }
106}