KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > providers > impl > AlertProviderImpl


1 /*
2  * Copyright (C) 2006 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.impl;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.log4j.Logger;
29 import org.efs.openreports.objects.ReportAlert;
30 import org.efs.openreports.objects.ReportDataSource;
31 import org.efs.openreports.objects.ReportLog;
32 import org.efs.openreports.objects.ReportUserAlert;
33 import org.efs.openreports.providers.AlertProvider;
34 import org.efs.openreports.providers.DataSourceProvider;
35 import org.efs.openreports.providers.DataSourceProviderAware;
36 import org.efs.openreports.providers.ProviderException;
37 import org.efs.openreports.providers.ReportLogProvider;
38 import org.efs.openreports.providers.ReportLogProviderAware;
39 import org.efs.openreports.providers.persistence.AlertPersistenceProvider;
40 import org.efs.openreports.util.LocalStrings;
41
42 import com.opensymphony.xwork.ActionContext;
43 import com.opensymphony.xwork.interceptor.component.ComponentManager;
44
45 public class AlertProviderImpl
46     implements AlertProvider, DataSourceProviderAware, ReportLogProviderAware
47 {
48     protected static Logger log =
49         Logger.getLogger(AlertProviderImpl.class.getName());
50
51     private AlertPersistenceProvider alertPersistenceProvider;
52     private DataSourceProvider dataSourceProvider;
53     private ReportLogProvider reportLogProvider;
54     
55     // constructor for Spring IOC
56
public AlertProviderImpl(DataSourceProvider dataSourceProvider, ReportLogProvider reportLogProvider) throws ProviderException
57     {
58         this.dataSourceProvider = dataSourceProvider;
59         this.reportLogProvider = reportLogProvider;
60         init();
61     }
62     
63     // constructor for WebWork IOC
64
public AlertProviderImpl() throws ProviderException
65     {
66         ComponentManager container =
67             (ComponentManager) ActionContext.getContext().get(
68                 "com.opensymphony.xwork.interceptor.component.ComponentManager");
69
70         container.initializeObject(this);
71         
72         init();
73     }
74     
75     protected void init() throws ProviderException
76     {
77         alertPersistenceProvider = new AlertPersistenceProvider();
78         log.info("Created");
79     }
80
81     public ReportAlert getReportAlert(Integer JavaDoc id) throws ProviderException
82     {
83         return alertPersistenceProvider.getReportAlert(id);
84     }
85
86     public List JavaDoc getReportAlerts() throws ProviderException
87     {
88         return alertPersistenceProvider.getReportAlerts();
89     }
90
91     public ReportAlert insertReportAlert(ReportAlert reportAlert)
92         throws ProviderException
93     {
94         return alertPersistenceProvider.insertReportAlert(reportAlert);
95     }
96
97     public void updateReportAlert(ReportAlert reportAlert)
98         throws ProviderException
99     {
100         alertPersistenceProvider.updateReportAlert(reportAlert);
101     }
102
103     public void deleteReportAlert(ReportAlert reportAlert)
104         throws ProviderException
105     {
106         alertPersistenceProvider.deleteReportAlert(reportAlert);
107     }
108     
109     public ReportUserAlert executeAlert(ReportUserAlert userAlert, boolean includeReportInLog) throws ProviderException
110     {
111         if (userAlert == null) return null;
112         
113         Connection JavaDoc conn = null;
114         PreparedStatement JavaDoc pStmt = null;
115         ResultSet JavaDoc rs = null;
116         
117         ReportLog alertLog = new ReportLog(userAlert.getUser(), userAlert.getAlert(), new Date JavaDoc());
118         if (includeReportInLog) alertLog.setReport(userAlert.getReport());
119         
120         try
121         {
122             reportLogProvider.insertReportLog(alertLog);
123             
124             ReportDataSource dataSource = userAlert.getAlert().getDataSource();
125             conn = dataSourceProvider.getConnection(dataSource.getId());
126             
127             pStmt = conn.prepareStatement(userAlert.getAlert().getQuery());
128
129             rs = pStmt.executeQuery();
130
131             if (!rs.next())
132             {
133                 userAlert.setCount(0);
134             }
135
136             userAlert.setCount(rs.getInt(1));
137             
138             if (userAlert.isTriggered())
139             {
140                 alertLog.setStatus(ReportLog.STATUS_TRIGGERED);
141             }
142             else
143             {
144                 alertLog.setStatus(ReportLog.STATUS_NOT_TRIGGERED);
145             }
146             
147             alertLog.setMessage("Count: " + userAlert.getCount() + " Condition: "
148                     + userAlert.getCondition());
149             
150             alertLog.setEndTime(new Date JavaDoc());
151             reportLogProvider.updateReportLog(alertLog);
152         }
153         catch (Exception JavaDoc e)
154         {
155             alertLog.setMessage(e.getMessage());
156             alertLog.setStatus(ReportLog.STATUS_FAILURE);
157             alertLog.setEndTime(new Date JavaDoc());
158
159             reportLogProvider.updateReportLog(alertLog);
160             
161             throw new ProviderException(LocalStrings
162                     .getString(LocalStrings.ERROR_ALERTQUERY_INVALID)
163                     + ": " + e.toString());
164         }
165         finally
166         {
167             try
168             {
169                 if (rs != null) rs.close();
170                 if (pStmt != null) pStmt.close();
171                 if (conn != null) conn.close();
172             }
173             catch (Exception JavaDoc c)
174             {
175                 log.error("Error closing");
176             }
177         }
178         
179         return userAlert;
180     }
181     
182     public void setDataSourceProvider(DataSourceProvider dataSourceProvider)
183     {
184         this.dataSourceProvider = dataSourceProvider;
185     }
186
187     public void setReportLogProvider(ReportLogProvider reportLogProvider)
188     {
189         this.reportLogProvider = reportLogProvider;
190     }
191 }
Popular Tags