View Javadoc

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   * This screen displays some numbers about the objects in
20   * the MusiController database.
21   * 
22   * @author deksels
23   * @version $Id: Database.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
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  	 * Returns the number of bands in the database.
37  	 * @return The number of bands in the database.
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  	 * Returns the number of playlists in the database.
49  	 * @return The number of playlists in the database.
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  	 * Returns the number of songs in the database.
61  	 * @return The number of songs in the database.
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  	 * Returns the number of keywords in the database.
73  	 * @return The number of keywords in the database.
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  	 * Returns the number of play events in the database.
85  	 * @return The number of play events in the database.
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  	 * Returns the number of request events in the database.
99  	 * @return The number of request events in the database.
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 	 * Returns the number of skip events in the database.
113 	 * @return The number of skip events in the database.
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 		//Nothing to do here
127 	}
128 
129 	/**
130 	 * @return A nice string, explaining the meaning of the green bar-statistic.
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 	 * @return String representation of the month we're currently in, in the form
141 	 * M
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 	 * @return String representation of the month we're currently in, in the form
152 	 * MMMMM yyyy
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 	 * @return String representation of the growth in the month we're currently in
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 	 * Gets the usage statistics for a specific user.
218 	 * @return The usage statistics.
219 	 */
220 	public Map<String, Integer> getUsageStatistics() {
221 		return getDao().listMonthlyStatistics(getUser());
222 	}
223 	
224 	/**
225 	 * Returns the playlistId for a list of Songs inserted in the current month
226 	 * @return The Playlist-Id.
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 }