/* Convert from an ordinal value to a PurchaseState */
 public static PurchaseState valueOf(int index) {
   PurchaseState[] values = PurchaseState.values();
   if (index < 0 || index >= values.length) {
     return CANCELED;
   }
   return values[index];
 }
  public void updateWithChange(SignedData.Order change, Set<EasyBillingListener> listenerSet) {
    SQLiteDatabase db = helper.getWritableDatabase();
    String orderId = change.orderId;
    String productId = change.productId;

    Log.d(TAG, "@@updateWithOrder orderId: " + orderId);

    db.beginTransaction();
    try {
      { // put to permanent log
        ContentValues cv = new ContentValues();
        cv.put("orderId", change.orderId);
        cv.put("packageName", change.packageName);
        cv.put("productId", change.productId);
        cv.put("purchaseTime", change.purchaseTime);
        cv.put("purchaseState", change.purchaseState.ordinal());
        cv.put("developerPayload", change.developerPayload);
        db.insert("Changes", null, cv);
      }

      boolean orderInDb = count(db, "Orders", "orderId=?", orderId) > 0;
      Log.d(TAG, "This order is in db?: " + orderInDb);

      if (orderInDb) {
        PurchaseState oldPurchaseState =
            PurchaseState.valueOf(getInt(db, "Orders", "purchaseState", -1, "orderId=?", orderId));
        PurchaseState newPurchaseState = change.purchaseState;
        boolean oldHave = oldPurchaseState == PurchaseState.PURCHASED;
        boolean newHave = newPurchaseState == PurchaseState.PURCHASED;

        { // update db
          ContentValues cv = new ContentValues();
          cv.put("orderId", change.orderId);
          cv.put("purchaseState", change.purchaseState.ordinal());
          db.update("Orders", cv, "orderId=?", new String[] {orderId});
        }

        if (oldHave == newHave) {
          Log.d(
              TAG,
              "productId "
                  + change.productId
                  + " inventory not updated, purchase state old="
                  + oldPurchaseState
                  + " new="
                  + newPurchaseState);
        } else {
          int[] amounts = modifyAmount(db, productId, newHave ? +1 : -1);
          for (EasyBillingListener listener : listenerSet) {
            listener.onInventoryAmountChange(productId, newPurchaseState, amounts[0], amounts[1]);
          }
        }
      } else {
        PurchaseState newPurchaseState = change.purchaseState;
        boolean newHave = newPurchaseState == PurchaseState.PURCHASED;

        { // insert to db
          ContentValues cv = new ContentValues();
          cv.put("orderId", change.orderId);
          cv.put("purchaseState", change.purchaseState.ordinal());
          db.insert("Orders", null, cv);
        }

        if (newHave) {
          int[] amounts = modifyAmount(db, productId, +1);
          for (EasyBillingListener listener : listenerSet) {
            listener.onInventoryAmountChange(productId, newPurchaseState, amounts[0], amounts[1]);
          }
        } else {
          Log.d(
              TAG,
              "new purchase state: "
                  + newPurchaseState
                  + " for non-existing product in inventory, not modifying amount");
        }
      }

      db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
    }
  }