KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > statistics > services > StatisticsService


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.services;
21
22 import java.util.Date JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import net.sf.hibernate.HibernateException;
26 import net.sf.hibernate.util.HibernateUtil;
27 import za.org.coefficient.core.Project;
28 import za.org.coefficient.interfaces.StatisticsMessageSender;
29 import za.org.coefficient.statistics.data.ModuleStatistics;
30 import za.org.coefficient.statistics.data.ProjectStatistics;
31
32 /**
33  * @pojo2ejb.class
34  * name="StatisticsService"
35  * jndi-prefix="za/org/coefficient/service/"
36  *
37  * @web.resource-env-ref
38  * name="za/org/coefficient/service/StatisticsService"
39  * type="za.org.coefficient.statistics.services.StatisticsService"
40  * @web.resource-env-ref
41  * name="StatisticsService"
42  * type="za.org.coefficient.statistics.services.StatisticsService"
43  */

44 public class StatisticsService {
45
46     public void doStatistics(String JavaDoc name, Long JavaDoc projectId, String JavaDoc action) throws HibernateException {
47         
48         long prjId = projectId.longValue();
49         Project prj =
50             (Project) HibernateUtil.load(Project.class, new Long JavaDoc(prjId));
51         ProjectStatistics ps = prj.getStatistics();
52         
53         if(ps != null) {
54             // Give the stats a chance to archive themselves if needed
55
ps.archiveStats();
56
57             // Do updates
58
if ((name == null || name.trim().equals(""))
59                 && StatisticsMessageSender.VIEW.equals(action)) {
60                 ps.getCurrentData().setViewCount(ps.getCurrentData().getViewCount() + 1);
61             } if ((name == null || name.trim().equals(""))
62                   && StatisticsMessageSender.DOWNLOAD.equals(action)) {
63                 ps.getCurrentData().setDownloadCount(ps.getCurrentData().getDownloadCount() + 1);
64             } else if (name != null && !name.trim().equals("")) {
65                 Map JavaDoc modStats = ps.getModuleStatistics();
66                 ModuleStatistics ms = (ModuleStatistics) modStats.get(name);
67                 
68                 // create it if it does not exist
69
if (ms == null) {
70                     ms = new ModuleStatistics();
71                     ms.setStatsDate(new Date JavaDoc(System.currentTimeMillis()));
72                     ms.setName(name);
73                     ps.addModuleStatistics(ms);
74                 }
75                 if (StatisticsMessageSender.UPDATE.equals(action)) {
76                     ms.getCurrentData().setUpdateCount(ms.getCurrentData().getUpdateCount() + 1);
77                 } else if (StatisticsMessageSender.CREATE.equals(action)) {
78                     ms.getCurrentData().setCreateCount(ms.getCurrentData().getCreateCount() + 1);
79                 } else if (StatisticsMessageSender.COMPLETE.equals(action)) {
80                     ms.getCurrentData().setCompleteCount(ms.getCurrentData().getCompleteCount() + 1);
81                 }
82             }
83
84             // THIS SECTION SHOULD GO INTO A SCHEDULED CALL
85
ps.calculateRank();
86             HibernateUtil.saveOrUpdate(ps);
87         } else {
88             System.err.println("<< project statistics were null");
89         }
90
91     }
92
93 }
94
Popular Tags