KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > trade > TradeManagerBean


1 /*
2  * Created on 27.09.2004
3  *
4  */

5 package com.nightlabs.ipanema.trade;
6
7 import java.rmi.RemoteException JavaDoc;
8 import java.util.Date JavaDoc;
9
10 import javax.ejb.CreateException JavaDoc;
11 import javax.ejb.EJBException JavaDoc;
12 import javax.ejb.SessionBean JavaDoc;
13 import javax.ejb.SessionContext JavaDoc;
14 import javax.jdo.PersistenceManager;
15
16 import org.apache.log4j.Logger;
17
18 import com.nightlabs.ModuleException;
19 import com.nightlabs.ipanema.accounting.Currency;
20 import com.nightlabs.ipanema.accounting.Invoice;
21 import com.nightlabs.ipanema.accounting.id.CurrencyID;
22 import com.nightlabs.ipanema.base.BaseSessionBeanImpl;
23 import com.nightlabs.ipanema.security.User;
24 import com.nightlabs.ipanema.store.Product;
25 import com.nightlabs.ipanema.store.id.ProductID;
26 import com.nightlabs.ipanema.trade.id.OfferID;
27 import com.nightlabs.ipanema.trade.id.OfferItemID;
28 import com.nightlabs.ipanema.trade.id.OrderID;
29
30
31 /**
32  * @ejb.bean name="TradeManager"
33  * jndi-name="TradeManagerBean"
34  * type="Stateful"
35  * transaction-type="Container"
36  *
37  */

38 public abstract class TradeManagerBean
39 extends BaseSessionBeanImpl
40 implements SessionBean JavaDoc
41 {
42     public static final Logger LOGGER = Logger.getLogger(TradeManagerBean.class);
43
44     ////////////////////// EJB "constuctor" ////////////////////////////
45

46     /**
47      * @ejb.create-method
48      * @ejb.permission role-name="TradeManager-read"
49      */

50     public void ejbCreate()
51     throws CreateException JavaDoc
52     {
53         try
54         {
55             LOGGER.debug("TradeManagerBean created by " + this.getPrincipalString());
56         }
57         catch (Exception JavaDoc e)
58         {
59             LOGGER.error("TradeManagerBean.ejbCreate() failed!", e);
60             throw new CreateException JavaDoc(e.getMessage());
61         }
62     }
63
64     // //// begin EJB stuff ////
65
/**
66      * @see com.nightlabs.ipanema.base.BaseSessionBeanImpl#setSessionContext(javax.ejb.SessionContext)
67      */

68     public void setSessionContext(SessionContext JavaDoc ctx)
69         throws EJBException JavaDoc, RemoteException JavaDoc
70     {
71         super.setSessionContext(ctx);
72     }
73     // //// end EJB stuff ////
74

75     /**
76      * Creates a new order. This method is only usable, if the user (principal)
77      * is an organisation, because this organisation will automatically be set
78      * as the customer for the new Order.
79      *
80      * @throws ModuleException
81      *
82      * @ejb.interface-method
83      * @ejb.permission role-name="TradeManager-write"
84      * @ejb.transaction type = "Required"
85      **/

86     public Order createOrder(String JavaDoc currencyID)
87         throws ModuleException
88     {
89         PersistenceManager pm = getPersistenceManager();
90         try {
91             Trader trader = Trader.getTrader(pm);
92             pm.getExtent(Currency.class);
93             Currency currency = (Currency)pm.getObjectById(CurrencyID.create(currencyID), true);
94
95             if (!getPrincipal().userIsOrganisation())
96                 throw new IllegalStateException JavaDoc("This method cannot be called by a user who is not an organisation!");
97
98             OrganisationLegalEntity customer = trader.createCustomerOrganisationLegalEntity(getPrincipal().getUserID());
99             // TODO: create foreign order
100
Order order = trader.createOrder(trader.getMandator(), customer, currency);
101
102             return (Order)pm.detachCopy(order); // TODO We need some fetchgroups
103
} finally {
104             pm.close();
105         }
106     }
107
108     /**
109      * Creates a new Offer within a given Order.
110      *
111      * @param orderID The orderID defining the Order in which to create a new Offer.
112      * @throws ModuleException
113      *
114      * @ejb.interface-method
115      * @ejb.permission role-name="TradeManager-write"
116      * @ejb.transaction type = "Required"
117      **/

118     public Offer createOffer(OrderID orderID)
119         throws ModuleException
120     {
121         PersistenceManager pm = getPersistenceManager();
122         try {
123             Trader trader = Trader.getTrader(pm);
124 // Offer offer = trader.createOffer();
125
// pm.getExtent(Order.class);
126
// Order order = (Order) pm.getObjectById(orderID, true);
127
Offer offer = trader.createOffer(trader.getOrder(orderID));
128             return (Offer) pm.detachCopy(offer); // TODO fetchgroups?!
129
} finally {
130             pm.close();
131         }
132     }
133     
134     /**
135      * Creates a new OfferItem within a given Offer to be sold.
136      *
137      * @param offerID The offerID which defines the offer in which to create the item.
138      * @param productID The productID which defines the product to be put into an offer.
139      * @throws ModuleException
140      *
141      * @ejb.interface-method
142      * @ejb.permission role-name="TradeManager-write"
143      * @ejb.transaction type = "Required"
144      **/

145     public OfferItem createOfferItemToSell(OfferID offerID, ProductID productID, Date JavaDoc autoReleaseDT)
146         throws ModuleException
147     {
148         PersistenceManager pm = getPersistenceManager();
149         try {
150             pm.getExtent(Offer.class);
151             pm.getExtent(Product.class);
152             pm.getExtent(User.class);
153             Offer offer = (Offer) pm.getObjectById(offerID, true);
154             Product product = (Product) pm.getObjectById(productID, true);
155             User user = User.getUser(pm, getPrincipal());
156             OfferItem offerItem = offer.createOfferItemToSell(user, product, autoReleaseDT);
157             return (OfferItem)pm.detachCopy(offerItem); // TODO fetchgroups?!
158
} finally {
159             pm.close();
160         }
161     }
162
163     /**
164      * Creates a new OfferItem within a given Offer to be refunded.
165      *
166      * @param offerID The offerID which defines the offer in which to create the item.
167      * @param offerItemID The offerItemID which defines the original OfferItem to be refunded.
168      * @throws ModuleException
169      *
170      * @ejb.interface-method
171      * @ejb.permission role-name="TradeManager-write"
172      * @ejb.transaction type = "Required"
173      **/

174     public OfferItem createOfferItemToRefund(OfferID offerID, OfferItemID offerItemID)
175         throws ModuleException
176     {
177         PersistenceManager pm = getPersistenceManager();
178         try {
179             pm.getExtent(Offer.class);
180             pm.getExtent(OfferItem.class);
181 // pm.getExtent(Product.class);
182
Offer offer = (Offer) pm.getObjectById(offerID, true);
183             OfferItem offerItemToRefund = (OfferItem) pm.getObjectById(offerItemID, true);
184             User user = User.getUser(pm, getPrincipal());
185             OfferItem offerItem = offer.createOfferItemToRefund(user, offerItemToRefund);
186             return (OfferItem)pm.detachCopy(offerItem); // TODO fetchgroups?!
187
} finally {
188             pm.close();
189         }
190     }
191     
192     
193     /**
194      * @ejb.interface-method
195      * @ejb.permission role-name="TradeManager-write"
196      * @ejb.transaction type = "Required"
197      **/

198     public Invoice createInvoice()
199         throws ModuleException
200     {
201         return null;
202     }
203     
204 }
205
Popular Tags