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.model.SystemProperty;
033import org.ametys.cms.search.query.Query;
034import org.ametys.cms.search.query.Query.Operator;
035import org.ametys.cms.search.systemprop.AbstractIndexableSystemProperty;
036import org.ametys.plugins.repository.AmetysObjectResolver;
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 AbstractIndexableSystemProperty<ContentValue, ContentValue, Content> implements ContentElementDefinition
043{
044    /** Solr field name. */
045    public static final String SOLR_FIELD_NAME = "refParents";
046    
047    /** The ametys object resolver */
048    protected AmetysObjectResolver _resolver;
049    /** The helper component for hierarchical simple contents */
050    private HierarchicalReferenceTablesHelper _hierarchicalReferenceTablesHelper;
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
057    }
058    
059    @Override
060    public boolean isMultiple()
061    {
062        return true;
063    }
064    
065    @Override
066    public String getRenderer()
067    {
068        return "Ametys.plugins.cms.search.SearchGridHelper.renderContent";
069    }
070    
071    @Override
072    public String getDefaultCriterionWidget()
073    {
074        return "edition.hidden";
075        // return "edition.select-referencetable-content";
076    }
077    
078    @Override
079    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
080    {
081        if (value instanceof String)
082        {
083            return () -> SOLR_FIELD_NAME + ":" + ClientUtils.escapeQueryChars((String) value);
084        }
085        else
086        {
087            return null;
088        }
089    }
090
091    @Override
092    public String getSolrFieldName()
093    {
094        return SOLR_FIELD_NAME;
095    }
096    
097    @Override
098    public String getSolrSortFieldName()
099    {
100        // Not sortable.
101        return null;
102    }
103    
104    @Override
105    public String getSolrFacetFieldName()
106    {
107        return SOLR_FIELD_NAME + "_dv";
108    }
109
110    @Override
111    public Object getValue(Content content)
112    {
113        List<String> parentsAndMe = _getHierarchicalReferenceTablesHelper().getAllParents(content);
114        parentsAndMe.add(content.getId());
115        
116        return parentsAndMe.stream()
117                           .map(id -> new ContentValue(_resolver, id))
118                           .toArray(ContentValue[]::new);
119    }
120    
121    @Override
122    protected String getTypeId()
123    {
124        return ModelItemTypeConstants.CONTENT_ELEMENT_TYPE_ID;
125    }
126    
127    @Override
128    public String getContentTypeId()
129    {
130        return null;
131    }
132    
133    @Override
134    public void setContentTypeId(String contentTypeId)
135    {
136        // Do nothing by default
137    }
138    
139    @Override
140    public Collection< ? extends ModelItem> getModelItems()
141    {
142        return Collections.EMPTY_LIST;
143    }
144    
145    /**
146     * Retrieves the hierarchical reference tables helper
147     * @return the hierarchical reference tables helper
148     */
149    protected HierarchicalReferenceTablesHelper _getHierarchicalReferenceTablesHelper()
150    {
151        if (_hierarchicalReferenceTablesHelper == null)
152        {
153            try
154            {
155                _hierarchicalReferenceTablesHelper = (HierarchicalReferenceTablesHelper) __serviceManager.lookup(HierarchicalReferenceTablesHelper.ROLE);
156            }
157            catch (ServiceException e)
158            {
159                throw new RuntimeException("Unable to lookup after the hierarchical reference tables helper", e);
160            }
161        }
162        
163        return _hierarchicalReferenceTablesHelper;
164    }
165}