String[] minIds = new String[3];
Reservation[] reservIds = new Reservation[3];
boolean isFound = false;
Reservation[] reservations = new Reservation[objId.length];
Customer customer = null;
DirectoryManager locator = HyFlow.getLocator();
while(true){
int i=0;
try {
for(; i<objId.length; i++)
try {
reservations[i] = (Reservation)locator.open(objId[i]);
} catch (NotRegisteredKeyException e) {
}
try {
customer = (Customer)locator.open(customerId);
} catch (NotRegisteredKeyException e) {
}
break;
}catch (TimeoutException e) {
for(int j=0; j<i; j++){
if(reservations[j]!=null)
locator.release(reservations[j]);
}
}
}
try {
for(int i=0; i<Benchmark.queryPerTransaction; i++){
int price = Integer.MAX_VALUE;
Reservation car = (Reservation)reservations[i];
if(car==null)
continue;
if(car.isAvailable())
price = car.getPrice();
if(price < minPrice[objType[i]]){
minPrice[objType[i]] = price;
minIds[objType[i]] = objId[i];
reservIds[objType[i]] = car;
isFound = true;
}
}
if(isFound){
// create customer
if(customer==null){
customer = new Customer(customerId);
Logger.info(">>>>>>>>>>Customer added..." + customerId);
}
for(int i=0; i<minPrice.length; i++)
if(minPrice[i]!=Integer.MAX_VALUE){
String reserveId = "-reserve-" + Math.random(); // generate random id
Reservation reservation = reservIds[i];
if(reservation.reserve()){
ReservationInfo reservationInfo = new ReservationInfo(reserveId, minIds[i], minPrice[i], i);
customer.addReservation((String)reservationInfo.getId());
Logger.info(">>>>>>>>>>Reservation done...");
}
}
}
} finally{
for(int i=0; i<reservations.length; i++){
Reservation reservation = (Reservation)reservations[i];
if(reservation!=null)
locator.release(reservation);
}
if(customer!=null)
locator.release(customer);
}
}