1 19 20 package org.efs.openreports.providers.persistence; 21 22 import java.util.Calendar ; 23 import java.util.List ; 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 id) throws ProviderException 48 { 49 return (ReportLog) HibernateProvider.load(ReportLog.class, id); 50 } 51 52 public List getReportLogs(String status, Integer userId, Integer reportId, Integer 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 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 getTopReportsByUser() throws ProviderException 133 { 134 String 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 getTopReports() throws ProviderException 140 { 141 String 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 getTopFailures() throws ProviderException 147 { 148 String 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 getTopEmptyReports() throws ProviderException 154 { 155 String 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 getTopReportsForPeriod(int daysBack) throws ProviderException 161 { 162 try 163 { 164 Calendar calendar = Calendar.getInstance(); 165 calendar.add(Calendar.DATE, -daysBack); 166 167 Session session = HibernateProvider.openSession(); 168 169 try 170 { 171 List 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 getTopAlertsByUser() throws ProviderException 193 { 194 String 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 getTopAlerts() throws ProviderException 200 { 201 String 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 getTopTriggeredAlerts() throws ProviderException 207 { 208 String 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 getTopNotTriggeredAlerts() throws ProviderException 214 { 215 String 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 |