Question

I am new to app-engine Datastore and to NoSQL world in common. I am developing a simple application where a user can declare his/her expenses everyday. Every user(Account) has its own declared expenses. The dash board contains a simple GWT Cell Tree which contains all the years in which the use declared expenses and when he/she clicks on a years, he gets all the months of the years then he clicks on the month and he gets all the days of the month and finally clicking on a day and he gets all the expenses declared in that day. It is something like

*2010

|_ jan
 |_1
 |_2
  |_Food 12d 
  |_Dress 200d 
  |_Fun 150d
  |_ ...

|_ feb 
|_ ...

*2011

|_ jan

|_ feb |_...

I save expenses entities in the data store for each user(Account) as the account the parent of all the expenses. my expense is as follow: public class Expense implements Serializable, Comparable {

 private String name;
  private double price;
  private Date date;

  public Expense(String name, double price, Date date) {

    this.name = name;
    this.price = price;
    this.date = date;
  }

  public Expense() {
  }

  public String getName() {
    return name;
  }

  public double getPrice() {
    return price;
  }

  public boolean isPriceValid() {
    return price > 0;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setPrice(double price) {
    this.price = price;
  }

  public Date getDate() {
    return date;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  @Override
  public int compareTo(Expense expense) {
    if (name.equals(expense.getName())) {
      if (date.equals(expense.getDate())) {
        return new Double(price).compareTo(expense.getPrice());
      }
      return date.compareTo(expense.getDate());
    }
    return name.compareTo(expense.getName());
  }

My QUESTION IS: How to query the expenses in the data store and return all different years relater to a specified Account and put them in a list or set or anything else where I can list them ? does I need to fetch all the expenses entities and iterate over them and get all the different years. doesn't sound reasonable. Any advice will be welcome and THANKS IN ADVANCE.

Was it helpful?

Solution

Several comments related to your post :

--> I wouldn't store a financial amount as a Double. Going that route will lead you to big problems with rounding errors. There are a lot of posts on this one. I would suggest you to store it as "DollarCent" and declare it as an integer. You simply multiply the amount by 100 when you store it and when displaying it you divide by 100.

--> Why do you declare your entity in the Datastore as implementing Serializable ? I would store without Serializable.

--> Related to the specific question on displaying the data by year, reading your question I see no other way than fetching the data. What I would do is ask GAE to order the data to avoid having to order it afterwards. Using Objectify, it would simply be q.filter(...).order(-date).order(amount).

Hope this helps !

Hugues

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top