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 getConverter()
069    {
070        return "Ametys.plugins.cms.search.SearchGridHelper.convertContent";
071    }
072    
073    @Override
074    public String getRenderer()
075    {
076        return "Ametys.plugins.cms.search.SearchGridHelper.renderContent";
077    }
078    
079    @Override
080    public String getCriterionWidget()
081    {
082        return "edition.hidden";
083        // return "edition.select-referencetable-content";
084    }
085    
086    @Override
087    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
088    {
089        if (value instanceof String)
090        {
091            return () -> ParentContentSearchField.NAME + ":" + ClientUtils.escapeQueryChars((String) value);
092        }
093        else
094        {
095            return null;
096        }
097    }
098
099    @Override
100    public SearchField getSearchField()
101    {
102        return new ParentContentSearchField();
103    }
104
105    @Override
106    public Object getValue(Content content)
107    {
108        List<String> parentsAndMe = _hierarchicalSimpleContentsHelper.getAllParents(content);
109        parentsAndMe.add(content.getId());
110        
111        return parentsAndMe.stream()
112                           .map(id -> new ContentValue(_resolver, id))
113                           .collect(Collectors.toList())
114                           .toArray(new ContentValue[parentsAndMe.size()]);
115    }
116    
117    @Override
118    protected String _getTypeId()
119    {
120        return ModelItemTypeConstants.CONTENT_ELEMENT_TYPE_ID;
121    }
122    
123    @Override
124    public String getContentTypeId()
125    {
126        return null;
127    }
128    
129    @Override
130    public void setContentTypeId(String contentTypeId)
131    {
132        // Do nothing by default
133    }
134    
135    @Override
136    public Collection< ? extends ModelItem> getModelItems()
137    {
138        return Collections.EMPTY_LIST;
139    }
140}