001    package railo.runtime.functions.orm;
002    
003    
004    import java.util.Iterator;
005    import java.util.Map.Entry;
006    
007    import railo.runtime.Component;
008    import railo.runtime.PageContext;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.op.Caster;
011    import railo.runtime.orm.ORMSession;
012    import railo.runtime.orm.ORMUtil;
013    import railo.runtime.type.Collection.Key;
014    import railo.runtime.type.Struct;
015    import railo.runtime.type.util.KeyConstants;
016    
017    public class EntityNew {
018    
019            public static Object call(PageContext pc, String name) throws PageException {
020                    return call(pc, name, null);
021            }
022            
023            public static Object call(PageContext pc, String name,Struct properties) throws PageException {
024                    ORMSession session=ORMUtil.getSession(pc);
025                    if(properties==null)return session.create(pc,name);
026                    
027                    Component entity = session.create(pc,name);
028                    setPropeties(pc,entity,properties,false);
029                    return entity;
030                    
031            }
032    
033            public static void setPropeties(PageContext pc, Component cfc, Struct properties, boolean ignoreNotExisting) throws PageException { 
034                    if(properties==null) return;
035                    
036                    // argumentCollection
037                    if(properties.size()==1 && properties.containsKey(KeyConstants._argumentCollection) && !cfc.containsKey(KeyConstants._setArgumentCollection)) {
038                            properties=Caster.toStruct(properties.get(KeyConstants._argumentCollection));
039                    }
040                    
041                    Iterator<Entry<Key, Object>> it = properties.entryIterator();
042                    Entry<Key, Object> e;
043                    while(it.hasNext()){
044                            e = it.next();
045                            if(ignoreNotExisting) {
046                                    try {
047                                            cfc.call(pc, "set"+e.getKey().getString(), new Object[]{e.getValue()});
048                                    }
049                                    catch(Throwable t){}
050                            }
051                            else {
052                                    cfc.call(pc, "set"+e.getKey().getString(), new Object[]{e.getValue()});
053                            }
054                    }
055            }
056    }