1 64 65 package com.jcorporate.expresso.core.dbobj; 66 67 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData; 68 import com.jcorporate.expresso.core.db.DBException; 69 import com.jcorporate.expresso.core.misc.ConfigManager; 70 import com.jcorporate.expresso.core.misc.StringUtil; 71 import com.jcorporate.expresso.kernel.util.ClassLocator; 72 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 73 import org.apache.log4j.Logger; 74 75 76 85 abstract public class NextNumber { 86 public static final String DEFAULT_CLASS_HANDLER = NextNumberImpl.class.getName(); 87 88 93 protected static final boolean CHECK_PARAMETERS = true; 94 95 98 protected static Logger log = Logger.getLogger(NextNumber.class); 99 100 103 protected static NextNumber theInstance = null; 104 105 108 protected NextNumber() { 109 } 110 111 116 public static synchronized NextNumber getInstance() 117 throws DBException { 118 if (theInstance == null) { 119 if (theInstance == null) { 121 String classHandler = null; 122 classHandler = ConfigManager.getClassHandler("nextNumber"); 123 124 if (classHandler == null) { 125 classHandler = DEFAULT_CLASS_HANDLER; 126 } 127 try { 128 theInstance = (NextNumber) ClassLocator.loadClass(classHandler).newInstance(); 129 } catch (ClassNotFoundException cnfe) { 130 log.error("Class Not Found: " + classHandler, cnfe); 131 throw new DBException(cnfe); 132 } catch (IllegalAccessException iae) { 133 log.error("Class Not Found: " + classHandler, iae); 134 throw new DBException(iae); 135 } catch (InstantiationException ine) { 136 log.error("Class Not Found: " + classHandler, ine); 137 throw new DBException(ine); 138 } 139 } 140 } 141 return theInstance; 142 } 143 144 153 protected long getMax(String db, DBObject callingObject, String oneField) 154 throws DBException { 155 callingObject.setDataContext(db); 156 157 String value = callingObject.getMax(oneField); 158 long lo; 159 160 if (StringUtil.notNull(value).length() == 0) { 161 return 0; 162 } 163 try { 164 lo = Long.parseLong(value); 165 } catch (NumberFormatException nfe) { 166 throw new DBException("Error converting max values for field: " + 167 oneField, nfe); 168 } 169 170 return (lo); } 172 173 181 abstract public void registerField(String db, DBObject callingDBObject, 182 String fieldName) 183 throws DBException; 184 185 193 abstract public long getNext(String db, DBObject callingDBObject, 194 String fieldName) 195 throws DBException; 196 197 205 abstract public void reset(String db); 206 207 214 abstract public void reset(String db, DBObject callingObject); 215 216 222 public static synchronized void destroy() { 223 if (theInstance == null) { 224 return; 225 } else { 226 227 theInstance = null; 229 } 230 } 231 232 243 protected String getKey(DBObject callingDBObject, String fieldName) 244 throws DBException { 245 FastStringBuffer result = FastStringBuffer.getInstance(); 246 String returnValue = null; 247 try { 248 result.append(((JDBCObjectMetaData) callingDBObject.getMetaData()).getTargetTable()); 249 result.append("."); 250 result.append(fieldName); 251 returnValue = result.toString(); 252 } finally { 253 result.release(); 254 result = null; 255 } 256 257 return returnValue; 258 } 259 260 } 261 | Popular Tags |