@Override public void notifyResult(AuctionItem item) { if (registeredUsers.containsKey(item.getCreator_id())) { RMIClientIntf creator = registeredUsers.get(item.getCreator_id()); try { creator.callBack("Your Auction has produced the following results :\n "); creator.callBack(item.getResult()); } catch (RemoteException e) { System.out.println("Creator no longer online\n"); } } else { System.out.println("Creator id does not exist\n"); } // Normally an auction with no creator registered should not exist but since this is a // simplified implementation // We allow bids on auctions with no creator (usually the initialised ones) for testing // purposes. if (item.numberOfBids() != 0) { System.out.println("Notifying bidders"); Iterator<Bid> it = item.getBidders(); while (it.hasNext()) { Bid bidder = it.next(); if (registeredUsers.containsKey(bidder.getUserId())) { RMIClientIntf client = registeredUsers.get(bidder.getUserId()); try { client.callBack(item.getResult()); } catch (RemoteException e) { System.out.println("Bidder with id " + bidder.getUserId() + " no longer online\n"); } } else { System.out.println("User id does not exist\n"); } } } }
@Override public boolean registerBid(double value, int uniqueUserId, int auctionId) throws RemoteException { RMIClientIntf clientIntf = registeredUsers.get(uniqueUserId); Bid bid = new Bid(value, uniqueUserId); if (!liveAuctionItems.containsKey(auctionId)) { clientIntf.callBack("Auction item id does not exist\n"); return false; } if (uniqueUserId == liveAuctionItems.get(auctionId).getCreator_id()) { clientIntf.callBack("Not able to register bid - bidder is the creator of this auction\n"); return false; } else { if (liveAuctionItems.get(auctionId).addBidder(bid)) { clientIntf.callBack( "Bid registered to auction with id " + auctionId + " and amount " + bid.getBid() + "\n"); return true; } else { clientIntf.callBack( "Bid rejected, user has already placed a bid for this auction, can only place higher bids\n"); return false; } } }
public static void newBids(Long id) { // count new bids long newBids = Bid.count( "from AuctionItem a join a.bids as b " + "where a.id = ? AND b.date > ?", id, request.date); // wait if needed if (newBids == 0) { await("1s"); } // return the JSON output of the new bids AuctionItem item = AuctionItem.findById(id); JsonObject json = new JsonObject(); json.addProperty("next", item.getNextBidPrice()); json.addProperty("top", item.getCurrentTopBid()); renderJSON(json.toString()); }
/** * Goes trough the table auction and checks which auctions are over. If the auction is over, finds * all bids for that auction, selects the highest bid and sends winning message, also sends to all * other users message that auction is over. */ public static void checkAuctionOutcome() { // Declaring list of all auctions. List<Auction> auctions = finder.all(); // Declaring variable that represents current date. Date currentDate = new Date(); // Going trough all auctions. for (int i = 0; i < auctions.size(); i++) { // Declaring variable that represents auction ending date. Date auctionEndingDate = auctions.get(i).endingDate; // Checking if the auction is active and if the auction ending date is before current date. if (auctions.get(i).isActive && auctionEndingDate.before(currentDate)) { // Finding all auction bids. List<Bid> bids = Bid.getAuctionBids(auctions.get(i)); // Declaring variable that represents highest bid. Bid highestBid = bids.get(0); // Declaring string variable that represents winning message. String winningMessage = "Congratulations!!! You won bitBay auction for item #" + auctions.get(i).product.id + " - " + auctions.get(i).product.name + ".\n\r \n\r To proceed with transaction please contact: " + auctions.get(i).product.user.email; // Declaring and saving winning message. Message message = new Message( CommonHelpers.serviceUser(), highestBid.user, "Auction Winning", winningMessage); message.save(); // Sending SMS notification to auction winner. String sms = "Congratulations!!! You won bitBay auction for item #" + auctions.get(i).product.id + " - " + auctions.get(i).product.name + ". " + "This product has been sent to your cart"; if (highestBid.user.phoneNumber != null) { SmsHelper.sendSms(sms, highestBid.user.phoneNumber); } // After user won on auction create new cart if user don't have one, and put auction item to // his cart Cart cart = Cart.findCartByUser(highestBid.user); if (cart == null) { Cart newCart = new Cart(); newCart.user = highestBid.user; newCart.save(); CartItem cartItem = new CartItem(auctions.get(i).product, highestBid.user, newCart); cartItem.save(); } else { CartItem cartItem = new CartItem(auctions.get(i).product, highestBid.user, cart); cartItem.save(); } // Declaring string set that will contain emails of all users that have not had highest bid. Set<String> bidUsers = new HashSet<>(); // Adding all user emails to the set. for (int j = 0; j < bids.size(); j++) { bidUsers.add(bids.get(j).user.email); } // Removing email of highest bit user from the set. bidUsers.remove(highestBid.user.email); // Declaring string variable that represents losing message. String losingMessage = "Biding for item #" + auctions.get(i).product.id + " - " + auctions.get(i).product.name + " is over.\n\r \n\r We are sorry to inform you that your bid was not enough."; // Declaring iterator list of all user emails. Iterator<String> iter = bidUsers.iterator(); // Going trough all emails and sending message. while (iter.hasNext()) { User receiver = User.getUserByEmail(iter.next()); Message msg = new Message(CommonHelpers.serviceUser(), receiver, "Auction results", losingMessage); msg.save(); } // Setting auction status to inactive. auctions.get(i).isActive = false; auctions.get(i).update(); } } }