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;
022import java.util.stream.Collectors;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.solr.client.solrj.util.ClientUtils;
027
028import org.ametys.cms.content.referencetable.HierarchicalReferenceTablesHelper;
029import org.ametys.cms.data.ContentValue;
030import org.ametys.cms.data.type.ModelItemTypeConstants;
031import org.ametys.cms.model.ContentElementDefinition;
032import org.ametys.cms.repository.Content;
033import org.ametys.cms.search.SearchField;
034import org.ametys.cms.search.model.SystemProperty;
035import org.ametys.cms.search.query.Query;
036import org.ametys.cms.search.query.Query.Operator;
037import org.ametys.cms.search.systemprop.AbstractSystemProperty;
038import org.ametys.runtime.model.ModelItem;
039
040/**
041 * {@link SystemProperty} which represents all parents (parent, parent of the parent, etc.) of a simple content.
042 */
043public class ParentContentSystemProperty extends AbstractSystemProperty<ContentValue, Content> implements ContentElementDefinition
044{
045    /** The helper component for hierarchical simple contents */
046    protected HierarchicalReferenceTablesHelper _hierarchicalSimpleContentsHelper;
047    
048    @Override
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        super.service(manager);
052        _hierarchicalSimpleContentsHelper = (HierarchicalReferenceTablesHelper) manager.lookup(HierarchicalReferenceTablesHelper.ROLE);
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 getCriterionWidget()
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        
105        return parentsAndMe.stream()
106                           .map(id -> new ContentValue(_resolver, id))
107                           .collect(Collectors.toList())
108                           .toArray(new ContentValue[parentsAndMe.size()]);
109    }
110    
111    @Override
112    protected String _getTypeId()
113    {
114        return ModelItemTypeConstants.CONTENT_ELEMENT_TYPE_ID;
115    }
116    
117    @Override
118    public String getContentTypeId()
119    {
120        return null;
121    }
122    
123    @Override
124    public void setContentTypeId(String contentTypeId)
125    {
126        // Do nothing by default
127    }
128    
129    @Override
130    public Collection< ? extends ModelItem> getModelItems()
131    {
132        return Collections.EMPTY_LIST;
133    }
134}