please excuse my bad English…
In my database there are stored article sets. In every article sets there are several different articles in it. Each article has a different demand with a date and a quantity.
In the project there is an enum which looks like this:
public enum PeriodDefinition {
Individual,
Day,
Week,
Month,
Quarter,
HalfYear,
Year
}
The entity of Article is like that:
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
@Entity
public class Article extends ArticleContainer {
private float price;
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
The entity of Article Container:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
@Entity
public class ArticleContainer extends BaseEntity {
@Column(nullable = false, unique = true)
private String number;
@Column(nullable = false)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
The Entity of Article Set:
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
@Entity
public class ArticleSet extends ArticleContainer {
private static final long serialVersionUID = 6236522228097421880L;
@ManyToMany
private List<Article> articles = new ArrayList<Article>();
@OneToMany
private List<ArticleSet> children = new ArrayList<ArticleSet>();
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
public List<ArticleSet> getChildren() {
return children;
}
public void setChildren(List<ArticleSet> children) {
this.children = children;
}
}
And finally the Demand entity
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Demand extends BaseEntity {
@ManyToOne(optional = false)
private Article article;
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
private Date demandTime;
private double quantity;
public Article getArticle() {
return article;
}
public void setArticle(Article article) {
this.article = article;
}
public Date getTimeStamp() {
return demandTime;
}
public void setTimeStamp(Date demandTime) {
this.demandTime = demandTime;
}
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
}
For example there exists an article set with the name “ArticleSetOfTwo” and the number 002 and contains the articles “Article 1” and “Article 2” with the numbers 1 and 2.
Article 1 has the following demand: (Left is date and right is quantity)
- 07-11-2011, 10
- 08-11-2011, 50
- 15-11-2011, 200
- 15-11-2011, 300
- 16-11-2011, 100
Article 2 has the following demand:
- 08-11-2011, 20
- 09-11-2011, 10
- 14-11-2011, 150
- 15-11-2011, 150
- 16-11-2011, 100
Now I want to sum all quantities in the article set divided by a period of our enum, e.g. week. Then this would be for the Articleset:
For the first week starting at 07-11-2011 until 13-11-2011 it would be: 90
For the second week starting at 14-11-2011 until 20-11-2011 it would be: 1000
These values I’d like to store in a ArrayList but I don’t know how to do it. Maybe someone can solve it. Thank you very much in advance!
I’m not sure it’s doable using a query.
I would just select all the date/quantity pairs for the article set, ordered by date, and then do a loop starting from the start date and ending with the end date, and add the quantities in Java.
The HQL would look like this:
Then, for each week, loop through the pairs and, if the demand time is between the start and the end of the week, add the quantity of the demand to the week sum.