KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > datasource > MemoryHelper


1 /*
2  * $Id: MemoryHelper.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 package org.ofbiz.entity.datasource;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
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 /**
51  * Partial GenericHelper implementation that is entirely memory-based,
52  * to be used for simple unit testing (can't do anything beyond searches
53  * for primary keys, findByOr and findByAnd).
54  *
55  * @author <a HREF="mailto:plightbo@.com">Pat Lightbody</a>
56  */

57 public class MemoryHelper implements GenericHelper {
58
59     public static final String JavaDoc module = MemoryHelper.class.getName();
60     private static Map JavaDoc cache = new HashMap JavaDoc();
61
62     public static void clearCache() {
63         cache = new HashMap JavaDoc();
64     }
65
66     private String JavaDoc 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 JavaDoc entityCache = (HashMap JavaDoc) cache.get(value.getEntityName());
79         if (entityCache == null) {
80             entityCache = new HashMap JavaDoc();
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 JavaDoc entityCache = (HashMap JavaDoc) 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 JavaDoc entityCache = (HashMap JavaDoc) cache.get(pk.getEntityName());
112         if (entityCache == null) {
113             return 0;
114         }
115
116         Object JavaDoc o = entityCache.remove(pk);
117         if (o == null) {
118             return 0;
119         } else {
120             return 1;
121         }
122     }
123
124     private int removeFromCache(String JavaDoc entityName, EntityCondition condition) {
125         if (entityName == null || condition == null) {
126             return 0;
127         }
128
129         HashMap JavaDoc entityCache = (HashMap JavaDoc) cache.get(entityName);
130         if (entityCache == null) {
131             return 0;
132         }
133
134         Iterator JavaDoc 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 JavaDoc values, Map JavaDoc fields) {
147         for (Iterator JavaDoc iterator = fields.entrySet().iterator(); iterator.hasNext();) {
148             Map.Entry JavaDoc mapEntry = (Map.Entry JavaDoc) 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 JavaDoc e) {
159                     return false;
160                 }
161             }
162         }
163
164         return true;
165     }
166
167     private boolean isOrMatch(Map JavaDoc values, Map JavaDoc fields) {
168         for (Iterator JavaDoc iterator = fields.entrySet().iterator(); iterator.hasNext();) {
169             Map.Entry JavaDoc mapEntry = (Map.Entry JavaDoc) 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 JavaDoc 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         // make sure the PKs exist
192
for (Iterator JavaDoc iterator = me.getPksIterator(); iterator.hasNext();) {
193             ModelField field = (ModelField) iterator.next();
194             if (!value.containsKey(field.getName())) {
195                 return false;
196             }
197         }
198
199         // make sure the value doesn't have any extra (unknown) fields
200
for (Iterator JavaDoc iterator = value.entrySet().iterator(); iterator.hasNext();) {
201             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
202             if (me.getField((String JavaDoc) entry.getKey()) == null) {
203                 return false;
204             }
205         }
206
207         // make sure all fields that are in the value are of the right type
208
for (Iterator JavaDoc iterator = me.getFieldsIterator(); iterator.hasNext();) {
209             ModelField field = (ModelField) iterator.next();
210             Object JavaDoc 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 JavaDoc)) {
222                             return false;
223                         }
224                         break;
225                     case 2:
226                         if (!(o instanceof java.sql.Timestamp JavaDoc)) {
227                             return false;
228                         }
229                         break;
230
231                     case 3:
232                         if (!(o instanceof java.sql.Time JavaDoc)) {
233                             return false;
234                         }
235                         break;
236
237                     case 4:
238                         if (!(o instanceof java.sql.Date JavaDoc)) {
239                             return false;
240                         }
241                         break;
242
243                     case 5:
244                         if (!(o instanceof Integer JavaDoc)) {
245                             return false;
246                         }
247                         break;
248
249                     case 6:
250                         if (!(o instanceof Long JavaDoc)) {
251                             return false;
252                         }
253                         break;
254
255                     case 7:
256                         if (!(o instanceof Float JavaDoc)) {
257                             return false;
258                         }
259                         break;
260
261                     case 8:
262                         if (!(o instanceof Double JavaDoc)) {
263                             return false;
264                         }
265                         break;
266
267                     case 9:
268                         if (!(o instanceof Boolean JavaDoc)) {
269                             return false;
270                         }
271                         break;
272                 }
273             }
274         }
275
276         return true;
277     }
278
279     private ModelFieldTypeReader modelFieldTypeReader;
280
281     public MemoryHelper(String JavaDoc helperName) {
282         this.helperName = helperName;
283         modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperName);
284     }
285
286     public String JavaDoc 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 JavaDoc keys) throws GenericEntityException {
307         GenericValue value = findFromCache(primaryKey);
308         value.setFields(value.getFields(keys));
309         return value;
310     }
311
312     public List JavaDoc findAllByPrimaryKeys(List JavaDoc primaryKeys) throws GenericEntityException {
313         ArrayList JavaDoc result = new ArrayList JavaDoc(primaryKeys.size());
314         for (Iterator JavaDoc 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 JavaDoc findByAnd(ModelEntity modelEntity, Map JavaDoc fields, List JavaDoc orderBy) throws GenericEntityException {
327         HashMap JavaDoc entityCache = (HashMap JavaDoc) cache.get(modelEntity.getEntityName());
328         if (entityCache == null) {
329             return Collections.EMPTY_LIST;
330         }
331
332         ArrayList JavaDoc result = new ArrayList JavaDoc();
333         for (Iterator JavaDoc iterator = entityCache.entrySet().iterator(); iterator.hasNext();) {
334             Map.Entry JavaDoc mapEntry = (Map.Entry JavaDoc) 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 JavaDoc findByAnd(ModelEntity modelEntity, List JavaDoc expressions, List JavaDoc orderBy) throws GenericEntityException {
346         return null;
347     }
348
349     public List JavaDoc findByLike(ModelEntity modelEntity, Map JavaDoc fields, List JavaDoc orderBy) throws GenericEntityException {
350         return null;
351     }
352
353     public List JavaDoc findByOr(ModelEntity modelEntity, Map JavaDoc fields, List JavaDoc orderBy) throws GenericEntityException {
354         HashMap JavaDoc entityCache = (HashMap JavaDoc) cache.get(modelEntity.getEntityName());
355         if (entityCache == null) {
356             return Collections.EMPTY_LIST;
357         }
358
359         ArrayList JavaDoc result = new ArrayList JavaDoc();
360         for (Iterator JavaDoc iterator = entityCache.entrySet().iterator(); iterator.hasNext();) {
361             Map.Entry JavaDoc mapEntry = (Map.Entry JavaDoc) 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 JavaDoc findByOr(ModelEntity modelEntity, List JavaDoc expressions, List JavaDoc orderBy) throws GenericEntityException {
374         return null;
375     }
376
377     public List JavaDoc findByCondition(ModelEntity modelEntity, EntityCondition entityCondition,
378                                 Collection JavaDoc fieldsToSelect, List JavaDoc orderBy) throws GenericEntityException {
379         return null;
380     }
381
382     public List JavaDoc findByMultiRelation(GenericValue value, ModelRelation modelRelationOne, ModelEntity modelEntityOne,
383                                     ModelRelation modelRelationTwo, ModelEntity modelEntityTwo, List JavaDoc orderBy) throws GenericEntityException {
384         return null;
385     }
386
387     public EntityListIterator findListIteratorByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition,
388                                                           EntityCondition havingEntityCondition, Collection JavaDoc fieldsToSelect, List JavaDoc 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 JavaDoc fields) throws GenericEntityException {
398         HashMap JavaDoc entityCache = (HashMap JavaDoc) cache.get(modelEntity.getEntityName());
399         if (entityCache == null) {
400             return 0;
401         }
402
403         ArrayList JavaDoc removeList = new ArrayList JavaDoc();
404         for (Iterator JavaDoc iterator = entityCache.entrySet().iterator(); iterator.hasNext();) {
405             Map.Entry JavaDoc mapEntry = (Map.Entry JavaDoc) 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 JavaDoc 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 JavaDoc values) throws GenericEntityException {
432         int count = 0;
433         for (Iterator JavaDoc 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 JavaDoc dummyPKs) throws GenericEntityException {
444         int count = 0;
445         for (Iterator JavaDoc 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 JavaDoc modelEntities, List JavaDoc messages, boolean addMissing) throws GenericEntityException {
454         messages.add("checkDataSource not implemented for MemoryHelper");
455     }
456 }
457
Popular Tags