/* * ==================================================================== * * TORPEDO * A Testbed of Object Relational Products for Enterprise Distributed Objects * Copyright (c) 2004 The Middleware Company All Rights Reserved * @version 7.19.04 * * ==================================================================== */ package com.middleware_company.torpedo.auction.toplink; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import javax.naming.NamingException; import oracle.toplink.expressions.*; import oracle.toplink.queryframework.*; import oracle.toplink.threetier.Server; import oracle.toplink.tools.sessionconfiguration.*; import oracle.toplink.tools.sessionmanagement.*; import oracle.toplink.sessions.*; import oracle.toplink.publicinterface.Descriptor; import com.middleware_company.torpedo.auction.AuctionServiceException; /** * Torpedo Persistence implementation for OracleAS TopLink 9.0.4. * This persists the Torpedo POJO Auction object model through the TopLink API. * This implementation uses several TopLink optimizations including, * - joining * - batch reading * - partial object reading * - batch writing * - parameterized SQL * - unit of work */ public class Persistence extends com.middleware_company.torpedo.auction.Persistence { protected Server serverSession; /** * Load the tunned TopLink session from the sessions.xml configuration file. */ public Server getServerSession() { if (serverSession == null) { serverSession = (Server) SessionManager.getManager().getSession(new XMLLoader("com/middleware_company/torpedo/auction/toplink/sessions.xml"), "TorpedoSession", getClass().getClassLoader()); } return serverSession; } /** * Return a unit of work if in a JTA transaction, otherwise a client session. */ public Session getActiveSession() { Session session = getServerSession().getActiveUnitOfWork(); if (session == null) { session = getServerSession().acquireClientSession(); } return session; } /** * Create a new bid, set the aution and buyer. */ public com.middleware_company.torpedo.auction.Bid createBid( String ID, com.middleware_company.torpedo.auction.Auction auction, com.middleware_company.torpedo.auction.User buyer, Float amount, Float maxAmount) throws AuctionServiceException,NamingException { List result = null; UnitOfWork unitOfWork = getServerSession().getActiveUnitOfWork(); Bid bid= new Bid(ID, auction, buyer, amount, maxAmount); unitOfWork.registerNewObject(bid); return bid; } /** * Find all auctions. * This uses joining to optimize the loading of the auction's item. * This uses batch reading to optimize the loading of the auction's bid's, * note that batch reading is used instead of joining because even though it causes an extra line of SQL, * it reads in the minimal amount of data and does not require an inefficient outerjoin, * joining would read a lot of duplicate data which is much less efficient for large reads. * This benifits joining in the bid's buyer's mapping to optimize the loading of the bid's buyer. */ public Collection findAllAuctions() throws AuctionServiceException, NamingException { Session session = getServerSession().getActiveSession(); ExpressionBuilder builder = new ExpressionBuilder(); ReadAllQuery auctionsQuery = new ReadAllQuery(Auction.class, builder); auctionsQuery.addJoinedAttribute(builder.getAllowingNull("item")); auctionsQuery.addBatchReadAttribute(builder.anyOfAllowingNone("bids")); List auctions = (List)session.executeQuery(auctionsQuery); return auctions; } /** * Find the high bid(s) for an auction. * If there are several bids at the same price, this may be multiple bids. * This uses a sub-select to find the max bid amout. * This benifits from joining in the bid's buyer's mapping to optimize the loading of the bid's buyer. */ public Collection findHighBids(String auctionID) throws AuctionServiceException, NamingException { Session session = getServerSession().getActiveSession(); // Max amount sub-query. ExpressionBuilder maxBid = new ExpressionBuilder(Bid.class); ReportQuery subQuery = new ReportQuery(Bid.class, maxBid); subQuery.addMaximum("amount"); subQuery.setSelectionCriteria(maxBid.get("auction").get("id").equal(auctionID)); // High bid query. ExpressionBuilder builder = new ExpressionBuilder(); ReadAllQuery bidsQuery = new ReadAllQuery(Bid.class, builder); bidsQuery.setSelectionCriteria(builder.get("auction").get("id").equal(auctionID).and(builder.get("amount").equal(subQuery))); List bids = (List)session.executeQuery(bidsQuery); return bids; } /** * Return the auction for the id. * This uses joining to optimize the loading of the auction's item. */ public com.middleware_company.torpedo.auction.Auction getAuction(String auctionID) throws AuctionServiceException, NamingException { Session session = getServerSession().getActiveSession(); ExpressionBuilder builder = new ExpressionBuilder(); ReadObjectQuery readAuctionQuery = new ReadObjectQuery(Auction.class, builder); readAuctionQuery.addJoinedAttribute(builder.getAllowingNull("item")); readAuctionQuery.setSelectionCriteria(builder.get("id").equal(auctionID)); Auction result = (Auction)session.executeQuery(readAuctionQuery); return result; } /** * Return an auction with only the required data. * This uses a partial object query. */ public com.middleware_company.torpedo.auction.Auction getPartialAuction(String auctionID) throws AuctionServiceException,NamingException { Session session = getServerSession().getActiveSession(); ExpressionBuilder builder = new ExpressionBuilder(); ReadObjectQuery readAuctionQuery = new ReadObjectQuery(Auction.class, builder); readAuctionQuery.addPartialAttribute("lowPrice"); readAuctionQuery.addPartialAttribute("id"); readAuctionQuery.addPartialAttribute("item"); readAuctionQuery.addPartialAttribute(builder.get("item").get("itemName")); readAuctionQuery.dontMaintainCache(); readAuctionQuery.setSelectionCriteria(builder.get("id").equal(auctionID)); Auction result = (Auction)session.executeQuery(readAuctionQuery); return result; } /** * Return the user for the id. */ public com.middleware_company.torpedo.auction.User getUser(String userID) throws AuctionServiceException, NamingException { Session session = getServerSession().getActiveSession(); ExpressionBuilder builder = new ExpressionBuilder(); ReadObjectQuery userQuery = new ReadObjectQuery(User.class, builder); userQuery.setSelectionCriteria(builder.get("id").equal(userID)); User result = (User)session.executeQuery(userQuery); return result; } }