// Reserve an item. @Override public boolean reserveCustomer(int id, int customerId, String key, String location, int price) { synchronized (syncLock) { Customer cust = (Customer) readData(id, Customer.getKey(customerId)); if (cust == null) { Trace.warn( "RM::reserveItem(" + id + ", " + customerId + ", " + key + ", " + location + ") failed: customer doesn't exist."); return false; } else { // Do reservation. cust.reserve(key, location, price); writeData(id, cust.getKey(), cust); Trace.warn( "RM::reserveItem(" + id + ", " + customerId + ", " + key + ", " + location + ") OK."); return true; } } }
private boolean cancelReserveItem(int id, int customerId, String key, String location) { synchronized (syncLock) { // Check if the item is available. ReservableItem item = (ReservableItem) readData(id, key); if (item == null) { Trace.warn( "RM::cancelReserveItem(" + id + ", " + customerId + ", " + key + ", " + location + ") failed: item doesn't exist."); return false; } else if (item.getReserved() == 0) { Trace.warn( "RM::cancelReserveItem(" + id + ", " + customerId + ", " + key + ", " + location + ") failed: no reservations."); return false; } else { // Increase the number of available items in the storage. item.setCount(item.getCount() + 1); item.setReserved(item.getReserved() - 1); Trace.warn( "RM::cancelReserveItem(" + id + ", " + customerId + ", " + key + ", " + location + ") OK."); return true; } } }
// Delete the entire item. protected boolean deleteItem(int id, String key) { Trace.info("RM::deleteItem(" + id + ", " + key + ") called."); synchronized (syncLock) { ReservableItem curObj = (ReservableItem) readData(id, key); // Check if there is such an item in the storage. if (curObj == null) { Trace.warn("RM::deleteItem(" + id + ", " + key + ") failed: " + " item doesn't exist."); return false; } else { if (curObj.getReserved() == 0) { removeData(id, curObj.getKey()); Trace.info("RM::deleteItem(" + id + ", " + key + ") OK."); return true; } else { Trace.info( "RM::deleteItem(" + id + ", " + key + ") failed: " + "some customers have reserved it."); return false; } } } }
@Override public void error(Throwable err) { Trace.warn( "WatchManager Reconnecting. There was an error watching connection {0}: ", err, conn.toString()); restart(); }
// deletes the entire item protected boolean deleteItem(int id, String key) { Trace.info("RM::deleteItem(" + id + ", " + key + ") called"); ReservableItem curObj = (ReservableItem) readData(id, key); Flight tempItem = new Flight(Integer.parseInt(curObj.getLocation()), curObj.getCount(), curObj.getPrice()); tempItem.setReserved(curObj.getReserved()); // Check if there is such an item in the storage if (curObj == null) { Trace.warn("RM::deleteItem(" + id + ", " + key + ") failed--item doesn't exist"); return false; } else { if (curObj.getReserved() == 0) { tempItem.setType(0); writeDataToLog(id, curObj.getKey(), tempItem); removeData(id, curObj.getKey()); Trace.info("RM::deleteItem(" + id + ", " + key + ") item deleted"); return true; } else { Trace.info( "RM::deleteItem(" + id + ", " + key + ") item can't be deleted because some customers reserved it"); return false; } } // if }
// Deletes customer from the database. public boolean deleteCustomer(int id, int customerID) throws RemoteException { Trace.info("RM::deleteCustomer(" + id + ", " + customerID + ") called"); Customer cust = (Customer) readData(id, Customer.getKey(customerID)); if (cust == null) { Trace.warn( "RM::deleteCustomer(" + id + ", " + customerID + ") failed--customer doesn't exist"); return false; } else { Customer temp = cust.clone(); // Increase the reserved numbers of all reservable items which the customer reserved. RMHashtable reservationHT = cust.getReservations(); for (Enumeration e = reservationHT.keys(); e.hasMoreElements(); ) { String reservedkey = (String) (e.nextElement()); ReservedItem reserveditem = cust.getReservedItem(reservedkey); Trace.info( "RM::deleteCustomer(" + id + ", " + customerID + ") has reserved " + reserveditem.getKey() + " " + reserveditem.getCount() + " times"); ReservableItem item = (ReservableItem) readData(id, reserveditem.getKey()); Trace.info( "RM::deleteCustomer(" + id + ", " + customerID + ") has reserved " + reserveditem.getKey() + "which is reserved" + item.getReserved() + " times and is still available " + item.getCount() + " times"); Flight tempItem = new Flight(Integer.parseInt(item.getLocation()), item.getCount(), item.getPrice()); tempItem.setReserved(item.getReserved()); tempItem.setType(0); item.setReserved(item.getReserved() - reserveditem.getCount()); item.setCount(item.getCount() + reserveditem.getCount()); if (readDataFromLog(id, item.getKey(), id) == null) writeDataToLog(id, item.getKey(), tempItem); } // remove the customer from the storage temp.setType(1); if (readDataFromLog(id, cust.getKey(), id) == null) writeDataToLog(id, cust.getKey(), temp); removeData(id, cust.getKey()); Trace.info("RM::deleteCustomer(" + id + ", " + customerID + ") succeeded"); return true; } // if }
// return a bill public String queryCustomerInfo(int id, int customerID) throws RemoteException { Trace.info("RM::queryCustomerInfo(" + id + ", " + customerID + ") called"); Customer cust = (Customer) readData(id, Customer.getKey(customerID)); if (cust == null) { Trace.warn( "RM::queryCustomerInfo(" + id + ", " + customerID + ") failed--customer doesn't exist"); return ""; // NOTE: don't change this--WC counts on this value indicating a customer does not // exist... } else { String s = cust.printBill(); Trace.info("RM::queryCustomerInfo(" + id + ", " + customerID + "), bill follows..."); System.out.println(s); return s; } // if }
// Returns data structure containing customer reservation info. Returns null if the // customer doesn't exist. Returns empty RMHashtable if customer exists but has no // reservations. public RMHashtable getCustomerReservations(int id, int customerID) throws RemoteException { Trace.info("RM::getCustomerReservations(" + id + ", " + customerID + ") called"); Customer cust = (Customer) readData(id, Customer.getKey(customerID)); if (cust == null) { Trace.warn( "RM::getCustomerReservations failed(" + id + ", " + customerID + ") failed--customer doesn't exist"); return null; } else { return cust.getReservations(); } // if }
private IClient getClientFor(IProject project) { IClient client = project.accept( new CapabilityVisitor<IClientCapability, IClient>() { @Override public IClient visit(IClientCapability cap) { return cap.getClient(); } }, null); if (client == null) { Trace.warn( "Unable to start watch. Project {0} does not support IClientCapability", null, project.getName()); } return client; }
// Return a bill. @Override public String queryCustomerInfo(int id, int customerId) { Trace.info("RM::queryCustomerInfo(" + id + ", " + customerId + ") called."); synchronized (syncLock) { Customer cust = (Customer) readData(id, Customer.getKey(customerId)); if (cust == null) { Trace.warn( "RM::queryCustomerInfo(" + id + ", " + customerId + ") failed: customer doesn't exist."); // Returning an empty bill means that the customer doesn't exist. return ""; } else { String s = cust.printBill(); Trace.info("RM::queryCustomerInfo(" + id + ", " + customerId + "): \n"); System.out.println(s); return s; } } }
// reserve an item protected boolean reserveItem(int id, int customerID, String key, String location) { Trace.info( "RM::reserveItem( " + id + ", customer=" + customerID + ", " + key + ", " + location + " ) called"); // Read customer object if it exists (and read lock it) Customer cust = (Customer) readData(id, Customer.getKey(customerID)); if (cust == null) { Trace.warn( "RM::reserveCar( " + id + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist"); return false; } // check if the item is available ReservableItem item = (ReservableItem) readData(id, key); if (item == null) { Trace.warn( "RM::reserveItem( " + id + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist"); return false; } else if (item.getCount() == 0) { Trace.warn( "RM::reserveItem( " + id + ", " + customerID + ", " + key + ", " + location + ") failed--No more items"); return false; } else { Flight tempItem = new Flight(Integer.parseInt(item.getLocation()), item.getCount(), item.getPrice()); tempItem.setReserved(item.getReserved()); Customer temp = cust.clone(); temp.setType(1); if (readDataFromLog(id, cust.getKey(), id) == null) { writeDataToLog(id, cust.getKey(), temp); } cust.reserve(key, location, item.getPrice()); writeData(id, cust.getKey(), cust); // decrease the number of available items in the storage item.setCount(item.getCount() - 1); item.setReserved(item.getReserved() + 1); if (readDataFromLog(id, item.getKey(), id) == null) { tempItem.setType(0); writeDataToLog(id, item.getKey(), tempItem); } Trace.info( "RM::reserveItem( " + id + ", " + customerID + ", " + key + ", " + location + ") succeeded"); return true; } }