Beispiel #1
0
 public PriceListItem getItemPrice(int id, byte dat) throws SQLException, Exception {
   if (tempCache != null
       && tempCache.ID() == id
       && tempCache.Data() == dat
       && (System.currentTimeMillis() - tempCache.getTime()) / 1000 < tempCacheTTL) {
     // use temp
     return tempCache;
   }
   if (databaseType == DBType.MYSQL && !useCache) {
     tempCache = MySQLpricelist.getItem(id, dat);
   } else {
     updateCache(true);
     PriceListItem f = null;
     for (PriceListItem p : priceList) {
       if (p != null && p.ID() == id && p.Data() == dat) {
         f = p;
         break;
       }
     }
     if (f == null) {
       return null;
     }
     if (tempCache == null) {
       tempCache = new PriceListItem(f);
     } else {
       tempCache.Set(f);
     }
   }
   return tempCache;
 }
Beispiel #2
0
 public int compare(PriceListItem o1, PriceListItem o2) {
   if (sortOrder.size() > 0) {
     int o1i = sortOrder.indexOf(o1.IdDatStr());
     int o2i = sortOrder.indexOf(o2.IdDatStr());
     if (o1i != -1 && o2i != -1) {
       return o2i - o1i;
     } else if (o1i != -1) {
       return -1;
     } else if (o2i != -1) {
       return 1;
     }
   }
   return (o1.ID() * 100 - o2.ID() * 100 + o1.Data() - o2.Data()) * (descending ? 1 : -1);
 }
Beispiel #3
0
 public boolean isForSale(ItemStockEntry i) throws SQLException, Exception {
   if (tempCache != null && tempCache.ID() == i.itemNum && tempCache.Data() == i.itemSub) {
     // has been retrieved recently
     return tempCache.sell >= 0;
   }
   // if itemExists, tempCache will contain the item
   return itemExists(i) && tempCache.sell >= 0; // getSellPrice(i) >= 0;
 }
Beispiel #4
0
 public boolean canBuy(ItemStockEntry i) throws SQLException, Exception {
   if (tempCache != null && tempCache.ID() == i.itemNum && tempCache.Data() == i.itemSub) {
     // has been retrieved recently
     return tempCache.buy >= 0;
   }
   // if itemExists, tempCache will contain the item
   return itemExists(i) && tempCache.buy >= 0;
 }
Beispiel #5
0
  public boolean saveFile(File tosave) throws IOException {
    if (tosave != null && !tosave.isDirectory()) {
      if (!tosave.exists()) {
        File dir =
            new File(
                tosave
                    .getAbsolutePath()
                    .substring(0, tosave.getAbsolutePath().lastIndexOf(File.separatorChar)));
        dir.mkdirs();
        try {
          if (!tosave.createNewFile()) {
            return false;
          }
        } catch (Exception e) {
          return false;
        }
      }
      if (tosave.canWrite()) {

        FileWriter fstream = null;
        try {
          fstream = new FileWriter(tosave.getAbsolutePath());
          // System.out.println("writing to " + tosave.getAbsolutePath());
          BufferedWriter out = new BufferedWriter(fstream);
          out.write("id,subdata,buyprice,sellprice,name");
          out.newLine();
          for (PriceListItem i : priceList) {
            // names provided for others to easily edit db
            // out.write(i.ID() + "," + i.Data() + "," + i.buy + "," + i.sell + "," + i.Name());
            out.write(
                String.format(
                    Locale.US, "%d,%d,%.2f,%.2f,%s", i.ID(), i.Data(), i.buy, i.sell, i.Name()));
            out.newLine();
          }
          out.flush();
          out.close();
        } catch (IOException ex) {
          if (!log_nothrow) {
            throw new IOException("Error opening " + tosave.getName() + " for writing", ex);
          }
          logger.log(Level.SEVERE, "Error opening " + tosave.getName() + " for writing", ex);
        } finally {
          if (fstream != null) {
            try {
              fstream.close();
            } catch (IOException ex) {
              /*
              if (!log_nothrow) {
              throw new IOException("Error closing " + tosave.getName(), ex);
              }
              logger.log(Level.SEVERE, "Error closing " + tosave.getName(), ex);*/

            }
          }
        }
        return true;
      }
    } else if (tosave == null) {
      throw new IOException("no file to save to");
    } else if (tosave.isDirectory()) {
      throw new IOException("file to save is a directory");
    }
    return false;
  }