/* * Copyright (c) 2004 Standard Performance Evaluation Corporation (SPEC) * All rights reserved. * * This source code is provided as is, without any express or implied warranty. * * History: * Date ID, Company Description * ---------- ---------------- ------------------------------------------------------------------ * 2003/01/01 John Stecher, IBM Created for SPECjAppServer2004 * 2003/11/22 Tom Daly , Sun Add support for finding items by category, don't use findAll() * of itemEnt, remove cache of ItemDataBeans, replace with a cache * of ItemEnt references instead * 2004/02/16 Samuel Kounev, Darmstadt Removed unneeded import statements. */ package org.spec.jappserver.orders.itemBrowserSes.ejb; /** * Bean implementation class for Enterprise Bean: ItemBrowserSes */ import java.util.ArrayList; import java.util.HashMap; import javax.ejb.EJBException; import javax.naming.InitialContext; import javax.naming.NamingException; import org.spec.jappserver.common.Debug; import org.spec.jappserver.common.DebugPrint; import org.spec.jappserver.orders.itement.ejb.ItemEntHomeLocal; import org.spec.jappserver.orders.itement.ejb.ItemEntLocal; public class ItemBrowserSesEJB implements javax.ejb.SessionBean { protected ItemEntHomeLocal itemEntHome; private static int itemsPerPage = 10; private javax.ejb.SessionContext sessionContext; private java.util.ArrayList itemsList; private int itemsListSize; private int currentMax, currentMin; private ArrayList search; private int querySize; private HashMap itemsMap; protected Debug debug; protected boolean debugging; /** * getSessionContext */ public javax.ejb.SessionContext getSessionContext() { return sessionContext; } /** * setSessionContext */ public void setSessionContext(javax.ejb.SessionContext ctx) { this.sessionContext = ctx; InitialContext initCtx; try { initCtx = new InitialContext(); } catch( Exception e ) { throw new EJBException("Cannot construct InitialContext!"); } try { int debugLevel = ((Integer) initCtx.lookup("java:comp/env/debuglevel")) .intValue(); if( debugLevel > 0 ) { debug = new DebugPrint(debugLevel, this); debugging = true; } else { debug = new Debug(); debugging = false; } } catch( NamingException ne ) { System.out.println("ItemBrowserSes: debuglevel Property not set. " + "Turning off debug messages"); debug = new Debug(); } try { itemEntHome = (ItemEntHomeLocal) initCtx.lookup("java:comp/env/ejb/ItemEntLocal"); } catch( NamingException ne ) { debug.printStackTrace(ne); throw new EJBException("Name not found " + ne); } } /** * ejbActivate */ public void ejbActivate() { } /** * ejbCreate */ public void ejbCreate() throws javax.ejb.CreateException { } /** * ejbPassivate */ public void ejbPassivate() { } /** * ejbRemove */ public void ejbRemove() { } // This method returns to the user a collection of 10 items // from the query they specified (All or specific vehicles). /** * @param queryItems * @param category what category of items/vehicles to search on * @return Collection of 10 items from the query they specified (All or specific vehicles) and within * the category specified */ public java.util.Collection getItems(ArrayList queryItems, int category) { ItemEntLocal iel =null; if( debugging ) debug.println(3, "getItems "); // Case of general query on all items if( queryItems == null ) { queryItems = new ArrayList(); if( itemsList == null ) { initItemsList(category); } for( int i = 0; (i < itemsPerPage) && (currentMax < itemsListSize); i++ ) { iel = (ItemEntLocal) itemsList.get(i); queryItems.add( iel.getDataBean() ); } currentMax = itemsPerPage; currentMin = 0; search = null; querySize = 0; // Case of specific query on items (vehicle1, vehicle2, etc.) } else { search = queryItems; queryItems = new ArrayList(); if( itemsList == null ) { initItemsList(category); } currentMin = 0; querySize = search.size(); for( currentMax = 0; (currentMax < querySize) && (currentMax < itemsPerPage); currentMax++ ) { iel = (ItemEntLocal) itemsMap.get(((String)search.get(currentMax))); queryItems.add(iel.getDataBean()); } } return queryItems; } // Returns the next 10 items in the query of items the user executed public java.util.Collection browseForward() { ItemEntLocal iel = null; if( debugging ) debug.println(3, "browseForward "); ArrayList itemsToReturn = new ArrayList(); currentMin = currentMax; // Case of all item query if( search == null ) { for( ; (currentMax < (currentMin+itemsPerPage)) && (currentMax < itemsListSize); currentMax++ ) { iel = (ItemEntLocal) itemsList.get(currentMax); itemsToReturn.add(iel.getDataBean()); //itemsToReturn.add(itemsList.get(currentMax)); } // Case of specific query } else { for( ; (currentMax < (currentMin+itemsPerPage)) && (currentMax < querySize); currentMax++ ) { iel = (ItemEntLocal) itemsMap.get(((String)search.get(currentMax))); itemsToReturn.add(iel.getDataBean()); //itemsToReturn.add(itemsMap.get(((String)search.get(currentMax)))); } } return itemsToReturn; } // Returns the previous 10 items in the query of items the user executed public java.util.Collection browseReverse() { ItemEntLocal iel = null; if( debugging ) debug.println(3, "browseReverse "); ArrayList itemsToReturn = new ArrayList(); currentMax = currentMin; // Case of all item query currentMin = currentMin - itemsPerPage; currentMax = currentMin; if( search == null ) { for( ; (currentMax < (currentMin+itemsPerPage)) && (currentMax < itemsListSize); currentMax++ ) { iel = (ItemEntLocal) itemsList.get(currentMax); itemsToReturn.add(iel.getDataBean()); } // Case of specific query } else { for( ; (currentMax < (currentMin+itemsPerPage)) && (currentMax < querySize); currentMax++ ) { iel = (ItemEntLocal) itemsMap.get(((String)search.get(currentMax))); itemsToReturn.add(iel.getDataBean()); //itemsToReturn.add(itemsMap.get(((String)search.get(currentMax)))); } } return itemsToReturn; } public int getTotalItems() { if( debugging ) debug.println(3, "getTotalItems "); if( search!=null ) { return querySize; } return itemsListSize; } // Inits the item list into the stateful session bean for lightening quick // access after the first hit on purchase inventory page. private void initItemsList(int category) { if( debugging ) debug.println(3, "initItemsList "); java.util.Collection items = null; try { //Tom Daly: replace findall, use categories to limit search //items = itemEntHome.findAll(); //System.out.println("DEBUG:> About to run findByCategory for category " + category); items = itemEntHome.findByCategory(category); //System.out.println("IBS DEBUG> Found " + items.size() + " items for category " + category ); } catch( javax.ejb.FinderException e ) { e.printStackTrace(); throw new javax.ejb.EJBException(e); } itemsMap = new HashMap(); itemsList = new ArrayList(); itemsListSize = 0; if( items != null ) { java.util.Iterator itr = items.iterator(); while( itr.hasNext() ) { ItemEntLocal iel = (ItemEntLocal) itr.next(); itemsMap.put( iel.getId(), iel ); itemsList.add( iel ); itemsListSize++; } } } /** * Returns the current max item the user is browsing (ie 20 if the user is browsing 11-20) * @return int */ public int getCurrentMax() { return currentMax; } /** * Returns the current min item the user is browsing (ie 11 if the user is browsing 11-20) * @return int */ public int getCurrentMin() { return currentMin; } }