Exemplo n.º 1
0
 /**
  * Load data from price cache
  *
  * @param typeIDs typeIDs to load from cache
  * @return available price data
  */
 protected Map<Integer, PriceData> processLoad(Set<Integer> typeIDs) {
   Pricing pricing = PricingFactory.getPricing(new DefaultPricingOptions());
   LOG.info("Price data loading");
   priceDataList = new HashMap<Integer, PriceData>();
   for (int typeID : typeIDs) { // For each typeID
     PriceData priceData = priceDataList.get(typeID);
     if (priceData == null) {
       priceData = new PriceData();
       priceDataList.put(typeID, priceData);
     }
     boolean ok = false;
     for (PriceMode priceMode :
         PriceMode
             .values()) { // For each PriceMode (all combinations of PricingNumber & PricingType)
       PricingType pricingType = priceMode.getPricingType();
       PricingNumber pricingNumber = priceMode.getPricingNumber();
       if (pricingNumber == null || pricingType == null) {
         continue; // Ignore calculated prices - f.ex. PriceMode.PRICE_MIDPOINT
       }
       Double price = pricing.getPriceCache(typeID, pricingType, pricingNumber);
       if (price != null) {
         ok = true; // Something is set
         PriceMode.setDefaultPrice(priceData, priceMode, price);
       }
     }
     if (!ok) {
       priceDataList.remove(typeID); // Remove failed typeID
     }
   }
   if (!priceDataList.isEmpty()) {
     LOG.info("	Price data loaded");
     Map<Integer, PriceData> hashMap = new HashMap<Integer, PriceData>();
     hashMap.putAll(priceDataList);
     priceDataList.clear(); // Free memory
     return hashMap; // Return copy of Map
   } else {
     LOG.info("	Price data not loaded");
     return null;
   }
 }
Exemplo n.º 2
0
  /**
   * @param task UpdateTask to update progress on
   * @param updateAll true to update all prices. false to only update new/missing prices
   * @param pricingOptions Options used doing update
   * @param typeIDs TypeIDs to get price data for
   * @param priceSource Price data source to update from (only used in log)
   * @return
   */
  protected Map<Integer, PriceData> processUpdate(
      final UpdateTask task,
      final boolean updateAll,
      final PricingOptions pricingOptions,
      final Set<Integer> typeIDs,
      final PriceSource priceSource) {
    this.updateTask = task;
    this.update = updateAll;
    this.typeIDs = new HashSet<Integer>(typeIDs);
    this.failed = new HashSet<Integer>();
    this.okay = new HashSet<Integer>();
    this.queue = new HashSet<Integer>(typeIDs);
    this.priceDataList = new HashMap<Integer, PriceData>();

    if (updateAll) {
      LOG.info("Price data update all (" + priceSource + "):");
    } else {
      LOG.info("Price data update new (" + priceSource + "):");
    }

    Pricing pricing = PricingFactory.getPricing(pricingOptions);

    // Reset cache timers...
    if (updateAll) {
      for (int id : typeIDs) {
        pricing.setPrice(id, -1.0);
      }
    }
    pricing.addPricingListener(this);

    // Load price data (Update as needed)
    for (int id : typeIDs) {
      createPriceData(id, pricing);
    }
    while (!getQueue().isEmpty()) {
      try {
        synchronized (this) {
          wait();
        }
      } catch (InterruptedException ex) {
        LOG.info("Failed to update price");
        pricing.cancelAll();
        if (updateTask != null) {
          updateTask.addError("Price data", "Cancelled");
          updateTask.setTaskProgress(100, 100, 0, 100);
          updateTask = null;
        }
        clear(pricing);
        return null;
      }
    }
    if (!failed.isEmpty()) {
      StringBuilder errorString = new StringBuilder();
      for (int typeID : failed) {
        if (!errorString.toString().isEmpty()) {
          errorString.append(", ");
        }
        errorString.append(typeID);
      }
      LOG.error("Failed to update price data for the following typeIDs: " + errorString.toString());
      if (updateTask != null) {
        updateTask.addError(
            "Price data", "Failed to update price data for " + failed.size() + " item types");
      }
    }
    boolean updated = (!priceDataList.isEmpty() && (typeIDs.size() * 5 / 100) > failed.size()); //
    if (updated) { // All Updated
      if (updateAll) {
        LOG.info("	Price data updated");
      } else {
        LOG.info("	Price data loaded");
      }
      try {
        pricing.writeCache();
        LOG.info("	Price data cached saved");
      } catch (IOException ex) {
        LOG.error("Failed to write price data cache", ex);
      }
      // We only set the price data if everthing worked (AKA all updated)
      try {
        // return new HashMap<Integer, PriceData>(priceDataList);
        // XXX - Workaround for ConcurrentModificationException in HashMap constructor
        Map<Integer, PriceData> hashMap = new HashMap<Integer, PriceData>();
        priceDataList.keySet().removeAll(failed);
        hashMap.putAll(priceDataList);
        return hashMap;
      } finally {
        clear(pricing);
      }
    } else { // None or some updated
      LOG.info("	Failed to update price data");
      if (updateTask != null) {
        updateTask.addError("Price data", "Failed to update price data");
        updateTask.setTaskProgress(100, 100, 0, 100);
      }
      clear(pricing);
      return null;
    }
  }