1 26 package org.objectweb.speedo.query.lib; 27 28 import java.lang.reflect.Constructor ; 29 30 import javax.jdo.JDOUserException; 31 import javax.jdo.PersistenceManager; 32 33 import org.objectweb.jorm.naming.api.PName; 34 import org.objectweb.medor.api.MedorException; 35 import org.objectweb.medor.tuple.api.Tuple; 36 import org.objectweb.medor.tuple.api.TupleCollection; 37 import org.objectweb.perseus.persistence.api.ConnectionHolder; 38 import org.objectweb.perseus.persistence.api.PersistenceException; 39 import org.objectweb.perseus.persistence.api.WorkingSet; 40 import org.objectweb.speedo.api.SpeedoException; 41 import org.objectweb.speedo.workingset.api.Transaction; 42 import org.objectweb.util.monolog.api.BasicLevel; 43 import org.objectweb.util.monolog.api.Logger; 44 45 51 public class QueryResultCommon { 52 55 protected TupleCollection tc = null; 56 57 61 protected Object [] conns = null; 62 63 67 protected PersistenceManager pm = null; 68 69 protected Logger logger; 70 71 protected boolean debug; 72 73 77 private Class resultClass; 78 79 83 private boolean firstIsResult; 84 85 88 private Class [] selectedFieldTypes; 89 90 94 private Constructor cons; 95 96 102 private int firstElementIdx; 103 104 112 public QueryResultCommon(TupleCollection _tc, 113 PersistenceManager _pm, 114 Object [] _conns, 115 Class _resultClazz, 116 Class [] _selectedFieldTypes, 117 boolean staticFirstElementIndex, 118 Logger _logger) 119 throws MedorException, SpeedoException { 120 this.tc = _tc; 121 this.pm = _pm; 122 this.conns = _conns; 123 this.resultClass = _resultClazz; 124 this.selectedFieldTypes = _selectedFieldTypes; 125 this.logger = _logger; 126 this.firstElementIdx = 0; 127 128 debug = logger != null && logger.isLoggable(BasicLevel.DEBUG); 129 if (pm == null || pm.isClosed() || ((Transaction) pm.currentTransaction()).getStatus() 130 == WorkingSet.CTX_CLOSED) { 131 throw new JDOUserException( 132 "Impossible to use a query result without opened persistence manager"); 133 } 134 firstIsResult = selectedFieldTypes.length == 1 && (resultClass == null || resultClass == selectedFieldTypes[0] ); 138 139 if (staticFirstElementIndex) { 140 this.firstElementIdx = getFirstElementIndex(); 142 if (debug) { 143 logger.log(BasicLevel.DEBUG, "index of the pname: " + firstElementIdx); 144 } 145 } 147 if (resultClass != null) { 148 try { 149 cons = resultClass.getConstructor(selectedFieldTypes); 150 } catch (Exception e) { 151 StringBuffer sb = new StringBuffer (); 152 sb.append("No consutructor '"); 153 sb.append(resultClass.getName()); 154 sb.append("("); 155 String sep = ""; 156 for (int i = 0; i < selectedFieldTypes.length; i++) { 157 Class c = selectedFieldTypes[i]; 158 sb.append(sep); 159 sep = ", "; 160 sb.append(c == null ? "null" : selectedFieldTypes[i].getName()); 161 } 162 sb.append(")' found. This constructor is required in a query result. Be carefull to not use non static inner class."); 163 throw new JDOUserException(sb.toString(), e); 164 } 165 } 166 } 167 168 172 public void close() { 173 pm = null; 174 if (tc != null) { 175 try { 176 tc.close(); 177 } catch (MedorException e) { 178 logger.log(BasicLevel.WARN, "Error during the connection closing: ", e); 179 } 180 tc = null; 181 } 182 if (conns != null) { 183 for(int i=0; i<conns.length; i++) { 184 closeConnection(conns[i]); 185 } 186 } 187 conns = null; 188 } 189 190 public static void closeConnection(Object conn) { 191 if (conn instanceof ConnectionHolder) { 192 try { 193 ((ConnectionHolder) conn).releaseCHConnection(); 194 } catch (PersistenceException e) { 195 } 196 } 197 } 198 199 204 private int getFirstElementIndex() throws MedorException { 205 if (firstElementIdx == 0) { 206 return tc.getMetaData().getSize() - selectedFieldTypes.length + 1; 207 } else { 208 return firstElementIdx; 209 } 210 } 211 212 218 protected Object pname2Object(PName pn) { 219 if (pn != null && !pn.isNull()) { 220 try { 221 return pm.getObjectById(pn, false); 222 } catch (JDOUserException e) { 223 if (debug) { 224 logger.log(BasicLevel.DEBUG, 225 "Ignore a deleted object, oid=" + pn, e); 226 } 227 } catch (Exception e) { 230 logger.log(BasicLevel.WARN, "Unexpected exception", e); 231 } 232 } 233 return null; 234 } 235 236 244 protected Object getValue(Tuple tuple) throws MedorException { 245 if (firstIsResult) { 246 Object o = tuple.getObject(getFirstElementIndex()); 248 if (o instanceof PName) { 249 return pname2Object((PName) o); 252 } else { 253 return o; 254 } 255 } else { 256 259 Object [] values = new Object [selectedFieldTypes.length]; 261 for (int i = 0; i < values.length; i++) { 262 values[i] = tuple.getObject(getFirstElementIndex() + i); 263 if (values[i] instanceof PName) { 264 values[i] = pname2Object((PName) values[i]); 267 } 268 } 269 if (resultClass == null) { 270 return values; 273 } 274 try { 275 return cons.newInstance(values); 277 } catch (Exception e) { 278 StringBuffer sb = new StringBuffer (); 279 sb.append("Class '"); 280 sb.append(resultClass.getName()); 281 sb.append("' used as query result is compatible with the following result: ["); 282 String sep = ""; 283 for (int i = 0; i < selectedFieldTypes.length; i++) { 284 sb.append(sep); 285 sep = ", "; 286 sb.append(values[i]); 287 } 288 sb.append("]"); 289 throw new JDOUserException(sb.toString()); 290 } 291 } 292 } 293 } 294 | Popular Tags |