KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > petstore > dao > OrderDAOImpl


1 /*
2  * JFox - The most lightweight Java EE Application Server!
3  * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
4  *
5  * JFox is licenced and re-distributable under GNU LGPL.
6  */

7 package org.jfox.petstore.dao;
8
9 import java.sql.SQLException JavaDoc;
10 import java.util.List JavaDoc;
11 import javax.ejb.Local JavaDoc;
12 import javax.ejb.Stateless JavaDoc;
13 import javax.persistence.EntityManager;
14 import javax.persistence.NamedNativeQueries;
15 import javax.persistence.NamedNativeQuery;
16 import javax.persistence.PersistenceContext;
17
18 import org.jfox.petstore.entity.LineItem;
19 import org.jfox.petstore.entity.Order;
20 import org.jfox.entity.EntityManagerExt;
21 import org.jfox.entity.dao.DAOSupport;
22
23 /**
24  * @author <a HREF="mailto:jfox.young@gmail.com">Young Yang</a>
25  */

26
27
28 @NamedNativeQueries(
29         {
30         @NamedNativeQuery(
31                 name = OrderDAOImpl.GET_ORDER,
32                 query = "select " +
33                         "billaddr1, " +
34                         "billaddr2, " +
35                         "billcity, " +
36                         "billcountry, " +
37                         "billstate, " +
38                         "billtofirstname, " +
39                         "billtolastname, " +
40                         "billzip, " +
41                         "shipaddr1, " +
42                         "shipaddr2, " +
43                         "shipcity, " +
44                         "shipcountry, " +
45                         "shipstate, " +
46                         "shiptofirstname, " +
47                         "shiptolastname, " +
48                         "shipzip, " +
49                         "cardtype, " +
50                         "courier, " +
51                         "creditcard, " +
52                         "exprdate, " +
53                         "locale, " +
54                         "orders.orderdate as orderdate, " +
55                         "orders.orderid as orderid, " +
56                         "totalprice, " +
57                         "userid, " +
58                         "status " +
59                         "from orders, orderstatus " +
60                         "where orders.orderid = $orderid and orders.orderid = orderstatus.orderid",
61                 resultClass = Order.class
62         ),
63         @NamedNativeQuery(
64                 name = OrderDAOImpl.GET_ORDERS_BY_USERNAME,
65                 query = "select " +
66                         "billaddr1, " +
67                         "billaddr2, " +
68                         "billcity, " +
69                         "billcountry, " +
70                         "billstate, " +
71                         "billtofirstname, " +
72                         "billtolastname, " +
73                         "billzip, " +
74                         "shipaddr1, " +
75                         "shipaddr2, " +
76                         "shipcity, " +
77                         "shipcountry, " +
78                         "shipstate, " +
79                         "shiptofirstname, " +
80                         "shiptolastname, " +
81                         "shipzip, " +
82                         "cardtype, " +
83                         "courier, " +
84                         "creditcard, " +
85                         "exprdate, " +
86                         "locale, " +
87                         "orders.orderdate as orderdate, " +
88                         "orders.orderid as orderid, " +
89                         "totalprice, " +
90                         "userid, " +
91                         "status " +
92                         "from orders, orderstatus " +
93                         "where " +
94                         "orders.userid = $username and orders.orderid = orderstatus.orderid",
95                 resultClass = Order.class
96         ),
97         @NamedNativeQuery(
98                 name = OrderDAOImpl.INSERT_ORDER,
99                 query = "insert into orders (" +
100                         "orderid, " +
101                         "userid, " +
102                         "orderdate, " +
103                         "shipaddr1, " +
104                         "shipaddr2, " +
105                         "shipcity, " +
106                         "shipstate, " +
107                         "shipzip, " +
108                         "shipcountry, " +
109                         "billaddr1, " +
110                         "billaddr2, " +
111                         "billcity, " +
112                         "billstate, " +
113                         "billzip, " +
114                         "billcountry, " +
115                         "courier, " +
116                         "totalprice, " +
117                         "billtofirstname, " +
118                         "billtolastname, " +
119                         "shiptofirstname, " +
120                         "shiptolastname, " +
121                         "creditcard, " +
122                         "exprdate, " +
123                         "cardtype, " +
124                         "locale" +
125                         ") values (" +
126                         "$order.getOrderId(), " +
127                         "$order.getUsername(), " +
128                         "$order.getOrderDate(), " +
129                         "$order.getShipAddress1(), " +
130                         "$order.getShipAddress2(), " +
131                         "$order.getShipCity(), " +
132                         "$order.getShipState(), " +
133                         "$order.getShipZip(), " +
134                         "$order.getShipCountry(), " +
135                         "$order.getBillAddress1(), " +
136                         "$order.getBillAddress2(), " +
137                         "$order.getBillCity(), " +
138                         "$order.getBillState(), " +
139                         "$order.getBillZip(), " +
140                         "$order.getBillCountry(), " +
141                         "$order.getCourier(), " +
142                         "$order.getTotalPrice(), " +
143                         "$order.getBillToFirstName(), " +
144                         "$order.getBillToLastName(), " +
145                         "$order.getShipToFirstName(), " +
146                         "$order.getShipToLastName(), " +
147                         "$order.getCreditCard(), " +
148                         "$order.getExpiryDate(), " +
149                         "$order.getCardType(), " +
150                         "$order.getLocale())"
151         ),
152         @NamedNativeQuery(
153                 name = OrderDAOImpl.INSERT_ORDER_STATUS,
154                 query = "insert into orderstatus (orderid, linenum, orderdate, status) values ($order.getOrderId(), $order.getOrderId(), $order.getOrderDate(), $order.getStatus())"
155         ),
156         @NamedNativeQuery(
157                 name = OrderDAOImpl.GET_LINEITEMS_BY_ORDERID,
158                 query = "select orderid, linenum, itemid, quantity, unitprice from lineitem where orderid = $orderid",
159                 resultClass = LineItem.class
160         ),
161         @NamedNativeQuery(
162                 name = OrderDAOImpl.INSERT_LINE_ITEM,
163                 query = "insert into lineitem (orderid, linenum, itemid, quantity, unitprice) values ($lineitem.getOrderId(), $lineitem.getLineNumber(), $lineitem.getItemId(), $lineitem.getQuantity(), $lineitem.getUnitPrice())"
164         )
165                 }
166 )
167 @Stateless JavaDoc
168 @Local JavaDoc
169 @SuppressWarnings JavaDoc("unchecked")
170 public class OrderDAOImpl extends DAOSupport implements OrderDAO {
171
172     public static final String JavaDoc INSERT_ORDER = "insertOrder";
173     public static final String JavaDoc GET_ORDER = "getOrder";
174     public static final String JavaDoc GET_ORDERS_BY_USERNAME = "getOrdersByUsername";
175     public static final String JavaDoc INSERT_ORDER_STATUS = "insertOrderStatus";
176     public static final String JavaDoc GET_LINEITEMS_BY_ORDERID = "getLineItemsByOrderId";
177     public static final String JavaDoc INSERT_LINE_ITEM = "insertLineItem";
178
179     @PersistenceContext(unitName = "JPetstoreMysqlDS")
180     EntityManager em;
181
182
183     protected EntityManager getEntityManager() {
184         return (EntityManagerExt)em;
185     }
186
187     public Order getOrder(long orderId) throws SQLException JavaDoc {
188         return (Order)createNamedNativeQuery(GET_ORDER).setParameter("orderid",orderId).getSingleResult();
189     }
190
191     public List JavaDoc<Order> getOrdersByUsername(String JavaDoc username) throws SQLException JavaDoc {
192         return (List JavaDoc<Order>)createNamedNativeQuery(GET_ORDERS_BY_USERNAME).setParameter("username",username).getResultList();
193     }
194
195     public void insertOrder(Order order) throws SQLException JavaDoc {
196             createNamedNativeQuery(INSERT_ORDER).setParameter("order", order).executeUpdate();
197             createNamedNativeQuery(INSERT_ORDER_STATUS).setParameter("order", order).executeUpdate();
198             for (LineItem lineItem : order.getLineItems()) {
199                 createNamedNativeQuery(INSERT_LINE_ITEM).setParameter("lineitem", lineItem).executeUpdate();
200             }
201     }
202
203     public static void main(String JavaDoc[] args) {
204
205     }
206 }
207
Popular Tags