001/*
002 *  Copyright 2018 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.contenttype;
017
018import java.util.Collection;
019import java.util.Collections;
020
021import org.ametys.cms.repository.Content;
022import org.ametys.runtime.model.ModelItem;
023import org.ametys.runtime.model.ModelItemContainer;
024
025/**
026 * Definition of content types attributes of type Content
027 */
028public class ContentAttributeDefinition extends AttributeDefinition<Content> implements ModelItemContainer
029{
030    private ContentTypesHelper _contentTypesHelper;
031    private ContentTypeExtensionPoint _contentTypeExtensionPoint;
032    private String _contentTypeId;
033    private String _invertRelationPath;
034    private boolean _forceInvert;
035    
036    /**
037     * Defintion's constructor
038     * @param contentTypeExtensionPoint the content type extension point
039     * @param contentAttributeTypeExtensionPoint the content attribute type extension point
040     */
041    public ContentAttributeDefinition(ContentTypeExtensionPoint contentTypeExtensionPoint, ContentTypesHelper contentAttributeTypeExtensionPoint)
042    {
043        _contentTypeExtensionPoint = contentTypeExtensionPoint;
044        _contentTypesHelper = contentAttributeTypeExtensionPoint;
045    }
046    
047    /**
048     * Get the attribute's content type identifier.
049     * @return the attribute's content type identifier.
050     */
051    public String getContentTypeId()
052    {
053        return _contentTypeId;
054    }
055    
056    /**
057     * Set the attribute's content type identifier.
058     * @param contentTypeId the content type identifier to set.
059     */
060    public void setContentTypeId(String contentTypeId)
061    {
062        _contentTypeId = contentTypeId;
063    }
064    
065    /**
066     * Get the attribute's mutual relationship path.
067     * @return the attribute's mutual relationship path.
068     */
069    public String getInvertRelationPath()
070    {
071        return _invertRelationPath;
072    }
073    
074    /**
075     * Set the attribute's mutual relationship path.
076     * @param invertRelationPath the attribute's mutual relationship path, separated by slashes.
077     */
078    public void setInvertRelationPath(String invertRelationPath)
079    {
080        _invertRelationPath = invertRelationPath;
081    }
082    
083    /**
084     * Force mutual relationship regardless of user's rights (only applicable for an attribute with invert relation path).
085     * @param force true to force mutual relationship regardless of user's rights
086     */
087    public void setForceInvert (boolean force)
088    {
089        _forceInvert = force;
090    }
091    
092    /**
093     * Returns true if mutual relationship should be set regardless of user's rights
094     * @return true if mutual relationship should be set regardless of user's rights
095     */
096    public boolean getForceInvert ()
097    {
098        return _forceInvert;
099    }
100    
101    public Collection< ? extends ModelItem> getModelItems()
102    {
103        if (_contentTypeId != null && _contentTypeExtensionPoint.hasExtension(_contentTypeId))
104        {
105            ContentType contentType = _contentTypeExtensionPoint.getExtension(_contentTypeId);
106            return contentType.getModelItems();
107        }
108        else
109        {
110            return Collections.singleton(_contentTypesHelper.getTitleAttributeDefinition());
111        }
112    }
113}