1 64 65 96 97 99 package com.jcorporate.expresso.services.controller.dbmaint; 100 101 import com.jcorporate.expresso.core.controller.session.HTTPPersistentSession; 102 import com.jcorporate.expresso.core.db.DBException; 103 import com.jcorporate.expresso.core.dbobj.DBObject; 104 import com.jcorporate.expresso.core.dbobj.ValidValue; 105 import com.jcorporate.expresso.core.misc.SerializableString; 106 import com.jcorporate.expresso.core.misc.StringUtil; 107 import org.apache.log4j.Logger; 108 109 import javax.servlet.http.HttpServletRequest ; 110 import java.io.IOException ; 111 import java.io.ObjectInputStream ; 112 import java.io.ObjectOutputStream ; 113 import java.io.Serializable ; 114 import java.lang.reflect.Method ; 115 import java.util.HashMap ; 116 import java.util.Iterator ; 117 import java.util.Vector ; 118 119 121 public class Lookup implements Serializable { 122 123 124 transient private static Logger log = Logger.getLogger(Lookup.class); 125 126 public static final String DBF_DBNAME = "dbf_dbname"; 127 public static final String DBF_DBKEY = "dbf_dbkey"; 128 public static final String DBF_DBDESC = "dbf_dbdesc"; 129 public static final String DBF_DB_FILTER_FNAME = "dbf_db_filter_fname"; 130 public static final String DBF_DB_FILTER_VALUE = "dbf_db_filter_value"; 131 public static final String DBF_DB_FILTER_WHERE = "dbf_db_filter_where"; 132 133 public static final String DBM_ACTION_KEY = "dbm_action"; 134 public static final String DBM_ACTION_SEARCH = "search"; 135 136 protected HashMap theFilter; 137 138 139 144 public Lookup() { 145 super(); 146 theFilter = new HashMap (); 147 } 148 149 156 public HashMap getTheFilter() { 157 return theFilter; 158 } 159 160 174 public void setFilter(String dbname, String dbkey, String dbdesc, 175 String db_filter_fname, String db_filter_value) { 176 theFilter.put(DBF_DBNAME, dbname); 177 theFilter.put(DBF_DBKEY, dbkey); 178 theFilter.put(DBF_DBDESC, dbdesc); 179 theFilter.put(DBF_DB_FILTER_FNAME, db_filter_fname); 180 theFilter.put(DBF_DB_FILTER_VALUE, db_filter_value); 181 } 182 183 196 public void setFilter(String dbname, String dbkey, String dbdesc, 197 String db_filter_where) { 198 theFilter.put(DBF_DBNAME, dbname); 199 theFilter.put(DBF_DBKEY, dbkey); 200 theFilter.put(DBF_DBDESC, dbdesc); 201 theFilter.put(DBF_DB_FILTER_WHERE, db_filter_where); 202 } 203 204 207 225 public static Vector getLookupValues 226 (DBObject myDBObj, String fieldName, HTTPPersistentSession mySession) 227 throws DBException { 228 229 Vector values = null; 230 String sessionString; 231 232 if (log.isDebugEnabled()) { 234 log.debug("*** getLookupValues entered"); 235 } 236 237 sessionString = myDBObj.getMetaData().getName().replace('.', '_') 238 + "_filter_" + fieldName; 239 241 HashMap dbFilter = null; 242 boolean getPersistentAttribute_bug_fixed; 243 244 getPersistentAttribute_bug_fixed = true; 245 getPersistentAttribute_bug_fixed = false; 246 247 if (getPersistentAttribute_bug_fixed) { 248 263 sessionString += "_"; 264 Object lookup_obj = mySession.getPersistentAttribute(sessionString); 265 Lookup lookup = (Lookup) lookup_obj; 266 dbFilter = lookup.getTheFilter(); 267 } else { 268 dbFilter = (HashMap ) mySession.getPersistentAttribute(sessionString); 269 } 270 271 if (dbFilter == null) { 273 values = myDBObj.getValidValues(fieldName); 275 } else { 276 values = new Vector (); 279 280 try { 281 Class c = Class.forName((String ) dbFilter.get(DBF_DBNAME)); 282 Object dbo = c.newInstance(); 283 284 try { 288 Class params[] = {java.lang.String .class}; 289 Method m = c.getMethod("setDBName", params); 290 if (m != null) { 291 Object paramValues[] = {myDBObj.getDataContext()}; 292 m.invoke(dbo, paramValues); 293 } 294 } catch (NoSuchMethodException nsme) { 295 } catch (SecurityException se) { 297 throw new DBException("Security Exception trying to call" 298 + " setDBName in loading lookup object", se); 299 } catch (java.lang.reflect.InvocationTargetException ive) { 300 ive.printStackTrace(); 301 throw new DBException("Error calling setDBName in " + 302 "loading lookup object", ive.getTargetException()); 303 } catch (IllegalAccessException ilae) { 304 } 306 307 DBObject dboList = (DBObject) dbo; 309 DBObject oneRecord = null; 310 String dbf_key; 312 String dbf_val; 313 dboList.setField((String ) dbFilter.get(DBF_DB_FILTER_FNAME), 315 (String ) dbFilter.get(DBF_DB_FILTER_VALUE)); 316 dbf_key = (String ) dbFilter.get(DBF_DBKEY); 317 dbf_val = (String ) dbFilter.get(DBF_DBDESC); 318 Iterator ii = dboList.searchAndRetrieveList().iterator(); 320 while (ii.hasNext()) { 321 oneRecord = (DBObject) ii.next(); 322 values.addElement(new 323 ValidValue(oneRecord.getField(dbf_key), 324 oneRecord.getField(dbf_val))); 325 } 326 } catch (ClassNotFoundException cn) { 327 throw new DBException("Lookup object not found", cn); 328 } catch (InstantiationException ie) { 329 throw new DBException("Lookup object cannot be instantiated", ie); 330 } catch (IllegalAccessException iae) { 331 throw new DBException("llegal access loading Lookup object", iae); 332 } 333 334 } 336 if (values == null) { 338 throw new DBException("Valid values for field " 339 + fieldName + " from object " 340 + myDBObj.getMetaData().getName() + " were null"); 341 } 342 343 sessionString = StringUtil.notNull 345 ((SerializableString) mySession.getPersistentAttribute(DBM_ACTION_KEY)); 346 if (DBM_ACTION_SEARCH.equalsIgnoreCase(sessionString)) { 347 values.add(new ValidValue("", "All Values")); 348 } 349 return values; 350 } 351 352 355 public String toString() { 356 return theFilter.toString(); 357 } 358 359 362 363 private void writeObject(ObjectOutputStream oos) throws IOException { 364 oos.defaultWriteObject(); 365 } 366 367 private void readObject(ObjectInputStream ois) throws IOException , 368 ClassNotFoundException { 369 ois.defaultReadObject(); 370 } 371 372 375 380 private void remove(HttpServletRequest request) { 381 } 383 } 384 385 386 387 | Popular Tags |