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 021 022import java.util.Iterator; 023import java.util.Map.Entry; 024 025import lucee.commons.lang.ExceptionUtil; 026import lucee.runtime.Component; 027import lucee.runtime.PageContext; 028import lucee.runtime.exp.PageException; 029import lucee.runtime.op.Caster; 030import lucee.runtime.orm.ORMSession; 031import lucee.runtime.orm.ORMUtil; 032import lucee.runtime.type.Collection.Key; 033import lucee.runtime.type.Struct; 034import lucee.runtime.type.util.KeyConstants; 035 036public class EntityNew { 037 038 public static Object call(PageContext pc, String name) throws PageException { 039 return call(pc, name, null); 040 } 041 042 public static Object call(PageContext pc, String name,Struct properties) throws PageException { 043 ORMSession session=ORMUtil.getSession(pc); 044 if(properties==null)return session.create(pc,name); 045 046 Component entity = session.create(pc,name); 047 setPropeties(pc,entity,properties,false); 048 return entity; 049 050 } 051 052 public static void setPropeties(PageContext pc, Component cfc, Struct properties, boolean ignoreNotExisting) throws PageException { 053 if(properties==null) return; 054 055 // argumentCollection 056 if(properties.size()==1 && properties.containsKey(KeyConstants._argumentCollection) && !cfc.containsKey(KeyConstants._setArgumentCollection)) { 057 properties=Caster.toStruct(properties.get(KeyConstants._argumentCollection)); 058 } 059 060 Iterator<Entry<Key, Object>> it = properties.entryIterator(); 061 Entry<Key, Object> e; 062 while(it.hasNext()){ 063 e = it.next(); 064 if(ignoreNotExisting) { 065 try { 066 cfc.call(pc, "set"+e.getKey().getString(), new Object[]{e.getValue()}); 067 } 068 catch(Throwable t){ 069 ExceptionUtil.rethrowIfNecessary(t); 070 } 071 } 072 else { 073 cfc.call(pc, "set"+e.getKey().getString(), new Object[]{e.getValue()}); 074 } 075 } 076 } 077}