| 2308 |
mtayler |
1 |
/*
|
|
|
2 |
* Created on 1/04/2005
|
|
|
3 |
*/
|
|
|
4 |
package DMS;
|
|
|
5 |
|
|
|
6 |
import java.sql.Connection;
|
|
|
7 |
import java.util.Vector;
|
|
|
8 |
import java.util.HashMap;
|
|
|
9 |
import java.util.Enumeration;
|
|
|
10 |
import java.sql.PreparedStatement;
|
|
|
11 |
import java.sql.SQLException;
|
|
|
12 |
import java.sql.ResultSet;
|
|
|
13 |
import java.text.DecimalFormat;
|
|
|
14 |
import java.text.SimpleDateFormat;
|
|
|
15 |
import java.util.Date;
|
|
|
16 |
import java.util.Calendar;
|
|
|
17 |
import java.util.GregorianCalendar;
|
|
|
18 |
import javax.servlet.http.HttpServletRequest;
|
|
|
19 |
import java.sql.Timestamp;
|
|
|
20 |
import java.util.TimeZone;
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* @author mtayler
|
|
|
25 |
*/
|
|
|
26 |
public class dbTemplateData {
|
|
|
27 |
|
|
|
28 |
private int FRecPos;
|
|
|
29 |
private Vector FRecords;
|
|
|
30 |
private Vector FFilteredRecords;
|
|
|
31 |
private templateItem FTemplate;
|
|
|
32 |
private HttpServletRequest FRequest;
|
|
|
33 |
private Calendar FLastLoadTime = null;
|
|
|
34 |
|
|
|
35 |
private static int LOAD_INTERVAL_MIN = 1; //interval that the data should be reloaded
|
|
|
36 |
|
|
|
37 |
public dbTemplateData(templateItem template, HttpServletRequest request) {
|
|
|
38 |
FRecords = new Vector();
|
|
|
39 |
FFilteredRecords = new Vector();
|
|
|
40 |
FTemplate = template;
|
|
|
41 |
FRequest = request;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
private String getParameter(String key) {
|
|
|
45 |
if (FRequest!=null) return FRequest.getParameter(key);
|
|
|
46 |
else return null;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
private String getData(String name, String datatype, String format, ResultSet rs) {
|
|
|
50 |
try {
|
|
|
51 |
if (datatype.toLowerCase().equals("string")||datatype.toLowerCase().equals("varchar")||datatype.toLowerCase().equals("char")) {
|
|
|
52 |
return rs.getString(name);
|
|
|
53 |
} else if (datatype.toLowerCase().equals("number")||datatype.toLowerCase().equals("int")||datatype.toLowerCase().equals("integer")||datatype.toLowerCase().equals("bigint")) {
|
|
|
54 |
double data = rs.getDouble(name);
|
|
|
55 |
DecimalFormat df = new DecimalFormat(format);
|
|
|
56 |
return df.format(data);
|
|
|
57 |
} else if (datatype.toLowerCase().equals("date")||datatype.toLowerCase().equals("datetime")) {
|
|
|
58 |
//need to add 8 hours to the date because the db stores the date as GMT+0 instead of GMT+8
|
|
|
59 |
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT+08:00"));
|
|
|
60 |
Timestamp data = rs.getTimestamp(name, cal);
|
|
|
61 |
|
|
|
62 |
SimpleDateFormat df = new SimpleDateFormat(format);
|
|
|
63 |
return df.format(data);
|
|
|
64 |
}
|
|
|
65 |
} catch (SQLException e) {
|
|
|
66 |
e.printStackTrace();
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
return "";
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private boolean isFilteredRecord(HashMap map) {
|
|
|
73 |
columnItem [] cols = FTemplate.getColumns().getColumns();
|
|
|
74 |
|
|
|
75 |
for (int i = 0; i<cols.length;i++) {
|
|
|
76 |
|
|
|
77 |
String dataValue = DMSUtils.NVL((String)map.get(cols[i].getName()));
|
|
|
78 |
|
|
|
79 |
if (cols[i].getDataType().equals("date")||cols[i].getDataType().equals("datetime")) {
|
|
|
80 |
String nameToDay="daterangefield" + cols[i].getName() + "ToDay";
|
|
|
81 |
String nameToMonth="daterangefield" + cols[i].getName() + "ToMonth";
|
|
|
82 |
String nameToYear="daterangefield" + cols[i].getName() + "ToYear";
|
|
|
83 |
|
|
|
84 |
String nameFromDay="daterangefield" + cols[i].getName() + "FromDay";
|
|
|
85 |
String nameFromMonth="daterangefield" + cols[i].getName() + "FromMonth";
|
|
|
86 |
String nameFromYear="daterangefield" + cols[i].getName() + "FromYear";
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
String defaultValueToDay = DMSUtils.NVL(getParameter(nameToDay));
|
|
|
90 |
String defaultValueToMonth = DMSUtils.NVL(getParameter(nameToMonth));
|
|
|
91 |
String defaultValueToYear = DMSUtils.NVL(getParameter(nameToYear));
|
|
|
92 |
|
|
|
93 |
String defaultValueFromDay = DMSUtils.NVL(getParameter(nameFromDay));
|
|
|
94 |
String defaultValueFromMonth = DMSUtils.NVL(getParameter(nameFromMonth));
|
|
|
95 |
String defaultValueFromYear = DMSUtils.NVL(getParameter(nameFromYear));
|
|
|
96 |
|
|
|
97 |
Timestamp toDate = DMSUtils.StringToTimestampEndOfDay(defaultValueToYear, defaultValueToMonth, defaultValueToDay);
|
|
|
98 |
Timestamp fromDate = DMSUtils.StringToTimestamp(defaultValueFromYear, defaultValueFromMonth, defaultValueFromDay);
|
|
|
99 |
Timestamp dataValueDate = DMSUtils.StringToTimestamp(dataValue, cols[i].getFormat());
|
|
|
100 |
|
|
|
101 |
if (dataValueDate!=null && ((fromDate!=null && fromDate.compareTo(dataValueDate) > 0) || (toDate!=null && toDate.compareTo(dataValueDate)<0))) {
|
|
|
102 |
return false;
|
|
|
103 |
}
|
|
|
104 |
} else if (cols[i].getDataType().equals("number")||cols[i].getDataType().equals("integer")||cols[i].getDataType().equals("int")||cols[i].getDataType().equals("bigint")) {
|
|
|
105 |
String nameFrom="numberrangefield" + cols[i].getName() + "From";
|
|
|
106 |
String nameTo="numberrangefield" + cols[i].getName() + "To";
|
|
|
107 |
|
|
|
108 |
String defaultValueFrom = DMSUtils.NVL(getParameter(nameFrom));
|
|
|
109 |
String defaultValueTo = DMSUtils.NVL(getParameter(nameTo));
|
|
|
110 |
|
|
|
111 |
Integer fromInt = DMSUtils.StringToInteger(defaultValueFrom);
|
|
|
112 |
Integer toInt = DMSUtils.StringToInteger(defaultValueTo);
|
|
|
113 |
Integer dataValueInt = DMSUtils.StringToInteger(dataValue);
|
|
|
114 |
|
|
|
115 |
if (dataValueInt!=null && ((fromInt!=null && fromInt.compareTo(dataValueInt) > 0) || (toInt!=null && toInt.compareTo(dataValueInt)<0))) {
|
|
|
116 |
return false;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
} else {
|
|
|
120 |
String filterValue = DMSUtils.NVL(getParameter("filterfield"+cols[i].getName()));
|
|
|
121 |
if (!filterValue.equals("") && !filterValue.equals("ALL") && !filterValue.equals(dataValue)) {
|
|
|
122 |
return false;
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
}
|
|
|
127 |
return true;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public String getSortField() {
|
|
|
131 |
Enumeration e = null;
|
|
|
132 |
|
|
|
133 |
if (FRequest!=null) e = FRequest.getParameterNames();
|
|
|
134 |
|
|
|
135 |
while (e!=null && e.hasMoreElements()) {
|
|
|
136 |
String name = DMSUtils.NVL((String) e.nextElement());
|
|
|
137 |
if (name.length()>=9 && name.toLowerCase().substring(0,9).equals("sortfield")) {
|
|
|
138 |
String value = DMSUtils.NVL(getParameter(name));
|
|
|
139 |
if (value.equals("asc") || value.equals("desc"))
|
|
|
140 |
return name.toLowerCase();
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
return "";
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
private void sortValue(String sortField, boolean isAscending, HashMap data, int startIndex, int endIndex) {
|
|
|
147 |
String firstRecordValue = getFieldValue(sortField, startIndex);
|
|
|
148 |
String lastRecordValue = getFieldValue(sortField, endIndex);
|
|
|
149 |
String newValue = (String)data.get(sortField);
|
|
|
150 |
|
|
|
151 |
columnItem item = FTemplate.getColumnByName(sortField);
|
|
|
152 |
String datatype = item.getDataType();
|
|
|
153 |
String format = item.getFormat();
|
|
|
154 |
|
|
|
155 |
if (DMSUtils.compareToValue(firstRecordValue, newValue, datatype, format, isAscending)>0) { //if newValue is less than firstValue
|
|
|
156 |
FFilteredRecords.insertElementAt(data,startIndex); //insert before the first record
|
|
|
157 |
} else if (DMSUtils.compareToValue(lastRecordValue, newValue, datatype, format, isAscending)<=0) { //if newValue is greater than or equal to the lastValue
|
|
|
158 |
FFilteredRecords.insertElementAt(data,endIndex+1); //insert after the last record
|
|
|
159 |
} else if (startIndex == endIndex-1) { //if there are no more records in between
|
|
|
160 |
FFilteredRecords.insertElementAt(data,startIndex+1); //insert after the first record
|
|
|
161 |
} else {
|
|
|
162 |
//recursive sort
|
|
|
163 |
int midIndex = (startIndex + endIndex) / 2;
|
|
|
164 |
String midRecordValue = getFieldValue(sortField, midIndex);
|
|
|
165 |
|
|
|
166 |
if (DMSUtils.compareToValue(midRecordValue, newValue, datatype, format, isAscending)>0) { //if newValue is less than midRecordValue
|
|
|
167 |
sortValue(sortField, isAscending, data, startIndex+1, midIndex);
|
|
|
168 |
} else {
|
|
|
169 |
sortValue(sortField, isAscending, data, midIndex, endIndex-1);
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
private void addFilteredRecord(HashMap data) {
|
|
|
175 |
//get the field to sort on
|
|
|
176 |
String sortFieldParam = getSortField();
|
|
|
177 |
boolean isAscending = DMSUtils.NVL(getParameter(sortFieldParam)).toLowerCase().equals("asc");
|
|
|
178 |
|
|
|
179 |
if (FFilteredRecords.size()==0 || sortFieldParam.equals("")) { //if empty or no sorting is performed append the record
|
|
|
180 |
FFilteredRecords.add(data);
|
|
|
181 |
} else {
|
|
|
182 |
String sortField = sortFieldParam.substring(9);
|
|
|
183 |
|
|
|
184 |
sortValue(sortField, isAscending, data, 0, FFilteredRecords.size()-1);
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
private boolean hasTimeExceeded() {
|
|
|
189 |
if (FLastLoadTime!=null) {
|
|
|
190 |
long lastTimeMin = FLastLoadTime.getTimeInMillis()/60000;
|
|
|
191 |
|
|
|
192 |
Calendar currentTime = new GregorianCalendar();
|
|
|
193 |
currentTime.setTime(new Date());
|
|
|
194 |
long newTimeMin = currentTime.getTimeInMillis()/60000;
|
|
|
195 |
|
|
|
196 |
return (newTimeMin-lastTimeMin)>LOAD_INTERVAL_MIN;
|
|
|
197 |
}
|
|
|
198 |
return true;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
public void load(HttpServletRequest request) {
|
|
|
202 |
FRequest = request; //update request object
|
|
|
203 |
|
|
|
204 |
FFilteredRecords.clear(); //clear old records
|
|
|
205 |
FRecPos = -1;
|
|
|
206 |
|
|
|
207 |
if (!hasTimeExceeded()) {
|
|
|
208 |
for (int i = 0; i < FRecords.size(); i++) {
|
|
|
209 |
HashMap data = (HashMap)FRecords.get(i);
|
|
|
210 |
if (isFilteredRecord(data)) addFilteredRecord(data);
|
|
|
211 |
}
|
|
|
212 |
} else {
|
|
|
213 |
//reset time
|
|
|
214 |
FLastLoadTime = new GregorianCalendar();
|
|
|
215 |
FLastLoadTime.setTime(new Date());
|
|
|
216 |
|
|
|
217 |
FRecords.clear(); //clear old records
|
|
|
218 |
|
|
|
219 |
Connection conn = dbConnector.getConnection(FTemplate.getDSName());
|
|
|
220 |
try {
|
|
|
221 |
try {
|
|
|
222 |
String SQL = FTemplate.getSQL();
|
|
|
223 |
PreparedStatement pstmnt = conn.prepareStatement(SQL);
|
|
|
224 |
|
|
|
225 |
ResultSet rs = pstmnt.executeQuery();
|
|
|
226 |
try {
|
|
|
227 |
while (rs.next()) {
|
|
|
228 |
HashMap data = new HashMap();
|
|
|
229 |
|
|
|
230 |
columnItem [] cols = FTemplate.getColumns().getColumns();
|
|
|
231 |
for (int i = 0; i< cols.length; i++) {
|
|
|
232 |
String sValue = getData(cols[i].getName(),cols[i].getDataType(), cols[i].getFormat(), rs);
|
|
|
233 |
data.put(cols[i].getName(), sValue);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
FRecords.add(data);
|
|
|
237 |
if (isFilteredRecord(data)) addFilteredRecord(data);
|
|
|
238 |
|
|
|
239 |
}
|
|
|
240 |
} finally {
|
|
|
241 |
rs.close();
|
|
|
242 |
}
|
|
|
243 |
} finally {
|
|
|
244 |
conn.close();
|
|
|
245 |
}
|
|
|
246 |
} catch (SQLException e) {
|
|
|
247 |
e.printStackTrace();
|
|
|
248 |
}
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
public boolean next() {
|
|
|
253 |
FRecPos++;
|
|
|
254 |
return FRecPos<FFilteredRecords.size();
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
public void first() {
|
|
|
258 |
if (FFilteredRecords.size()>0) FRecPos=0;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
public void last() {
|
|
|
262 |
if (FFilteredRecords.size()>0) FRecPos=FFilteredRecords.size()-1;
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
public String getFieldValue(String fieldname, int index) {
|
|
|
266 |
String fieldvalue = "";
|
|
|
267 |
if (index>-1 && index<FFilteredRecords.size()) {
|
|
|
268 |
HashMap map = (HashMap)FFilteredRecords.get(index);
|
|
|
269 |
fieldvalue = (String)map.get(fieldname);
|
|
|
270 |
}
|
|
|
271 |
return fieldvalue;
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
public String getFieldValue(String fieldname) {
|
|
|
275 |
return getFieldValue(fieldname, FRecPos);
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
public HashMap getRecord(int index) {
|
|
|
279 |
if (index>-1 && index<FFilteredRecords.size()) {
|
|
|
280 |
return (HashMap)FFilteredRecords.get(index);
|
|
|
281 |
}
|
|
|
282 |
return null;
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
public HashMap getRecord() {
|
|
|
286 |
return getRecord(FRecPos);
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
public String[] getDistinctValues(String fieldname) {
|
|
|
290 |
Vector v = new Vector();
|
|
|
291 |
|
|
|
292 |
for (int i=0;i<FRecords.size();i++) {
|
|
|
293 |
HashMap map = (HashMap)FRecords.get(i);
|
|
|
294 |
String data = (String)map.get(fieldname);
|
|
|
295 |
if (!v.contains(data)) v.add(data);
|
|
|
296 |
}
|
|
|
297 |
return (String[])v.toArray(new String[0]);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
public int getTotalRecordSize() {
|
|
|
301 |
return FRecords.size();
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
public int getFilteredRecordSize() {
|
|
|
305 |
return FFilteredRecords.size();
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
* @return Returns the fTemplate.
|
|
|
310 |
*/
|
|
|
311 |
public templateItem getTemplate() {
|
|
|
312 |
return FTemplate;
|
|
|
313 |
}
|
|
|
314 |
}
|