001    package railo.runtime.functions.other;
002    
003    import railo.runtime.Component;
004    import railo.runtime.PageContext;
005    import railo.runtime.exp.ExpressionException;
006    import railo.runtime.exp.PageException;
007    import railo.runtime.functions.orm.EntityNew;
008    import railo.runtime.op.Caster;
009    import railo.runtime.op.Decision;
010    import railo.runtime.type.FunctionValue;
011    import railo.runtime.type.Struct;
012    import railo.runtime.type.UDF;
013    import railo.runtime.type.util.KeyConstants;
014    
015    public class _CreateComponent {
016            
017            private static final Object[] EMPTY = new Object[0]; 
018    
019            public static Object call(PageContext pc , Object[] objArr) throws PageException {
020                    String path = Caster.toString(objArr[objArr.length-1]);
021                    Component cfc = CreateObject.doComponent(pc, path);
022                    
023                    // no init method
024                    if(!(cfc.get(KeyConstants._init,null) instanceof UDF)){
025                            
026                            if(objArr.length>1) {
027                                    Object arg1 = objArr[0];
028                                    if(arg1 instanceof FunctionValue) {
029                                            Struct args=Caster.toFunctionValues(objArr,0,objArr.length-1);
030                                            EntityNew.setPropeties(pc, cfc, args,true);
031                                    }
032                                    else if(Decision.isStruct(arg1)){
033                                            Struct args=Caster.toStruct(arg1);
034                                            EntityNew.setPropeties(pc, cfc, args,true);
035                                    }
036                            }
037                            
038                            return cfc;
039                    }
040                    
041                    Object rtn;
042                    // no arguments
043                    if(objArr.length==1) {
044                            rtn = cfc.call(pc, KeyConstants._init, EMPTY);
045                    }       
046                    // named arguments
047                    else if(objArr[0] instanceof FunctionValue) {
048                            Struct args=Caster.toFunctionValues(objArr,0,objArr.length-1);
049                            rtn = cfc.callWithNamedValues(pc, KeyConstants._init, args);
050                    }
051                    // no name arguments
052                    else {
053                            Object[] args = new Object[objArr.length-1];
054                            for(int i=0;i<objArr.length-1;i++) {
055                                    args[i]=objArr[i];
056                                    if(args[i] instanceof FunctionValue) 
057                                            throw new ExpressionException("invalid argument defintion,when using named parameters to a function, every parameter must have a name.");
058                            }
059                            rtn = cfc.call(pc, KeyConstants._init, args);
060                    }
061                    if(rtn==null)return cfc;
062                    return rtn;
063            }
064    
065    }