001/*
002 *  Copyright 2024 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.model.properties;
017
018import java.util.Collection;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022
023import org.ametys.cms.data.ContentValue;
024import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject;
025import org.ametys.cms.model.ContentElementDefinition;
026import org.ametys.runtime.model.ModelItem;
027
028/**
029 * Property referencing a {@link ContentElementDefinition}
030 * @param <X> Type of ametys object supported by this property
031 */
032public class ContentElementReferencingProperty<X extends ModelAwareDataAwareAmetysObject> extends ElementReferencingProperty<ContentValue, X> implements ContentElementDefinition
033{
034    @Override
035    protected void configureReferences(Configuration configuration) throws ConfigurationException
036    {
037        _reference = configuration.getAttribute("content-path");
038    }
039
040    public String getContentTypeId()
041    {
042        return _getReferenceDefinition(_reference).getContentTypeId();
043    }
044
045    public void setContentTypeId(String contentTypeId)
046    {
047        throw new UnsupportedOperationException("The property '" + getPath() + "' references an element of type content. The content type id can not be set to the property. Set it directly to the referenced element");
048    }
049
050    public Collection< ? extends ModelItem> getModelItems()
051    {
052        return _getReferenceDefinition(_reference).getModelItems();
053    }
054
055    @Override
056    protected ContentElementDefinition _getReferenceDefinition(String reference)
057    {
058        return (ContentElementDefinition) getModel().getModelItem(reference);
059    }
060}