001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.other;
020
021import lucee.runtime.Component;
022import lucee.runtime.PageContext;
023import lucee.runtime.exp.ExpressionException;
024import lucee.runtime.exp.PageException;
025import lucee.runtime.functions.orm.EntityNew;
026import lucee.runtime.op.Caster;
027import lucee.runtime.op.Decision;
028import lucee.runtime.type.FunctionValue;
029import lucee.runtime.type.Struct;
030import lucee.runtime.type.UDF;
031import lucee.runtime.type.util.KeyConstants;
032
033public class _CreateComponent {
034        
035        private static final Object[] EMPTY = new Object[0]; 
036
037        public static Object call(PageContext pc , Object[] objArr) throws PageException {
038                String path = Caster.toString(objArr[objArr.length-1]);
039                Component cfc = CreateObject.doComponent(pc, path);
040                
041                // no init method
042                if(!(cfc.get(KeyConstants._init,null) instanceof UDF)){
043                        
044                        if(objArr.length>1) {
045                                Object arg1 = objArr[0];
046                                if(arg1 instanceof FunctionValue) {
047                                        Struct args=Caster.toFunctionValues(objArr,0,objArr.length-1);
048                                        EntityNew.setPropeties(pc, cfc, args,true);
049                                }
050                                else if(Decision.isStruct(arg1)){
051                                        Struct args=Caster.toStruct(arg1);
052                                        EntityNew.setPropeties(pc, cfc, args,true);
053                                }
054                        }
055                        
056                        return cfc;
057                }
058                
059                Object rtn;
060                // no arguments
061                if(objArr.length==1) {
062                        rtn = cfc.call(pc, KeyConstants._init, EMPTY);
063                }       
064                // named arguments
065                else if(objArr[0] instanceof FunctionValue) {
066                        Struct args=Caster.toFunctionValues(objArr,0,objArr.length-1);
067                        rtn = cfc.callWithNamedValues(pc, KeyConstants._init, args);
068                }
069                // no name arguments
070                else {
071                        Object[] args = new Object[objArr.length-1];
072                        for(int i=0;i<objArr.length-1;i++) {
073                                args[i]=objArr[i];
074                                if(args[i] instanceof FunctionValue) 
075                                        throw new ExpressionException("invalid argument defintion,when using named parameters to a function, every parameter must have a name.");
076                        }
077                        rtn = cfc.call(pc, KeyConstants._init, args);
078                }
079                if(rtn==null)return cfc;
080                return rtn;
081        }
082
083}