001    package railo.runtime.orm;
002    
003    import java.util.HashSet;
004    
005    import railo.commons.lang.SystemOut;
006    import railo.runtime.Component;
007    import railo.runtime.ComponentPro;
008    import railo.runtime.PageContext;
009    import railo.runtime.PageContextImpl;
010    import railo.runtime.component.Property;
011    import railo.runtime.config.ConfigImpl;
012    import railo.runtime.exp.PageException;
013    import railo.runtime.op.Caster;
014    import railo.runtime.op.Decision;
015    import railo.runtime.op.Operator;
016    import railo.runtime.orm.hibernate.HBMCreator;
017    import railo.runtime.type.Collection;
018    import railo.runtime.type.Collection.Key;
019    import railo.runtime.type.KeyImpl;
020    import railo.runtime.type.util.ComponentUtil;
021    
022    public class ORMUtil {
023    
024            public static ORMSession getSession(PageContext pc) throws PageException {
025                    return getSession(pc,true);
026            }
027            
028            public static ORMSession getSession(PageContext pc, boolean create) throws PageException {
029                    return ((PageContextImpl) pc).getORMSession(create);
030            }
031    
032            public static ORMEngine getEngine(PageContext pc) throws PageException {
033                    ConfigImpl config=(ConfigImpl) pc.getConfig();
034                    return config.getORMEngine(pc);
035            }
036    
037            /**
038             * 
039             * @param pc
040             * @param force if set to false the engine is on loaded when the configuration has changed
041             * @throws PageException
042             */
043            public static void resetEngine(PageContext pc, boolean force) throws PageException {
044                    ConfigImpl config=(ConfigImpl) pc.getConfig();
045                    config.resetORMEngine(pc,force);
046            }
047            
048            public static void printError(Throwable t, ORMEngine engine) {
049                    printError(t, engine, t.getMessage());
050            }
051    
052            public static void printError(String msg, ORMEngine engine) {
053                    printError(null, engine, msg);
054            }
055    
056            private static void printError(Throwable t, ORMEngine engine,String msg) {
057                    SystemOut.printDate("{"+engine.getLabel().toUpperCase()+"} - "+msg,SystemOut.ERR);
058                    if(t==null)t=new Throwable();
059                    t.printStackTrace(SystemOut.getPrinWriter(SystemOut.ERR));
060            }
061    
062            public static boolean equals(Object left, Object right) {
063                    HashSet<Object> done=new HashSet<Object>();
064                    return _equals(done, left, right);
065            }
066            
067            private static boolean _equals(HashSet<Object> done,Object left, Object right) {
068                    
069                    if(left==right) return true;
070                    if(left==null || right==null) return false;
071                    
072                    // components
073                    if(left instanceof Component && right instanceof Component){
074                            return _equals(done,(Component)left, (Component)right);
075                    }
076    
077                    // arrays
078                    if(Decision.isArray(left) && Decision.isArray(right)){
079                            return _equals(done,Caster.toArray(left,null), Caster.toArray(right,null));
080                    }
081    
082                    // struct
083                    if(Decision.isStruct(left) && Decision.isStruct(right)){
084                            return _equals(done,Caster.toStruct(left,null), Caster.toStruct(right,null));
085                    }
086                    
087                    try {
088                            return Operator.equals(left,right,false);
089                    } catch (PageException e) {
090                            return false;
091                    }
092            }
093            
094            private static boolean _equals(HashSet<Object> done,Collection left, Collection right) {
095                    if(done.contains(left)) return done.contains(right);
096                    done.add(left);
097                    done.add(right);
098                    
099                    if(left.size()!=right.size()) return false;
100                    Key[] keys = left.keys();
101                    Object l,r;
102                    for(int i=0;i<keys.length;i++){
103                            l=left.get(keys[i],null);
104                            r=right.get(keys[i],null);
105                            if(r==null || !_equals(done,l, r)) return false;
106                    }
107                    return true;
108            }
109            
110            private static boolean _equals(HashSet<Object> done,Component left, Component right) {
111                    if(done.contains(left)) return done.contains(right);
112                    done.add(left);
113                    done.add(right);
114                    
115                    
116                    ComponentPro cpl =ComponentUtil.toComponentPro(left,null);
117                    ComponentPro cpr = ComponentUtil.toComponentPro(right,null);
118                    
119                    if(cpl==null || cpr==null) return false;
120                    if(!cpl.getPageSource().equals(cpr.getPageSource())) return false;
121                    Property[] props = cpl.getProperties(true);
122                    Object l,r;
123                    props=HBMCreator.getIds(null,null,props,null,true);
124                    for(int i=0;i<props.length;i++){
125                            l=cpl.getComponentScope().get(KeyImpl.getInstance(props[i].getName()),null);
126                            r=cpr.getComponentScope().get(KeyImpl.getInstance(props[i].getName()),null);
127                            if(!_equals(done,l, r)) return false;
128                    }
129                    return true;
130            }
131            
132            public static Object getPropertyValue(Component cfc, String name, Object defaultValue) {
133                    ComponentPro cp =ComponentUtil.toComponentPro(cfc,null);
134                    
135                    Property[] props = cp.getProperties(true);
136                    for(int i=0;i<props.length;i++){
137                            if(!props[i].getName().equalsIgnoreCase(name)) continue;
138                            return cp.getComponentScope().get(KeyImpl.getInstance(name),null);
139                    }
140                    return defaultValue;
141            }
142    }