@Override
	public int addNewBid(String auctionId, String bidPrice, String userName) {
		// Declare a bidStatus variable, where 0 = bid accepted. 1 = wrong bid price (too low). 2 = expired bid
		int bidStatus = 0;
		System.out.println("++++++++ START addNewBid()++++++++++");
		TypedQuery<Bid> bidQuery = emgr.createNamedQuery("Bid.findBidByAuction", Bid.class);
		bidQuery.setParameter("auctionId", Integer.parseInt(auctionId));
		try {
			Bid bid = bidQuery.getSingleResult();
			// Check if the given bidPrice is above the current bid's price
			if (Double.valueOf(bidPrice) > bid.getCurrentHighest()) {
				// Check if the auction isn't expired
				Auction auction = emgr.find(Auction.class, Integer.parseInt(auctionId));

				// Obtain the current date and time
				Calendar currentDateCalendar = Calendar.getInstance();
				System.out.println("CURRENT DATE " + currentDateCalendar.getTime().toString());
				// Obtain the end time and date
				Time endTime = auction.getEndTime();
				System.out.println("END TIME = " + endTime.toString());
				Calendar endDateCal = new GregorianCalendar();
				endDateCal.setTime(auction.getEndDate());
				System.out.println("PRE- END DATE " + endDateCal.getTime().toString());
				
				// Assign the time to the end date calendar
				String[] splitEndTime = endTime.toString().split(":");
				
				endDateCal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitEndTime[0]));
				endDateCal.set(Calendar.MINUTE, Integer.parseInt(splitEndTime[1]));
				endDateCal.set(Calendar.SECOND, Integer.parseInt(splitEndTime[2]));
				System.out.println("END DATE " + endDateCal.getTime().toString());
		
				if (currentDateCalendar.after(endDateCal)) {
					System.out.println("EXPIRED!!!!!");
					bidStatus = 2;
					//TODO Set the auction as expired.
				} else {
					System.out.println("NOT EXPIRED!");
					// Update the Bid record
					
					bid.setAuctionId(Integer.parseInt(auctionId));
					int consequence = bid.getBidConsequence() + 1;
					bid.setBidConsequence(consequence);
					bid.setBidDate(currentDateCalendar.getTime());
					Time startTime = new Time(currentDateCalendar.getTimeInMillis());
					bid.setBidTime(startTime);
					bid.setCurrentHighest(Double.valueOf(bidPrice));
					bid.setUserName(userName);
					
					// Add new Bid History
					HistoryBid historyBid = new HistoryBid();
					historyBid.setBidAmount(Double.valueOf(bidPrice));
					historyBid.setBidDate(currentDateCalendar.getTime());
					historyBid.setBidId(bid.getBidId());
					historyBid.setBidTime(startTime);
					historyBid.setConsequence(consequence);
					historyBid.setUserName(userName);
					
					//XXX may not work.
					emgr.merge(bid);
					emgr.persist(historyBid);
				}
				
			} else { // given bidPrice is equal or below the current highest
				bidStatus = 1;
			}
		} catch (NoResultException e) {
			// First bid on the auction
			
			// Check if the given bidPrice is above the start price of the auction
			Auction auction = emgr.find(Auction.class, Integer.parseInt(auctionId));
			String startPrice = auction.getStartPrice();
			if (Double.valueOf(bidPrice) > Double.valueOf(startPrice)) {
				// Check if the auction isn't expired
				
				// Obtain the current date and time
				Calendar currentDateCalendar = Calendar.getInstance();
				System.out.println("CURRENT DATE " + currentDateCalendar.getTime().toString());
				// Obtain the end time and date
				Time endTime = auction.getEndTime();
				System.out.println("END TIME = " + endTime.toString());
				Calendar endDateCal = new GregorianCalendar();
				endDateCal.setTime(auction.getEndDate());
				System.out.println("PRE- END DATE " + endDateCal.getTime().toString());
				
				// Assign the time to the end date calendar
				String[] splitEndTime = endTime.toString().split(":");
				
				endDateCal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitEndTime[0]));
				endDateCal.set(Calendar.MINUTE, Integer.parseInt(splitEndTime[1]));
				endDateCal.set(Calendar.SECOND, Integer.parseInt(splitEndTime[2]));
				System.out.println("END DATE " + endDateCal.getTime().toString());
		
				if (currentDateCalendar.after(endDateCal)) {
					System.out.println("EXPIRED!!!!!");
					bidStatus = 2;
					//TODO Set the auction as expired.
				} else {
					System.out.println("NOT EXPIRED!");
					
					// Add the new Bid
					Bid bid = new Bid();
					bid.setBidConsequence(1);
					bid.setAuctionId(Integer.valueOf(auctionId));
					bid.setBidDate(currentDateCalendar.getTime());
					Time startTime = new Time(currentDateCalendar.getTimeInMillis());
					bid.setBidTime(startTime);
					bid.setCurrentHighest(Double.valueOf(bidPrice));
					bid.setUserName(userName);
					
					emgr.persist(bid);
					emgr.flush();
					
					// Add the new BidHistory
					HistoryBid historyBid = new HistoryBid();
					historyBid.setBidAmount(Double.valueOf(bidPrice));
					historyBid.setBidDate(currentDateCalendar.getTime());
					historyBid.setBidId(bid.getBidId());
					Time startTimeOfBid = new Time(currentDateCalendar.getTimeInMillis());
					historyBid.setBidTime(startTimeOfBid);
					historyBid.setConsequence(1);
					historyBid.setUserName(userName);
					emgr.persist(historyBid);
				}
				
			} else { // bidPrice is lower/equal than start price
				bidStatus = 1;
			}
			
			
		}
		System.out.println("++++++++ END addNewBid()++++++++++");
		return bidStatus;
	} // End of addNewBid()