Example #1
0
 @Override
 public void save() {
   if (this.getOwnerUserId() == null) {
     this.setOwnerUserId(HyjApplication.getInstance().getCurrentUser().getId());
   }
   super.save();
 }
  /**
   * This is where the bulk of our work is done. This function is called in a background thread and
   * should generate a new set of data to be published by the loader.
   */
  @Override
  public List<HyjModel> loadInBackground() {

    String dateFrom = mDateFormat.format(new Date(mDateFrom));
    String dateTo = mDateFormat.format(new Date(mDateTo));
    ArrayList<HyjModel> list = new ArrayList<HyjModel>();

    String currentUserId = HyjApplication.getInstance().getCurrentUser().getId();

    List<HyjModel> moneyExpenses =
        new Select("main.*")
            .from(MoneyExpense.class)
            .as("main")
            .leftJoin(MoneyExpenseApportion.class)
            .as("mea")
            .on("main.moneyExpenseApportionId = mea.id")
            .where(
                "(mea.id IS NULL OR (mea.friendUserId IS NULL AND main.ownerUserId = ?)) AND date > ? AND date <= ?",
                currentUserId,
                dateFrom,
                dateTo)
            .orderBy("date DESC")
            .execute();
    list.addAll(moneyExpenses);

    List<HyjModel> moneyIncomes =
        new Select("main.*")
            .from(MoneyIncome.class)
            .as("main")
            .leftJoin(MoneyIncomeApportion.class)
            .as("mea")
            .on("main.moneyIncomeApportionId = mea.id")
            .where(
                "(mea.id IS NULL OR (mea.friendUserId IS NULL AND main.ownerUserId = ?)) AND date > ? AND date <= ?",
                currentUserId,
                dateFrom,
                dateTo)
            .orderBy("date DESC")
            .execute();
    list.addAll(moneyIncomes);

    Collections.sort(list, mDateComparator);
    return list;
  }