001/*
002 *  Copyright 2015 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.web.search.systemprop;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.data.type.indexing.IndexableElementType;
024import org.ametys.cms.model.CMSDataContext;
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition;
027import org.ametys.cms.search.model.CriterionDefinitionHelper;
028import org.ametys.cms.search.model.SystemProperty;
029import org.ametys.cms.search.query.Query;
030import org.ametys.cms.search.query.Query.Operator;
031import org.ametys.cms.search.systemprop.AbstractSystemProperty;
032import org.ametys.cms.search.systemprop.IndexationAwareSystemProperty;
033import org.ametys.runtime.model.type.ModelItemTypeConstants;
034import org.ametys.web.repository.content.WebContent;
035import org.ametys.web.search.query.OrphanQuery;
036
037/**
038 * {@link SystemProperty} which represents the orphan status of a Content.
039 */
040public class OrphanSystemProperty extends AbstractSystemProperty<Boolean, Content> implements CriterionDefinitionAwareElementDefinition<Boolean>, IndexationAwareSystemProperty<Boolean, Content>
041{
042    /** System property identifier */
043    public static final String SYSTEM_PROPERTY_ID = "orphan";
044    
045    /** Solr field name. */
046    public static final String SOLR_FIELD_NAME = SYSTEM_PROPERTY_ID;
047    
048    /** The criterion definition helper */
049    protected CriterionDefinitionHelper _criterionDefinitionHelper;
050    
051    @Override
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        super.service(manager);
055        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
056    }
057    
058    @Override
059    public Integer getColumnWidth()
060    {
061        return 80;
062    }
063    
064    @Override
065    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
066    {
067        if (value == null)
068        {
069            return new OrphanQuery();
070        }
071        return new OrphanQuery((Boolean) value, operator);
072    }
073    
074    public String getSolrFieldName()
075    {
076        return SOLR_FIELD_NAME;
077    }
078    
079    public String getSolrSortFieldName()
080    {
081        return SOLR_FIELD_NAME;
082    }
083    
084    public String getSolrFacetFieldName()
085    {
086        return SOLR_FIELD_NAME;
087    }
088    
089    @Override
090    public Object getValue(Content content)
091    {
092        if (content instanceof WebContent webContent)
093        {
094            return webContent.getReferencingPages().isEmpty();
095        }
096        
097        return null;
098    }
099
100    @Override
101    protected String getTypeId()
102    {
103        return ModelItemTypeConstants.BOOLEAN_TYPE_ID;
104    }
105    
106    public IndexableElementType getDefaultCriterionType()
107    {
108        CMSDataContext context = CMSDataContext.newInstance()
109                                               .withModelItem(this);
110        
111        String typeId = getType().getDefaultCriterionTypeId(context);
112        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
113    }
114}