1 package org.musicontroller.gui.statistics;
2
3 import java.util.Date;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.apache.tapestry.IExternalPage;
10 import org.apache.tapestry.IRequestCycle;
11 import org.apache.tapestry.html.BasePage;
12 import org.musicontroller.core.Event;
13 import org.musicontroller.dao.Dao;
14 import org.musicontroller.dao.PlaylistKey;
15 import org.musicontroller.security.User;
16 import org.varienaja.util.DateTools;
17
18
19
20
21
22
23
24
25 public abstract class Database extends BasePage implements IExternalPage {
26
27 public abstract Dao getDao();
28 public abstract User getUser();
29
30 public abstract Object[] getMonth();
31 public abstract void setMonth(Object[] month);
32
33 public int songCount;
34
35
36
37
38
39 public int getBandCount() {
40 try {
41 return getDao().count("select count(band) from Band band", null);
42 } catch (Exception e) {
43 return 0;
44 }
45 }
46
47
48
49
50
51 public int getPlaylistCount() {
52 try {
53 return getDao().count("select count(playlist) from Playlist playlist", null);
54 } catch (Exception e) {
55 return 0;
56 }
57 }
58
59
60
61
62
63 public int getSongCount() {
64 try {
65 return getDao().count("select count(song) from Song song", null);
66 } catch (Exception e) {
67 return 0;
68 }
69 }
70
71
72
73
74
75 public int getKeywordCount() {
76 try {
77 return getDao().count("select count(kw) from Keyword kw", null);
78 } catch (Exception e) {
79 return 0;
80 }
81 }
82
83
84
85
86
87 public int getPlayedEventCount() {
88 try {
89 Map<String, Object> params = new HashMap<String,Object>();
90 params.put("kind", Event.played);
91 return getDao().count("select count(ev) from Event ev where ev.eventkind=:kind", params);
92 } catch (Exception e) {
93 return 0;
94 }
95 }
96
97
98
99
100
101 public int getRequestedEventCount() {
102 try {
103 Map<String, Object> params = new HashMap<String,Object>();
104 params.put("kind", Event.requested);
105 return getDao().count("select count(ev) from Event ev where ev.eventkind=:kind", params);
106 } catch (Exception e) {
107 return 0;
108 }
109 }
110
111
112
113
114
115 public int getSkippedEventCount() {
116 try {
117 Map<String, Object> params = new HashMap<String,Object>();
118 params.put("kind", Event.skipped);
119 return getDao().count("select count(ev) from Event ev where ev.eventkind=:kind", params);
120 } catch (Exception e) {
121 return 0;
122 }
123 }
124
125 public void activateExternalPage(Object[] args, IRequestCycle cycle) {
126
127 }
128
129
130
131
132 public String getInsertedDescription() {
133 Object[] items = getMonth();
134 Date d = DateTools.encodeDate(1, Integer.parseInt(items[0].toString()), Integer.parseInt(items[1].toString()));
135
136 return items[2] +" songs in the database in " + DateTools.formatDate(d, "MMMMM yyyy");
137 }
138
139
140
141
142
143 public String getMonthIndicator() {
144 Object[] items = getMonth();
145 Date d = DateTools.encodeDate(1, Integer.parseInt(items[0].toString()), Integer.parseInt(items[1].toString()));
146
147 return DateTools.formatDate(d, "MMM").substring(0,1);
148 }
149
150
151
152
153
154 public String getLongMonthIndicator() {
155 Object[] items = getMonth();
156 Date d = DateTools.encodeDate(1, Integer.parseInt(items[0].toString()), Integer.parseInt(items[1].toString()));
157
158 return DateTools.formatDate(d, "MMMMM yyyy");
159 }
160
161
162
163
164 public String getInsertedSize() {
165 int sze = Integer.parseInt(getMonth()[2].toString());
166
167 int pct = 200*sze / songCount;
168
169 return Integer.toString(pct);
170 }
171
172 public List<Object[]> getMonths() {
173 List<Object[]> months = getDao().listMonthlySongCounts();
174 List<Object[]> result = new LinkedList<Object[]>();
175
176 int currentsize = 0;
177
178 if (months.size()>0) {
179 int currentMonth = (Integer) months.get(0)[0];
180 int currentYear = (Integer) months.get(0)[1];
181
182
183 for (Object[] item : months) {
184 while (!( (Integer) item[0]==currentMonth &&
185 (Integer) item[1]==currentYear)) {
186 String[] extra = new String[3];
187 extra[0] = Integer.toString(currentMonth);
188 extra[1] = Integer.toString(currentYear);
189 extra[2] = Integer.toString(currentsize);
190 result.add(extra);
191
192 currentMonth++;
193 if (currentMonth>12) {
194 currentMonth = 1;
195 currentYear++;
196 }
197 }
198 result.add(item);
199
200 int growth = Integer.parseInt(item[2].toString());
201 currentsize += growth;
202 item[2] = Integer.toString(currentsize);
203
204 currentMonth++;
205 if (currentMonth>12) {
206 currentMonth = 1;
207 currentYear++;
208 }
209 }
210 songCount = currentsize;
211 }
212
213 return result;
214 }
215
216
217
218
219
220 public Map<String, Integer> getUsageStatistics() {
221 return getDao().listMonthlyStatistics(getUser());
222 }
223
224
225
226
227
228 public long getInsertedIdThisMonth() {
229 Object[] items = getMonth();
230 return (new PlaylistKey(-2001,
231 DateTools.encodeDate(1, Integer.parseInt(items[0].toString()),
232 Integer.parseInt(items[1].toString())
233 ))).getId();
234 }
235
236 }