/* * ==================================================================== * * 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 javax.jdo.spi.PersistenceCapable; 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; import com.middleware_company.torpedo.logger.TORPEDOLogger; import com.versant.core.jdo.VersantPersistenceManager; import com.versant.core.jdo.VersantQuery; /** * Tuned for Versant Open Access. */ public class Persistence extends com.middleware_company.torpedo.auction.Persistence { private static Context _context=null; private static boolean firstPM = true; 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); JDOBid newBid = new JDOBid(); newBid.id = ID; newBid.setAuction(auction); newBid.setBuyer(buyer); newBid.setAmount(amount); newBid.setMaxAmount(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,false); return (Auction) o; } public Auction getPartialAuction(String auctionID) throws NamingException { PersistenceManager pm = getPersistenceManager(); Object oid = pm.newObjectIdInstance(JDOAuction.class,auctionID); Object o = pm.getObjectById(oid,false); ((VersantPersistenceManager)pm).loadFetchGroup((PersistenceCapable)o, "partial"); 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); ((VersantQuery)q).setBounded(true); 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 { PersistenceManager pm = getPersistenceManagerFactory ().getPersistenceManager (); // Clear the log to get rid of once off initialization SQL (testing // connections etc.) as this skews the test results. This is only // done for the first PM retrieved from a PMF so is only an issue // for benchmarks like this. if (TORPEDOLogger.logger()!=null) { if (firstPM) { TORPEDOLogger.logger().clear(); firstPM = false; } } return pm; } protected static PersistenceManagerFactory getPersistenceManagerFactory () throws NamingException { return (PersistenceManagerFactory) PortableRemoteObject.narrow (getContext ().lookup ("versant"), PersistenceManagerFactory.class); } private static Context getContext () throws NamingException { if (_context == null) _context = new InitialContext (); return _context; } }