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.odf.orgunit;
017
018import java.util.Collection;
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.Set;
022
023import org.apache.avalon.framework.service.ServiceException;
024
025import org.ametys.cms.contenttype.ContentType;
026import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
027import org.ametys.cms.contenttype.ContentTypesHelper;
028import org.ametys.cms.data.ContentValue;
029import org.ametys.cms.data.type.ModelItemTypeConstants;
030import org.ametys.cms.model.ContentElementDefinition;
031import org.ametys.cms.model.properties.AbstractProperty;
032import org.ametys.cms.model.properties.Property;
033import org.ametys.runtime.model.ModelItem;
034
035/**
036 * {@link Property} to get all orgUnit ancestors id in one field.
037 */
038public class OrgUnitAncestorProperty extends AbstractProperty<ContentValue, OrgUnit> implements ContentElementDefinition
039{
040    private ContentTypeExtensionPoint _contentTypeExtensionPoint;
041    private ContentTypesHelper _contentTypesHelper;
042    
043    @Override
044    public Object getValue(OrgUnit orgUnit)
045    {
046        Set<ContentValue> values = new HashSet<>();
047        values.add(new ContentValue(orgUnit));
048        
049        OrgUnit ancestor = orgUnit.getParentOrgUnit();
050        while (ancestor != null)
051        {
052            values.add(new ContentValue(ancestor));
053            ancestor = ancestor.getParentOrgUnit();
054        }
055        
056        return values.toArray(new ContentValue[values.size()]);
057    }
058    
059    @Override
060    public boolean isMultiple()
061    {
062        return true;
063    }
064    
065    @Override
066    protected String _getTypeId()
067    {
068        return ModelItemTypeConstants.CONTENT_ELEMENT_TYPE_ID;
069    }
070
071    public String getContentTypeId()
072    {
073        return OrgUnitFactory.ORGUNIT_CONTENT_TYPE;
074    }
075    
076    public Collection< ? extends ModelItem> getModelItems()
077    {
078        if (_getContentTypeExtensionPoint().hasExtension(getContentTypeId()))
079        {
080            ContentType contentType = _getContentTypeExtensionPoint().getExtension(getContentTypeId());
081            return contentType.getModelItems();
082        }
083        else
084        {
085            return Collections.singleton(_getContentTypesHelper().getTitleAttributeDefinition());
086        }
087    }
088
089    /**
090     * Retrieves the {@link ContentTypeExtensionPoint} 
091     * @return the {@link ContentTypeExtensionPoint}
092     */
093    protected ContentTypeExtensionPoint _getContentTypeExtensionPoint()
094    {
095        if (_contentTypeExtensionPoint == null)
096        {
097            try
098            {
099                _contentTypeExtensionPoint = (ContentTypeExtensionPoint) __serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
100            }
101            catch (ServiceException e)
102            {
103                throw new RuntimeException("Unable to lookup after the content type extension point", e);
104            }
105        }
106        
107        return _contentTypeExtensionPoint;
108    }
109    
110    /**
111     * Retrieves the {@link ContentTypesHelper}
112     * @return the {@link ContentTypesHelper}
113     */
114    protected ContentTypesHelper _getContentTypesHelper()
115    {
116        if (_contentTypesHelper == null)
117        {
118            try
119            {
120                _contentTypesHelper = (ContentTypesHelper) __serviceManager.lookup(ContentTypesHelper.ROLE);
121            }
122            catch (ServiceException e)
123            {
124                throw new RuntimeException("Unable to lookup after the content types helper component", e);
125            }
126        }
127        
128        return _contentTypesHelper;
129    }
130}