1 19 20 package za.org.coefficient.statistics.data; 21 22 import java.text.SimpleDateFormat ; 23 24 import java.util.Calendar ; 25 import java.util.Date ; 26 import java.util.GregorianCalendar ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 35 public class ProjectStatistics implements java.io.Serializable { 36 37 public static final SimpleDateFormat DATE_FORMAT = 38 new SimpleDateFormat ("dd/MM/yyyy"); 39 40 42 private Date statsDate; 43 private Long id; 44 private Long projectId; 45 private Map moduleStatistics = new HashMap (); 46 private Map archive = new HashMap (); 47 private ProjectStatisticsData currentData; 48 49 51 public ProjectStatistics() { 52 archive = new HashMap (); 53 currentData = new ProjectStatisticsData(); 54 } 55 56 58 59 public boolean archiveStats() { 60 boolean retVal = false; 61 Calendar c1 = new GregorianCalendar (); 62 c1.setTime(this.getLatestModifiedDate()); 63 Calendar c2 = new GregorianCalendar (); 64 if (c1.get(Calendar.DAY_OF_YEAR) < c2.get(Calendar.DAY_OF_YEAR)) { 65 retVal = true; 66 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 (System.currentTimeMillis()); 75 76 Map modStats = this.moduleStatistics; 77 for (Iterator it = modStats.keySet().iterator(); it.hasNext();) { 78 String key = (String ) it.next(); 79 80 ModuleStatistics ms = (ModuleStatistics) modStats.get(key); 82 ms.archiveStats(); 83 } 84 } 85 return retVal; 86 } 87 88 93 public void setId(Long argId) { 94 this.id = argId; 95 } 96 97 104 public Long getId() { 105 return this.id; 106 } 107 108 public Date getLatestModifiedDate() { 109 Date retVal = statsDate; 110 for (Iterator it = moduleStatistics.keySet().iterator(); it.hasNext();) { 111 String key = (String ) 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 126 public void setModuleStatistics(Map argModuleStatistics) { 127 this.moduleStatistics = argModuleStatistics; 128 } 129 130 146 public Map getModuleStatistics() { 147 return this.moduleStatistics; 148 } 149 150 155 public void setProjectId(Long argProjectId) { 156 this.projectId = argProjectId; 157 } 158 159 166 public Long getProjectId() { 167 return this.projectId; 168 } 169 170 175 public void setStatsDate(Date argStatsDate) { 176 this.statsDate = argStatsDate; 177 } 178 179 186 public Date 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 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 225 public Map getArchive() { 226 return this.archive; 227 } 228 229 234 public void setArchive(Map argArchive) { 235 this.archive = argArchive; 236 } 237 238 247 public ProjectStatisticsData getCurrentData() { 248 return this.currentData; 249 } 250 251 256 public void setCurrentData(ProjectStatisticsData argCurrentData) { 257 this.currentData = argCurrentData; 258 } 259 260 270 public Map getTotalActivityForNumberOfDaysIncludingToday(int days) { 271 HashMap activity = new HashMap (); 272 Date theDate = statsDate; 273 274 for(int i = 0; i < days; i++) { 275 long modulesTotal = 0; 276 for(Iterator it = moduleStatistics.keySet().iterator(); it.hasNext();) { 277 String moduleName = (String )it.next(); 278 ModuleStatistics modStat = 279 (ModuleStatistics)moduleStatistics.get(moduleName); 280 modulesTotal += modStat.getStatsTotalForDate(theDate); 281 } 282 activity.put(theDate, new Long (getViewStatsForDate(theDate) 283 + getDownloadStatsForDate(theDate) 284 + modulesTotal)); 285 theDate = new Date (theDate.getTime() - (1000 * 60 * 60 * 24)); 287 } 288 return activity; 289 } 290 291 public long getViewStatsForDate(Date 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 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 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 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 337 public Map getViewActivityForNumberOfDaysIncludingToday(int days) { 338 HashMap activity = new HashMap (); 339 Date theDate = statsDate; 340 341 for(int i = 0; i < days; i++) { 342 activity.put(theDate, new Long (getViewStatsForDate(theDate))); 343 theDate = new Date (theDate.getTime() - (1000 * 60 * 60 * 24)); 345 } 346 return activity; 347 } 348 349 357 public long getViewActivityTotalForNumberOfDaysIncludingToday(int days) { 358 long total = 0; 359 Date theDate = statsDate; 360 361 for(int i = 0; i < days; i++) { 362 total += getViewStatsForDate(theDate); 363 theDate = new Date (theDate.getTime() - (1000 * 60 * 60 * 24)); 365 } 366 return total; 367 } 368 369 378 public Map getDownloadActivityForNumberOfDaysIncludingToday(int days) { 379 HashMap activity = new HashMap (); 380 Date theDate = statsDate; 381 382 for(int i = 0; i < days; i++) { 383 activity.put(theDate, new Long (getDownloadStatsForDate(theDate))); 384 theDate = new Date (theDate.getTime() - (1000 * 60 * 60 * 24)); 386 } 387 return activity; 388 } 389 390 398 public long getDownloadActivityTotalForNumberOfDaysIncludingToday(int days) { 399 long total = 0; 400 Date theDate = statsDate; 401 402 for(int i = 0; i < days; i++) { 403 total += getDownloadStatsForDate(theDate); 404 theDate = new Date (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 theDate = statsDate; 413 414 415 if((days - 1) < archive.size()) { 416 for(int i = 0; i < days; i++) { 418 theDate = new Date (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 (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 it = moduleStatistics.keySet() 437 .iterator(); it.hasNext();) { 438 String key = (String ) it.next(); 439 ModuleStatistics ms = (ModuleStatistics) moduleStatistics.get(key); 440 retVal += ms.calculateStats(); 441 } 442 443 return retVal; 444 } 445 446 } 447 | Popular Tags |