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.orm;
020
021import lucee.runtime.PageContext;
022import lucee.runtime.exp.PageException;
023import lucee.runtime.op.Caster;
024import lucee.runtime.op.Decision;
025import lucee.runtime.orm.ORMSession;
026import lucee.runtime.orm.ORMUtil;
027import lucee.runtime.type.Struct;
028import lucee.runtime.type.StructImpl;
029
030public class EntityLoad {
031        
032        public static Object call(PageContext pc, String name) throws PageException {
033                
034                ORMSession session=ORMUtil.getSession(pc);
035                return session.loadAsArray(pc,name,new StructImpl());
036        }
037        
038        public static Object call(PageContext pc, String name,Object idOrFilter) throws PageException {
039                return call(pc, name, idOrFilter, Boolean.FALSE);
040        }
041        public static Object call(PageContext pc, String name,Object idOrFilter, Object uniqueOrOptions) throws PageException {
042                ORMSession session=ORMUtil.getSession(pc);
043                
044                // id
045                if(Decision.isSimpleValue(idOrFilter)){
046                        // id,unique
047                        if(Decision.isCastableToBoolean(uniqueOrOptions)){
048                                // id,unique=true
049                                if(Caster.toBooleanValue(uniqueOrOptions))
050                                        return session.load(pc,name, Caster.toString(idOrFilter));
051                                // id,unique=false
052                                return session.loadAsArray(pc,name, Caster.toString(idOrFilter));
053                        }
054                        else if(Decision.isString(uniqueOrOptions)){
055                                return session.loadAsArray(pc,name,Caster.toString(idOrFilter),Caster.toString(uniqueOrOptions));
056                        }
057                        
058                        // id,options
059                        return session.loadAsArray(pc,name,Caster.toString(idOrFilter));
060                }
061                
062                // filter,[unique|sortorder]
063                if(Decision.isSimpleValue(uniqueOrOptions)){
064                        // filter,unique
065                        if(Decision.isBoolean(uniqueOrOptions)){
066                                if(Caster.toBooleanValue(uniqueOrOptions))
067                                        return session.load(pc,name,Caster.toStruct(idOrFilter));
068                                return session.loadAsArray(pc,name,Caster.toStruct(idOrFilter));
069                        }
070                        // filter,sortorder
071                        return session.loadAsArray(pc,name,Caster.toStruct(idOrFilter),(Struct)null,Caster.toString(uniqueOrOptions));
072                }
073                // filter,options
074                return session.loadAsArray(pc,name,Caster.toStruct(idOrFilter),Caster.toStruct(uniqueOrOptions));
075        }
076        
077        
078        
079        public static Object call(PageContext pc, String name,Object filter, Object order, Object options) throws PageException {
080                ORMSession session=ORMUtil.getSession(pc);
081                return session.loadAsArray(pc,name,Caster.toStruct(filter),Caster.toStruct(options),Caster.toString(order));
082        }
083}