Source Code Cross Referenced for MemoryHelper.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » entity » datasource » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » SourceTap CRM » org.ofbiz.entity.datasource 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: MemoryHelper.java,v 1.4 2004/02/03 08:14:41 jonesde Exp $
003:         *
004:         *  Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
005:         *
006:         *  Permission is hereby granted, free of charge, to any person obtaining a
007:         *  copy of this software and associated documentation files (the "Software"),
008:         *  to deal in the Software without restriction, including without limitation
009:         *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         *  and/or sell copies of the Software, and to permit persons to whom the
011:         *  Software is furnished to do so, subject to the following conditions:
012:         *
013:         *  The above copyright notice and this permission notice shall be included
014:         *  in all copies or substantial portions of the Software.
015:         *
016:         *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         */
024:
025:        package org.ofbiz.entity.datasource;
026:
027:        import java.util.ArrayList;
028:        import java.util.Collection;
029:        import java.util.Collections;
030:        import java.util.HashMap;
031:        import java.util.Iterator;
032:        import java.util.List;
033:        import java.util.Map;
034:        import java.util.Set;
035:
036:        import org.ofbiz.base.util.Debug;
037:        import org.ofbiz.entity.GenericEntityException;
038:        import org.ofbiz.entity.GenericNotImplementedException;
039:        import org.ofbiz.entity.GenericPK;
040:        import org.ofbiz.entity.GenericValue;
041:        import org.ofbiz.entity.condition.EntityCondition;
042:        import org.ofbiz.entity.jdbc.SqlJdbcUtil;
043:        import org.ofbiz.entity.model.ModelEntity;
044:        import org.ofbiz.entity.model.ModelField;
045:        import org.ofbiz.entity.model.ModelFieldTypeReader;
046:        import org.ofbiz.entity.model.ModelRelation;
047:        import org.ofbiz.entity.util.EntityFindOptions;
048:        import org.ofbiz.entity.util.EntityListIterator;
049:
050:        /**
051:         * Partial GenericHelper implementation that is entirely memory-based,
052:         * to be used for simple unit testing (can't do anything beyond searches
053:         * for primary keys, findByOr and findByAnd).
054:         *
055:         * @author <a href="mailto:plightbo@.com">Pat Lightbody</a>
056:         */
057:        public class MemoryHelper implements  GenericHelper {
058:
059:            public static final String module = MemoryHelper.class.getName();
060:            private static Map cache = new HashMap();
061:
062:            public static void clearCache() {
063:                cache = new HashMap();
064:            }
065:
066:            private String helperName;
067:
068:            private boolean addToCache(GenericValue value) {
069:                if (value == null) {
070:                    return false;
071:                }
072:
073:                if (!veryifyValue(value)) {
074:                    return false;
075:                }
076:
077:                value = (GenericValue) value.clone();
078:                HashMap entityCache = (HashMap) cache
079:                        .get(value.getEntityName());
080:                if (entityCache == null) {
081:                    entityCache = new HashMap();
082:                    cache.put(value.getEntityName(), entityCache);
083:                }
084:
085:                entityCache.put(value.getPrimaryKey(), value);
086:                return true;
087:            }
088:
089:            private GenericValue findFromCache(GenericPK pk) {
090:                if (pk == null) {
091:                    return null;
092:                }
093:
094:                HashMap entityCache = (HashMap) cache.get(pk.getEntityName());
095:                if (entityCache == null) {
096:                    return null;
097:                }
098:
099:                GenericValue value = (GenericValue) entityCache.get(pk);
100:                if (value == null) {
101:                    return null;
102:                } else {
103:                    return (GenericValue) value.clone();
104:                }
105:            }
106:
107:            private int removeFromCache(GenericPK pk) {
108:                if (pk == null) {
109:                    return 0;
110:                }
111:
112:                HashMap entityCache = (HashMap) cache.get(pk.getEntityName());
113:                if (entityCache == null) {
114:                    return 0;
115:                }
116:
117:                Object o = entityCache.remove(pk);
118:                if (o == null) {
119:                    return 0;
120:                } else {
121:                    return 1;
122:                }
123:            }
124:
125:            private boolean isAndMatch(Map values, Map fields) {
126:                for (Iterator iterator = fields.entrySet().iterator(); iterator
127:                        .hasNext();) {
128:                    Map.Entry mapEntry = (Map.Entry) iterator.next();
129:                    if (mapEntry.getValue() == null) {
130:                        if (values.get(mapEntry.getKey()) != null) {
131:                            return false;
132:                        }
133:                    } else {
134:                        try {
135:                            if (!mapEntry.getValue().equals(
136:                                    values.get(mapEntry.getKey()))) {
137:                                return false;
138:                            }
139:                        } catch (Exception e) {
140:                            return false;
141:                        }
142:                    }
143:                }
144:
145:                return true;
146:            }
147:
148:            private boolean isOrMatch(Map values, Map fields) {
149:                for (Iterator iterator = fields.entrySet().iterator(); iterator
150:                        .hasNext();) {
151:                    Map.Entry mapEntry = (Map.Entry) iterator.next();
152:                    if (mapEntry.getValue() == null) {
153:                        if (values.get(mapEntry.getKey()) == null) {
154:                            return true;
155:                        }
156:                    } else {
157:                        try {
158:                            if (mapEntry.getValue().equals(
159:                                    values.get(mapEntry.getKey()))) {
160:                                return true;
161:                            }
162:                        } catch (Exception e) {
163:                            Debug.logError(e, module);
164:                        }
165:                    }
166:                }
167:
168:                return false;
169:            }
170:
171:            private boolean veryifyValue(GenericValue value) {
172:                ModelEntity me = value.getModelEntity();
173:
174:                // make sure the PKs exist
175:                for (Iterator iterator = me.getPksIterator(); iterator
176:                        .hasNext();) {
177:                    ModelField field = (ModelField) iterator.next();
178:                    if (!value.containsKey(field.getName())) {
179:                        return false;
180:                    }
181:                }
182:
183:                // make sure the value doesn't have any extra (unknown) fields
184:                for (Iterator iterator = value.entrySet().iterator(); iterator
185:                        .hasNext();) {
186:                    Map.Entry entry = (Map.Entry) iterator.next();
187:                    if (me.getField((String) entry.getKey()) == null) {
188:                        return false;
189:                    }
190:                }
191:
192:                // make sure all fields that are in the value are of the right type
193:                for (Iterator iterator = me.getFieldsIterator(); iterator
194:                        .hasNext();) {
195:                    ModelField field = (ModelField) iterator.next();
196:                    Object o = value.get(field.getName());
197:                    int typeValue = 0;
198:                    try {
199:                        typeValue = SqlJdbcUtil.getType(modelFieldTypeReader
200:                                .getModelFieldType(field.getType())
201:                                .getJavaType());
202:                    } catch (GenericNotImplementedException e) {
203:                        return false;
204:                    }
205:
206:                    if (o != null) {
207:                        switch (typeValue) {
208:                        case 1:
209:                            if (!(o instanceof  String)) {
210:                                return false;
211:                            }
212:                            break;
213:                        case 2:
214:                            if (!(o instanceof  java.sql.Timestamp)) {
215:                                return false;
216:                            }
217:                            break;
218:
219:                        case 3:
220:                            if (!(o instanceof  java.sql.Time)) {
221:                                return false;
222:                            }
223:                            break;
224:
225:                        case 4:
226:                            if (!(o instanceof  java.sql.Date)) {
227:                                return false;
228:                            }
229:                            break;
230:
231:                        case 5:
232:                            if (!(o instanceof  Integer)) {
233:                                return false;
234:                            }
235:                            break;
236:
237:                        case 6:
238:                            if (!(o instanceof  Long)) {
239:                                return false;
240:                            }
241:                            break;
242:
243:                        case 7:
244:                            if (!(o instanceof  Float)) {
245:                                return false;
246:                            }
247:                            break;
248:
249:                        case 8:
250:                            if (!(o instanceof  Double)) {
251:                                return false;
252:                            }
253:                            break;
254:
255:                        case 9:
256:                            if (!(o instanceof  Boolean)) {
257:                                return false;
258:                            }
259:                            break;
260:                        }
261:                    }
262:                }
263:
264:                return true;
265:            }
266:
267:            private ModelFieldTypeReader modelFieldTypeReader;
268:
269:            public MemoryHelper(String helperName) {
270:                this .helperName = helperName;
271:                modelFieldTypeReader = ModelFieldTypeReader
272:                        .getModelFieldTypeReader(helperName);
273:            }
274:
275:            public String getHelperName() {
276:                return helperName;
277:            }
278:
279:            public GenericValue create(GenericValue value)
280:                    throws GenericEntityException {
281:                if (addToCache(value)) {
282:                    return value;
283:                } else {
284:                    return null;
285:                }
286:            }
287:
288:            public GenericValue create(GenericPK primaryKey)
289:                    throws GenericEntityException {
290:                return create(new GenericValue(primaryKey));
291:            }
292:
293:            public GenericValue findByPrimaryKey(GenericPK primaryKey)
294:                    throws GenericEntityException {
295:                return findFromCache(primaryKey);
296:            }
297:
298:            public GenericValue findByPrimaryKeyPartial(GenericPK primaryKey,
299:                    Set keys) throws GenericEntityException {
300:                GenericValue value = findFromCache(primaryKey);
301:                value.setFields(value.getFields(keys));
302:                return value;
303:            }
304:
305:            public List findAllByPrimaryKeys(List primaryKeys)
306:                    throws GenericEntityException {
307:                ArrayList result = new ArrayList(primaryKeys.size());
308:                for (Iterator iterator = primaryKeys.iterator(); iterator
309:                        .hasNext();) {
310:                    GenericPK pk = (GenericPK) iterator.next();
311:                    result.add(this .findByPrimaryKey(pk));
312:                }
313:
314:                return result;
315:            }
316:
317:            public int removeByPrimaryKey(GenericPK primaryKey)
318:                    throws GenericEntityException {
319:                return removeFromCache(primaryKey);
320:            }
321:
322:            public List findByAnd(ModelEntity modelEntity, Map fields,
323:                    List orderBy) throws GenericEntityException {
324:                HashMap entityCache = (HashMap) cache.get(modelEntity
325:                        .getEntityName());
326:                if (entityCache == null) {
327:                    return Collections.EMPTY_LIST;
328:                }
329:
330:                ArrayList result = new ArrayList();
331:                for (Iterator iterator = entityCache.entrySet().iterator(); iterator
332:                        .hasNext();) {
333:                    Map.Entry mapEntry = (Map.Entry) iterator.next();
334:                    GenericValue value = (GenericValue) mapEntry.getValue();
335:
336:                    if (isAndMatch(value.getAllFields(), fields)) {
337:                        result.add(value);
338:                    }
339:                }
340:
341:                return result;
342:            }
343:
344:            public List findByAnd(ModelEntity modelEntity, List expressions,
345:                    List orderBy) throws GenericEntityException {
346:                return null;
347:            }
348:
349:            public List findByLike(ModelEntity modelEntity, Map fields,
350:                    List orderBy) throws GenericEntityException {
351:                return null;
352:            }
353:
354:            public List findByOr(ModelEntity modelEntity, Map fields,
355:                    List orderBy) throws GenericEntityException {
356:                HashMap entityCache = (HashMap) cache.get(modelEntity
357:                        .getEntityName());
358:                if (entityCache == null) {
359:                    return Collections.EMPTY_LIST;
360:                }
361:
362:                ArrayList result = new ArrayList();
363:                for (Iterator iterator = entityCache.entrySet().iterator(); iterator
364:                        .hasNext();) {
365:                    Map.Entry mapEntry = (Map.Entry) iterator.next();
366:                    GenericValue value = (GenericValue) mapEntry.getValue();
367:
368:                    if (isOrMatch(value.getAllFields(), fields)) {
369:                        result.add(value);
370:                    }
371:                }
372:
373:                return result;
374:
375:            }
376:
377:            public List findByOr(ModelEntity modelEntity, List expressions,
378:                    List orderBy) throws GenericEntityException {
379:                return null;
380:            }
381:
382:            public List findByCondition(ModelEntity modelEntity,
383:                    EntityCondition entityCondition, Collection fieldsToSelect,
384:                    List orderBy) throws GenericEntityException {
385:                return null;
386:            }
387:
388:            public List findByMultiRelation(GenericValue value,
389:                    ModelRelation modelRelationOne, ModelEntity modelEntityOne,
390:                    ModelRelation modelRelationTwo, ModelEntity modelEntityTwo,
391:                    List orderBy) throws GenericEntityException {
392:                return null;
393:            }
394:
395:            public EntityListIterator findListIteratorByCondition(
396:                    ModelEntity modelEntity,
397:                    EntityCondition whereEntityCondition,
398:                    EntityCondition havingEntityCondition,
399:                    Collection fieldsToSelect, List orderBy,
400:                    EntityFindOptions findOptions)
401:                    throws GenericEntityException {
402:                return null;
403:            }
404:
405:            public long findCountByCondition(ModelEntity modelEntity,
406:                    EntityCondition whereEntityCondition,
407:                    EntityCondition havingEntityCondition)
408:                    throws GenericEntityException {
409:                return 0;
410:            }
411:
412:            public int removeByAnd(ModelEntity modelEntity, Map fields)
413:                    throws GenericEntityException {
414:                HashMap entityCache = (HashMap) cache.get(modelEntity
415:                        .getEntityName());
416:                if (entityCache == null) {
417:                    return 0;
418:                }
419:
420:                ArrayList removeList = new ArrayList();
421:                for (Iterator iterator = entityCache.entrySet().iterator(); iterator
422:                        .hasNext();) {
423:                    Map.Entry mapEntry = (Map.Entry) iterator.next();
424:                    GenericValue value = (GenericValue) mapEntry.getValue();
425:                    if (isAndMatch(value.getAllFields(), fields)) {
426:                        removeList.add(mapEntry.getKey());
427:                    }
428:                }
429:
430:                return removeAll(removeList);
431:            }
432:
433:            public int store(GenericValue value) throws GenericEntityException {
434:                if (addToCache(value)) {
435:                    return 1;
436:                } else {
437:                    return 0;
438:                }
439:            }
440:
441:            public int storeAll(List values) throws GenericEntityException {
442:                int count = 0;
443:                for (Iterator iterator = values.iterator(); iterator.hasNext();) {
444:                    GenericValue gv = (GenericValue) iterator.next();
445:                    if (addToCache(gv)) {
446:                        count++;
447:                    }
448:                }
449:
450:                return count;
451:            }
452:
453:            public int removeAll(List dummyPKs) throws GenericEntityException {
454:                int count = 0;
455:                for (Iterator iterator = dummyPKs.iterator(); iterator
456:                        .hasNext();) {
457:                    GenericPK pk = (GenericPK) iterator.next();
458:                    count = count + removeFromCache(pk);
459:                }
460:
461:                return count;
462:            }
463:
464:            public void checkDataSource(Map modelEntities, List messages,
465:                    boolean addMissing) throws GenericEntityException {
466:                messages
467:                        .add("checkDataSource not implemented for MemoryHelper");
468:            }
469:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.