001/*
002 *  Copyright 2022 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.odf.program;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.data.type.indexing.IndexableElementType;
022import org.ametys.cms.model.CMSDataContext;
023import org.ametys.cms.model.properties.AbstractProperty;
024import org.ametys.cms.model.properties.Property;
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.IndexationAwareElementDefinition;
029import org.ametys.cms.search.model.IndexationAwareElementDefinitionHelper;
030import org.ametys.runtime.model.type.ModelItemTypeConstants;
031
032/**
033 * {@link Property} for the order of degree
034 */
035public class DegreeOrderProperty extends AbstractProperty<Long, Content> implements CriterionDefinitionAwareElementDefinition<Long>, IndexationAwareElementDefinition<Long, Content>
036{
037    private CriterionDefinitionHelper _criterionDefinitionHelper;
038    private IndexationAwareElementDefinitionHelper _indexationAwareElementDefinitionHelper;
039    
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
045        _indexationAwareElementDefinitionHelper = (IndexationAwareElementDefinitionHelper) manager.lookup(IndexationAwareElementDefinitionHelper.ROLE);
046    }
047    
048    public Object getValue(Content content)
049    {
050        if (content instanceof Program)
051        {
052            return content.getValue("degree/order", false, Long.MAX_VALUE);
053        }
054        
055        return null;
056    }
057
058    @Override
059    protected String getTypeId()
060    {
061        return ModelItemTypeConstants.LONG_TYPE_ID;
062    }
063    
064    public IndexableElementType getDefaultCriterionType()
065    {
066        CMSDataContext context = CMSDataContext.newInstance()
067                                               .withModelItem(this);
068        
069        String typeId = getType().getDefaultCriterionTypeId(context);
070        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
071    }
072    
073    public String getSolrSortFieldName()
074    {
075        return _indexationAwareElementDefinitionHelper.getDefaultSolrSortFieldName(this);
076    }
077}