KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > plugin > OPMUtil


1 /*
2  * OPMUtil.java
3  *
4  * Created on March 6, 2003, 7:17 AM
5  */

6
7 package com.quikj.application.web.talk.plugin;
8
9 import java.util.*;
10
11 /**
12  *
13  * @author bhm
14  *
15  * Use one instance of this class to handle any number/type of OPMs for a given key in a given
16  * DB table. Key can be one or more strings, associated with one or more key columns in a DB record.
17  * Tell this class one time what the table name and key column values are.
18  * Each DB record will look like:
19  * YourKeyColValue1 (YourKeyColValueN) time_stamp opm_name opm_value
20  *
21  * Call collectOPM() to accumulate an individual OPM.
22  * When you call averageOPMs(), this class averages the individual OPMs collected since the last
23  * clearOPMs(), per opm_name. Any previous average is discarded.
24  * When you call storeOPMs(), it generates an SQL insert statement for either the averages (if
25  * you call averageOPMs() first) or the individual OPMs (averageOPMs() not called), and
26  * stores the data in the database.
27  *
28  * Call clearOPMs() to clear out the individual OPMs and the averages.
29  *
30  */

31 public class OPMUtil
32 {
33     
34     /** Creates a new instance of OPMUtil */
35     public OPMUtil()
36     {
37     }
38     
39     public void collectOPM(String JavaDoc opm_name, int opm_value)
40     {
41         collectOPM(new Date(), opm_name, opm_value);
42     }
43     
44     public void collectOPM(Date time_stamp, String JavaDoc opm_name, int opm_value)
45     {
46         ArrayList opms = (ArrayList) opmList.get(opm_name);
47         if (opms != null)
48         {
49             opms.add(new OPMValue(time_stamp, opm_value));
50         }
51         else
52         {
53             opms = new ArrayList();
54             opms.add(new OPMValue(time_stamp, opm_value));
55             opmList.put(new String JavaDoc(opm_name), opms);
56         }
57     }
58     
59     public void averageOPMs()
60     {
61         averageOPMs(new Date());
62     }
63     
64     public void averageOPMs(Date time_stamp)
65     {
66         lastAverages.clear();
67         
68         Iterator e = opmList.keySet().iterator();
69         while (e.hasNext())
70         {
71             String JavaDoc opm_name = (String JavaDoc) e.next();
72             ArrayList opms = (ArrayList) opmList.get(opm_name);
73             
74             long sum = 0;
75             int size = opms.size();
76             for (int i = 0; i < size; i++)
77             {
78                 sum += ((OPMValue) opms.get(i)).getOpmValue();
79             }
80             //System.out.print("opm = " + opm_name + " ");
81
//System.out.print("sum = " + sum + " ");
82
//System.out.println("size = " + size);
83

84             if (size > 0)
85             {
86                 int average = (int)(sum/size);
87                 //System.out.println("average = " + average);
88
if ((((sum % size) >= (size/2)) && (size % 2 == 0)) ||
89                 (((sum % size) > (size/2)) && (size % 2 != 0)))
90                 {
91                     average++;
92                     //System.out.println("bumping average");
93
}
94                 
95                 lastAverages.put(opm_name, new OPMValue(time_stamp, average));
96             }
97         }
98     }
99     
100     public boolean storeOPMs()
101     {
102         if (OPMHandler.getInstance() == null)
103         {
104             return true;
105         }
106         
107         // use 1 string - INSERT [INTO] tbl_name VALUES ((expression | DEFAULT),...),(...),...
108

109         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("insert into " + tableName + " values ");
110         
111         if (lastAverages.size() > 0) // store the averages only and get out
112
{
113             Iterator e = lastAverages.keySet().iterator();
114             while (e.hasNext())
115             {
116                 String JavaDoc opm_name = (String JavaDoc) e.next();
117                 OPMValue opm = (OPMValue) lastAverages.get(opm_name);
118                 
119                 sql.append("('" + keyColumnsSql + "','"
120                 + getDateString(opm.getTimestamp()) + "','"
121                 + opm_name + "',"
122                 + new Integer JavaDoc(opm.getOpmValue()).toString()
123                 + "),");
124             }
125             sql.setLength(sql.length() - 1);
126             
127             return OPMHandler.getInstance().executeSQL(sql.toString());
128         }
129         
130         // store the individual OPMs
131
Iterator e = opmList.keySet().iterator();
132         while (e.hasNext())
133         {
134             String JavaDoc opm_name = (String JavaDoc) e.next();
135             ArrayList opms = (ArrayList) opmList.get(opm_name);
136             
137             int size = opms.size();
138             for (int i = 0; i < size; i++)
139             {
140                 OPMValue opm = (OPMValue) opms.get(i);
141                 
142                 sql.append("('" + keyColumnsSql + "','"
143                 + getDateString(opm.getTimestamp()) + "','"
144                 + opm_name + "',"
145                 + new Integer JavaDoc(opm.getOpmValue()).toString()
146                 + "),");
147             }
148         }
149         sql.setLength(sql.length() - 1);
150         
151         return OPMHandler.getInstance().executeSQL(sql.toString());
152     }
153     
154     public void clearOPMs()
155     {
156         opmList.clear();
157         lastAverages.clear();
158     }
159     
160     public int getNumCollectedOPMs(String JavaDoc opm_name)
161     {
162         ArrayList opms = (ArrayList) opmList.get(opm_name);
163         if (opms != null)
164         {
165             return opms.size();
166         }
167         
168         return 0;
169     }
170     
171     private String JavaDoc getDateString(java.util.Date JavaDoc timestamp)
172     {
173         Calendar cal = Calendar.getInstance();
174         cal.setTime(timestamp);
175         
176         String JavaDoc date_string = cal.get(Calendar.YEAR) + "-"
177         + (cal.get(Calendar.MONTH) + 1) + "-"
178         + cal.get(Calendar.DAY_OF_MONTH) + " "
179         + cal.get(Calendar.HOUR_OF_DAY) + ":"
180         + cal.get(Calendar.MINUTE) + ":"
181         + cal.get(Calendar.SECOND);
182         
183         return date_string;
184     }
185     
186     public void setTableName(java.lang.String JavaDoc tableName)
187     {
188         this.tableName = tableName;
189     }
190     
191     public void setKeyColumnValues(java.lang.String JavaDoc[] keyColumnValues)
192     {
193         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
194         for (int i = 0; i < keyColumnValues.length; i++)
195         {
196             buf.append(keyColumnValues[i]);
197             if (i < (keyColumnValues.length - 1))
198             {
199                 buf.append(',');
200             }
201         }
202         
203         keyColumnsSql = buf.toString();
204     }
205     
206     public void setKeyColumnValue(String JavaDoc val) // if you have just 1 key column
207
{
208         keyColumnsSql = new String JavaDoc(val);
209     }
210     
211     private class OPMValue
212     {
213         private Date timestamp;
214         private int opmValue;
215         
216         public OPMValue(Date ts, int val)
217         {
218             timestamp = ts;
219             opmValue = val;
220         }
221         
222         public OPMValue()
223         {
224         }
225         
226         /** Getter for property opmValue.
227          * @return Value of property opmValue.
228          *
229          */

230         public int getOpmValue()
231         {
232             return opmValue;
233         }
234         
235         /** Setter for property opmValue.
236          * @param opmValue New value of property opmValue.
237          *
238          */

239         public void setOpmValue(int opmValue)
240         {
241             this.opmValue = opmValue;
242         }
243         
244         /** Getter for property timestamp.
245          * @return Value of property timestamp.
246          *
247          */

248         public java.util.Date JavaDoc getTimestamp()
249         {
250             return timestamp;
251         }
252         
253         /** Setter for property timestamp.
254          * @param timestamp New value of property timestamp.
255          *
256          */

257         public void setTimestamp(java.util.Date JavaDoc timestamp)
258         {
259             this.timestamp = timestamp;
260         }
261         
262     }
263     
264     private HashMap opmList = new HashMap(); // <key=opm_name, value=ArrayList of OPMValue>
265
private HashMap lastAverages = new HashMap(); // <key=opm_name, value=OPMValue>
266

267     private String JavaDoc tableName;
268     private String JavaDoc keyColumnsSql;
269     
270 }
271
Popular Tags