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.model;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.cocoon.ProcessingException;
022import org.apache.cocoon.xml.XMLUtils;
023import org.xml.sax.ContentHandler;
024import org.xml.sax.SAXException;
025
026import org.ametys.cms.contenttype.ContentType;
027import org.ametys.cms.model.restrictions.ContentRestrictedModelItemHelper;
028import org.ametys.cms.model.restrictions.RestrictedModelItem;
029import org.ametys.cms.model.restrictions.Restrictions;
030import org.ametys.cms.repository.Content;
031import org.ametys.plugins.repository.model.CompositeDefinition;
032import org.ametys.runtime.model.DefinitionContext;
033
034/**
035 * Definition of a composite for content types attributes
036 */
037public class ContentRestrictedCompositeDefinition extends CompositeDefinition implements RestrictedModelItem<Content>
038{
039    private ContentRestrictedModelItemHelper _restrictedModelItemHelper;
040    private Restrictions _restrictions;
041    
042    /**
043     * Set the restrictions for the composite
044     * @param restrictions the restrictions to set
045     */
046    public void setRestrictions(Restrictions restrictions)
047    {
048        _restrictions = restrictions;
049    }
050
051    public boolean canRead(Content content)
052    {
053        ContentRestrictedModelItemHelper restrictedModelItemHelper = _getRestrictedModelItemHelper();
054        ContentType contentType = (ContentType) getModel();
055        return restrictedModelItemHelper.canRead(content, this, _restrictions) && contentType.canRead(content, this);
056    }
057
058    public boolean canWrite(Content content)
059    {
060        ContentRestrictedModelItemHelper restrictedModelItemHelper = _getRestrictedModelItemHelper();
061        ContentType contentType = (ContentType) getModel();
062        return restrictedModelItemHelper.canWrite(content, this, _restrictions) && contentType.canWrite(content, this);
063    }
064    
065    private ContentRestrictedModelItemHelper _getRestrictedModelItemHelper()
066    {
067        if (_restrictedModelItemHelper == null)
068        {
069            try
070            {
071                _restrictedModelItemHelper = (ContentRestrictedModelItemHelper) __serviceManager.lookup(ContentRestrictedModelItemHelper.ROLE);
072            }
073            catch (ServiceException e)
074            {
075                throw new RuntimeException("Unable to lookup after the restricted model item helper component", e);
076            }
077        }
078        
079        return _restrictedModelItemHelper;
080    }
081    
082    @Override
083    protected Map<String, Object> _toJSON(DefinitionContext context, boolean includeChildren) throws ProcessingException
084    {
085        Map<String, Object> result = super._toJSON(context, includeChildren);
086        
087        Content content = context.getObject()
088                .filter(Content.class::isInstance)
089                .map(Content.class::cast)
090                .orElse(null);
091        
092        if (!canWrite(content))
093        {
094            result.put("can-not-write", true);
095        }
096        
097        return result;
098    }
099    
100    @Override
101    public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException
102    {
103        super.toSAX(contentHandler, context);
104        
105        Content content = context.getObject()
106                .filter(Content.class::isInstance)
107                .map(Content.class::cast)
108                .orElse(null);
109        
110        if (!canWrite(content))
111        {
112            XMLUtils.createElement(contentHandler, "can-not-write", String.valueOf(true));
113        }
114    }
115    
116    @Override
117    protected boolean _shouldJSONBeEmpty(DefinitionContext context)
118    {
119        return context.getObject()
120                .filter(Content.class::isInstance)
121                .map(Content.class::cast)
122                .map(content -> !canRead(content))
123                .orElse(false);
124    }
125}