1 64 65 package com.jcorporate.expresso.core.dbobj; 66 67 import com.jcorporate.expresso.core.cache.CacheException; 68 import com.jcorporate.expresso.core.cache.CacheManager; 69 import com.jcorporate.expresso.core.cache.CacheSystem; 70 import com.jcorporate.expresso.core.dataobjects.DataField; 71 import com.jcorporate.expresso.core.dataobjects.DefaultDataField; 72 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData; 73 import com.jcorporate.expresso.core.db.DBException; 74 import org.apache.log4j.Logger; 75 76 import java.util.Iterator ; 77 78 79 84 85 public class CacheUtils { 86 87 private static final Logger log = Logger.getLogger(CacheUtils.class); 88 89 public CacheUtils() { 90 } 91 92 93 101 public void addUnmodifiedToCache(DBObject theDBObj) throws DBException { 102 JDBCObjectMetaData metadata = theDBObj.getJDBCMetaData(); 103 String theClassName = theDBObj.getClass().getName(); 104 105 if (metadata.getKeyFieldListArray().size() == 0) { 109 return; 110 } 111 112 115 int cacheSize = theDBObj.getCacheSize(); 116 if (cacheSize == 0) { 117 return; 118 } 119 120 CacheSystem cs = CacheManager.getCacheSystem(theDBObj.getDataContext()); 121 if (cs == null) { 122 return; 123 } 124 try { 125 prepareCache(cs, cacheSize, theClassName); 126 DBObject newObj = prepareForStorage(theDBObj, metadata); 127 128 129 Integer i = (Integer ) theDBObj.getAttribute("TTL"); 130 if (i == null || i.intValue() == 0) { 131 cs.put(theClassName, newObj); 132 } else { 133 cs.put(theClassName, newObj, 135 i.intValue() * 1000 * 60); 136 } 137 138 139 if (log.isDebugEnabled()) { 140 log.debug("Adding item " + theDBObj.getKey() + 141 " to cache for " + theClassName); 142 } 143 } catch (CacheException ce) { 144 throw new DBException(ce); 145 } 146 147 } 148 149 150 160 public void addToCache(DBObject theDBObj) 161 throws DBException { 162 JDBCObjectMetaData metadata = theDBObj.getJDBCMetaData(); 163 String theClassName = theDBObj.getClass().getName(); 164 165 if (metadata.getKeyFieldListArray().size() == 0) { 169 return; 170 } 171 172 175 int cacheSize = theDBObj.getCacheSize(); 176 if (cacheSize == 0) { 177 return; 178 } 179 180 CacheSystem cs = CacheManager.getCacheSystem(theDBObj.getDataContext()); 181 if (cs == null) { 182 return; 183 } 184 try { 185 prepareCache(cs, cacheSize, theClassName); 186 DBObject newObj = prepareForStorage(theDBObj, metadata); 187 188 189 Integer i = (Integer ) theDBObj.getAttribute("TTL"); 190 if (i == null || i.intValue() == 0) { 191 cs.addItem(theClassName, newObj); 192 } else { 193 cs.addItem(theClassName, newObj, 195 i.intValue() * 1000 * 60); 196 } 197 198 199 if (log.isDebugEnabled()) { 200 log.debug("Adding item " + theDBObj.getKey() + 201 " to cache for " + theClassName); 202 } 203 } catch (CacheException ce) { 204 throw new DBException(ce); 205 } 206 } 207 208 216 private void prepareCache(CacheSystem cs, int cacheSize, String cacheName) throws CacheException { 217 if (!cs.existsCache(cacheName)) { 218 cs.createCache(cacheName, false, 219 cacheSize); 220 } 221 222 } 223 224 232 private DBObject prepareForStorage(DBObject theDBObj, 233 JDBCObjectMetaData metadata) 234 throws DBException { 235 236 DBObject newObj = theDBObj.newInstance(); 237 newObj.setDataContext(theDBObj.getDataContext()); 238 239 DBField oneField = null; 240 241 for (Iterator i = metadata.getAllFieldsMap().values().iterator(); 242 i.hasNext();) { 243 oneField = (DBField) i.next(); 244 245 if (!oneField.isVirtual() && !oneField.isBinaryObjectType()) { 246 DataField df = DefaultDataField.getInstance(oneField, newObj); 247 df.setValue(theDBObj.getDataField(oneField.getName()).getValue()); 248 newObj.setDataField(oneField.getName(), df); 249 } 250 } 251 return newObj; 252 } 253 } | Popular Tags |