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.cms.model;
017
018import java.util.Collection;
019import java.util.Collections;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.cocoon.ProcessingException;
024import org.xml.sax.ContentHandler;
025import org.xml.sax.SAXException;
026
027import org.ametys.cms.contenttype.ContentType;
028import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
029import org.ametys.cms.contenttype.ContentTypesHelper;
030import org.ametys.cms.data.ContentValue;
031import org.ametys.core.util.XMLUtils;
032import org.ametys.runtime.model.DefaultElementDefinition;
033import org.ametys.runtime.model.DefinitionContext;
034import org.ametys.runtime.model.ModelItem;
035
036/**
037 * Default implementation for element definition of type Content
038 */
039public class DefaultContentElementDefinition extends DefaultElementDefinition<ContentValue> implements ContentElementDefinition
040{
041    private ContentTypesHelper _contentTypesHelper;
042    private ContentTypeExtensionPoint _contentTypeExtensionPoint;
043    
044    private String _contentTypeId;
045    
046    public String getContentTypeId()
047    {
048        return _contentTypeId;
049    }
050    
051    public void setContentTypeId(String contentTypeId)
052    {
053        _contentTypeId = contentTypeId;
054    }
055    
056    public Collection< ? extends ModelItem> getModelItems()
057    {
058        if (_contentTypeId != null && _getContentTypeExtensionPoint().hasExtension(_contentTypeId))
059        {
060            ContentType contentType = _getContentTypeExtensionPoint().getExtension(_contentTypeId);
061            return contentType.getModelItems();
062        }
063        else
064        {
065            return Collections.singleton(_getContentTypesHelper().getTitleAttributeDefinition());
066        }
067    }
068    
069    @Override
070    protected Map<String, Object> _toJSON(DefinitionContext context) throws ProcessingException
071    {
072        Map<String, Object> result = super._toJSON(context);
073        result.put("contentType", _contentTypeId);
074        result.put("canCreate", _getContentTypesHelper().hasRight(_contentTypeId));
075        return result;
076    }
077    
078    @Override
079    public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException
080    {
081        super.toSAX(contentHandler, context);
082        XMLUtils.createElementIfNotNull(contentHandler, "contentType", _contentTypeId);
083    }
084    
085    /**
086     * Retrieves the {@link ContentTypesHelper}
087     * @return the {@link ContentTypesHelper}
088     */
089    protected ContentTypesHelper _getContentTypesHelper()
090    {
091        if (_contentTypesHelper == null)
092        {
093            try
094            {
095                _contentTypesHelper = (ContentTypesHelper) __serviceManager.lookup(ContentTypesHelper.ROLE);
096            }
097            catch (ServiceException e)
098            {
099                throw new RuntimeException("Unable to lookup after the content types helper component", e);
100            }
101        }
102        
103        return _contentTypesHelper;
104    }
105    
106    /**
107     * Retrieves the {@link ContentTypeExtensionPoint} 
108     * @return the {@link ContentTypeExtensionPoint}
109     */
110    protected ContentTypeExtensionPoint _getContentTypeExtensionPoint()
111    {
112        if (_contentTypeExtensionPoint == null)
113        {
114            try
115            {
116                _contentTypeExtensionPoint = (ContentTypeExtensionPoint) __serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
117            }
118            catch (ServiceException e)
119            {
120                throw new RuntimeException("Unable to lookup after the content type extension point", e);
121            }
122        }
123        
124        return _contentTypeExtensionPoint;
125    }
126}