/*
* Copyright (C) 2005-2012 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see .
*/
package org.alfresco.cmis.mapping;
import java.io.Serializable;
import org.alfresco.cmis.CMISAllowedActionEnum;
import org.alfresco.cmis.CMISDictionaryModel;
import org.alfresco.cmis.CMISInvalidArgumentException;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.apache.chemistry.abdera.ext.CMISAllowableActions;
/**
* This evaluator determines an action availability in accordance with collection of {@link PropertyDescriptor} and in accordance with rules of checking of the object properties.
* The rules are:
* - should each specific property in the list satisfy all the conditions, which are specified in appropriate {@link PropertyDescriptor} instance or at least 1 property
* satisfaction is enough.
*
* This evaluator is generic, because it is used in the scope of {@link CompositeActionEvaluator}
*
* @author Dmitry Velichkevich
* @see PropertyDescriptor
*/
public class PropertyActionEvaluator extends AbstractActionEvaluator
{
private PropertyDescriptor[] propertyIdsAndExpectedValues;
private boolean allPropertiesConcur;
private boolean defaultAllowing;
/**
* Constructor
*
* @param serviceRegistry - {@link ServiceRegistry} instance
* @param action - {@link CMISAllowableActions} enumeration value, which determines the action to check
* @param allPropertiesConcur - {@link Boolean} value, which determines: true
- each specific object property should satisfy all the conditions of appropriate
* {@link PropertyDescriptor}, false
- at least 1 object property satisfaction is enough
* @param defaultAllowing - {@link Boolean} value, which determines availability of action for several special cases (invalid object id, empty collection of the aspects etc.)
* @param propertyIdsAndExpectedValues - {@link PropertyDescriptor}... collection, which specifies all the properties, which should be validated on an object
*/
public PropertyActionEvaluator(ServiceRegistry serviceRegistry, CMISAllowedActionEnum action, boolean allPropertiesConcur, boolean defaultAllowing,
PropertyDescriptor... propertyIdsAndExpectedValues)
{
super(serviceRegistry, action);
this.propertyIdsAndExpectedValues = propertyIdsAndExpectedValues;
this.allPropertiesConcur = allPropertiesConcur;
}
@Override
public boolean isAllowed(ValidatingObjectType object)
{
boolean result = defaultAllowing;
if (null != propertyIdsAndExpectedValues)
{
for (PropertyDescriptor descriptor : propertyIdsAndExpectedValues)
{
if ((null != descriptor) && (null != descriptor.getPropertyId()))
{
Serializable left = null;
try
{
if (object instanceof NodeRef)
{
left = getServiceRegistry().getCMISService().getProperty((NodeRef) object, descriptor.getPropertyId());
}
else
{
if (object instanceof AssociationRef)
{
left = getServiceRegistry().getCMISService().getProperty((AssociationRef) object, descriptor.getPropertyId());
}
else
{
return false;
}
}
}
catch (CMISInvalidArgumentException e)
{
throw new RuntimeException(e.toString(), e);
}
result = descriptor.satisfies(left);
if ((allPropertiesConcur && !result) || (!allPropertiesConcur && result))
{
break;
}
}
}
}
return result;
}
/**
* This class encapsulates description of object property to validate some actual object property against defined condition. This, in turn, allows determine, if some action is
* allowable for an object in accordance with value or values of the property or properties.
*
* N.B.:null
expected value is supported!
* The class introduces the following fields:
* - property definition id (subject to reference is {@link CMISDictionaryModel}; {@link PropertyDescriptor#getPropertyId()}); - expected property value
* {@link PropertyDescriptor#getPropertyValue()}; - may property be equal to null
{@link PropertyDescriptor#isNullExpected()}
*
* @author Dmitry Velichkevich
* @see CMISDictionaryModel
*/
public static class PropertyDescriptor
{
private String propertyId;
private Serializable propertyValue;
private boolean nullExpected;
/**
* Constructor
*
* @param propertyId - {@link String} value, which determines property definition id (subject to reference is {@link CMISDictionaryModel})
* @param propertyValue - {@link Serializable} instance, which specifies expected property value
* @param nullExpected - {@link Boolean} value, which determines: true
- property may be null
, false
- property can't be equal to
* null
(this leads to ignoring {@link PropertyDescriptor#getPropertyValue()} value)
*/
public PropertyDescriptor(String propertyId, Serializable propertyValue, boolean nullExpected)
{
this.propertyId = propertyId;
this.propertyValue = propertyValue;
this.nullExpected = nullExpected;
}
/**
* Getter
*
* @return {@link String} value, which represents one of the {@link CMISDictionaryModel} property definition ids
*/
public String getPropertyId()
{
return propertyId;
}
/**
* Getter
*
* @return {@link Serializable} instance, which specifies expected property value
*/
public Serializable getPropertyValue()
{
return propertyValue;
}
/**
* Getter
*
* @return {@link Boolean} value, which determines: true
- property may be null
, false
- property can't be equal to null
* (this leads to ignoring {@link PropertyDescriptor#getPropertyValue()} value)
*/
public boolean isNullExpected()
{
return nullExpected;
}
/**
* This method checks whether specified value
satisfies to all the defined conditions in current instance of {@link PropertyDescriptor}
*
* @param value - {@link Serializable} instance, which represents actual value of some object property
* @return {@link Boolean} value, which determines: true
- specified value
satisfies to all the defined conditions, false
- specified
* value
doesn't satisfy to all the defined conditions
*/
public boolean satisfies(Serializable value)
{
if (!nullExpected)
{
return null != value;
}
return (null != value) ? (value.equals(propertyValue)) : (null == propertyValue);
}
}
}