//Source file: H:\\temp\\generated\\modTransf\\rules\\core\\CollectionDescriptor.java
package modTransf.rules.core;
import modTransf.engine.RuleContext;
import modTransf.engine.TransformationException;
import modTransf.model.ModelException;
import modTransf.engine.EngineException;
import java.util.Collection;
import modTransf.util.Filter;
import modTransf.util.UnmodifiableFilteredCollection;
/**
* Describe a collection.
* @todo Add guards methods.
*/
public class CollectionDescriptor extends TemplateDescriptorBase
{
public CollectionDescriptor()
{
}
/**
* @param parentBean
* @param request
* @return Object
* @throws modTransf.engine.TransformationException
*/
public Object getPropertyValue(Object parentBean, RuleContext request)
throws TransformationException
{
try
{
return request.getTransformation().getModels().getProperty(parentBean, propertyName);
}
catch(ModelException ex)
{
throw new TransformationException(ex);
}
}
/**
* Set the value of the property denoted by this descriptor.
* The value is set only if it is of the requested type.
* If the value is a collection, add all elements matching the requested type.
* If the value is a single concept, add it if it is of the requested type.
* @param parentBean
* @param value
* @param request
* @throws modTransf.engine.TransformationException
*/
public void setPropertyValue(Object parentBean, Object value, RuleContext request)
throws TransformationException
{
//System.out.println("CollectionDescriptor.setPropertyValuexx(" + value + ")" );
try
{
Collection col = (Collection)getDomain().getModelHelper(request).getProperty(parentBean, propertyName);
//System.out.println("CollectionDescriptor.setPropertyValue2(" + value + ")" );
if( value instanceof Collection )
{
if( type != null )
// Check the type before adding
col.addAll( new UnmodifiableFilteredCollection( (Collection)value, new TypeFilter(request) ) );
else
col.addAll( (Collection)value);
}
else
{
// Check the type
if( ! isOfSpecifiedType(value, request) )
return;
col.add(value);
}
}
catch(ClassCastException ex)
{
ex.printStackTrace();
throw new TransformationException("Property '" + propertyName +"' should denote a collection", ex);
}
catch(ModelException ex)
{
ex.printStackTrace();
throw new TransformationException(ex);
}
catch(Exception ex)
{
ex.printStackTrace();
throw new TransformationException(ex);
}
}
/**
* create
*
* @param request RuleContext
* @return Object
*/
public Object create(RuleContext request)
throws TransformationException
{
// nothing to create for a collection
return null;
}
/**
* Execute the initialization from
* @param parentBean Object
* @param request RuleContext
*/
public void executePostAction(Object parentBean, RuleContext request)
throws TransformationException
{
//System.out.println("CollectionDescriptor.executePostAction()");
// Get the local value if defined
Object value = getLocalValue(request);
// Create from initExpr if any
if( initValueAction != null )
value = initValueAction.execute(null, request);
// If a value is found, add it to the collection
if( value != null )
{
// Create the concept if needed
setPropertyValue( parentBean, value, request);
}
super.executePostAction(parentBean, request);
}
/**
* @param context
*/
public void engineStart(RuleContext context)
throws EngineException
{
//System.out.println( "AbstractConceptDescriptor.engineStart() propertyDescriptors=" + propertyDescriptors );
super.engineStart(context);
// Set the various guard and actions.
// setVar
if( varName != null)
{
//System.out.println( "Register setUpVarName action for '" + varName + "'" );
addSetUpLocalVarAction(new SetUpVarName());
}
}
/**
* Nested class.
*/
protected class SetUpVarName extends EngineLifeCycleBase implements Action
{
/**
* execute
*
* @param request RuleContext
*/
public Object execute(Object bean, RuleContext request)
throws TransformationException
{
Object value = getPropertyValue(bean, request);
//System.out.println( "localContext <-- bean." + propertyName + "='" + value +"'" );
setLocalValue(value, request);
return null;
}
}
protected class TypeFilter implements Filter
{
RuleContext context;
TypeFilter( RuleContext currentContext )
{
this.context = currentContext ;
}
/**
* isAllowed
*
* @param object Object
* @return boolean
*/
public boolean isAllowed(Object object)
{
try
{
System.out.println("check " + object.getClass().getName() );
return isOfSpecifiedType(object, context);
}
catch(ModelException ex)
{
if( ruleLog.isWarnEnabled() )
ruleLog.warn("Can't filter collection", ex);
return false;
}
}
}
}