1 27 28 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 101 102 public abstract class ItemHelperBean implements SessionBean { 103 SessionContext ejbContext; 104 105 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 132 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 { 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 item.setMainPage (java.lang.Boolean.FALSE); 181 item.setSpecialOffer (java.lang.Boolean.FALSE); 182 183 return item; 184 } 185 186 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 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 238 public Vector getPropertiesForType ( String type ) throws Exception { 239 240 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 return propValueArr; 255 } 256 257 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 282 283 public void saveItem ( ItemValue itemVal ) throws Exception { 284 285 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 307 308 private void updateItem ( olstore.entity.ItemLocal item , ItemValue itemVal ) throws Exception { 309 310 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 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 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 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 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 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 itemVal.setTypeId ( item.getTypeId().getName() ); 379 380 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 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 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 |