public void claimAuction(WinnerDetails auctionObj) { System.out.println( "In claim auction with currentAuctionkey as " + currentAuctionKey + "and product as " + auctionObj.getProductID()); System.out.println("In claim auction"); ClaimDetails claimDetailsObj = new ClaimDetails(); claimDetailsObj.setAuctionId(currentAuctionKey); claimDetailsObj.setProductId(auctionObj.getProductID()); ClientConfig config = new DefaultClientConfig(); Client remoteClient = Client.create(config); DBClient dbClient = DBClient.getInstance(); BasicDBObject jsonAddr = dbClient.getClusterAddress("cars"); String restAddr = "http://" + jsonAddr.getString("ip") + ":" + jsonAddr.getInt("port") + "/DistributedAuction/rest/claimAuction"; System.out.println("Contacting " + restAddr + " for claiming auction"); WebResource webResource = remoteClient.resource(restAddr); ClientResponse response = null; try { response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, claimDetailsObj); String isSuccess = response.getEntity(String.class); FacesMessage message = null; if (isSuccess.equals("true")) { message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Claim successfully completed !!! ", null); } else { message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Claim failed !!! ", null); } FacesContext.getCurrentInstance().addMessage(null, message); } catch (Exception e) { e.printStackTrace(); } }
public void claimSubscriptionAuction() { System.out.println("In claim subscription"); SubscriptionAuctionDetails obj = new SubscriptionAuctionDetails(); System.out.println("Current auction key:" + currentAuctionKey); obj.setAuctionId(currentAuctionKey); obj.setUserId(getUserObj().getName()); if (currentAuctionKey == null) { return; } ClientConfig config = new DefaultClientConfig(); Client remoteClient = Client.create(config); DBClient dbClient = DBClient.getInstance(); BasicDBObject jsonAddr = dbClient.getClusterAddress("cars"); String restAddr = "http://" + jsonAddr.getString("ip") + ":" + jsonAddr.getInt("port") + "/DistributedAuction/rest/claimSubscriptionAuction"; System.out.println("Contacting " + restAddr + " for claiming subscription"); WebResource webResource = remoteClient.resource(restAddr); ClientResponse response = null; try { response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, obj); String isSuccess = response.getEntity(String.class); FacesMessage message = null; if (isSuccess.equals("success")) { message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successfully subscribed !!! ", null); } else { message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Subscription failed !!! ", null); } FacesContext.getCurrentInstance().addMessage(null, message); } catch (Exception e) { e.printStackTrace(); } }
public static AuctionResults getAuctionResults(String auctionID) { AuctionResults results = null; BasicDBObject auctionDetails = getAuctionDetails(auctionID); Boolean atLeastOneDealClaimed = false; if (auctionDetails != null) { BasicDBList localBids = (BasicDBList) ((BasicDBObject) auctionDetails.get(AuctionServerPersistance.FIELD_LOCAL_RESULTS)) .get(AuctionServerPersistance.FIELD_BIDS); BasicDBList remoteBids = (BasicDBList) ((BasicDBObject) auctionDetails.get(AuctionServerPersistance.FIELD_REMOTE_RESULTS)) .get(AuctionServerPersistance.FIELD_BIDS); List<WinnerDetails> localWinners = new ArrayList<WinnerDetails>(); List<WinnerDetails> remoteWinners = new ArrayList<WinnerDetails>(); List<String> productIDs = new ArrayList<String>(); for (Object bid : localBids) { BasicDBObject bidObj = (BasicDBObject) bid; productIDs.add(bidObj.getString(AuctionServerPersistance.FIELD_PRODUCT_ID)); if (bidObj.containsField(AuctionServerPersistance.FIELD_CLAIMED) && bidObj.getBoolean(AuctionServerPersistance.FIELD_CLAIMED)) { atLeastOneDealClaimed = true; } } for (Object bid : remoteBids) { BasicDBObject bidObj = (BasicDBObject) bid; productIDs.add(bidObj.getString(AuctionServerPersistance.FIELD_PRODUCT_ID)); if (bidObj.containsField(AuctionServerPersistance.FIELD_CLAIMED) && bidObj.getBoolean(AuctionServerPersistance.FIELD_CLAIMED)) { atLeastOneDealClaimed = true; } } DBClient client = DBClient.getInstance(); Map<String, BasicDBObject> productDetails = client.getProductDetails(productIDs); /* * for (String s : productDetails.keySet()) { * System.out.println(productDetails.get(s)); } */ for (Object bid : localBids) { BasicDBObject bidObj = (BasicDBObject) bid; BasicDBObject product = productDetails.get(bidObj.getString(AuctionServerPersistance.FIELD_PRODUCT_ID)); WinnerDetails winnerDetails = new WinnerDetails( bidObj.getDouble(AuctionServerPersistance.FIELD_BID), product.getString(AuctionServer.FIELD_SELLER_ID), product.getString(AuctionServerPersistance.FIELD_PRODUCT_ID), product.getString(SellerDetails.FIELD_NAME), product.getString(SellerDetails.FIELD_MODEL), product.getString(SellerDetails.FIELD_ADDRESS), product.getString(SellerDetails.FIELD_IMAGE)); localWinners.add(winnerDetails); } for (Object bid : remoteBids) { BasicDBObject bidObj = (BasicDBObject) bid; BasicDBObject product = productDetails.get(bidObj.getString(AuctionServerPersistance.FIELD_PRODUCT_ID)); WinnerDetails winnerDetails = new WinnerDetails( bidObj.getDouble(AuctionServerPersistance.FIELD_BID), product.getString(AuctionServer.FIELD_SELLER_ID), product.getString(AuctionServerPersistance.FIELD_PRODUCT_ID), product.getString(SellerDetails.FIELD_NAME), product.getString(SellerDetails.FIELD_ADDRESS), product.getString(SellerDetails.FIELD_IMAGE)); remoteWinners.add(winnerDetails); } /* * System.out .println("For auction: " + auctionDetails * .getString(AuctionServerPersistance.FIELD_AUCTION_ID)); * System.out.println("Local winners"); for (WinnerDetails winner : * localWinners) { winner.printDetails(); } * * System.out.println("Remote winners"); for (WinnerDetails winner : * remoteWinners) { winner.printDetails(); } */ if (!auctionDetails.containsField(AuctionServerPersistance.FIELD_VIEWED_AT)) { System.out.println("Viewed at recorded"); BuyerCriteria criteria = new BuyerCriteria( (BasicDBObject) auctionDetails.get(AuctionServerPersistance.FIELD_BUYER_CRITERIA)); AuctionServerPersistance writer = new AuctionServerPersistance( criteria, auctionID, auctionDetails.getInt(AuctionServerPersistance.FIELD_VERSION)); writer.recordViewedAt(); } results = new AuctionResults(remoteWinners, localWinners, atLeastOneDealClaimed); } else { System.out.println("Auction still running!"); } return results; }
private static DBClient getDBClient() { DBClient dbClient = DBClient.getInstance(); return dbClient; }