Source Code Cross Referenced for OrderDAO.java in  » Database-ORM » ODAL » com » completex » objective » persistency » examples » ex003 » app » 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 » Database ORM » ODAL » com.completex.objective.persistency.examples.ex003.app 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.completex.objective.persistency.examples.ex003.app;
002:
003:        import com.completex.objective.components.persistency.*;
004:        import com.completex.objective.components.persistency.core.impl.BasicLifeCycleController;
005:        import com.completex.objective.components.persistency.type.TracingArrayList;
006:        import com.completex.objective.components.persistency.type.TracingArrayListCollectionFactory;
007:        import com.completex.objective.components.persistency.type.ArrayListCollectionFactory;
008:        import com.completex.objective.persistency.examples.ex003.domain.Order;
009:        import com.completex.objective.persistency.examples.ex003.domain.OrderItem;
010:
011:        import java.util.List;
012:        import java.util.HashMap;
013:
014:        /**
015:         * @author Gennady Krizhevsky
016:         */
017:        public class OrderDAO {
018:            private Persistency persistency;
019:
020:            public OrderDAO(Persistency persistency) {
021:                this .persistency = persistency;
022:            }
023:
024:            public void insertOrder(final Order order) throws OrderException {
025:                try {
026:                    persistency.insert(order);
027:                    insertOrderItems(order);
028:                } catch (OdalPersistencyException e) {
029:                    throw new OrderException(e);
030:                }
031:            }
032:
033:            private void insertOrderItems(final Order order)
034:                    throws OdalPersistencyException {
035:                List orderItems = order.getOrderItems();
036:                if (orderItems != null) {
037:                    for (int i = 0; i < orderItems.size(); i++) {
038:                        OrderItem orderItem = (OrderItem) orderItems.get(i);
039:                        orderItem.setOrderId(order.getOrderId());
040:                        persistency.insert(orderItem);
041:                    }
042:                }
043:            }
044:
045:            public void updateOrder(final Order order) throws OrderException {
046:                try {
047:                    persistency.update(order);
048:                } catch (OdalPersistencyException e) {
049:                    throw new OrderException(e);
050:                }
051:            }
052:
053:            public void updateOrderDetails(final Order order)
054:                    throws OrderException {
055:                try {
056:                    List rels = order.getOrderItems();
057:                    persistency.update(rels);
058:                } catch (OdalPersistencyException e) {
059:                    throw new OrderException(e);
060:                }
061:            }
062:
063:            public Order loadOrder(final Long orderId) throws OrderException {
064:                try {
065:                    Order order = (Order) persistency.load(new Order(orderId));
066:                    return loadOrderDetails(order);
067:                } catch (OdalPersistencyException e) {
068:                    throw new OrderException(e);
069:                }
070:            }
071:
072:            public Order loadOrderDetails(final Order order)
073:                    throws OrderException {
074:                try {
075:                    OrderItem rel = new OrderItem();
076:                    rel.setOrderId(order.getOrderId());
077:                    Query query = persistency.getQueryFactory()
078:                            .newQueryByExample(rel);
079:                    query
080:                            .setMultipleResultFactory(TracingArrayListCollectionFactory
081:                                    .factory());
082:                    TracingArrayList contactRels = (TracingArrayList) persistency
083:                            .select(query);
084:                    contactRels.setTrace();
085:                    order.setOrderItems(contactRels);
086:                    return order;
087:                } catch (OdalPersistencyException e) {
088:                    throw new OrderException(e);
089:                }
090:            }
091:
092:            /**
093:             * Example how to "manually" populate order/orderItems tree getting all the orders
094:             * for particular customer in specific state into the map. 
095:             * 
096:             * The query returns a "flat" structure that contains all the orders and order items of
097:             * particular customer that then gets transfomed into the  
098:             * 
099:             * @param customerId
100:             * @param state
101:             * @return
102:             * @throws OrderException
103:             */
104:            public Order[] loadOrders(Long customerId, String state)
105:                    throws OrderException {
106:                try {
107:                    OrderItem queryOrderItem = new OrderItem();
108:                    queryOrderItem.setState(state);
109:                    Order queryOrder = new Order();
110:                    queryOrder.setCustomerId(customerId);
111:                    CompoundPersistentObject compoundQuery = new CompoundPersistentObject(
112:                            new PersistentObject[] { queryOrder, queryOrderItem });
113:                    Query query = persistency.getQueryFactory()
114:                            .newQueryByCompoundExample(compoundQuery);
115:                    query
116:                            .setMultipleResultFactory(ArrayListCollectionFactory.NULL_ARRAY_LIST_FACTORY);
117:                    LoadOrdersCtl controller = new LoadOrdersCtl();
118:                    persistency.select(query, controller);
119:                    return controller.getOrders();
120:                } catch (OdalPersistencyException e) {
121:                    throw new OrderException(e);
122:                }
123:            }
124:
125:            class LoadOrdersCtl extends BasicLifeCycleController {
126:                final HashMap ordersMap = new HashMap();
127:
128:                public void afterSelect(
129:                        AbstractPersistentObject persistentObject) {
130:                    CompoundPersistentObject compound = (CompoundPersistentObject) persistentObject;
131:                    Order order = ((Order) compound.compoundEntries()[0]);
132:                    PersistentObject orderItem = compound.compoundEntries()[1];
133:                    Order cachedOrder = (Order) ordersMap.get(order
134:                            .getOrderId());
135:                    if (cachedOrder == null) {
136:                        ordersMap.put(order.getOrderId(), order);
137:                        cachedOrder = order;
138:                        cachedOrder.unsetTrace();
139:                    }
140:                    List orderItems = cachedOrder.getOrderItems();
141:                    orderItems.add(orderItem);
142:                }
143:
144:                public Order[] getOrders() {
145:                    Order[] orders = (Order[]) ordersMap.values().toArray(
146:                            new Order[ordersMap.size()]);
147:                    for (int i = 0; i < orders.length; i++) {
148:                        Order order = orders[i];
149:                        order.setTrace();
150:                    }
151:                    return orders;
152:                }
153:            }
154:
155:        }
w___w__w.j___a__v___a__2s__.___c___o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.