KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.SimpleDateFormat JavaDoc;
23
24 import java.util.Calendar JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.GregorianCalendar JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * @hibernate.class
33  * table="COEFFICIENT_PROJECT_STATISTICS"
34  */

35 public class ProjectStatistics implements java.io.Serializable JavaDoc {
36
37     public static final SimpleDateFormat JavaDoc DATE_FORMAT =
38         new SimpleDateFormat JavaDoc("dd/MM/yyyy");
39
40     //~ Instance fields ========================================================
41

42     private Date JavaDoc statsDate;
43     private Long JavaDoc id;
44     private Long JavaDoc projectId;
45     private Map JavaDoc moduleStatistics = new HashMap JavaDoc();
46     private Map JavaDoc archive = new HashMap JavaDoc();
47     private ProjectStatisticsData currentData;
48
49     //~ Constructors ===========================================================
50

51     public ProjectStatistics() {
52         archive = new HashMap JavaDoc();
53         currentData = new ProjectStatisticsData();
54     }
55
56     //~ Methods ================================================================
57

58
59     public boolean archiveStats() {
60         boolean retVal = false;
61         Calendar JavaDoc c1 = new GregorianCalendar JavaDoc();
62         c1.setTime(this.getLatestModifiedDate());
63         Calendar JavaDoc c2 = new GregorianCalendar JavaDoc();
64         if (c1.get(Calendar.DAY_OF_YEAR) < c2.get(Calendar.DAY_OF_YEAR)) {
65             retVal = true;
66             // Roll the current stats into the archive
67
this.archive.put(DATE_FORMAT.format(statsDate), currentData);
68             ProjectStatisticsData newData = new ProjectStatisticsData();
69             newData.setCarryOverViewCount(currentData.getViewCount()
70                                           + currentData.getCarryOverViewCount());
71             newData.setCarryOverDownloadCount(currentData.getDownloadCount()
72                                               + currentData.getCarryOverDownloadCount());
73             currentData = newData;
74             statsDate = new Date JavaDoc(System.currentTimeMillis());
75
76             Map JavaDoc modStats = this.moduleStatistics;
77             for (Iterator JavaDoc it = modStats.keySet().iterator(); it.hasNext();) {
78                 String JavaDoc key = (String JavaDoc) it.next();
79
80                 // Give each module a chance to archive itself
81
ModuleStatistics ms = (ModuleStatistics) modStats.get(key);
82                 ms.archiveStats();
83             }
84         }
85         return retVal;
86     }
87
88     /**
89      * Sets the value of id
90      *
91      * @param argId Value to assign to this.id
92      */

93     public void setId(Long JavaDoc argId) {
94         this.id = argId;
95     }
96
97     /**
98      * Gets the value of id
99      *
100      * @return the value of id
101      * @hibernate.id
102      * generator-class="native"
103      */

104     public Long JavaDoc getId() {
105         return this.id;
106     }
107
108     public Date JavaDoc getLatestModifiedDate() {
109         Date JavaDoc retVal = statsDate;
110         for (Iterator JavaDoc it = moduleStatistics.keySet().iterator(); it.hasNext();) {
111             String JavaDoc key = (String JavaDoc) it.next();
112             ModuleStatistics ms = (ModuleStatistics) moduleStatistics.get(key);
113             if (ms.getStatsDate().after(retVal)) {
114                 retVal = ms.getStatsDate();
115             }
116         }
117
118         return retVal;
119     }
120
121     /**
122      * Sets the value of moduleStatistics
123      *
124      * @param argModuleStatistics Value to assign to this.moduleStatistics
125      */

126     public void setModuleStatistics(Map JavaDoc argModuleStatistics) {
127         this.moduleStatistics = argModuleStatistics;
128     }
129
130     /**
131      * Gets the value of moduleStatistics
132      *
133      * @return the value of moduleStatistics
134      * @hibernate.map
135      * cascade="all"
136      * table="COEFFICIENT_PROJECT_STATISTICS_MODULE_STATISTICS"
137      * @hibernate.collection-key
138      * column="PROJECT_STATISTICS_ID"
139      * @hibernate.collection-index
140      * column="IDX"
141      * type="string"
142      * @hibernate.collection-many-to-many
143      * class="za.org.coefficient.statistics.data.ModuleStatistics"
144      * column="MODULE_STATISTIC"
145      */

146     public Map JavaDoc getModuleStatistics() {
147         return this.moduleStatistics;
148     }
149
150     /**
151      * Sets the value of projectId
152      *
153      * @param argProjectId Value to assign to this.projectId
154      */

155     public void setProjectId(Long JavaDoc argProjectId) {
156         this.projectId = argProjectId;
157     }
158
159     /**
160      * Gets the value of projectId
161      *
162      * @return the value of projectId
163      * @hibernate.property
164      * column="PROJECT_ID"
165      */

166     public Long JavaDoc getProjectId() {
167         return this.projectId;
168     }
169
170     /**
171      * Sets the value of statsDate
172      *
173      * @param argStatsDate Value to assign to this.statsDate
174      */

175     public void setStatsDate(Date JavaDoc argStatsDate) {
176         this.statsDate = argStatsDate;
177     }
178
179     /**
180      * Gets the value of statsDate
181      *
182      * @return the value of statsDate
183      * @hibernate.property
184      * column="STATISTICS_DATE"
185      */

186     public Date JavaDoc getStatsDate() {
187         return this.statsDate;
188     }
189
190     public void addModuleStatistics(ModuleStatistics ms) {
191         ms.setProjectId(getProjectId());
192         this.moduleStatistics.put(ms.getName(), ms);
193     }
194
195     public void calculateRank() {
196         // perform some equation that calculates the rank points
197
long newRank = getCurrentData().getCarryOverViewCount()
198             + getCurrentData().getCarryOverDownloadCount()
199             + calculateStats();
200         if (newRank != getCurrentData().getRank()) {
201             getCurrentData().setRank(newRank);
202         }
203     }
204
205     public void deleteModuleStatistics(ModuleStatistics ms) {
206         this.moduleStatistics.remove(ms.getName());
207     }
208
209     /**
210      * Gets the value of archive
211      *
212      * @return the value of archive
213      * @hibernate.map
214      * cascade="all"
215      * table="COEFFICIENT_PROJECT_STATISTICS_ARCHIVE"
216      * @hibernate.collection-key
217      * column="PROJECT_STATISTICS_ID"
218      * @hibernate.collection-index
219      * column="IDX"
220      * type="string"
221      * @hibernate.collection-many-to-many
222      * class="za.org.coefficient.statistics.data.ProjectStatisticsData"
223      * column="PROJECT_STATISTICS_DATA"
224      */

225     public Map JavaDoc getArchive() {
226         return this.archive;
227     }
228
229     /**
230      * Sets the value of archive
231      *
232      * @param argArchive Value to assign to this.archive
233      */

234     public void setArchive(Map JavaDoc argArchive) {
235         this.archive = argArchive;
236     }
237
238     /**
239      * Gets the value of currentData
240      *
241      * @return the value of currentData
242      * @hibernate.many-to-one
243      * cascade="all"
244      * unique="true"
245      * column="CURRENT_DATA"
246      */

247     public ProjectStatisticsData getCurrentData() {
248         return this.currentData;
249     }
250
251     /**
252      * Sets the value of currentData
253      *
254      * @param argCurrentData Value to assign to this.currentData
255      */

256     public void setCurrentData(ProjectStatisticsData argCurrentData) {
257         this.currentData = argCurrentData;
258     }
259
260     /**
261      * This method will get the total activity for each day up to the number
262      * of specified days from today. For example if daysAgo is 7 the returned
263      * map will return the total activity for the last 7 days including today.
264      *
265      * @param days indicates how many days including today to report back on. If
266      * there is no data for a day then the activity result will be 0.
267      * @return Map is a map keyed by a java.util.Date object containing the
268      * java.lang.Long value that is the total activity for the day.
269      */

270     public Map JavaDoc getTotalActivityForNumberOfDaysIncludingToday(int days) {
271         HashMap JavaDoc activity = new HashMap JavaDoc();
272         Date JavaDoc theDate = statsDate;
273
274         for(int i = 0; i < days; i++) {
275             long modulesTotal = 0;
276             for(Iterator JavaDoc it = moduleStatistics.keySet().iterator(); it.hasNext();) {
277                 String JavaDoc moduleName = (String JavaDoc)it.next();
278                 ModuleStatistics modStat =
279                     (ModuleStatistics)moduleStatistics.get(moduleName);
280                 modulesTotal += modStat.getStatsTotalForDate(theDate);
281             }
282             activity.put(theDate, new Long JavaDoc(getViewStatsForDate(theDate)
283                                            + getDownloadStatsForDate(theDate)
284                                            + modulesTotal));
285             // decrement the date
286
theDate = new Date JavaDoc(theDate.getTime() - (1000 * 60 * 60 * 24));
287         }
288         return activity;
289     }
290
291     public long getViewStatsForDate(Date JavaDoc date) {
292         long retVal = 0;
293
294         if(ProjectStatistics.DATE_FORMAT.format(statsDate)
295            .equals(ProjectStatistics.DATE_FORMAT.format(date))) {
296             retVal = currentData.getViewCount();
297         } else {
298             // look to the archive for this data
299
ProjectStatisticsData prjStatData =
300                 (ProjectStatisticsData)archive.get(DATE_FORMAT.format(date));
301             if(prjStatData != null) {
302                 retVal = prjStatData.getViewCount();
303             }
304         }
305
306         return retVal;
307     }
308
309     public long getDownloadStatsForDate(Date JavaDoc date) {
310         long retVal = 0;
311
312         if(ProjectStatistics.DATE_FORMAT.format(statsDate)
313            .equals(ProjectStatistics.DATE_FORMAT.format(date))) {
314             retVal = currentData.getDownloadCount();
315         } else {
316             // look to the archive for this data
317
ProjectStatisticsData prjStatData =
318                 (ProjectStatisticsData)archive.get(DATE_FORMAT.format(date));
319             if(prjStatData != null) {
320                 retVal = prjStatData.getDownloadCount();
321             }
322         }
323
324         return retVal;
325     }
326
327     /**
328      * This method will get the view activity for each day up to the number
329      * of specified days from today. For example if daysAgo is 7 the returned
330      * map will return the view activity for the last 7 days including today.
331      *
332      * @param days indicates how many days including today to report back on. If
333      * there is no data for a day then the activity result will be 0.
334      * @return Map is a map keyed by a java.util.Date object containing the
335      * java.lang.Long value that is the view activity for the day.
336      */

337     public Map JavaDoc getViewActivityForNumberOfDaysIncludingToday(int days) {
338         HashMap JavaDoc activity = new HashMap JavaDoc();
339         Date JavaDoc theDate = statsDate;
340
341         for(int i = 0; i < days; i++) {
342             activity.put(theDate, new Long JavaDoc(getViewStatsForDate(theDate)));
343             // decrement the date
344
theDate = new Date JavaDoc(theDate.getTime() - (1000 * 60 * 60 * 24));
345         }
346         return activity;
347     }
348
349     /**
350      * This method will get the view activity for each day up to the number
351      * of specified days from today.
352      *
353      * @param days indicates how many days including today to report back on. If
354      * there is no data for a day then the activity result will be 0.
355      * @return long is the total for the date range
356      */

357     public long getViewActivityTotalForNumberOfDaysIncludingToday(int days) {
358         long total = 0;
359         Date JavaDoc theDate = statsDate;
360
361         for(int i = 0; i < days; i++) {
362             total += getViewStatsForDate(theDate);
363             // decrement the date
364
theDate = new Date JavaDoc(theDate.getTime() - (1000 * 60 * 60 * 24));
365         }
366         return total;
367     }
368
369     /**
370      * This method will get the download activity for each day up to the number
371      * of specified days from today.
372      *
373      * @param days indicates how many days including today to report back on. If
374      * there is no data for a day then the activity result will be 0.
375      * @return Map is a map keyed by a java.util.Date object containing the
376      * java.lang.Long value that is the download activity for the day.
377      */

378     public Map JavaDoc getDownloadActivityForNumberOfDaysIncludingToday(int days) {
379         HashMap JavaDoc activity = new HashMap JavaDoc();
380         Date JavaDoc theDate = statsDate;
381
382         for(int i = 0; i < days; i++) {
383             activity.put(theDate, new Long JavaDoc(getDownloadStatsForDate(theDate)));
384             // decrement the date
385
theDate = new Date JavaDoc(theDate.getTime() - (1000 * 60 * 60 * 24));
386         }
387         return activity;
388     }
389
390     /**
391      * This method will get the download activity for each day up to the number
392      * of specified days from today.
393      *
394      * @param days indicates how many days including today to report back on. If
395      * there is no data for a day then the activity result will be 0.
396      * @return long is the total for the date range
397      */

398     public long getDownloadActivityTotalForNumberOfDaysIncludingToday(int days) {
399         long total = 0;
400         Date JavaDoc theDate = statsDate;
401
402         for(int i = 0; i < days; i++) {
403             total += getDownloadStatsForDate(theDate);
404             // decrement the date
405
theDate = new Date JavaDoc(theDate.getTime() - (1000 * 60 * 60 * 24));
406         }
407         return total;
408     }
409
410     public long getRankForNumberOfDaysIncludingToday(int days) {
411         long rank = currentData.getRank();
412         Date JavaDoc theDate = statsDate;
413
414
415         if((days - 1) < archive.size()) {
416             // Find the rank on the day we want
417
for(int i = 0; i < days; i++) {
418                 theDate = new Date JavaDoc(theDate.getTime() - (1000 * 60 * 60 * 24));
419             }
420             ProjectStatisticsData prjStatData =
421                 (ProjectStatisticsData)archive.get(DATE_FORMAT.format(theDate));
422             while(prjStatData == null) {
423                 theDate = new Date JavaDoc(theDate.getTime() - (1000 * 60 * 60 * 24));
424                 prjStatData = (ProjectStatisticsData)archive
425                     .get(DATE_FORMAT.format(theDate));
426             }
427             rank -= prjStatData.getRank();
428         }
429         return rank;
430     }
431
432     private long calculateStats() {
433         long retVal = 0;
434         retVal = getCurrentData().getViewCount()
435             + getCurrentData().getDownloadCount();
436         for (Iterator JavaDoc it = moduleStatistics.keySet()
437                  .iterator(); it.hasNext();) {
438             String JavaDoc key = (String JavaDoc) it.next();
439             ModuleStatistics ms = (ModuleStatistics) moduleStatistics.get(key);
440             retVal += ms.calculateStats();
441         }
442
443         return retVal;
444     }
445
446 }
447
Popular Tags