summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/impl/session/CartBean.java
blob: d5e2b1bd2d475f4cfa7249a8fc4a14b183579350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package examples.shop.impl.session;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import examples.shop.impl.entity.Customer;
import examples.shop.impl.entity.LineItem;
import examples.shop.impl.entity.Order;
import examples.shop.impl.entity.Product;
import examples.shop.logic.Cart;

/**
 * This stateful session bean represents a cart which a customer is
 * working on.  A cart is a set of products that the customer is
 * interested in, but has not committed to buying yet.  
 *
 * A cart consists of a series of line items.  Each line item
 * represents one particular product the customer wants, as well as a
 * quantity and discount for that product.
 *
 * The distinction between a cart and an order is that a quote is only
 * temporary and in-memory (hence a stateful session bean), whereas an
 * order is a persistent record (hence an entity).  The cart bean 
 * knows how to transform itself into an order (via the purchase() 
 * method).
 */

@Stateful
@Remote(Cart.class)
public class CartBean implements Cart {

    /**
     * The shopping cart owner
     */
    private String owner;

    /**
     * The shopping cart contents - a vector of LineItems
     */
    private List<LineItem> contents = new ArrayList();

    /**
     * The subtotal (price before taxes) of the goods
     */
    private double subTotal;

    /**
     * The taxes on the goods
     */
    private double taxes;

    @PersistenceContext
    private EntityManager manager;

    public CartBean() {
    }

    /**
     * Get method for the shopping cart owner's name
     * 
     * @return shopping cart owner
     */
    public String getOwner() {
        return owner;
    }

    /**
     * Set method for the shopping cart owner's name
     * 
     * @param shopping
     *            cart owner
     */
    public void setOwner(String owner) {
        this.owner = owner;
    }

    /**
     * Adds an item to the shopping cart
     * 
     * @param shopping
     *            cart lineitem
     */
    public void add(LineItem lineItem) {
        /*
         * Search for an exiting line item in the cart that match 
         * the productID passed in. 
         */
        for (Iterator<LineItem> iter = contents.iterator(); iter.hasNext();) {
            LineItem item = iter.next();
            Product product = item.getProduct();

            if (product != null
                    && product.getProductID().equals(
                            lineItem.getProduct().getProductID())) {
                // found one, modify that line item's quantity and return
                item.setQuantity(item.getQuantity() + lineItem.getQuantity());
                return;
            }
        }

        /*
         * Did not find a match, so add the line item as new.
         */
        contents.add(lineItem);
    }

    /**
     * Changes the quantities of contents in the shopping cart
     * 
     * @param product
     *            Id
     * @param number
     *            of items.
     */
    public void modify(String productID, int quantity) throws Exception {
        System.out.println("CartBean.modify()");

        /*
         * Search for a lineitem in the cart that match the productID passed in.
         * If found, modify that lineitem.
         */
        for (Iterator<LineItem> iter = contents.iterator(); iter.hasNext();) {
            LineItem item = iter.next();
            Product product = item.getProduct();
            if (product != null && product.getProductID().equals(productID)) {
                item.setQuantity(quantity);
                if (quantity == 0) {
                    iter.remove();
                }
                return;
            }
        }
        throw new Exception("CartBean.modify() error: Could not find product "
                + productID);
    }

    /**
     * Returns all the shopping cart line items
     * 
     * @return A collection of Cart LineItems
     */
    public List<LineItem> getAll() {
        return contents;
    }

    /**
     * @return  the subtotal price which has been previously set by
     * setSubtotal().
     */
    public double getSubtotal() {
        return subTotal;
    }

    /**
     * Sets the subtotal price.
     * @param subTotal the subtotal price
     */
    public void setSubtotal(double subTotal) {
        this.subTotal = subTotal;
    }

    /**
     * @return the taxes computed for this Cart.
     */
    public double getTaxes() {
        return taxes;
    }

    /**
     * Sets the taxes for this Cart.
     */
    public void setTaxes(double taxes) {
        System.out.println("CartBean.setTaxes(): " + taxes);
        this.taxes = taxes;
    }

    /**
     * Get the total price. The total Price is computed from:
     * <ol> 
     * <li>Subtotal price
     * <li>Tax
     * </ol>
     * @return the total price. 
     */
    public double getTotalPrice() {
        return subTotal + taxes;
    }

    /**
     * Empties the shopping cart
     */
    public void clear() {
        contents.clear();
    }

    /**
     * Purchases this cart. The cart will create an order in the database 
     * and return the order ID as a confirmation number.
     * 
     * @return The Order confirmation number
     */
    public String purchase() {
        System.out.println("CartBean.purchase()");
        Customer customer = manager.find(Customer.class, owner);

        /*
         * Create the order entity
         */
        Order order = new Order();
        String orderID = makeUniqueID();
        order.init(orderID, customer, Order.Status.Unverified, subTotal, taxes);
        order.setLineItems(prepareContents());
        manager.persist(order);

        /*
         * Sends the JMS message to the topic
         */
        try {
            sendMessage(orderID);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /*
         * Return the order ID
         */
        return orderID;
    }

    /**
     * Sends a JMS message to a destination (Queue or Topic)
     * 
     * @param a
     *            message string, in this case orderId
     */
    private void sendMessage(String text) throws Exception {
        Connection connection = null;
        MessageProducer producer = null;
        Session session = null;

        try {
            Context jndiContext = new InitialContext();
            ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext
                    .lookup("jms/QueueConnectionFactory");
            Destination destination = (Destination) jndiContext
                    .lookup("jms/OrderQueue");
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(destination);

            connection.start();
            TextMessage msg = session.createTextMessage();
            msg.setText(text);
            producer.send(msg);
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (connection != null) {
                try {
                    producer.close();
                    connection.close();
                    session.close();
                } catch (JMSException e) {
                    // ignore
                }
            }
        }
    }

    /**
     * Generates primary keys for line items and loads the products 
     * into the persistence context.
     */

    private List<LineItem> prepareContents() {
        for (Iterator<LineItem> iter = contents.iterator(); iter.hasNext();) {
            LineItem lineItem = iter.next();
            Product p = manager.find(Product.class, lineItem.getProduct()
                    .getProductID());
            lineItem.setProduct(p);
            lineItem.setId(makeUniqueID());
        }
        return contents;
    }

    /**
     * Returns a unique Id based on current time. Can be removed
     * when AUTO key generation works in Glassfish      * 
     */
    private String makeUniqueID() {
        String id = new Long(System.nanoTime()).toString();
        System.out.println("makeUniqueID: " + id);
        return id;
    }

    /**
     * Removes this quote, and hence all client-
     * specific state.
     */
    @Remove
    public void remove() {
    }
}