String myItemId = anInventory.getItemIdentificationPG().getItemIdentification();
  public void calculateRefills(Collection touchedInventories, ComparatorModule 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);    long start;    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);      start = today + (orderShipTime * thePG.getBucketMillis());      // DEBUG      //   String myOrgName = inventoryPlugin.getOrgName();      // String myItemId = anInventory.getItemIdentificationPG().getItemIdentification();            //Only magically recalculate the initial inventory level      //(the prepo) up to when the prepo arrives.      if(today <= inventoryPlugin.getPrepoArrivalTime()) {	  thePG.recalculateInitialLevel();      }      // only process Level 6 inventories      if (! thePG.getIsLevel2()) {        //clear the refills        oldRefills.addAll(thePG.clearRefillTasks(new Date(today)));        int startBucket = thePG.convertTimeToBucket(start, false);        // 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), true);        // Do not count partial buckets hear because when planning we want the start of the        // bucket.  This prevents us from removing refills that because of OST, cannot be        // replaced leaving holes in supply.        int maxLeadBucket = thePG.convertTimeToBucket(today, false) + maxLeadTime;	int firstLegalRefillBucket = 	    thePG.convertTimeToBucket(inventoryPlugin.getRefillStartTime(),true) + orderShipTime;        double prevTarget = 0;        //calculate inventory levels for time ZERO through start (today + OST)        //Time period which refillGenerator should not touch        calculateInventoryLevelsForRG(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));          }          // Note we had the following to fix some weirdness in subsistence charts          // but the critical == 0.0 prevents refills from being created during          // times when we get demand that causes us to go below 0 and the critical level          // is 0.  For now just check that we are below and if we see weird behavior with          // subsistence charts we'll put in another check.          //  if ((thePG.getCriticalLevel(refillBucket)< invLevel)  ||          //     (thePG.getCriticalLevel(refillBucket) == 0.0)) {          // create an offset to account for the ammo rounding - otherwise we refill          // more then we need to because the refill might cause us to go below the          // critical level by .05 or so - pad with .06 to make sure we cover the bases.          double criticalLevelOffset = 0.00;          if (inventoryPlugin.getSupplyType().equals("Ammunition")) {            criticalLevelOffset = 0.06;          }          if (((thePG.getCriticalLevel(refillBucket) - criticalLevelOffset) < invLevel) ||                  ((thePG.getCriticalLevel(refillBucket) == 0.0) && (invLevel >= 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);            if (thePG.getFillToCapacity()) {              double capacity = thePG.getCapacity();              if (targetLevel > capacity) {                logger.warn("Target Level of "+targetLevel+" for inventory: "+                            thePG.getResource().getTypeIdentificationPG().getTypeIdentification()+                            " at "+getMyOrganization()+                            " was clipped to meet capacity of "+capacity);              }              targetLevel = capacity;            }            double refillQty = targetLevel-invLevel;            // do some rounding/formatting//            System.out.println("\nrefillqty is:" + refillQty + " for agent:" +//                               inventoryPlugin.getClusterId() + " supplytype:" +//                               inventoryPlugin.getSupplyType() + " for item:"+//                               debugItem);            if (inventoryPlugin.getSupplyType().equals("Ammunition")) {              String formatted = getDecimalFormatter().format(refillQty);              refillQty = (new Double(formatted)).doubleValue();            } else {              // make the refill a integer              refillQty = Math.ceil(refillQty);              //refillQty = Math.rint(refillQty);            }//            System.out.println("\nrefillqty after formatting is:" + refillQty + " for agent:" +//                               inventoryPlugin.getClusterId() + " supplytype:" +//                               inventoryPlugin.getSupplyType() + " for item:"+//                               debugItem);	    if(refillBucket < firstLegalRefillBucket) {		if(logger.isWarnEnabled()) {		    logger.warn("At " + inventoryPlugin.getOrgName() + " not ordering a refill on day when we've fallen below critical level, because it is before supplier arrival time + ost.   Supplier arrival time is " + TimeUtils.dateString(inventoryPlugin.getSupplierArrivalTime()) + " and add ost which is " + orderShipTime);		}	    }            else if(refillQty > 0.00) {              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),                                                thePG, today, orderShipTime);              newRefills.add(theRefill);              invLevel = invLevel + refillQty;            }            else if (logger.isDebugEnabled()) {              logger.debug("Not placing a refill order of qty: " + refillQty) ;            }            thePG.setLevel(refillBucket, invLevel);            //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  }