001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Aizaz Ahmed
022: * Vivek Lakshmanan
023: * Andrew Overholt
024: * Matthew Wringe
025: *
026: */package olstore.session.helper;
027:
028: import java.util.Collection;
029: import java.util.Iterator;
030: import java.util.Vector;
031: import java.math.BigDecimal;
032:
033: import javax.ejb.CreateException;
034: import javax.ejb.EJBException;
035: import javax.ejb.SessionBean;
036: import javax.ejb.SessionContext;
037: import javax.naming.Context;
038: import javax.naming.InitialContext;
039: import javax.naming.NamingException;
040:
041: import olstore.dto.ItemValue;
042: import olstore.dto.PictureValue;
043: import olstore.dto.PropertyValue;
044: import olstore.framework.EJBHomeFactory;
045:
046: import org.apache.commons.beanutils.BeanUtils;
047:
048: import olstore.entity.ItemLocal;
049: import olstore.entity.ItemLocalHome;
050: import olstore.entity.PictureLocalHome;
051: import olstore.entity.PropertyLocal;
052: import olstore.entity.PropertyLocalHome;
053: import olstore.entity.TypeLocal;
054: import olstore.entity.TypeLocalHome;
055: import olstore.entity.UserLocal;
056: import olstore.entity.UserLocalHome;
057:
058: /**
059: * @ejb.bean name="ItemHelper"
060: * type="Stateless"
061: * view-type="local"
062: * local-jndi-name="ItemHelperLocal_L"
063: *
064: * @ejb.ejb-ref
065: * ejb-name="Type"
066: * view-type="local"
067: *
068: * @ejb.ejb-ref
069: * ejb-name="Item"
070: * view-type="local"
071: *
072: * @ejb.ejb-ref
073: * ejb-name="Property"
074: * view-type="local"
075: *
076: * @ejb.ejb-ref
077: * ejb-name="Picture"
078: * view-type="local"
079: *
080: * @ejb.transaction
081: * type="Required"
082: *
083: *--
084: * change this --> /transaction-type=/"Container"
085: *
086: * This is needed for JOnAS.
087: * If you are not using JOnAS you can safely remove the tags below.
088: * @jonas.bean ejb-name="ItemHelper"
089: * jndi-name="ItemHelperLocal"
090: *
091: *--
092: **/
093:
094: public abstract class ItemHelperBean implements SessionBean {
095: SessionContext ejbContext;
096:
097: // ------------------------------------------------------------------
098: // SessionBean implementation
099: // ------------------------------------------------------------------
100:
101: public void setSessionContext(SessionContext ctx) {
102: ejbContext = ctx;
103: }
104:
105: public void ejbRemove() {
106: }
107:
108: public void ejbCreate() throws CreateException {
109: }
110:
111: public void ejbPassivate() {
112: }
113:
114: public void ejbActivate() {
115: }
116:
117: // ------------------------------------------------------------------
118: // ItemHelper implementation
119: // ------------------------------------------------------------------
120:
121: /**
122: * A convenience method to help create new items. The fields
123: * {@param name}, {@param name}, {@param shortDescr}, {@param type}
124: * are mandatory.
125: *
126: * @ejb.interface-method
127: *
128: */
129:
130: public olstore.entity.ItemLocal createItem(String name,
131: String price, String shortDesc, String longDesc, String type)
132: throws NamingException, CreateException {
133:
134: Context initialContext = new InitialContext();
135: ItemLocal item = null;
136: EJBHomeFactory factory = EJBHomeFactory.getInstance();
137:
138: try {
139: ItemLocalHome itemHome = (ItemLocalHome) factory
140: .getLocalHome(EJBHomeFactory.ITEM);
141: item = itemHome.create(name, new BigDecimal(price));
142: item.setShortDesc(shortDesc);
143: if (longDesc == null) {
144: item.setLongDesc("");
145: } else {
146: item.setLongDesc(longDesc);
147: }
148: } catch (NamingException e) {
149: System.out.println("1: " + e.getMessage());
150: e.printStackTrace();
151: }
152:
153: //Try and get the appropriate type
154: try {
155: TypeLocalHome typeHome = (TypeLocalHome) factory
156: .getLocalHome(EJBHomeFactory.TYPE);
157: TypeLocal typeObj = typeHome.findByName(type);
158: item.setTypeId(typeObj);
159: } catch (NamingException e) {
160: System.out.println("2: " + e.getMessage());
161: e.printStackTrace();
162: } catch (Exception e) {
163: throw new EJBException("Could not register type: " + type
164: + " to new item", e);
165: }
166:
167: //Set default mainpage and special properties
168: item.setMainPage(java.lang.Boolean.FALSE);
169: item.setSpecialOffer(java.lang.Boolean.FALSE);
170:
171: return item;
172: }
173:
174: /**
175: * This method returns the first picture with priority 0.
176: * Will return a random picture if there is none with prioriy 0
177: *
178: * @ejb.interface-method
179: *
180: */
181: public olstore.entity.PictureLocal getPicture(
182: olstore.entity.ItemLocal item) {
183: olstore.entity.PictureLocal pic = null;
184: Collection pics = item.getPictures();
185: Iterator it = pics.iterator();
186: while (it.hasNext()) {
187: pic = (olstore.entity.PictureLocal) it.next();
188: if (pic.getPriority() == 0) {
189: break;
190: }
191: }
192:
193: return pic;
194: }
195:
196: /**
197: *
198: * @param itemId
199: * @return
200: * @throws Exception
201: *
202: * @ejb.interface-method
203: */
204:
205: public PictureValue getPicture(String itemId) throws Exception {
206: EJBHomeFactory factory = EJBHomeFactory.getInstance();
207: ItemLocalHome home = (ItemLocalHome) factory
208: .getLocalHome(EJBHomeFactory.ITEM);
209: olstore.entity.ItemLocal itemLocal = home
210: .findByPrimaryKey(new Integer(itemId));
211:
212: olstore.entity.PictureLocal picLocal = getPicture(itemLocal);
213: PictureValue picVal = new PictureValue();
214: if (picLocal != null) {
215: BeanUtils.copyProperties(picVal, picLocal);
216: }
217:
218: return picVal;
219: }
220:
221: /**
222: * This method will return an array of PropertyValues that indicate
223: * the default properties associated with the specified type
224: *
225: * @ejb.interface-method
226: */
227: public Vector getPropertiesForType(String type) throws Exception {
228:
229: //Load up the default properties for specified type
230: EJBHomeFactory factory = EJBHomeFactory.getInstance();
231: TypeLocalHome typeHome = (TypeLocalHome) factory
232: .getLocalHome(EJBHomeFactory.TYPE);
233: TypeLocal typeLocal = typeHome.findByName(type);
234: Collection props = typeLocal.getProperties();
235: Iterator it = props.iterator();
236: Vector propValueArr = new Vector();
237: while (it.hasNext()) {
238: PropertyLocal prop = (PropertyLocal) it.next();
239: PropertyValue propValue = new PropertyValue();
240: BeanUtils.copyProperties(propValue, prop);
241: propValueArr.add(propValue);
242: }
243: //Assume types have at least one property
244: return propValueArr;
245: }
246:
247: /**
248: *
249: * @param itemId
250: * @return
251: * @throws Exception
252: *
253: * @ejb.interface-method
254: */
255:
256: public ItemValue getItemValueForId(String itemId) throws Exception {
257:
258: EJBHomeFactory factory = EJBHomeFactory.getInstance();
259: ItemLocalHome itemHome = (ItemLocalHome) factory
260: .getLocalHome(EJBHomeFactory.ITEM);
261: olstore.entity.ItemLocal item = itemHome
262: .findByPrimaryKey(new Integer(itemId));
263: ItemValue itemVal = ItemToDTO(item);
264: return itemVal;
265: }
266:
267: /**
268: *
269: * @param itemVal
270: * @throws Exception
271: *
272: * @ejb.interface-method
273: */
274:
275: public void saveItem(ItemValue itemVal) throws Exception {
276:
277: //determine if it already exists
278: EJBHomeFactory factory = EJBHomeFactory.getInstance();
279: ItemLocalHome itemHome = (ItemLocalHome) factory
280: .getLocalHome(EJBHomeFactory.ITEM);
281: olstore.entity.ItemLocal item = null;
282:
283: Integer itemId = itemVal.getItemId();
284: if (itemId != null && itemId.intValue() != 0) {
285: item = itemHome.findByPrimaryKey(itemId);
286: } else {
287: item = itemHome.create(itemVal.getName(), itemVal
288: .getPrice());
289: }
290:
291: updateItem(item, itemVal);
292: }
293:
294: /**
295: *
296: * @param item
297: * @param itemVal
298: * @throws Exception
299: *
300: */
301:
302: private void updateItem(olstore.entity.ItemLocal item,
303: ItemValue itemVal) throws Exception {
304:
305: //Get instance vars we will need
306: EJBHomeFactory factory = EJBHomeFactory.getInstance();
307: PictureLocalHome picHome = (PictureLocalHome) factory
308: .getLocalHome(EJBHomeFactory.PIC);
309: TypeLocalHome typeHome = (TypeLocalHome) factory
310: .getLocalHome(EJBHomeFactory.TYPE);
311: PropertyLocalHome propHome = (PropertyLocalHome) factory
312: .getLocalHome(EJBHomeFactory.PROP);
313:
314: //Load up the values ( can't use BeanUtils here without a way to filter out
315: //certain fields (collections, PK, etc)
316: item.setName(itemVal.getName());
317: item.setPrice(itemVal.getPrice());
318: item.setShortDesc(itemVal.getShortDesc());
319: item.setLongDesc(itemVal.getLongDesc());
320: item.setMainPage(itemVal.getMainPage());
321: item.setSpecialOffer(itemVal.getSpecialOffer());
322:
323: item.setTypeId(typeHome.findByName(itemVal.getTypeId()));
324:
325: //Clear any existing Properties
326: Collection props = item.getProperties();
327: Iterator propIt = props.iterator();
328: while (propIt.hasNext()) {
329: PropertyLocal prop = (PropertyLocal) propIt.next();
330: propIt.remove();
331: prop.remove();
332: }
333:
334: //Load new Properties
335: Collection newProps = itemVal.getProperties();
336: Iterator newPropIt = newProps.iterator();
337: while (newPropIt.hasNext()) {
338: PropertyValue newProp = (PropertyValue) newPropIt.next();
339: PropertyLocal newPropLocal = propHome.create(newProp
340: .getName(), newProp.getValue());
341: props.add(newPropLocal);
342: }
343:
344: //Clear any existing Pictures
345: Collection pics = item.getPictures();
346: Iterator picIt = pics.iterator();
347: while (picIt.hasNext()) {
348: olstore.entity.PictureLocal pic = (olstore.entity.PictureLocal) picIt
349: .next();
350: picIt.remove();
351: pic.remove();
352: }
353:
354: //Load new Pictures
355: Collection newPics = itemVal.getPictures();
356: Iterator newPicIt = newPics.iterator();
357: while (newPicIt.hasNext()) {
358: PictureValue newPic = (PictureValue) newPicIt.next();
359: olstore.entity.PictureLocal newPicLocal = picHome.create(
360: newPic.getLocation(), newPic.getText(), newPic
361: .getPriority());
362: pics.add(newPicLocal);
363: }
364:
365: }
366:
367: private ItemValue ItemToDTO(olstore.entity.ItemLocal item)
368: throws Exception {
369: ItemValue itemVal = new ItemValue();
370: BeanUtils.copyProperties(itemVal, item);
371:
372: //Special Conversions
373: itemVal.setTypeId(item.getTypeId().getName());
374:
375: //properties
376: Vector newProps = new Vector();
377: Collection oldProps = item.getProperties();
378: Iterator it = oldProps.iterator();
379: while (it.hasNext()) {
380: PropertyLocal prop = (PropertyLocal) it.next();
381: PropertyValue propVal = new PropertyValue();
382: BeanUtils.copyProperties(propVal, prop);
383: newProps.add(propVal);
384: }
385: itemVal.setProperties(newProps);
386:
387: //pictures
388: Vector newPics = new Vector();
389: Collection oldPics = item.getPictures();
390: Iterator pics = oldPics.iterator();
391: while (pics.hasNext()) {
392: olstore.entity.PictureLocal pic = (olstore.entity.PictureLocal) pics
393: .next();
394: PictureValue picVal = new PictureValue();
395: BeanUtils.copyProperties(picVal, pic);
396: newPics.add(picVal);
397: }
398: itemVal.setPictures(newPics);
399:
400: return itemVal;
401: }
402:
403: /**
404: *
405: * @param itemId
406: * @param username
407: * @throws Exception
408: *
409: * @ejb.interface-method
410: */
411:
412: public void markItemAsInteresting(String itemId, String username)
413: throws Exception {
414: EJBHomeFactory factory = EJBHomeFactory.getInstance();
415: ItemLocalHome home = (ItemLocalHome) factory
416: .getLocalHome(EJBHomeFactory.ITEM);
417: UserLocalHome userHome = (UserLocalHome) factory
418: .getLocalHome(EJBHomeFactory.USER);
419:
420: olstore.entity.ItemLocal itemLocal = home
421: .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: }
|