View Javadoc

1   package org.musicontroller.gui.components;
2   
3   import java.util.Date;
4   import java.util.LinkedList;
5   import java.util.List;
6   import java.util.Map;
7   
8   import org.apache.tapestry.BaseComponent;
9   import org.musicontroller.core.Event;
10  import org.musicontroller.dao.PlaylistKey;
11  import org.varienaja.util.DateTools;
12  
13  /**
14   * This tapestry component displays the statistical usage of a certain user.
15   * @author varienaja
16   * @version $Id: Usage.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
17   */
18  public abstract class Usage extends BaseComponent {
19  	public abstract Map<String, Integer> getStatistics();
20  	
21  	public abstract int getKind();
22  	public abstract void setKind(int kind);
23  	public abstract int getYear();
24  	public abstract void setYear(int year);
25  	public abstract int getMonth();
26  	public abstract void setMonth(int month);
27  	
28  	/**
29  	 * @return A list of Integers, starting from the first year which appears
30  	 * in the statistics until the current year.
31  	 */
32  	public List<Integer> getYears() {
33  		List<Integer> result = new LinkedList<Integer>();
34  		String firstKey = null;
35  		int year = Integer.MAX_VALUE;
36  		if (getStatistics().size() > 0) {
37  			firstKey = getStatistics().keySet().iterator().next();
38  			if (firstKey != null) {
39  				String[] parts = firstKey.split(",");
40  				year = Integer.parseInt(parts[0]);
41  			}
42  		}
43  		
44  		int currentYear = DateTools.currentYear();
45  		for (int yr=year; yr<=currentYear; yr++) {
46  			result.add(0, yr); //We will show the current year first, the the last year, etc.
47  		}
48  		
49  		return result;
50  	}
51  	
52  	public Integer[] getMonths() {
53  		return new Integer[]{12,11,10,9,8,7,6,5,4,3,2,1};
54  	}
55  	
56  	public String getMonthIndicator() {
57  		Date d = DateTools.encodeDate(1, getMonth(), getYear());
58  		return DateTools.formatDate(d, "MMMMM").substring(0,1);
59  	}
60  
61  	public String getLongMonthIndicator() {
62  		Date d = DateTools.encodeDate(1, getMonth(), getYear());
63  		return DateTools.formatDate(d, "MMMMM yyyy");
64  	}
65  
66  	private Integer getAmount(int eventkind) {
67  		Integer amount = getStatistics().get(getYear()+","+getMonth()+","+eventkind);
68  		if (amount==null) {
69  			amount=0;
70  		}
71  		return amount;
72  	}
73  	
74  	/**
75  	 * Calculates the size of an image, depending on the amount of events.
76  	 * We do not return a relative image size. The advantage of a relative size is
77  	 * that the graph has a fixed size, but the disadvantage is that user cannot
78  	 * compare their usage statistics anymore. I decide to favour the latter
79  	 * behaviour: more events means a bigger graph.
80  	 * @param eventkind The eventkind to calculate the image size for.
81  	 * @return The size: the amount of events divided by 8.
82  	 */
83  	private String getImageSize(int eventkind) {
84  		return "" + getAmount(eventkind) / 8;
85  	}
86  	
87  	private String getAmountDesc(int eventkind) {
88  		Date d = DateTools.encodeDate(1, getMonth(), getYear());
89  		return getAmount(eventkind).toString() + " songs " + Event.getDescription(eventkind) + " in " + DateTools.formatDate(d, "MMMMM yyyy");
90  	}
91  
92  	public String getPlaysSize() {
93  		return getImageSize(Event.played);
94  	}
95  	public String getPlaysDesc() {
96  		return getAmountDesc(Event.played);
97  	}
98  
99  	public String getRequestsSize() {
100 		return getImageSize(Event.requested);
101 	}
102 	public String getRequestsDesc() {
103 		return getAmountDesc(Event.requested);
104 	}
105 
106 	public String getDownloadsSize() {
107 		return getImageSize(Event.downloaded);
108 	}
109 	public String getDownloadsDesc() {
110 		return getAmountDesc(Event.downloaded);
111 	}
112 
113 	public String getSkipsSize() {
114 		return getImageSize(Event.skipped);
115 	}
116 	public String getSkipsDesc() {
117 		return getAmountDesc(Event.skipped);
118 	}
119 
120 	/**
121 	 * Returns the playlistId for a top100 list of this year.
122 	 * @return The playlistID.
123 	 */
124 	public long getTop100IdThisYear() {
125 		return (new PlaylistKey(-8,DateTools.encodeDate(1, 1, getYear()))).getId();
126 	}
127 	
128 	/**
129 	 * Returns the playlistId for a top25 of this month
130 	 * @return The playlistID.
131 	 */
132 	public long getTop25IdThisMonth() {
133 		return (new PlaylistKey(-4,DateTools.encodeDate(1, getMonth(), getYear()))).getId();
134 	}
135 
136 	/**
137 	 * Returns the playlistId for a reqyest top25 of this month
138 	 * @return The playlistID.
139 	 */
140 	public long getRequestTop25IdThisMonth() {
141 		return (new PlaylistKey(-5,DateTools.encodeDate(1, getMonth(), getYear()))).getId();
142 	}
143 
144 }