// 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 }
public void abort(int transactionId) throws RemoteException, InvalidTransactionException { int indx = logContains(transactionId); Log temp; if (indx > -1) { temp = (Log) logArray.elementAt(indx); } else { System.out.println("nothing in array"); return; } for (Enumeration e = temp.getKeys(); e.hasMoreElements(); ) { System.out.println("For loop"); String key = (String) (e.nextElement()); RMItem obj = temp.get(key); if (obj.getType() == 0) { Flight flight = (Flight) obj; if (flight.getCount() == -1) { System.out.println("entering count=-1 block"); removeData(transactionId, key); } else { System.out.println("entering other block"); writeData(transactionId, key, flight); } } else if (obj.getType() == 1) { Customer cust = (Customer) obj; if (cust.getID() == -1) { System.out.println("entering remove data for customer"); removeData(transactionId, key); } else { System.out.println("entering write data for customer"); writeData(transactionId, key, obj); } } } }
// Adds flight reservation to this customer. public boolean reserveFlight(int id, int customerID, int flightNum) throws RemoteException { return reserveItem(id, customerID, Flight.getKey(flightNum), String.valueOf(flightNum)); }
// Returns price of this flight public int queryFlightPrice(int id, int flightNum) throws RemoteException { return queryPrice(id, Flight.getKey(flightNum)); }
public boolean deleteFlight(int id, int flightNum) throws RemoteException { return deleteItem(id, Flight.getKey(flightNum)); }
// Create a new flight, or add seats to existing flight // NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price public boolean addFlight(int id, int flightNum, int flightSeats, int flightPrice) throws RemoteException, InvalidTransactionException, TransactionAbortedException { Trace.info( "RM::addFlight(" + id + ", " + flightNum + ", $" + flightPrice + ", " + flightSeats + ") called"); Flight curObj = (Flight) readData(id, Flight.getKey(flightNum)); if (curObj == null) { // doesn't exist...add it Flight newObj = new Flight(flightNum, flightSeats, flightPrice); writeData(id, newObj.getKey(), newObj); String key = newObj.getKey(); if (readDataFromLog(id, key, id) == null) { Flight logObj = (Flight) newObj.clone(); logObj.setCount(-1); logObj.type = 0; writeDataToLog(id, key, logObj); } Trace.info( "RM::addFlight(" + id + ") created new flight " + flightNum + ", seats=" + flightSeats + ", price=$" + flightPrice); } else { // add seats to existing flight and update the price... Flight logObj = (Flight) curObj.clone(); if (readDataFromLog(id, curObj.getKey(), id) == null) writeDataToLog(id, curObj.getKey(), logObj); curObj.setCount(curObj.getCount() + flightSeats); if (flightPrice > 0) { curObj.setPrice(flightPrice); } // if writeData(id, curObj.getKey(), curObj); Trace.info( "RM::addFlight(" + id + ") modified existing flight " + flightNum + ", seats=" + curObj.getCount() + ", price=$" + flightPrice); } // else return (true); }
// 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; } }