1 24 25 package org.ofbiz.entity.datasource; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.entity.GenericEntityException; 38 import org.ofbiz.entity.GenericNotImplementedException; 39 import org.ofbiz.entity.GenericPK; 40 import org.ofbiz.entity.GenericValue; 41 import org.ofbiz.entity.condition.EntityCondition; 42 import org.ofbiz.entity.jdbc.SqlJdbcUtil; 43 import org.ofbiz.entity.model.ModelEntity; 44 import org.ofbiz.entity.model.ModelField; 45 import org.ofbiz.entity.model.ModelFieldTypeReader; 46 import org.ofbiz.entity.model.ModelRelation; 47 import org.ofbiz.entity.util.EntityFindOptions; 48 import org.ofbiz.entity.util.EntityListIterator; 49 50 57 public class MemoryHelper implements GenericHelper { 58 59 public static final String module = MemoryHelper.class.getName(); 60 private static Map cache = new HashMap (); 61 62 public static void clearCache() { 63 cache = new HashMap (); 64 } 65 66 private String helperName; 67 68 private boolean addToCache(GenericValue value) { 69 if (value == null) { 70 return false; 71 } 72 73 if (!veryifyValue(value)) { 74 return false; 75 } 76 77 value = (GenericValue) value.clone(); 78 HashMap entityCache = (HashMap ) cache.get(value.getEntityName()); 79 if (entityCache == null) { 80 entityCache = new HashMap (); 81 cache.put(value.getEntityName(), entityCache); 82 } 83 84 entityCache.put(value.getPrimaryKey(), value); 85 return true; 86 } 87 88 private GenericValue findFromCache(GenericPK pk) { 89 if (pk == null) { 90 return null; 91 } 92 93 HashMap entityCache = (HashMap ) cache.get(pk.getEntityName()); 94 if (entityCache == null) { 95 return null; 96 } 97 98 GenericValue value = (GenericValue) entityCache.get(pk); 99 if (value == null) { 100 return null; 101 } else { 102 return (GenericValue) value.clone(); 103 } 104 } 105 106 private int removeFromCache(GenericPK pk) { 107 if (pk == null) { 108 return 0; 109 } 110 111 HashMap entityCache = (HashMap ) cache.get(pk.getEntityName()); 112 if (entityCache == null) { 113 return 0; 114 } 115 116 Object o = entityCache.remove(pk); 117 if (o == null) { 118 return 0; 119 } else { 120 return 1; 121 } 122 } 123 124 private int removeFromCache(String entityName, EntityCondition condition) { 125 if (entityName == null || condition == null) { 126 return 0; 127 } 128 129 HashMap entityCache = (HashMap ) cache.get(entityName); 130 if (entityCache == null) { 131 return 0; 132 } 133 134 Iterator it = entityCache.values().iterator(); 135 int count = 0; 136 while (it.hasNext()) { 137 GenericValue value = (GenericValue) it.next(); 138 if (condition.entityMatches(value)) { 139 it.remove(); 140 count++; 141 } 142 } 143 return count; 144 } 145 146 private boolean isAndMatch(Map values, Map fields) { 147 for (Iterator iterator = fields.entrySet().iterator(); iterator.hasNext();) { 148 Map.Entry mapEntry = (Map.Entry ) iterator.next(); 149 if (mapEntry.getValue() == null) { 150 if (values.get(mapEntry.getKey()) != null) { 151 return false; 152 } 153 } else { 154 try { 155 if (!mapEntry.getValue().equals(values.get(mapEntry.getKey()))) { 156 return false; 157 } 158 } catch (Exception e) { 159 return false; 160 } 161 } 162 } 163 164 return true; 165 } 166 167 private boolean isOrMatch(Map values, Map fields) { 168 for (Iterator iterator = fields.entrySet().iterator(); iterator.hasNext();) { 169 Map.Entry mapEntry = (Map.Entry ) iterator.next(); 170 if (mapEntry.getValue() == null) { 171 if (values.get(mapEntry.getKey()) == null) { 172 return true; 173 } 174 } else { 175 try { 176 if (mapEntry.getValue().equals(values.get(mapEntry.getKey()))) { 177 return true; 178 } 179 } catch (Exception e) { 180 Debug.logError(e, module); 181 } 182 } 183 } 184 185 return false; 186 } 187 188 private boolean veryifyValue(GenericValue value) { 189 ModelEntity me = value.getModelEntity(); 190 191 for (Iterator iterator = me.getPksIterator(); iterator.hasNext();) { 193 ModelField field = (ModelField) iterator.next(); 194 if (!value.containsKey(field.getName())) { 195 return false; 196 } 197 } 198 199 for (Iterator iterator = value.entrySet().iterator(); iterator.hasNext();) { 201 Map.Entry entry = (Map.Entry ) iterator.next(); 202 if (me.getField((String ) entry.getKey()) == null) { 203 return false; 204 } 205 } 206 207 for (Iterator iterator = me.getFieldsIterator(); iterator.hasNext();) { 209 ModelField field = (ModelField) iterator.next(); 210 Object o = value.get(field.getName()); 211 int typeValue = 0; 212 try { 213 typeValue = SqlJdbcUtil.getType(modelFieldTypeReader.getModelFieldType(field.getType()).getJavaType()); 214 } catch (GenericNotImplementedException e) { 215 return false; 216 } 217 218 if (o != null) { 219 switch (typeValue) { 220 case 1: 221 if (!(o instanceof String )) { 222 return false; 223 } 224 break; 225 case 2: 226 if (!(o instanceof java.sql.Timestamp )) { 227 return false; 228 } 229 break; 230 231 case 3: 232 if (!(o instanceof java.sql.Time )) { 233 return false; 234 } 235 break; 236 237 case 4: 238 if (!(o instanceof java.sql.Date )) { 239 return false; 240 } 241 break; 242 243 case 5: 244 if (!(o instanceof Integer )) { 245 return false; 246 } 247 break; 248 249 case 6: 250 if (!(o instanceof Long )) { 251 return false; 252 } 253 break; 254 255 case 7: 256 if (!(o instanceof Float )) { 257 return false; 258 } 259 break; 260 261 case 8: 262 if (!(o instanceof Double )) { 263 return false; 264 } 265 break; 266 267 case 9: 268 if (!(o instanceof Boolean )) { 269 return false; 270 } 271 break; 272 } 273 } 274 } 275 276 return true; 277 } 278 279 private ModelFieldTypeReader modelFieldTypeReader; 280 281 public MemoryHelper(String helperName) { 282 this.helperName = helperName; 283 modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperName); 284 } 285 286 public String getHelperName() { 287 return helperName; 288 } 289 290 public GenericValue create(GenericValue value) throws GenericEntityException { 291 if (addToCache(value)) { 292 return value; 293 } else { 294 return null; 295 } 296 } 297 298 public GenericValue create(GenericPK primaryKey) throws GenericEntityException { 299 return create(GenericValue.create(primaryKey)); 300 } 301 302 public GenericValue findByPrimaryKey(GenericPK primaryKey) throws GenericEntityException { 303 return findFromCache(primaryKey); 304 } 305 306 public GenericValue findByPrimaryKeyPartial(GenericPK primaryKey, Set keys) throws GenericEntityException { 307 GenericValue value = findFromCache(primaryKey); 308 value.setFields(value.getFields(keys)); 309 return value; 310 } 311 312 public List findAllByPrimaryKeys(List primaryKeys) throws GenericEntityException { 313 ArrayList result = new ArrayList (primaryKeys.size()); 314 for (Iterator iterator = primaryKeys.iterator(); iterator.hasNext();) { 315 GenericPK pk = (GenericPK) iterator.next(); 316 result.add(this.findByPrimaryKey(pk)); 317 } 318 319 return result; 320 } 321 322 public int removeByPrimaryKey(GenericPK primaryKey) throws GenericEntityException { 323 return removeFromCache(primaryKey); 324 } 325 326 public List findByAnd(ModelEntity modelEntity, Map fields, List orderBy) throws GenericEntityException { 327 HashMap entityCache = (HashMap ) cache.get(modelEntity.getEntityName()); 328 if (entityCache == null) { 329 return Collections.EMPTY_LIST; 330 } 331 332 ArrayList result = new ArrayList (); 333 for (Iterator iterator = entityCache.entrySet().iterator(); iterator.hasNext();) { 334 Map.Entry mapEntry = (Map.Entry ) iterator.next(); 335 GenericValue value = (GenericValue) mapEntry.getValue(); 336 337 if (isAndMatch(value.getAllFields(), fields)) { 338 result.add(value); 339 } 340 } 341 342 return result; 343 } 344 345 public List findByAnd(ModelEntity modelEntity, List expressions, List orderBy) throws GenericEntityException { 346 return null; 347 } 348 349 public List findByLike(ModelEntity modelEntity, Map fields, List orderBy) throws GenericEntityException { 350 return null; 351 } 352 353 public List findByOr(ModelEntity modelEntity, Map fields, List orderBy) throws GenericEntityException { 354 HashMap entityCache = (HashMap ) cache.get(modelEntity.getEntityName()); 355 if (entityCache == null) { 356 return Collections.EMPTY_LIST; 357 } 358 359 ArrayList result = new ArrayList (); 360 for (Iterator iterator = entityCache.entrySet().iterator(); iterator.hasNext();) { 361 Map.Entry mapEntry = (Map.Entry ) iterator.next(); 362 GenericValue value = (GenericValue) mapEntry.getValue(); 363 364 if (isOrMatch(value.getAllFields(), fields)) { 365 result.add(value); 366 } 367 } 368 369 return result; 370 371 } 372 373 public List findByOr(ModelEntity modelEntity, List expressions, List orderBy) throws GenericEntityException { 374 return null; 375 } 376 377 public List findByCondition(ModelEntity modelEntity, EntityCondition entityCondition, 378 Collection fieldsToSelect, List orderBy) throws GenericEntityException { 379 return null; 380 } 381 382 public List findByMultiRelation(GenericValue value, ModelRelation modelRelationOne, ModelEntity modelEntityOne, 383 ModelRelation modelRelationTwo, ModelEntity modelEntityTwo, List orderBy) throws GenericEntityException { 384 return null; 385 } 386 387 public EntityListIterator findListIteratorByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, 388 EntityCondition havingEntityCondition, Collection fieldsToSelect, List orderBy, EntityFindOptions findOptions) 389 throws GenericEntityException { 390 return null; 391 } 392 393 public long findCountByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition) throws GenericEntityException { 394 return 0; 395 } 396 397 public int removeByAnd(ModelEntity modelEntity, Map fields) throws GenericEntityException { 398 HashMap entityCache = (HashMap ) cache.get(modelEntity.getEntityName()); 399 if (entityCache == null) { 400 return 0; 401 } 402 403 ArrayList removeList = new ArrayList (); 404 for (Iterator iterator = entityCache.entrySet().iterator(); iterator.hasNext();) { 405 Map.Entry mapEntry = (Map.Entry ) iterator.next(); 406 GenericValue value = (GenericValue) mapEntry.getValue(); 407 if (isAndMatch(value.getAllFields(), fields)) { 408 removeList.add(mapEntry.getKey()); 409 } 410 } 411 412 return removeAll(removeList); 413 } 414 415 public int removeByCondition(ModelEntity modelEntity, EntityCondition condition) throws GenericEntityException { 416 return removeFromCache(modelEntity.getEntityName(), condition); 417 } 418 419 public int storeByCondition(ModelEntity modelEntity, Map fieldsToSet, EntityCondition condition) throws GenericEntityException { 420 return 0; 421 } 422 423 public int store(GenericValue value) throws GenericEntityException { 424 if (addToCache(value)) { 425 return 1; 426 } else { 427 return 0; 428 } 429 } 430 431 public int storeAll(List values) throws GenericEntityException { 432 int count = 0; 433 for (Iterator iterator = values.iterator(); iterator.hasNext();) { 434 GenericValue gv = (GenericValue) iterator.next(); 435 if (addToCache(gv)) { 436 count++; 437 } 438 } 439 440 return count; 441 } 442 443 public int removeAll(List dummyPKs) throws GenericEntityException { 444 int count = 0; 445 for (Iterator iterator = dummyPKs.iterator(); iterator.hasNext();) { 446 GenericPK pk = (GenericPK) iterator.next(); 447 count = count + removeFromCache(pk); 448 } 449 450 return count; 451 } 452 453 public void checkDataSource(Map modelEntities, List messages, boolean addMissing) throws GenericEntityException { 454 messages.add("checkDataSource not implemented for MemoryHelper"); 455 } 456 } 457 | Popular Tags |