AccountEventRestView.java
4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.rest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import fi.codecrew.moya.beans.PlaceGroupBeanLocal;
import fi.codecrew.moya.beans.ProductBeanLocal;
import fi.codecrew.moya.model.AccountEvent;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.rest.highcharts.HcSeries;
import fi.codecrew.moya.rest.highcharts.HcSeriesRoot;
@RequestScoped
@Path("/acc")
//@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
//@Produces({ MediaType.APPLICATION_JSON })
public class AccountEventRestView {
@EJB
private PlaceGroupBeanLocal pgbean;
@EJB
private ProductBeanLocal productBean;
@GET
@Produces({ MediaType.APPLICATION_JSON + "; charset=UTF-8" })
@Path("/boughtTimecount/{productId}")
public ArrayList<HcSeriesRoot> boughtTimecount(@PathParam("productId") Integer productId) {
Product product = productBean.findById(productId);
SortedMap<Integer, SortedMap<Integer, SortedMap<Integer, Integer>>> counts = new TreeMap<>();
for (AccountEvent p : product.getAccountEvents())
{
Calendar d = p.getEventTime();
Integer year = d.get(Calendar.YEAR);
Integer month = d.get(Calendar.MONTH);
Integer day = d.get(Calendar.DAY_OF_MONTH);
SortedMap<Integer, SortedMap<Integer, Integer>> yearmap = counts.get(year);
if (yearmap == null) {
yearmap = new TreeMap<>();
counts.put(year, yearmap);
}
SortedMap<Integer, Integer> monthMap = yearmap.get(month);
if (monthMap == null) {
monthMap = new TreeMap<>();
yearmap.put(month, monthMap);
}
Integer dayValue = monthMap.get(day);
if (dayValue == null) {
dayValue = 0;
}
monthMap.put(day, dayValue += p.getQuantity().intValueExact());
}
boolean empty = true;
HcSeries data1 = new HcSeries("Daily");
HcSeries data2 = new HcSeries("Stacked");
Long data2Value = 0L;
for (Entry<Integer, SortedMap<Integer, SortedMap<Integer, Integer>>> ym : counts.entrySet()) {
Integer year = ym.getKey();
for (Entry<Integer, SortedMap<Integer, Integer>> mm : ym.getValue().entrySet()) {
Integer month = mm.getKey();
for (Entry<Integer, Integer> dm : mm.getValue().entrySet()) {
Integer day = dm.getKey();
// if (!empty) {
// data1.append(",");
// data2.append(",");
// } else {
// data1.append("\n");
// data2.append("\n");
// empty = false;
// }
Calendar c = Calendar.getInstance();
c.clear();
c.set(year, month, day);
//
data1.addDataentry(c, Long.valueOf(dm.getValue()));
data2.addDataentry(c, data2Value += dm.getValue());
//
// data1.append("\n [");
// data2.append("\n [");
//
// data1.append(c.getTimeInMillis());
// data2.append(c.getTimeInMillis());
//
// data1.append(", ");
// data2.append(", ");
//
// data1.append(dm.getValue()).append(" ] ");
// data2.append(data2Value += dm.getValue()).append(" ] ");
}
}
}
//
// StringBuilder retSb = new StringBuilder();
//
// retSb.append("[ { [ {\"name\": \"Set 1\", \"data\" : [ \n");
// retSb.append(data1.toString());
// retSb.append("\n ] } \n, {\"name\": \"Set 2\", \"data\": \n[\n");
// retSb.append(data2.toString());
// retSb.append("\n ] } ] \n} ]\n");
ArrayList<HcSeriesRoot> ret = new ArrayList<HcSeriesRoot>();
HcSeriesRoot seriesRoot = new HcSeriesRoot();
seriesRoot.getSeries().add(data1);
seriesRoot.getSeries().add(data2);
ret.add(seriesRoot);
ret.add(new HcSeriesRoot());
return ret;
}
}