KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > communicator > admin > model > LogsTable


1 /*
2  * LogsTable.java
3  *
4  * Created on April 29, 2003, 2:36 PM
5  */

6
7 package com.quikj.application.communicator.admin.model;
8
9 import java.sql.*;
10 import java.util.*;
11
12 /**
13  *
14  * @author Administrator
15  */

16 public class LogsTable
17 {
18     
19     /** Holds value of property connection. */
20     private Connection connection;
21     
22     /** Holds value of property errorMessage. */
23     private String JavaDoc errorMessage;
24     
25     private String JavaDoc database;
26     
27     /** Creates a new instance of LogsTable */
28     public LogsTable(String JavaDoc database)
29     {
30         this.database = database;
31     }
32     
33     /** Getter for property connection.
34      * @return Value of property connection.
35      *
36      */

37     public Connection getConnection()
38     {
39         return this.connection;
40     }
41     
42     /** Setter for property connection.
43      * @param connection New value of property connection.
44      *
45      */

46     public void setConnection(Connection connection)
47     {
48         this.connection = connection;
49     }
50     
51     /** Getter for property errorMessage.
52      * @return Value of property errorMessage.
53      *
54      */

55     public String JavaDoc getErrorMessage()
56     {
57         return this.errorMessage;
58     }
59     
60     /** Setter for property errorMessage.
61      * @param errorMessage New value of property errorMessage.
62      *
63      */

64     public void setErrorMessage(String JavaDoc errorMessage)
65     {
66         this.errorMessage = errorMessage;
67     }
68     
69     // if start_time == null, search from the start of the database
70
// if end_time_time == null, search till the end
71
// if severity == -1, all severity
72
// if process_name == null, all processes
73
// if message == null, all messages
74
public ArrayList search(Timestamp start_time,
75     Timestamp end_time,
76     int[] severity_levels,
77     Object JavaDoc[] process_names,
78     String JavaDoc message)
79     {
80         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("select * from log_tbl where ");
81         
82         boolean first_item = true;
83         
84         try
85         {
86             // if a start time is specified
87
if (start_time != null)
88             {
89                 sql.append("log_date >= ?");
90                 first_item = false;
91             }
92             
93             // if end time is specified
94
if (end_time != null)
95             {
96                 if (first_item == false)
97                 {
98                     sql.append(" and ");
99                     
100                 }
101                 else
102                 {
103                     first_item = false;
104                 }
105                 
106                 sql.append("log_date <= ?");
107             }
108             
109             if (severity_levels != null)
110             {
111                 if (severity_levels.length > 0)
112                 {
113                     if (first_item == false)
114                     {
115                         sql.append(" and ");
116                     }
117                     else
118                     {
119                         first_item = false;
120                     }
121                 }
122                 
123                 for (int i = 0; i < severity_levels.length; i++)
124                 {
125                     if (i == 0)
126                     {
127                         sql.append("(");
128                     }
129                     else
130                     {
131                         sql.append(" or ");
132                     }
133                     
134                     sql.append("severity = ?");
135                     
136                     if (i == (severity_levels.length - 1))
137                     {
138                         sql.append(") ");
139                     }
140                 }
141             }
142             
143             if (process_names != null)
144             {
145                 if (process_names.length > 0)
146                 {
147                     if (first_item == false)
148                     {
149                         sql.append(" and ");
150                     }
151                     else
152                     {
153                         first_item = false;
154                     }
155                 }
156                 
157                 for (int i = 0; i < process_names.length; i++)
158                 {
159                     if (i == 0)
160                     {
161                         sql.append("(");
162                     }
163                     else
164                     {
165                         sql.append(" or ");
166                     }
167                     
168                     sql.append("process_name = ?");
169                     
170                     if (i == (process_names.length - 1))
171                     {
172                         sql.append(") ");
173                     }
174                 }
175             }
176             
177             
178             if (message != null)
179             {
180                 if (message.length() > 0)
181                 {
182                     if (first_item == false)
183                     {
184                         sql.append(" and ");
185                     }
186                     else
187                     {
188                         first_item = false;
189                     }
190                     
191                     sql.append("message like ?");
192                 }
193             }
194             
195             sql.append(" order by log_date");
196  
197             
198             PreparedStatement stmt = connection.prepareStatement(sql.toString());
199             
200             int index = 1;
201             // if a start time is specified
202
if (start_time != null)
203             {
204                 stmt.setTimestamp(index++, start_time);
205             }
206             
207             // if end time is specified
208
if (end_time != null)
209             {
210                 stmt.setTimestamp(index++, end_time);
211             }
212             
213             if (severity_levels != null)
214             {
215                 for (int i = 0; i < severity_levels.length; i++)
216                 {
217                     stmt.setInt(index++, severity_levels[i]);
218                 }
219             }
220             
221             if (process_names != null)
222             {
223                 for (int i = 0; i < process_names.length; i++)
224                 {
225                     stmt.setString(index++, process_names[i].toString());
226                 }
227             }
228             
229             if (message != null)
230             {
231                 if (message.length() > 0)
232                 {
233                     stmt.setString(index++, message);
234                 }
235             }
236             
237             
238             Statement ustmt = connection.createStatement();
239             ustmt.executeUpdate("use " + database);
240             
241             ResultSet rs = stmt.executeQuery();
242             
243             ArrayList list = new ArrayList();
244             while (rs.next() == true)
245             {
246                 LogElement ele = new LogElement();
247                 ele.setDate(rs.getTimestamp(1));
248                 ele.setSeverity(rs.getInt(2));
249                 ele.setHostName(rs.getString(3));
250                 ele.setProcessName(rs.getString(4));
251                 ele.setProcessInstance(rs.getString(5));
252                 ele.setMessage(rs.getString(6));
253                 
254                 list.add(ele);
255             }
256             
257             return list;
258         }
259         catch (SQLException ex)
260         {
261             errorMessage = ex.getMessage();
262             return null;
263         }
264     }
265     
266     public ArrayList getUniqueProcessNames()
267     {
268         String JavaDoc sql = "select distinct(process_name) from log_tbl";
269         
270         try
271         {
272             ArrayList list = new ArrayList();
273             
274             Statement stmt = connection.createStatement();
275             stmt.executeUpdate("use " + database);
276             
277             ResultSet rs = stmt.executeQuery(sql);
278             while (rs.next() == true)
279             {
280                 list.add(rs.getString(1));
281             }
282             
283             return list;
284         }
285         catch (SQLException ex)
286         {
287             errorMessage = ex.getMessage();
288             return null;
289         }
290     }
291     
292     public int delete(Timestamp date)
293     {
294         try
295         {
296             PreparedStatement cmd = connection.prepareStatement("delete from "
297             + "log_tbl"
298             + " where "
299             + "log_date"
300             + " < ?");
301             
302             cmd.setTimestamp(1, date);
303             
304             Statement stmt = connection.createStatement();
305             stmt.executeUpdate("use " + database);
306             
307             int count = cmd.executeUpdate();
308             if (count == 0)
309             {
310                 errorMessage = null;
311             }
312             
313             return count;
314         }
315         catch (SQLException ex)
316         {
317             errorMessage = "SQLException: " + ex.getMessage();
318         }
319         
320         return 0;
321         
322     } // delete
323

324 } // LogsTable
325
Popular Tags