1 package xpetstore.services.petstore.ejb; 2 3 4 import java.sql.SQLException ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.Date ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 11 12 import javax.ejb.EJBException ; 13 import javax.ejb.Local ; 14 import javax.ejb.Stateless ; 15 import javax.ejb.TransactionAttribute ; 16 import javax.ejb.TransactionAttributeType ; 17 18 import javax.persistence.EntityManager; 19 import javax.persistence.PersistenceContext; 20 21 import org.jboss.annotation.ejb.LocalBinding; 22 23 import xpetstore.domain.catalog.ejb.Category; 24 import xpetstore.domain.catalog.ejb.Item; 25 import xpetstore.domain.catalog.ejb.Product; 26 import xpetstore.domain.customer.ejb.Customer; 27 import xpetstore.domain.order.ejb.Order; 28 import xpetstore.domain.order.ejb.OrderItem; 29 import xpetstore.domain.order.model.OrderStatus; 30 31 import xpetstore.domain.signon.ejb.Account; 32 33 import xpetstore.services.petstore.exceptions.CartEmptyOrderException; 34 import xpetstore.services.petstore.exceptions.DuplicateEmailException; 35 36 import xpetstore.util.JMSUtil; 37 import xpetstore.util.JNDINames; 38 import xpetstore.util.Page; 39 40 41 110 @Stateless (name="Petstore") 111 @LocalBinding(jndiBinding="ejb/Petstore") 112 @Local (Petstore.class) 113 @TransactionAttribute (TransactionAttributeType.REQUIRED) 114 public class PetstoreBean implements Petstore 115 { 116 118 @PersistenceContext 119 private EntityManager manager; 120 121 128 public boolean authenticate( String userId, 129 String password ) 130 { 131 try 132 { 133 Account act = manager.find(Account.class, userId ); 134 System.out.println("!! PetstoreBean.authenticate " + act + " " + userId + " " + password); 135 if (act == null) 136 return false; 137 return act.matchPassword( password ); 138 } 139 catch (Exception e) 140 { 141 e.printStackTrace(); 142 return false; 143 } 144 } 145 146 147 148 153 public Category getCategory( String categoryId ) 154 { 155 return manager.find(Category.class, categoryId ); 156 } 157 158 163 public Page getCategories( int start, 164 int count ) 165 { 166 return toPage( manager.createQuery("SELECT Category c FROM Category").getResultList(), start, count, Category.class ); 167 } 168 169 174 public Product getProduct( String productId ) 175 { 176 return manager.find( Product.class, productId ); 177 } 178 179 182 public Product getProductByItem( String itemId ) 183 { 184 Item item = manager.find( Item.class, itemId ); 185 return item.getProduct( ); 186 } 187 188 191 public Page getProducts( String categoryId, 192 int start, 193 int count ) 194 { 195 try 196 { 197 System.out.println("!! PetstoreBean.getProducts manager " + manager); 198 Category cat = manager.find( Category.class, categoryId ); 199 System.out.println("!! PetstoreBean.getProducts cat " + cat); 200 if (cat != null) 201 return toPage( cat.getProducts( ), start, count, Product.class ); 202 else 203 return Page.EMPTY_PAGE; 204 } 205 catch (Exception e) 206 { 207 e.printStackTrace(); 208 return null; 209 } 210 } 211 212 217 public Page searchProducts( String key, 218 int start, 219 int count ) 220 { 221 return toPage(manager.createNativeQuery("SELECT productId,name,description FROM T_PRODUCT WHERE (productId LIKE " + key + ") OR (name LIKE " + key + ") OR (description LIKE " + key + ")").getResultList(), start, count, Product.class); 222 } 223 224 229 public Item getItem( String itemId ) 230 { 231 return manager.find( Item.class, itemId ); 232 } 233 234 237 public Page getItems( String productId, 238 int start, 239 int count ) 240 { 241 Product prod = manager.find( Product.class, productId ); 242 return toPage( prod.getItems( ), start, count, Item.class ); 243 } 244 245 248 public String createCustomer( Customer customer ) 249 throws DuplicateEmailException 250 { 251 System.out.println("!!PetstoreBean.createCustomer " + customer); 252 253 254 String email = customer.getEmail( ); 255 List customers = null; 256 try 257 { 258 customers = manager.createQuery("SELECT c FROM Customer c WHERE c.email = '" + email + "'").getResultList(); 259 260 } 261 catch (Exception e) 262 { 263 264 } 265 266 if (customers != null && customers.size() > 0) 267 throw new DuplicateEmailException( email ); 268 269 270 Account account = customer.getAccount( ); 271 272 customer.setUserId(account.getUserId()); 273 274 manager.persist(account); 275 276 manager.persist(customer); 277 278 System.out.println("!!PetstoreBean.createCustomer userId " + customer); 279 280 return customer.getUserId( ); 281 } 282 283 286 public void updateCustomer( Customer customer ) 287 { 288 manager.merge(customer); 289 } 290 291 294 public Customer getCustomer( String userId ) 295 { 296 return manager.find( Customer.class, userId ); 297 } 298 299 309 public Integer createOrder( String userId, 310 Date orderDate, 311 Map items ) 312 throws CartEmptyOrderException 313 { 314 315 if ( items.size( ) == 0 ) 316 { 317 throw new CartEmptyOrderException( ); 318 } 319 320 321 Customer cst = manager.find( Customer.class, userId ); 322 323 324 Order order = new Order( ); 325 order.setOrderDate( orderDate ); 326 order.setStatus( OrderStatus.PENDING ); 327 order.setStreet1( cst.getStreet1( ) ); 328 order.setStreet2( cst.getStreet2( ) ); 329 order.setCity( cst.getCity( ) ); 330 order.setState( cst.getState( ) ); 331 order.setZipcode( cst.getZipcode( ) ); 332 order.setCountry( cst.getCountry( ) ); 333 order.setCreditCardNumber( cst.getCreditCardNumber( ) ); 334 order.setCreditCardType( cst.getCreditCardType( ) ); 335 order.setCreditCardExpiryDate( cst.getCreditCardExpiryDate( ) ); 336 337 order.setCustomer( cst ); 338 339 manager.persist(order); 340 341 342 Iterator keys = items.keySet( ).iterator( ); 343 while ( keys.hasNext( ) ) 344 { 345 String itemId = ( String ) keys.next( ); 346 Integer qty = ( Integer ) items.get( itemId ); 347 348 Item item = manager.find(Item.class, itemId); 349 350 System.out.println("!!PetstoreBean.createOrder item " + item + " " + itemId); 351 352 OrderItem orderItem = new OrderItem( qty, item.getListPrice( ) ); 353 orderItem.setItem( item ); 354 355 manager.persist(orderItem); 356 357 order.addOrderItem(orderItem); 358 359 order.addOrderItem( orderItem ); 360 } 361 362 363 try 364 { 365 JMSUtil.sendToJMSQueue( JNDINames.QUEUE_ORDER, order.getOrderUId( ), false ); 366 } 367 catch ( Exception e ) 368 { 369 throw new EJBException ( e ); 370 } 371 372 return order.getOrderUId( ); 373 } 374 375 380 public Page getCustomerOrders( String userId, 381 int start, 382 int count ) 383 { 384 List col = manager.createQuery("SELECT Order o FROM Order WHERE o.customer.userId = " + userId).getResultList(); 385 return toPage( col, start, count, Order.class ); 386 } 387 388 393 public Order getOrder( Integer orderUId ) 394 { 395 return manager.find( Order.class, orderUId ); 396 } 397 398 401 public Page getOrderItems( Integer orderUId, 402 int start, 403 int count ) 404 { 405 Collection col; 406 407 Order order = manager.find( Order.class, orderUId ); 408 if (order != null) 409 col = order.getOrderItems( ); 410 else 411 col = new ArrayList ( ); 412 413 return toPage( col, start, count, OrderItem.class ); 414 } 415 416 private Page toPage( Collection col, 417 int start, 418 int count, 419 Class type ) 420 { 421 int size = col.size( ); 422 if ( size == 0 ) 423 { 424 return Page.EMPTY_PAGE; 425 } 426 427 ArrayList lst = new ArrayList ( ); 428 Iterator it = col.iterator( ); 429 for ( int i = 0, imax = start + count; ( i < imax ) && it.hasNext( ); 430 i++ ) 431 { 432 Object obj = it.next( ); 433 if ( i >= start ) 434 { 435 lst.add( obj ); 436 } 437 } 438 439 return new Page( lst, start, ( start + count ) < size ); 440 } 441 } 442 | Popular Tags |