001     package railo.runtime.type;
002    
003    import railo.commons.lang.CFTypes;
004    import railo.commons.lang.StringUtil;
005    import railo.runtime.ComponentImpl;
006    import railo.runtime.PageContext;
007    import railo.runtime.component.Property;
008    import railo.runtime.exp.ExpressionException;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.op.Caster;
011    import railo.runtime.op.Decision;
012    import railo.runtime.orm.hibernate.HBMCreator;
013    import railo.runtime.type.Collection.Key;
014    import railo.runtime.type.util.CollectionUtil;
015    import railo.runtime.type.util.KeyConstants;
016    
017    public final class UDFSetterProperty extends UDFGSProperty {
018    
019            private static final Collection.Key VALIDATE_PARAMS = KeyImpl.intern("validateParams");
020            private final Property prop;
021            private final Key propName;
022            private String validate;
023            private Struct validateParams;
024    
025            public UDFSetterProperty(ComponentImpl component,Property prop) throws PageException {
026                    super(component,"set"+StringUtil.ucFirst(prop.getName()),new FunctionArgument[]{
027                            new FunctionArgumentImpl(
028                                            KeyImpl.init(prop.getName()),
029                                            prop.getType(),
030                                            CFTypes.toShortStrict(prop.getType(),CFTypes.TYPE_UNKNOW),
031                                            true)
032                    },CFTypes.TYPE_ANY,"wddx");
033                    
034                    
035                    this.prop=prop; 
036                    this.propName=KeyImpl.getInstance(prop.getName());
037                    
038                    this.validate=Caster.toString(prop.getDynamicAttributes().get(KeyConstants._validate,null),null);
039                    if(!StringUtil.isEmpty(validate,true)) {
040                            validate=validate.trim().toLowerCase();
041                            Object o = prop.getDynamicAttributes().get(VALIDATE_PARAMS,null);
042                            if(o!=null){
043                                    if(Decision.isStruct(o))validateParams=Caster.toStruct(o);
044                                    else {
045                                            String str=Caster.toString(o);
046                                            if(!StringUtil.isEmpty(str,true)) {
047                                                    validateParams=HBMCreator.convertToSimpleMap(str);
048                                                    if(validateParams==null)
049                                                            throw new ExpressionException("cannot parse string ["+str+"] as struct");
050                                            }
051                                    }
052                            }
053                    }
054            } 
055    
056            @Override
057            public UDF duplicate(ComponentImpl c) {
058                    try {
059                            return new UDFSetterProperty(c,prop);
060                    } catch (PageException e) {
061                            return null;
062                    }
063            }
064    
065            
066    
067            public UDF duplicate() {
068                    return duplicate(component);
069            }
070            @Override
071            public Object call(PageContext pageContext, Object[] args,boolean doIncludePath) throws PageException {
072                    if(args.length<1)
073                            throw new ExpressionException("The parameter "+prop.getName()+" to function "+getFunctionName()+" is required but was not passed in.");
074                    validate(validate,validateParams,args[0]);
075                    component.getComponentScope().set(propName, cast(this.arguments[0],args[0],1));
076                    return component;
077            }
078    
079            @Override
080            public Object callWithNamedValues(PageContext pageContext, Struct values,boolean doIncludePath) throws PageException {
081                    UDFImpl.argumentCollection(values,getFunctionArguments());
082                    Object value = values.get(propName,null);
083                    
084                    if(value==null){
085                            Key[] keys = CollectionUtil.keys(values);
086                            if(keys.length==1) {
087                                    value=values.get(keys[0]);
088                            }
089                            else throw new ExpressionException("The parameter "+prop.getName()+" to function "+getFunctionName()+" is required but was not passed in.");
090                    }
091                    component.getComponentScope().set(propName, cast(arguments[0],value,1));
092                    return component;
093            }
094    
095            @Override
096            public Object getDefaultValue(PageContext pc, int index) throws PageException {
097                    return prop.getDefault();
098            }
099            
100            @Override
101            public Object getDefaultValue(PageContext pc, int index, Object defaultValue) throws PageException {
102                    return prop.getDefault();
103            }
104    
105            @Override
106            public String getReturnTypeAsString() {
107                    return "any";
108            }
109    
110            @Override
111            public Object implementation(PageContext pageContext) throws Throwable {
112                    return null;
113            }
114    
115    }