/* * ==================================================================== * * TORPEDO * A Testbed of Object Relational Products for Enterprise Distributed Objects * Copyright (c) 2004 The Middleware Company All Rights Reserved * @author Bruce Martin * @version 8.25.04 * * ==================================================================== */ package com.middleware_company.torpedo.auction.jdo; import javax.jdo.*; import javax.rmi.PortableRemoteObject; import javax.naming.NamingException; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Collection; import java.util.Iterator; import java.util.ArrayList; import com.middleware_company.torpedo.auction.Auction; import com.middleware_company.torpedo.auction.User; import com.middleware_company.torpedo.auction.Bid; public class Persistence extends com.middleware_company.torpedo.auction.Persistence { private static Context _context=null; public Bid createBid( String ID, Auction auction, User buyer, Float amount, Float maxAmount) throws NamingException { PersistenceManager pm = getPersistenceManager(); Bid newBid = new JDOBid(ID,auction,buyer,amount,maxAmount); pm.makePersistent(newBid); return newBid; } public Auction getAuction(String auctionID) throws NamingException { PersistenceManager pm = getPersistenceManager(); Object oid = pm.newObjectIdInstance(JDOAuction.class,auctionID); Object o = pm.getObjectById(oid,true); return (Auction) o; } public User getUser(String ID) throws NamingException { PersistenceManager pm = getPersistenceManager(); Object o = pm.getObjectById(pm.newObjectIdInstance(JDOUser.class,ID),false); return (User) o; } public Collection findAllAuctions() throws NamingException { PersistenceManager pm = getPersistenceManager(); Query q = pm.newQuery (JDOAuction.class); Collection auctions = (Collection) q.execute(); return auctions; } public Collection findHighBids(String auctionID) throws NamingException { // JDO QL does not support aggregate functions. // So we must do this in Java code, may be inefficient. Auction theAuction = getAuction(auctionID); Collection allBids = theAuction.getBids(); Collection highBids = new ArrayList(); Iterator i = allBids.iterator(); float highAmount = 0; while (i.hasNext()) { float bidAmount = ((Bid)i.next()).getAmount().floatValue(); if (bidAmount > highAmount) highAmount = bidAmount; } // Have high amount -- now add any bids that match to collection i = allBids.iterator(); while (i.hasNext()) { Bid nextBid = (Bid)i.next(); float bidAmount = nextBid.getAmount().floatValue(); if (bidAmount == highAmount) highBids.add(nextBid); } return highBids; } private static PersistenceManager getPersistenceManager () throws NamingException { return getPersistenceManagerFactory ().getPersistenceManager (); } protected static PersistenceManagerFactory getPersistenceManagerFactory () throws NamingException { return (PersistenceManagerFactory) PortableRemoteObject.narrow (getContext ().lookup ("kodo"), PersistenceManagerFactory.class); } private static Context getContext () throws NamingException { if (_context == null) _context = new InitialContext (); return _context; } }