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