public boolean deleteAllocation(int assetClassId) {
    ExceptionHandler handler = new ExceptionHandler(getContext(), this);
    AssetClassRepository repo = new AssetClassRepository(getContext());

    // todo: use transaction? (see bulkUpdate)

    // Delete all child elements.
    List<Integer> childIds = getAllChildrenIds(assetClassId);
    repo.deleteAll(childIds);

    // delete any stock links
    AssetClassStockRepository stockRepo = new AssetClassStockRepository(getContext());
    boolean linksDeleted = stockRepo.deleteAllForAssetClass(assetClassId);
    if (!linksDeleted) {
      handler.showMessage("Error deleting stock links.");
      return false;
    }

    // delete allocation record
    boolean assetClassDeleted = repo.delete(assetClassId);
    if (!assetClassDeleted) {
      handler.showMessage("Error deleting asset class.");
      return false;
    }

    return true;
  }
  /**
   * Move the asset class up in the sort order. Increase sort order for this item. Finds the next in
   * order and decrease it's sort order.
   */
  public void moveClassUp(int id) {
    // todo: this is incomplete. Need to set the default value on creation and handle
    // deletions. Also pay attention if the order will be ascending or descending and adjust.

    //        List<AssetClass> bulk = new ArrayList();

    AssetClass up = repository.load(id);
    Integer currentPosition = up.getSortOrder();
    if (currentPosition == null) currentPosition = 0;
    int upPosition = currentPosition + 1;

    up.setSortOrder(upPosition);
    //        bulk.add(up);

    //        WhereStatementGenerator where = new WhereStatementGenerator();
    //        String filter = where.getStatement(AssetClass.SORTORDER, "=", upPosition);
    //        AssetClass down = repository.first(filter);
    //        if (down != null) {
    //            down.setSortOrder(currentPosition);
    //            bulk.add(down);
    //        }
    //
    //        // save in transaction
    //        repository.bulkUpdate(bulk);

    // for now, just increase the sort order on the selected item
    repository.update(up);
  }
  public void moveClassDown(int id) {
    AssetClass assetClass = repository.load(id);
    Integer currentPosition = assetClass.getSortOrder();
    if (currentPosition == null) currentPosition = 0;
    int nextPosition = currentPosition - 1;
    if (nextPosition < 0) return;

    assetClass.setSortOrder(nextPosition);

    repository.update(assetClass);
  }
  public List<Integer> getAllChildrenIds(int assetClassId) {
    List<Integer> ids = new ArrayList<>();

    AssetClassRepository repo = new AssetClassRepository(getContext());
    List<Integer> childIds = repo.loadAllChildrenIds(assetClassId);

    ids.addAll(childIds);

    // iterate recursively and get all children's children ids.
    for (int childId : childIds) {
      ids.addAll(getAllChildrenIds(childId));
    }

    return ids;
  }
  /**
   * Loads asset class name, given the id.
   *
   * @param id Id of the asset class.
   * @return String name of the asset class.
   */
  public String loadName(int id) {
    if (id == Constants.NOT_SET) return "";

    AssetClassRepository repo = new AssetClassRepository(this.context);
    Cursor c =
        repo.openCursor(
            new String[] {AssetClass.NAME},
            AssetClass.ID + "=?",
            new String[] {Integer.toString(id)});
    if (c == null) return null;

    c.moveToNext();
    AssetClass ac = AssetClass.from(c);
    c.close();

    return ac.getName();
  }
 private Cursor loadData() {
   Cursor c = repository.openCursor(null, null, null, AssetClass.PARENTID);
   return c;
 }