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