KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > statistics > data > ModuleStatistics


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.statistics.data;
21
22 import java.util.Calendar JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.GregorianCalendar JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * @hibernate.class
30  * table="COEFFICIENT_MODULE_STATISTICS"
31  */

32 public class ModuleStatistics implements java.io.Serializable JavaDoc {
33     //~ Instance fields ========================================================
34

35     private Date JavaDoc statsDate;
36     private Long JavaDoc id;
37     private Long JavaDoc projectId;
38     private String JavaDoc name;
39     private ModuleStatisticsData currentData;
40     private Map JavaDoc archive;
41
42     //~ Constructors ===========================================================
43

44     public ModuleStatistics() {
45         currentData = new ModuleStatisticsData();
46         archive = new HashMap JavaDoc();
47     }
48
49     //~ Methods ================================================================
50

51     public boolean archiveStats() {
52         boolean retVal = false;
53         Calendar JavaDoc c1 = new GregorianCalendar JavaDoc();
54         c1.setTime(this.statsDate);
55         Calendar JavaDoc c2 = new GregorianCalendar JavaDoc();
56         if (c1.get(Calendar.DAY_OF_YEAR) < c2.get(Calendar.DAY_OF_YEAR)) {
57             retVal = true;
58             // Roll the current stats into the archive
59
this.archive.put(ProjectStatistics.DATE_FORMAT.format(statsDate),
60                              currentData);
61
62             ModuleStatisticsData newData = new ModuleStatisticsData();
63             newData.setCarryOverCreateCount(currentData.getCarryOverCreateCount()
64                                             + currentData.getCreateCount());
65             newData.setCarryOverUpdateCount(currentData.getCarryOverUpdateCount()
66                                             + currentData.getUpdateCount());
67             newData.setCarryOverCompleteCount(currentData.getCarryOverCompleteCount()
68                                               + currentData.getCompleteCount());
69             currentData = newData;
70             statsDate = new Date JavaDoc(System.currentTimeMillis());
71         }
72         return retVal;
73     }
74
75     /**
76      * Sets the value of id
77      *
78      * @param argId Value to assign to this.id
79      */

80     public void setId(Long JavaDoc argId) {
81         this.id = argId;
82     }
83
84     /**
85      * Gets the value of id
86      *
87      * @return the value of id
88      * @hibernate.id
89      * generator-class="native"
90      */

91     public Long JavaDoc getId() {
92         return this.id;
93     }
94
95     /**
96      * Sets the value of name
97      *
98      * @param argName Value to assign to this.name
99      */

100     public void setName(String JavaDoc name) {
101         this.name = name;
102     }
103
104     /**
105      * Gets the value of name
106      *
107      * @return the value of name
108      * @hibernate.property
109      * column="NAME"
110      */

111     public String JavaDoc getName() {
112         return this.name;
113     }
114
115     /**
116      * Sets the value of projectId
117      *
118      * @param argProjectId Value to assign to this.projectId
119      */

120     public void setProjectId(Long JavaDoc argProjectId) {
121         this.projectId = argProjectId;
122     }
123
124     /**
125      * Gets the value of projectId
126      *
127      * @return the value of projectId
128      * @hibernate.property
129      * column="PROJECT_ID"
130      */

131     public Long JavaDoc getProjectId() {
132         return this.projectId;
133     }
134
135     /**
136      * Sets the value of statsDate
137      *
138      * @param argStatsDate Value to assign to this.statsDate
139      */

140     public void setStatsDate(Date JavaDoc argStatsDate) {
141         this.statsDate = argStatsDate;
142     }
143
144     /**
145      * Gets the value of statsDate
146      *
147      * @return the value of statsDate
148      * @hibernate.property
149      * column="STATISTICS_DATE"
150      */

151     public Date JavaDoc getStatsDate() {
152         return this.statsDate;
153     }
154
155
156     /**
157      * Gets the value of currentData
158      *
159      * @return the value of currentData
160      * @hibernate.many-to-one
161      * cascade="all"
162      * unique="true"
163      * column="CURRENT_DATA"
164      */

165     public ModuleStatisticsData getCurrentData() {
166         return this.currentData;
167     }
168
169     /**
170      * Sets the value of currentData
171      *
172      * @param argCurrentData Value to assign to this.currentData
173      */

174     public void setCurrentData(ModuleStatisticsData argCurrentData) {
175         this.currentData = argCurrentData;
176     }
177
178     /**
179      * Gets the value of archive
180      *
181      * @return the value of archive
182      * @hibernate.map
183      * cascade="all"
184      * table="COEFFICIENT_MODULE_STATISTICS_ARCHIVE"
185      * @hibernate.collection-key
186      * column="MODULE_STATISTIC_ID"
187      * @hibernate.collection-index
188      * column="IDX"
189      * type="string"
190      * @hibernate.collection-many-to-many
191      * class="za.org.coefficient.statistics.data.ModuleStatisticsData"
192      * column="MODULE_STATISTICS_DATA"
193      */

194     public Map JavaDoc getArchive() {
195         return this.archive;
196     }
197
198     /**
199      * Sets the value of archive
200      *
201      * @param argArchive Value to assign to this.archive
202      */

203     public void setArchive(Map JavaDoc argArchive) {
204         this.archive = argArchive;
205     }
206
207
208     public long calculateStats() {
209         long retVal = 0;
210
211         // Maybe add weights to this later
212
retVal =
213             currentData.getCarryOverCompleteCount() + currentData.getCompleteCount()
214             + currentData.getCarryOverCreateCount() + currentData.getCreateCount()
215             + currentData.getCarryOverUpdateCount() + currentData.getUpdateCount();
216
217         return retVal;
218     }
219
220     /**
221      * This method will get the complete activity for each day up to the number
222      * of specified days from today. For example if daysAgo is 7 the returned
223      * map will return the complete activity for the last 7 days including today.
224      *
225      * @param days indicates how many days including today to report back on. If
226      * there is no data for a day then the activity result will be 0.
227      * @param is the type of activity you want values for. The legal types are:
228      * create, update, and complete
229      *
230      * @return Map is a map keyed by a java.util.Date object containing the
231      * java.lang.Long value that is the update activity for the day.
232      */

233     public Map JavaDoc getActivityForNumberOfDaysIncludingToday(int days, String JavaDoc type) {
234         HashMap JavaDoc activity = new HashMap JavaDoc();
235         Date JavaDoc theDate = statsDate;
236
237         for(int i = 0; i < days; i++) {
238             Long JavaDoc val = null;
239             if("create".equalsIgnoreCase(type)) {
240                 val = new Long JavaDoc(getCreateStatsForDate(theDate));
241             } else if("update".equalsIgnoreCase(type)) {
242                 val = new Long JavaDoc(getUpdateStatsForDate(theDate));
243             } else if("complete".equalsIgnoreCase(type)) {
244                 val = new Long JavaDoc(getCompleteStatsForDate(theDate));
245             } else {
246                 throw new IllegalArgumentException JavaDoc("Type: "+type
247                                                    + " is not a valid type");
248             }
249             activity.put(theDate, val);
250             // decrement the date
251
theDate = new Date JavaDoc(theDate.getTime() - (1000 * 60 * 60 * 24));
252         }
253         return activity;
254     }
255
256     /**
257      * This method will get the total activity for each day up to the number
258      * of specified days from today.
259      *
260      * @param days indicates how many days including today to report back on. If
261      * there is no data for a day then the activity result will be 0.
262      * @return long is the total for the date range
263      */

264     public long getActivityTotalForNumberOfDaysIncludingToday(int days) {
265         long total = 0;
266         Date JavaDoc theDate = statsDate;
267
268         for(int i = 0; i < days; i++) {
269             total += getStatsTotalForDate(theDate);
270             // decrement the date
271
theDate = new Date JavaDoc(theDate.getTime() - (1000 * 60 * 60 * 24));
272         }
273         return total;
274     }
275
276     public long getStatsTotalForDate(Date JavaDoc date) {
277         return getCreateStatsForDate(date) + getUpdateStatsForDate(date) +
278             getCompleteStatsForDate(date);
279     }
280
281     public long getCreateStatsForDate(Date JavaDoc date) {
282         long retVal = 0;
283         if(ProjectStatistics.DATE_FORMAT.format(statsDate)
284            .equals(ProjectStatistics.DATE_FORMAT.format(date))) {
285             // Maybe add weights to this later
286
retVal = currentData.getCreateCount();
287         } else {
288             // look to the archive for this data
289
ModuleStatisticsData modStatData =
290                 (ModuleStatisticsData)archive
291                 .get(ProjectStatistics.DATE_FORMAT.format(date));
292             if(modStatData != null) {
293                 retVal = modStatData.getCreateCount();
294             }
295         }
296
297         return retVal;
298     }
299
300     public long getUpdateStatsForDate(Date JavaDoc date) {
301         long retVal = 0;
302         if(ProjectStatistics.DATE_FORMAT.format(statsDate)
303            .equals(ProjectStatistics.DATE_FORMAT.format(date))) {
304             // Maybe add weights to this later
305
retVal = currentData.getUpdateCount();
306         } else {
307             // look to the archive for this data
308
ModuleStatisticsData modStatData =
309                 (ModuleStatisticsData)archive
310                 .get(ProjectStatistics.DATE_FORMAT.format(date));
311             if(modStatData != null) {
312                 retVal = modStatData.getUpdateCount();
313             }
314         }
315
316         return retVal;
317     }
318
319     public long getCompleteStatsForDate(Date JavaDoc date) {
320         long retVal = 0;
321         if(ProjectStatistics.DATE_FORMAT.format(statsDate)
322            .equals(ProjectStatistics.DATE_FORMAT.format(date))) {
323             // Maybe add weights to this later
324
retVal = currentData.getCompleteCount();
325         } else {
326             // look to the archive for this data
327
ModuleStatisticsData modStatData =
328                 (ModuleStatisticsData)archive
329                 .get(ProjectStatistics.DATE_FORMAT.format(date));
330             if(modStatData != null) {
331                 retVal = modStatData.getCompleteCount();
332             }
333         }
334
335         return retVal;
336     }
337
338     public boolean equals(Object JavaDoc other) {
339         ModuleStatistics o = (ModuleStatistics) other;
340
341         return (this != null) && (o != null)
342             && (((id == null) && (o.id == null)) || id.equals(o.id))
343             && (((projectId == null) && (o.projectId == null))
344                 || projectId.equals(o.projectId))
345             && (((name == null) && (o.name == null)) || name.equals(o.name))
346             && (((statsDate == null) && (o.statsDate == null))
347                 || statsDate.equals(o.statsDate));
348     }
349 }
350
Popular Tags