oldRefills.addAll(thePG.clearRefillTasks(new Date(start)));
oldRefills.addAll(thePG.clearRefillTasks(new Date(today)));
  public void calculateRefills(Collection touchedInventories, RefillComparator myComparator) {    ArrayList newRefills = new ArrayList();    ArrayList oldRefills = new ArrayList();    int orderShipTime = inventoryPlugin.getOrderShipTime();    int maxLeadTime = inventoryPlugin.getMaxLeadTime();        //Should we push now to the end of today? For now we WILL NOT.    //long today = getTimeUtils().    //  pushToEndOfDay(inventoryPlugin.getCurrentTimeMillis());    long today = inventoryPlugin.getCurrentTimeMillis();    //start time (k) is today plus OST.    long start = getTimeUtils().addNDays(today, orderShipTime);        Iterator tiIter = touchedInventories.iterator();    while (tiIter.hasNext()) {      // clear the refill lists from the last inventory      oldRefills.clear();      newRefills.clear();      Inventory anInventory = (Inventory) tiIter.next();//       debugItem = anInventory.getItemIdentificationPG().getItemIdentification();      LogisticsInventoryPG thePG = (LogisticsInventoryPG)anInventory.        searchForPropertyGroup(LogisticsInventoryPG.class);      // only process Level 6 inventories      if (! thePG.getIsLevel2()) {	//clear the refills	oldRefills.addAll(thePG.clearRefillTasks(new Date(start)));        int inventoryBucket = thePG.convertTimeToBucket(today);        int startBucket = thePG.convertTimeToBucket(start);        // refill time is start + 1 bucket (k+1)        int refillBucket = startBucket + 1;         // max lead day is today + maxLeadTime        int maxLeadBucket = thePG.convertTimeToBucket(getTimeUtils().						      addNDays(today, maxLeadTime));	double prevTarget = 0;        //calculate inventory levels for time ZERO through start (today + OST)	//Time period which refillGenerator should not touch	calculateInventoryLevels(0, startBucket, thePG);// 	System.out.println("##############  ITEM "+debugItem);        //  create the refills	// RJB	int lastRefillBucket=refillBucket;        while (refillBucket <= maxLeadBucket) {          double invLevel = thePG.getLevel(startBucket) -             thePG.getActualDemand(refillBucket);	  if (logger.isDebugEnabled()) { 	      logger.debug("\n Item "+thePG.getResource()+" Inv Level for startBucket:"+startBucket+" is: "+			   thePG.getLevel(startBucket) + " - the actual demand for refillbucket:"+			   refillBucket + " is: " + thePG.getActualDemand(refillBucket) + 			   ".... The Critical Level for the refill bucket is: " +			   thePG.getCriticalLevel(refillBucket));	  }          if ((thePG.getCriticalLevel(refillBucket)< invLevel)  ||	      (thePG.getCriticalLevel(refillBucket) == 0.0)) {            thePG.setLevel(refillBucket, invLevel);	    if (logger.isDebugEnabled()) { 		logger.debug("\nSetting the Inv Level for refillbucket: " + refillBucket +			     " to level: " + invLevel);	    }	  } else {//             int reorderPeriodEndBucket = refillBucket + (int)thePG.getReorderPeriod();//             double refillQty = generateRefill(invLevel, refillBucket, // 					      reorderPeriodEndBucket, thePG);	    int reorderPeriodEndBucket = refillBucket + (int)thePG.getReorderPeriod();	    double targetLevel = getTargetLevel(refillBucket,reorderPeriodEndBucket, thePG);            double refillQty = targetLevel-invLevel;	    if(refillQty > 0.0) {	      lastRefillBucket=refillBucket;	      if (logger.isDebugEnabled()) { 		logger.debug("\n Creating Refill for bucket: " + refillBucket +			     " of quantity: " + refillQty);	      }	      // make a task for this refill and publish it to glue plugin	      // and apply it to the LogisticsInventoryBG	      Task theRefill = createRefillTask(refillQty, 						thePG.convertBucketToTime(refillBucket), 						anInventory, thePG, 						today, orderShipTime);	      newRefills.add(theRefill);      	    }	    else if (logger.isDebugEnabled()) {		logger.debug("Not placing a refill order of qty: " + refillQty) ;	    }	    thePG.setLevel(refillBucket, targetLevel);            //set the target level to invlevel + refillQty (if we get the refill we            //ask for we hit the target - otherwise we don't)            thePG.setTarget(refillBucket, targetLevel);	    if (logger.isDebugEnabled()) { 		double printsum = targetLevel;  		logger.debug("\nSetting the InventoryLevel and the TargetLevel to: " + printsum);	    }	    prevTarget = targetLevel;	  }	  //reset the buckets	  startBucket = refillBucket;	  refillBucket = startBucket + 1;	}	// Set the target levels for projection period here since very similar 	// calculations are done for both projection and refill period.	//setTargetForProjectionPeriod(thePG, maxLeadBucket+1, prevTarget);	// RJB	setTargetForProjectionPeriod(thePG, lastRefillBucket+1, prevTarget);	// call the Comparator for this Inventory which will compare the old and	// new Refills and then publish the new Refills and Rescind the old Refills.	myComparator.compareRefills(newRefills, oldRefills, anInventory);      } // end of if not level 2 inventory    } // done going through inventories  }