I use home interface methods to implement functionality like you describe.
For example, I have an Auction been, that has AuctionLineItems, each of
which has a reserve price (minimum acceptable bid.)  To calculate the total
reserve amount of all items currently available at auction:
In AuctionHome, I have a method like
  public double reserveValueOfOpenAuctions()
  throws RemoteException, FinderException;
In AuctionBean, I have a corresponding
  public double ejbHomeReserveValueOfOpenAuctions()
  throws RemoteException, FinderException {
    try {
      java.sql.Timestamp now = new
java.sql.Timestamp(System.currentTimeMillis());
      Method[] methods = getClass().getMethods();
      Method getContext = null;
      for(int i = 0; i < methods.length; i++) {
        if(methods[i].getName().indexOf("getContext") != -1) {
          getContext = methods[i];
          break;
        }
      }
      EntityContext theCtx = null;
      if(getContext != null) {
        theCtx = (EntityContext) getContext.invoke(this, null);
        if(DEBUG_AUCTION_VALUE)System.out.println("theCtx " + theCtx);
      }
      AuctionHome auctionHome = (AuctionHome) theCtx.getEJBHome();
      ArrayList openAuctions = new ArrayList();
      Collection auctions = auctionHome.findAll();
      if(DEBUG_AUCTION_VALUE)System.out.println("auctions " +
auctions.size());
      double totalReserve = 0;
      Iterator iAuctions = auctions.iterator();
      while(iAuctions.hasNext()) {
        Auction auction = (Auction) iAuctions.next();
        AuctionState state = auction.getAuctionState();
        if(DEBUG_AUCTION_VALUE)System.out.println("state " + state);
        if(state.startTime == null || state.endTime == null ||
          ((now.before(state.endTime) && state.startTime.before(now) ) ||
                  state.isPending())
        ) {
          Collection items = auction.getAuctionLineItemStates();
          if(DEBUG_AUCTION_VALUE)System.out.println("items " +
items.size());
          Iterator iItems = items.iterator();
          while(iItems.hasNext()) {
            AuctionLineItemState itemState =
(AuctionLineItemState)iItems.next();
            totalReserve += itemState.reservePrice;
            if(DEBUG_AUCTION_VALUE) System.out.println(itemState + ",
reserve: " + itemState.reservePrice + "tr: " + totalReserve);
          }
        }
      }
      return totalReserve;
    }
    catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

Admitedly, the code that finds the EntityContext is ugly.  The class that
implements this method is, I believe, derived from
com.evermind.server.ejb.ContainerManagedObject.  For some reason, that class
does not expose a getContext(), but does have a __getContext().  I have to
use reflection because javac doesn't know anything about __getContext().
Ugly, but it does work.
----- Original Message -----
From: "Alex Paransky" <[EMAIL PROTECTED]>
To: "Orion-Interest" <[EMAIL PROTECTED]>
Sent: Thursday, May 31, 2001 5:53 PM
Subject: How to execute a SUM function???


> I need to do something like this:
>
> select sum(price) from lineitem;
>
> I have an entity bean called LineItem, with a field called price
> (getPrice()/setPrice(float)).  How can I make this work with Orion?
>
> Thanks.
> -AP_
>
>
>



Reply via email to