KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > providers > persistence > ReportLogPersistenceProvider


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

19
20 package org.efs.openreports.providers.persistence;
21
22 import java.util.Calendar JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.log4j.Logger;
26 import org.efs.openreports.objects.ReportLog;
27 import org.efs.openreports.providers.HibernateProvider;
28 import org.efs.openreports.providers.ProviderException;
29 import org.hibernate.Criteria;
30 import org.hibernate.HibernateException;
31 import org.hibernate.Session;
32 import org.hibernate.criterion.Expression;
33 import org.hibernate.criterion.Restrictions;
34
35 public class ReportLogPersistenceProvider
36 {
37     protected static Logger log =
38         Logger.getLogger(ReportLogPersistenceProvider.class.getName());
39
40     public ReportLogPersistenceProvider() throws ProviderException
41     {
42         super();
43
44         log.info("ReportLogPersistenceProvider Created.");
45     }
46
47     public ReportLog getReportLog(Integer JavaDoc id) throws ProviderException
48     {
49         return (ReportLog) HibernateProvider.load(ReportLog.class, id);
50     }
51
52     public List JavaDoc getReportLogs(String JavaDoc status, Integer JavaDoc userId, Integer JavaDoc reportId, Integer JavaDoc alertId, int maxRows) throws ProviderException
53     {
54         Session session = HibernateProvider.openSession();
55
56         try
57         {
58             Criteria criteria = session.createCriteria(ReportLog.class);
59             criteria.setMaxResults(maxRows);
60             
61             if (status != null)
62             {
63                 criteria.add(Expression.eq("status", status));
64             }
65             
66             if (userId != null)
67             {
68                 criteria.add(Expression.eq("user.id", userId));
69             }
70             
71             if (reportId != null)
72             {
73                 if (reportId.intValue() == -1)
74                 {
75                     criteria.add(Restrictions.isNull("report"));
76                 }
77                 else
78                 {
79                     criteria.add(Expression.eq("report.id", reportId));
80                 }
81             }
82             
83             if (alertId != null)
84             {
85                 if (alertId.intValue() == -1)
86                 {
87                     criteria.add(Restrictions.isNull("alert"));
88                 }
89                 else
90                 {
91                     criteria.add(Expression.eq("alert.id", alertId));
92                 }
93             }
94             
95             List JavaDoc list = criteria.list();
96
97             return list;
98         }
99         catch (HibernateException he)
100         {
101             throw new ProviderException(he);
102         }
103         finally
104         {
105             HibernateProvider.closeSession(session);
106         }
107     }
108
109     public ReportLog insertReportLog(ReportLog reportLog)
110         throws ProviderException
111     {
112         return (ReportLog) HibernateProvider.save(reportLog);
113     }
114
115     public void updateReportLog(ReportLog reportLog) throws ProviderException
116     {
117         HibernateProvider.update(reportLog);
118     }
119
120     public void deleteReportLog(ReportLog reportLog) throws ProviderException
121     {
122         try
123         {
124             HibernateProvider.delete(reportLog);
125         }
126         catch (ConstraintException e)
127         {
128             throw new ProviderException(e.getMessage());
129         }
130     }
131     
132     public List JavaDoc getTopReportsByUser() throws ProviderException
133     {
134         String JavaDoc fromClause = "select log.user.name, log.report.name, count(*) from org.efs.openreports.objects.ReportLog log group by log.user.name, log.report.name order by log.user.name, count(*) desc ";
135
136         return HibernateProvider.query(fromClause);
137     }
138     
139     public List JavaDoc getTopReports() throws ProviderException
140     {
141         String JavaDoc fromClause = "select log.report.name, count(*) from org.efs.openreports.objects.ReportLog log group by log.report.name order by count(*) desc ";
142
143         return HibernateProvider.query(fromClause);
144     }
145     
146     public List JavaDoc getTopFailures() throws ProviderException
147     {
148         String JavaDoc fromClause = "select log.report.name, count(*) from org.efs.openreports.objects.ReportLog log where log.status = 'failure' group by log.report.name order by count(*) desc ";
149
150         return HibernateProvider.query(fromClause);
151     }
152     
153     public List JavaDoc getTopEmptyReports() throws ProviderException
154     {
155         String JavaDoc fromClause = "select log.report.name, count(*) from org.efs.openreports.objects.ReportLog log where log.status = 'empty' group by log.report.name order by count(*) desc ";
156
157         return HibernateProvider.query(fromClause);
158     }
159     
160     public List JavaDoc getTopReportsForPeriod(int daysBack) throws ProviderException
161     {
162         try
163         {
164             Calendar JavaDoc calendar = Calendar.getInstance();
165             calendar.add(Calendar.DATE, -daysBack);
166             
167             Session session = HibernateProvider.openSession();
168             
169             try
170             {
171                 List JavaDoc list = session.createQuery(
172                         "select log.report.name, count(*) from org.efs.openreports.objects.ReportLog log "
173                         + " where log.startTime > ? group by log.report.name order by count(*) desc").setDate(0, calendar.getTime()).list();
174
175                 return list;
176             }
177             catch (HibernateException he)
178             {
179                 throw he;
180             }
181             finally
182             {
183                 session.close();
184             }
185         }
186         catch (HibernateException he)
187         {
188             throw new ProviderException(he);
189         }
190     }
191     
192     public List JavaDoc getTopAlertsByUser() throws ProviderException
193     {
194         String JavaDoc fromClause = "select log.user.name, log.alert.name, count(*) from org.efs.openreports.objects.ReportLog log group by log.user.name, log.alert.name order by log.user.name, count(*) desc ";
195
196         return HibernateProvider.query(fromClause);
197     }
198     
199     public List JavaDoc getTopAlerts() throws ProviderException
200     {
201         String JavaDoc fromClause = "select log.alert.name, count(*) from org.efs.openreports.objects.ReportLog log group by log.alert.name order by count(*) desc ";
202
203         return HibernateProvider.query(fromClause);
204     }
205     
206     public List JavaDoc getTopTriggeredAlerts() throws ProviderException
207     {
208         String JavaDoc fromClause = "select log.alert.name, count(*) from org.efs.openreports.objects.ReportLog log where log.status = 'triggered' group by log.alert.name order by count(*) desc ";
209
210         return HibernateProvider.query(fromClause);
211     }
212     
213     public List JavaDoc getTopNotTriggeredAlerts() throws ProviderException
214     {
215         String JavaDoc fromClause = "select log.alert.name, count(*) from org.efs.openreports.objects.ReportLog log where log.status = 'not triggered' group by log.alert.name order by count(*) desc ";
216
217         return HibernateProvider.query(fromClause);
218     }
219     
220 }
Popular Tags