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.Collection;
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.solr.client.solrj.util.ClientUtils;
026
027import org.ametys.cms.content.referencetable.HierarchicalReferenceTablesHelper;
028import org.ametys.cms.data.ContentValue;
029import org.ametys.cms.data.type.ModelItemTypeConstants;
030import org.ametys.cms.model.ContentElementDefinition;
031import org.ametys.cms.repository.Content;
032import org.ametys.cms.search.SearchField;
033import org.ametys.cms.search.model.SystemProperty;
034import org.ametys.cms.search.query.Query;
035import org.ametys.cms.search.query.Query.Operator;
036import org.ametys.cms.search.systemprop.AbstractSystemProperty;
037import org.ametys.runtime.model.ModelItem;
038
039/**
040 * {@link SystemProperty} which represents all parents (parent, parent of the parent, etc.) of a simple content.
041 */
042public class ParentContentSystemProperty extends AbstractSystemProperty<ContentValue, Content> implements ContentElementDefinition
043{
044    /** The helper component for hierarchical simple contents */
045    protected HierarchicalReferenceTablesHelper _hierarchicalSimpleContentsHelper;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _hierarchicalSimpleContentsHelper = (HierarchicalReferenceTablesHelper) manager.lookup(HierarchicalReferenceTablesHelper.ROLE);
052    }
053    
054    @Override
055    public boolean isMultiple()
056    {
057        return true;
058    }
059    
060    @Override
061    public boolean isSortable()
062    {
063        return false;
064    }
065    
066    @Override
067    public String getRenderer()
068    {
069        return "Ametys.plugins.cms.search.SearchGridHelper.renderContent";
070    }
071    
072    @Override
073    public String getCriterionWidget()
074    {
075        return "edition.hidden";
076        // return "edition.select-referencetable-content";
077    }
078    
079    @Override
080    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
081    {
082        if (value instanceof String)
083        {
084            return () -> ParentContentSearchField.NAME + ":" + ClientUtils.escapeQueryChars((String) value);
085        }
086        else
087        {
088            return null;
089        }
090    }
091
092    @Override
093    public SearchField getSearchField()
094    {
095        return new ParentContentSearchField();
096    }
097
098    @Override
099    public Object getValue(Content content)
100    {
101        List<String> parentsAndMe = _hierarchicalSimpleContentsHelper.getAllParents(content);
102        parentsAndMe.add(content.getId());
103        
104        return parentsAndMe.stream()
105                           .map(id -> new ContentValue(_resolver, id))
106                           .toArray(ContentValue[]::new);
107    }
108    
109    @Override
110    protected String _getTypeId()
111    {
112        return ModelItemTypeConstants.CONTENT_ELEMENT_TYPE_ID;
113    }
114    
115    @Override
116    public String getContentTypeId()
117    {
118        return null;
119    }
120    
121    @Override
122    public void setContentTypeId(String contentTypeId)
123    {
124        // Do nothing by default
125    }
126    
127    @Override
128    public Collection< ? extends ModelItem> getModelItems()
129    {
130        return Collections.EMPTY_LIST;
131    }
132}