KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > session > helper > ItemHelperBean


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27
28 /*
29  * ItemHelperBean.java
30  *
31  * Created on Aug 16, 2004
32  *
33  */

34 package olstore.session.helper;
35
36 import java.util.Collection;
37 import java.util.Iterator;
38 import java.util.Vector;
39 import java.math.BigDecimal;
40
41 import javax.ejb.CreateException;
42 import javax.ejb.EJBException;
43 import javax.ejb.SessionBean;
44 import javax.ejb.SessionContext;
45 import javax.naming.Context;
46 import javax.naming.InitialContext;
47 import javax.naming.NamingException;
48
49 import olstore.dto.ItemValue;
50 import olstore.dto.PictureValue;
51 import olstore.dto.PropertyValue;
52 import olstore.framework.EJBHomeFactory;
53
54 import org.apache.commons.beanutils.BeanUtils;
55
56 import olstore.entity.ItemLocal;
57 import olstore.entity.ItemLocalHome;
58 import olstore.entity.PictureLocalHome;
59 import olstore.entity.PropertyLocal;
60 import olstore.entity.PropertyLocalHome;
61 import olstore.entity.TypeLocal;
62 import olstore.entity.TypeLocalHome;
63 import olstore.entity.UserLocal;
64 import olstore.entity.UserLocalHome;
65
66 /**
67  * @ejb.bean name="ItemHelper"
68  * type="Stateless"
69  * view-type="local"
70  * local-jndi-name="ItemHelperLocal_L"
71  *
72  * @ejb.ejb-ref
73  * ejb-name="Type"
74  * view-type="local"
75  *
76  * @ejb.ejb-ref
77  * ejb-name="Item"
78  * view-type="local"
79  *
80  * @ejb.ejb-ref
81  * ejb-name="Property"
82  * view-type="local"
83  *
84  * @ejb.ejb-ref
85  * ejb-name="Picture"
86  * view-type="local"
87  *
88  * @ejb.transaction
89  * type="Required"
90  *
91  *--
92  * change this --> /transaction-type=/"Container"
93  *
94  * This is needed for JOnAS.
95  * If you are not using JOnAS you can safely remove the tags below.
96  * @jonas.bean ejb-name="ItemHelper"
97  * jndi-name="ItemHelperLocal"
98  *
99  *--
100  **/

101
102 public abstract class ItemHelperBean implements SessionBean {
103     SessionContext ejbContext;
104     
105     // ------------------------------------------------------------------
106
// SessionBean implementation
107
// ------------------------------------------------------------------
108

109     
110     public void setSessionContext(SessionContext ctx) {
111         ejbContext = ctx;
112     }
113     
114     
115     public void ejbRemove() {
116     }
117     
118     
119     public void ejbCreate() throws CreateException {
120     }
121     
122     public void ejbPassivate() {
123     }
124     
125     public void ejbActivate() {
126     }
127     
128     // ------------------------------------------------------------------
129
// ItemHelper implementation
130
// ------------------------------------------------------------------
131

132     /**
133      * A convenience method to help create new items. The fields
134      * {@param name}, {@param name}, {@param shortDescr}, {@param type}
135      * are mandatory.
136      *
137      * @ejb.interface-method
138      *
139      */

140     
141     public olstore.entity.ItemLocal createItem ( String name,
142             String price,
143             String shortDesc,
144             String longDesc,
145             String type
146     ) throws NamingException, CreateException {
147         
148         Context initialContext = new InitialContext ();
149         ItemLocal item = null;
150         EJBHomeFactory factory = EJBHomeFactory.getInstance();
151         
152         try {
153             ItemLocalHome itemHome = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
154             item = itemHome.create ( name, new BigDecimal( price ));
155             item.setShortDesc ( shortDesc );
156             if ( longDesc == null ) {
157                 item.setLongDesc ( "" );
158             } else {
159                 item.setLongDesc ( longDesc );
160             }
161         } catch (NamingException e){
162             System.out.println("1: " + e.getMessage());
163             e.printStackTrace();
164         }
165         
166         //Try and get the appropriate type
167
try {
168             TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome(EJBHomeFactory.TYPE);
169             TypeLocal typeObj = typeHome.findByName ( type );
170             item.setTypeId ( typeObj );
171         } catch (NamingException e) {
172             System.out.println("2: " + e.getMessage());
173             e.printStackTrace();
174         } catch ( Exception e ) {
175             throw new EJBException ( "Could not register type: "
176                     + type + " to new item", e );
177         }
178         
179         //Set default mainpage and special properties
180
item.setMainPage (java.lang.Boolean.FALSE);
181         item.setSpecialOffer (java.lang.Boolean.FALSE);
182         
183         return item;
184     }
185     
186     /**
187      * This method returns the first picture with priority 0.
188      * Will return a random picture if there is none with prioriy 0
189      *
190      * @ejb.interface-method
191      *
192      */

193     public olstore.entity.PictureLocal getPicture ( olstore.entity.ItemLocal item ) {
194         olstore.entity.PictureLocal pic = null ;
195         Collection pics = item.getPictures ();
196         Iterator it = pics.iterator();
197         while ( it.hasNext() ) {
198             pic = (olstore.entity.PictureLocal) it.next ();
199             if ( pic.getPriority() == 0 ) {
200                 break;
201             }
202         }
203         
204         return pic ;
205     }
206     
207     /**
208      *
209      * @param itemId
210      * @return
211      * @throws Exception
212      *
213      * @ejb.interface-method
214      */

215     
216     public PictureValue getPicture ( String itemId ) throws Exception {
217         EJBHomeFactory factory=EJBHomeFactory.getInstance();
218         ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
219         olstore.entity.ItemLocal itemLocal = home.findByPrimaryKey ( new Integer ( itemId ) );
220         
221         olstore.entity.PictureLocal picLocal = getPicture ( itemLocal );
222         PictureValue picVal = new PictureValue() ;
223         if ( picLocal != null ) {
224             BeanUtils.copyProperties ( picVal, picLocal );
225         }
226         
227         return picVal;
228     }
229     
230     
231     
232     /**
233      * This method will return an array of PropertyValues that indicate
234      * the default properties associated with the specified type
235      *
236      * @ejb.interface-method
237      */

238     public Vector getPropertiesForType ( String type ) throws Exception {
239         
240         //Load up the default properties for specified type
241
EJBHomeFactory factory = EJBHomeFactory.getInstance();
242         TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE );
243         TypeLocal typeLocal = typeHome.findByName ( type );
244         Collection props = typeLocal.getProperties();
245         Iterator it = props.iterator() ;
246         Vector propValueArr = new Vector() ;
247         while ( it.hasNext() ) {
248             PropertyLocal prop = (PropertyLocal) it.next() ;
249             PropertyValue propValue = new PropertyValue();
250             BeanUtils.copyProperties ( propValue, prop );
251             propValueArr.add ( propValue );
252         }
253         //Assume types have at least one property
254
return propValueArr;
255     }
256     
257     /**
258      *
259      * @param itemId
260      * @return
261      * @throws Exception
262      *
263      * @ejb.interface-method
264      */

265     
266     public ItemValue getItemValueForId ( String itemId ) throws Exception {
267         
268         EJBHomeFactory factory = EJBHomeFactory.getInstance();
269         ItemLocalHome itemHome = (ItemLocalHome) factory.getLocalHome ( EJBHomeFactory.ITEM );
270         olstore.entity.ItemLocal item = itemHome.findByPrimaryKey ( new Integer ( itemId ) );
271         ItemValue itemVal = ItemToDTO ( item );
272         return itemVal;
273     }
274     
275     /**
276      *
277      * @param itemVal
278      * @throws Exception
279      *
280      * @ejb.interface-method
281      */

282     
283     public void saveItem ( ItemValue itemVal ) throws Exception {
284         
285         //determine if it already exists
286
EJBHomeFactory factory = EJBHomeFactory.getInstance();
287         ItemLocalHome itemHome = (ItemLocalHome) factory.getLocalHome ( EJBHomeFactory.ITEM );
288         olstore.entity.ItemLocal item = null;
289         
290         Integer itemId = itemVal.getItemId();
291         if ( itemId != null && itemId.intValue () != 0 ) {
292             item = itemHome.findByPrimaryKey ( itemId );
293         } else {
294             item = itemHome.create ( itemVal.getName(), itemVal.getPrice() );
295         }
296         
297         updateItem ( item, itemVal );
298     }
299     
300     /**
301      *
302      * @param item
303      * @param itemVal
304      * @throws Exception
305      *
306      */

307     
308     private void updateItem ( olstore.entity.ItemLocal item , ItemValue itemVal ) throws Exception {
309         
310         //Get instance vars we will need
311
EJBHomeFactory factory = EJBHomeFactory.getInstance();
312         PictureLocalHome picHome = (PictureLocalHome) factory.getLocalHome ( EJBHomeFactory.PIC );
313         TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE );
314         PropertyLocalHome propHome = (PropertyLocalHome) factory.getLocalHome ( EJBHomeFactory.PROP );
315         
316         
317         //Load up the values ( can't use BeanUtils here without a way to filter out
318
//certain fields (collections, PK, etc)
319
item.setName ( itemVal.getName() );
320         item.setPrice ( itemVal.getPrice () );
321         item.setShortDesc ( itemVal.getShortDesc() );
322         item.setLongDesc ( itemVal.getLongDesc() );
323         item.setMainPage ( itemVal.getMainPage () );
324         item.setSpecialOffer ( itemVal.getSpecialOffer () );
325         
326         item.setTypeId ( typeHome.findByName ( itemVal.getTypeId() ) );
327         
328         
329         //Clear any existing Properties
330
Collection props = item.getProperties();
331         Iterator propIt = props.iterator();
332         while ( propIt.hasNext() ) {
333             PropertyLocal prop = (PropertyLocal) propIt.next();
334             propIt.remove();
335             prop.remove();
336         }
337         
338         //Load new Properties
339
Collection newProps = itemVal.getProperties();
340         Iterator newPropIt = newProps.iterator();
341         while ( newPropIt.hasNext() ) {
342             PropertyValue newProp = (PropertyValue) newPropIt.next();
343             PropertyLocal newPropLocal = propHome.create (
344                     newProp.getName(), newProp.getValue() );
345             props.add ( newPropLocal );
346         }
347         
348         //Clear any existing Pictures
349
Collection pics = item.getPictures();
350         Iterator picIt = pics.iterator();
351         while ( picIt.hasNext() ) {
352             olstore.entity.PictureLocal pic = (olstore.entity.PictureLocal) picIt.next();
353             picIt.remove();
354             pic.remove();
355         }
356         
357         //Load new Pictures
358
Collection newPics = itemVal.getPictures();
359         Iterator newPicIt = newPics.iterator();
360         while ( newPicIt.hasNext() ) {
361             PictureValue newPic = (PictureValue) newPicIt.next();
362             olstore.entity.PictureLocal newPicLocal = picHome.create (
363                     newPic.getLocation(),
364                     newPic.getText(),
365                     newPic.getPriority() );
366             pics.add ( newPicLocal );
367         }
368         
369         
370         
371     }
372     
373     private ItemValue ItemToDTO ( olstore.entity.ItemLocal item ) throws Exception {
374         ItemValue itemVal = new ItemValue();
375         BeanUtils.copyProperties ( itemVal, item );
376         
377         //Special Conversions
378
itemVal.setTypeId ( item.getTypeId().getName() );
379         
380         //properties
381
Vector newProps = new Vector();
382         Collection oldProps = item.getProperties();
383         Iterator it = oldProps.iterator();
384         while ( it.hasNext() ) {
385             PropertyLocal prop = (PropertyLocal)it.next();
386             PropertyValue propVal = new PropertyValue();
387             BeanUtils.copyProperties ( propVal, prop );
388             newProps.add ( propVal );
389         }
390         itemVal.setProperties ( newProps );
391         
392         //pictures
393
Vector newPics = new Vector();
394         Collection oldPics = item.getPictures();
395         Iterator pics = oldPics.iterator();
396         while ( pics.hasNext() ) {
397             olstore.entity.PictureLocal pic = (olstore.entity.PictureLocal)pics.next();
398             PictureValue picVal = new PictureValue();
399             BeanUtils.copyProperties ( picVal, pic );
400             newPics.add ( picVal );
401         }
402         itemVal.setPictures ( newPics );
403         
404         return itemVal;
405     }
406     
407     /**
408      *
409      * @param itemId
410      * @param username
411      * @throws Exception
412      *
413      * @ejb.interface-method
414      */

415     
416     public void markItemAsInteresting ( String itemId, String username ) throws Exception {
417         EJBHomeFactory factory=EJBHomeFactory.getInstance();
418         ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
419         UserLocalHome userHome = (UserLocalHome)factory.getLocalHome(EJBHomeFactory.USER);
420         
421         olstore.entity.ItemLocal itemLocal = home.findByPrimaryKey ( new Integer ( itemId ) );
422         UserLocal user = userHome.findByUsername ( username );
423         
424         Collection purchased = user.getPurchased ();
425         if ( ! purchased.contains ( itemLocal ) ) {
426             purchased.add ( itemLocal );
427             user.setPurchased ( purchased );
428         }
429     }
430     
431 }
432
Popular Tags