@Override
	public ArrayList<Map<String, String>> getAuctionsByUser(String userid) {
		ArrayList<Map<String, String>> auctions = new ArrayList<Map<String,String>>();
		TypedQuery<Auction> auctionsByUserQuery = emgr.createNamedQuery(
				"Auction.findAllAuctionsByUser", Auction.class);
		auctionsByUserQuery.setParameter("userName", userid);
		List<Auction> obtainedAuctions = (List<Auction>) auctionsByUserQuery
				.getResultList();
		for (int i = 0; i < obtainedAuctions.size(); i++) {
			Map<String, String> auctionMap = new HashMap<String,String>();
			
			auctionMap.put("auctionDescription", obtainedAuctions.get(i).getAuctionDescription());
			auctionMap.put("auctionName", obtainedAuctions.get(i).getAuctionName());
			auctionMap.put("startPrice", obtainedAuctions.get(i).getStartPrice());
			
			int auctionExpired = obtainedAuctions.get(i).getAuctionExpired();
			Date startDate = obtainedAuctions.get(i).getStartDate();
			Time startTime = obtainedAuctions.get(i).getStartTime();
			Date endDate = obtainedAuctions.get(i).getEndDate();
			Time endTime = obtainedAuctions.get(i).getEndTime();
			int auctionId = obtainedAuctions.get(i).getAuctionId();
			int itemId = obtainedAuctions.get(i).getItemId();
			
			Calendar currentCal = Calendar.getInstance();
			Calendar endCal = Calendar.getInstance();
			
			Calendar startDateCal = new GregorianCalendar();
			startDateCal.setTime(startDate);
			
			Calendar endDateCal = new GregorianCalendar();
			endDateCal.setTime(endDate);
			
			
			// 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]));
			
			// Assign the time to the start date calendar
			String[] splitStartTime = startTime.toString().split(":");
			
			startDateCal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitStartTime[0]));
			startDateCal.set(Calendar.MINUTE, Integer.parseInt(splitStartTime[1]));
			startDateCal.set(Calendar.SECOND, Integer.parseInt(splitStartTime[2]));
			
			// Put the calendar dates to the map
			auctionMap.put("startDate", startDateCal.getTime().toString());
			auctionMap.put("endDate", endDateCal.getTime().toString());
			
			Item item = emgr.find(Item.class, itemId);
			String itemName = item.getItemName();
			String itemDescription = item.getItemDescription();
			String itemModel = item.getItemModel();
			String itemCategory = item.getCategoryName();
			auctionMap.put("itemName", itemName);
			auctionMap.put("itemDescription", itemDescription);
			auctionMap.put("itemModel", itemModel);
			auctionMap.put("itemCategory", itemCategory);
			
			String auctionStatus = "default";
			
			
			if (auctionExpired == 1) {
				// Auction expired.
				auctionStatus = "expired";
				// Obtain the bid record of this auction.
				TypedQuery<Bid> bidQuery = emgr.createNamedQuery("Bid.findBidByAuction", Bid.class);
				bidQuery.setParameter("auctionId", auctionId);
				try {
					Bid obtainedBid = bidQuery.getSingleResult();
					int bidConsequence = obtainedBid.getBidConsequence();
					double currentHighestBid = obtainedBid.getCurrentHighest();
					
					auctionMap.put("bidConsequence", Integer.toString(bidConsequence));
					auctionMap.put("highestBid", Double.toString(currentHighestBid));
					
				} catch(NoResultException e) {
					System.out.println("Catched NoResultException - " + e.getMessage());
					auctionMap.put("bidConsequence", "0");
					auctionMap.put("highestBid", obtainedAuctions.get(i).getStartPrice());
					
				}
			} else if (auctionExpired == 0) {
				// Auction still ongoing.
				
				// Check whether the auction is expired
				
				
				
				
				
				
				
				
		
				if (currentCal.after(endDateCal)) {
					System.out.println("EXPIRED! Setting as expired in database.");
					
					// Set the auction as expired.
					Auction auction = emgr.find(Auction.class, obtainedAuctions.get(i).getAuctionId());
					auction.setAuctionExpired((short) 1);
					emgr.persist(auction);
					auctionStatus = "expired";
					
					// Obtain the bid record of this auction.
					TypedQuery<Bid> bidQuery = emgr.createNamedQuery("Bid.findBidByAuction", Bid.class);
					bidQuery.setParameter("auctionId", auctionId);
					try {
						Bid obtainedBid = bidQuery.getSingleResult();
						int bidConsequence = obtainedBid.getBidConsequence();
						double currentHighestBid = obtainedBid.getCurrentHighest();
						
						auctionMap.put("bidConsequence", Integer.toString(bidConsequence));
						auctionMap.put("highestBid", Double.toString(currentHighestBid));
						
					} catch(NoResultException e) {
						System.out.println("Catched NoResultException - " + e.getMessage());
						auctionMap.put("bidConsequence", "0");
						auctionMap.put("highestBid", obtainedAuctions.get(i).getStartPrice());
						
						
					}
				} else {
					System.out.println("Not yet expired.");
					auctionStatus = "ongoing";
					
					int daysBetween = Days.daysBetween(new DateTime(currentCal.getTime()), new DateTime(endDateCal.getTime())).getDays();
					
					// Obtain the time difference and compensate for 1 hour (since the calendar starts at 01:00 and not 00:00)
					Date dateDiff = new Date(endDateCal.getTime().getTime() - currentCal.getTime().getTime() - 3600000);
					SimpleDateFormat timeFormat = new SimpleDateFormat("H:mm:ss");
					System.out.println("Duration: " + timeFormat.format(dateDiff));
					
					/*tempMap.put("auctionName", obtainedAuctions.get(i).getAuctionName());
					tempMap.put("auctionDescription", obtainedAuctions.get(i).getAuctionDescription());
					tempMap.put("auctionId", Integer.toString(obtainedAuctions.get(i).getAuctionId()));*/
					auctionMap.put("auctionDaysLeft", Integer.toString(daysBetween));
					auctionMap.put("auctionTimeLeft", timeFormat.format(dateDiff));
					
					// Obtain the bid record of this auction.
					TypedQuery<Bid> bidQuery = emgr.createNamedQuery("Bid.findBidByAuction", Bid.class);
					bidQuery.setParameter("auctionId", auctionId);
					try {
						Bid obtainedBid = bidQuery.getSingleResult();
						int bidConsequence = obtainedBid.getBidConsequence();
						double currentHighestBid = obtainedBid.getCurrentHighest();
						
						auctionMap.put("bidConsequence", Integer.toString(bidConsequence));
						auctionMap.put("highestBid", Double.toString(currentHighestBid));
						
					} catch(NoResultException e) {
						System.out.println("Catched NoResultException - " + e.getMessage());
						auctionMap.put("bidConsequence", "0");
						auctionMap.put("highestBid", obtainedAuctions.get(i).getStartPrice());
						
					}
				} // End of else (not expired)
				
				
				
				
			}
			
			auctionMap.put("auctionStatus", auctionStatus);
			
			auctions.add(auctionMap);
		}
		return auctions;

	}