001: /*
002: * Copyright (C) 2006 Erik Swenson - erik@oreports.com
003: *
004: * This program is free software; you can redistribute it and/or modify it
005: * under the terms of the GNU General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option)
007: * any later version.
008: *
009: * This program is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
012: * more details.
013: *
014: * You should have received a copy of the GNU General Public License along with
015: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
016: * Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package org.efs.openreports.providers.impl;
021:
022: import java.sql.Connection;
023: import java.sql.PreparedStatement;
024: import java.sql.ResultSet;
025: import java.util.Date;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import org.apache.log4j.Logger;
030: import org.efs.openreports.objects.ReportAlert;
031: import org.efs.openreports.objects.ReportDataSource;
032: import org.efs.openreports.objects.ReportLog;
033: import org.efs.openreports.objects.ReportUserAlert;
034: import org.efs.openreports.providers.AlertProvider;
035: import org.efs.openreports.providers.DataSourceProvider;
036: import org.efs.openreports.providers.HibernateProvider;
037: import org.efs.openreports.providers.ProviderException;
038: import org.efs.openreports.providers.ReportLogProvider;
039: import org.efs.openreports.util.LocalStrings;
040: import org.hibernate.HibernateException;
041: import org.hibernate.Session;
042: import org.hibernate.Transaction;
043:
044: public class AlertProviderImpl implements AlertProvider {
045: protected static Logger log = Logger
046: .getLogger(AlertProviderImpl.class.getName());
047:
048: private DataSourceProvider dataSourceProvider;
049: private ReportLogProvider reportLogProvider;
050: private HibernateProvider hibernateProvider;
051:
052: public AlertProviderImpl(DataSourceProvider dataSourceProvider,
053: ReportLogProvider reportLogProvider,
054: HibernateProvider hibernateProvider)
055: throws ProviderException {
056: this .dataSourceProvider = dataSourceProvider;
057: this .reportLogProvider = reportLogProvider;
058: this .hibernateProvider = hibernateProvider;
059:
060: log.info("Created");
061: }
062:
063: public ReportAlert getReportAlert(Integer id)
064: throws ProviderException {
065: return (ReportAlert) hibernateProvider.load(ReportAlert.class,
066: id);
067: }
068:
069: @SuppressWarnings("unchecked")
070: public List<ReportAlert> getReportAlerts() throws ProviderException {
071: String fromClause = "from org.efs.openreports.objects.ReportAlert reportAlert order by reportAlert.name ";
072:
073: return (List<ReportAlert>) hibernateProvider.query(fromClause);
074: }
075:
076: public ReportAlert insertReportAlert(ReportAlert reportAlert)
077: throws ProviderException {
078: return (ReportAlert) hibernateProvider.save(reportAlert);
079: }
080:
081: public void updateReportAlert(ReportAlert reportAlert)
082: throws ProviderException {
083: hibernateProvider.update(reportAlert);
084: }
085:
086: public void deleteReportAlert(ReportAlert reportAlert)
087: throws ProviderException {
088: Session session = hibernateProvider.openSession();
089: Transaction tx = null;
090:
091: try {
092: tx = session.beginTransaction();
093:
094: //delete alert
095: session.delete(reportAlert);
096:
097: //delete report log entries for alert
098: Iterator<?> iterator = session
099: .createQuery(
100: "from org.efs.openreports.objects.ReportLog reportLog where reportLog.alert.id = ? ")
101: .setInteger(0, reportAlert.getId().intValue())
102: .iterate();
103:
104: while (iterator.hasNext()) {
105: ReportLog reportLog = (ReportLog) iterator.next();
106: session.delete(reportLog);
107: }
108:
109: tx.commit();
110: } catch (HibernateException he) {
111: hibernateProvider.rollbackTransaction(tx);
112:
113: if (he.getCause() != null
114: && he.getCause().getMessage() != null
115: && he.getCause().getMessage().toUpperCase()
116: .indexOf("CONSTRAINT") > 0) {
117: throw new ProviderException(
118: LocalStrings.ERROR_ALERT_DELETION);
119: }
120:
121: log.error("deleteReportAlert", he);
122: throw new ProviderException(LocalStrings.ERROR_SERVERSIDE);
123: } finally {
124: hibernateProvider.closeSession(session);
125: }
126: }
127:
128: public ReportUserAlert executeAlert(ReportUserAlert userAlert,
129: boolean includeReportInLog) throws ProviderException {
130: if (userAlert == null)
131: return null;
132:
133: Connection conn = null;
134: PreparedStatement pStmt = null;
135: ResultSet rs = null;
136:
137: ReportLog alertLog = new ReportLog(userAlert.getUser(),
138: userAlert.getAlert(), new Date());
139: if (includeReportInLog)
140: alertLog.setReport(userAlert.getReport());
141:
142: try {
143: reportLogProvider.insertReportLog(alertLog);
144:
145: ReportDataSource dataSource = userAlert.getAlert()
146: .getDataSource();
147: conn = dataSourceProvider.getConnection(dataSource.getId());
148:
149: pStmt = conn.prepareStatement(userAlert.getAlert()
150: .getQuery());
151:
152: rs = pStmt.executeQuery();
153:
154: if (!rs.next()) {
155: userAlert.setCount(0);
156: }
157:
158: userAlert.setCount(rs.getInt(1));
159:
160: if (userAlert.isTriggered()) {
161: alertLog.setStatus(ReportLog.STATUS_TRIGGERED);
162: } else {
163: alertLog.setStatus(ReportLog.STATUS_NOT_TRIGGERED);
164: }
165:
166: alertLog.setMessage("Count: " + userAlert.getCount()
167: + " Condition: " + userAlert.getCondition());
168:
169: alertLog.setEndTime(new Date());
170: reportLogProvider.updateReportLog(alertLog);
171: } catch (Exception e) {
172: alertLog.setMessage(e.getMessage());
173: alertLog.setStatus(ReportLog.STATUS_FAILURE);
174: alertLog.setEndTime(new Date());
175:
176: reportLogProvider.updateReportLog(alertLog);
177:
178: throw new ProviderException(
179: LocalStrings.ERROR_ALERTQUERY_INVALID);
180: } finally {
181: try {
182: if (rs != null)
183: rs.close();
184: if (pStmt != null)
185: pStmt.close();
186: if (conn != null)
187: conn.close();
188: } catch (Exception c) {
189: log.error("Error closing");
190: }
191: }
192:
193: return userAlert;
194: }
195:
196: public void setDataSourceProvider(
197: DataSourceProvider dataSourceProvider) {
198: this .dataSourceProvider = dataSourceProvider;
199: }
200:
201: public void setReportLogProvider(ReportLogProvider reportLogProvider) {
202: this.reportLogProvider = reportLogProvider;
203: }
204: }
|