| 2308 |
mtayler |
1 |
/*
|
|
|
2 |
* Created on 31/03/2005
|
|
|
3 |
*/
|
|
|
4 |
package DMS;
|
|
|
5 |
|
|
|
6 |
import java.text.SimpleDateFormat;
|
|
|
7 |
import java.util.Vector;
|
|
|
8 |
import java.net.URLEncoder;
|
|
|
9 |
import java.io.InputStream;
|
|
|
10 |
import java.io.File;
|
|
|
11 |
import java.io.FileWriter;
|
|
|
12 |
import java.sql.Timestamp;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* @author mtayler
|
|
|
16 |
*/
|
|
|
17 |
public class DMSUtils {
|
|
|
18 |
|
|
|
19 |
//log errors for debugging
|
|
|
20 |
public static void log(String s) {
|
|
|
21 |
File log=null;
|
|
|
22 |
FileWriter fw=null;
|
|
|
23 |
|
|
|
24 |
if (log==null) {
|
|
|
25 |
log = new File ("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\logs\\log.txt");
|
|
|
26 |
try {
|
|
|
27 |
if (!log.exists()) log.createNewFile();
|
|
|
28 |
fw = new FileWriter(log, true);
|
|
|
29 |
} catch (Exception e){e.printStackTrace();}
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
try {
|
|
|
33 |
fw.write(s);
|
|
|
34 |
fw.write(13);
|
|
|
35 |
fw.write(10);
|
|
|
36 |
fw.flush();
|
|
|
37 |
fw.close();
|
|
|
38 |
} catch (Exception e){e.printStackTrace();}
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public static Object NVL(Object obj, Object defValue) {
|
|
|
42 |
if (obj==null) return defValue;
|
|
|
43 |
else return obj;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public static String NVL(String obj, String defValue) {
|
|
|
47 |
return (String)NVL((Object)obj,(Object)defValue);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public static String NVL(String obj) {
|
|
|
51 |
return (String)NVL((Object)obj,(Object)new String(""));
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public static comboDataItem[] genComboDataItemArray(String[] values, String[] descrips) {
|
|
|
55 |
Vector v = new Vector();
|
|
|
56 |
|
|
|
57 |
for (int i=0;i<values.length;i++) {
|
|
|
58 |
comboDataItem di = new comboDataItem(values[i],descrips[i]);
|
|
|
59 |
v.add(di);
|
|
|
60 |
}
|
|
|
61 |
return (comboDataItem[])v.toArray(new comboDataItem[0]);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
//generate a properly encoded URL string
|
|
|
66 |
public static String cleanURLStr(String value) {
|
|
|
67 |
String ret = null;
|
|
|
68 |
try {
|
|
|
69 |
ret = URLEncoder.encode(value, "UTF8");
|
|
|
70 |
} catch (Exception e) {
|
|
|
71 |
e.printStackTrace();
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
return ret;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
//generate a properly encoded HTML string
|
|
|
78 |
public static String cleanHTMLStr(String value, boolean showlinebreak) {
|
|
|
79 |
StringBuffer sbClean = new StringBuffer("");
|
|
|
80 |
|
|
|
81 |
if (value == null || value.trim().equals("")) sbClean.append(" ");
|
|
|
82 |
|
|
|
83 |
if (value != null) {
|
|
|
84 |
for (int i=0; i<value.length(); i++) {
|
|
|
85 |
switch (value.charAt(i)) {
|
|
|
86 |
case '<': sbClean.append("<"); break;
|
|
|
87 |
case '>': sbClean.append(">"); break;
|
|
|
88 |
case '\"': sbClean.append("""); break;
|
|
|
89 |
case '\'': sbClean.append("'"); break;
|
|
|
90 |
case '&': sbClean.append("&"); break;
|
|
|
91 |
case '\r': break;
|
|
|
92 |
case '\n':
|
|
|
93 |
if (showlinebreak)
|
|
|
94 |
sbClean.append("<br>\n");
|
|
|
95 |
else
|
|
|
96 |
sbClean.append('\r');
|
|
|
97 |
default:
|
|
|
98 |
int c = (int)value.charAt(i);
|
|
|
99 |
if (c<32 && c>126) sbClean.append("&#" + String.valueOf(c) + ";");
|
|
|
100 |
else sbClean.append(value.charAt(i));
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
return sbClean.toString();
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
//generate a properly encoded HTML string
|
|
|
108 |
public static String cleanHTMLStr(String value){
|
|
|
109 |
return cleanHTMLStr(value, false);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
//generate a properly encoded Javascript string
|
|
|
113 |
public static String cleanJStr(String value) {
|
|
|
114 |
String ret = value;
|
|
|
115 |
try {
|
|
|
116 |
ret = DMSUtils.StrReplace(ret, "\\\\", "\\", true);
|
|
|
117 |
ret = DMSUtils.StrReplace(ret, "\\\"", "\"", true);
|
|
|
118 |
ret = DMSUtils.StrReplace(ret, "\\'", "'", true);
|
|
|
119 |
} catch (Exception e) {
|
|
|
120 |
e.printStackTrace();
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
return ret;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
public static InputStream getResourceAsStream(String aFileName) {
|
|
|
128 |
//return ClassLoader.getSystemResourceAsStream(aFileName);
|
|
|
129 |
DMSUtils c = new DMSUtils();
|
|
|
130 |
return c.getClass().getClassLoader().getResourceAsStream(aFileName);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
public static String StrReplace(String aStr, String aNew, String aOld,boolean aRepAll){
|
|
|
134 |
int iStart = aStr.indexOf(aOld);
|
|
|
135 |
if (iStart==-1){
|
|
|
136 |
return aStr;
|
|
|
137 |
}
|
|
|
138 |
if (aRepAll){
|
|
|
139 |
return aStr.substring(0,iStart)+aNew+StrReplace(aStr.substring(iStart+aOld.length()),aNew,aOld,aRepAll);
|
|
|
140 |
}
|
|
|
141 |
else {
|
|
|
142 |
return aStr.substring(0,iStart)+aNew+aStr.substring(iStart+aOld.length());
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
public static String replaceParams(String URL, dbTemplateData templateData) {
|
|
|
147 |
int startIndex;
|
|
|
148 |
int endIndex;
|
|
|
149 |
String fieldName, data, tempURL;
|
|
|
150 |
|
|
|
151 |
tempURL = URL;
|
|
|
152 |
startIndex = 0;
|
|
|
153 |
while (startIndex != -1) {
|
|
|
154 |
startIndex = tempURL.indexOf("{");
|
|
|
155 |
endIndex = tempURL.indexOf("}");
|
|
|
156 |
if (startIndex != -1) {
|
|
|
157 |
fieldName = tempURL.substring(startIndex+1,endIndex).trim();
|
|
|
158 |
data=DMSUtils.NVL(templateData.getFieldValue(fieldName));
|
|
|
159 |
tempURL=tempURL.substring(0,startIndex) + DMSUtils.cleanURLStr(data) + tempURL.substring(endIndex+1,tempURL.length());
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
return tempURL;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public static int compareToValue(String value1, String value2, boolean isAscending) {
|
|
|
167 |
return compareToValue(value1, value2, "string", "", isAscending);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
public static int compareToValue(String value1, String value2, String datatype, String format, boolean isAscending) {
|
|
|
171 |
int sign = 0;
|
|
|
172 |
if (isAscending) sign = 1;
|
|
|
173 |
else sign = -1;
|
|
|
174 |
|
|
|
175 |
if (datatype.equals("date")||datatype.equals("datetime")) {
|
|
|
176 |
Timestamp tvalue1 = StringToTimestamp(value1, format);
|
|
|
177 |
Timestamp tvalue2 = StringToTimestamp(value2, format);
|
|
|
178 |
|
|
|
179 |
if (tvalue1==null && tvalue2!=null) return -1*sign;
|
|
|
180 |
else if (tvalue1==null && tvalue2==null) return 0;
|
|
|
181 |
else if (tvalue1!=null && tvalue2==null) return 1*sign;
|
|
|
182 |
else return tvalue1.compareTo(tvalue2)*sign;
|
|
|
183 |
|
|
|
184 |
} else if (datatype.equals("number")||datatype.equals("integer")||datatype.equals("int")||datatype.equals("bigint")) {
|
|
|
185 |
Integer ivalue1 = DMSUtils.StringToInteger(value1);
|
|
|
186 |
Integer ivalue2 = DMSUtils.StringToInteger(value2);
|
|
|
187 |
|
|
|
188 |
if (ivalue1==null && ivalue2!=null) return -1*sign;
|
|
|
189 |
else if (ivalue1==null && ivalue2==null) return 0;
|
|
|
190 |
else if (ivalue1!=null && ivalue2==null) return 1*sign;
|
|
|
191 |
else return ivalue1.compareTo(ivalue2)*sign;
|
|
|
192 |
|
|
|
193 |
} else {
|
|
|
194 |
return DMSUtils.NVL(value1).compareToIgnoreCase(DMSUtils.NVL(value2))*sign;
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
private static void addSortedStringArray(Vector array, boolean isAscending, String data, int startIndex, int endIndex) {
|
|
|
200 |
String firstRecordValue = DMSUtils.NVL((String)array.get(startIndex));
|
|
|
201 |
String lastRecordValue = DMSUtils.NVL((String)array.get(endIndex));
|
|
|
202 |
String newValue = DMSUtils.NVL(data);
|
|
|
203 |
|
|
|
204 |
if (compareToValue(firstRecordValue, newValue, isAscending)>0) { //if newValue is less than firstValue
|
|
|
205 |
array.insertElementAt(data,startIndex); //insert before the first record
|
|
|
206 |
} else if (compareToValue(lastRecordValue, newValue, isAscending)<=0) { //if newValue is greater than or equal to the lastValue
|
|
|
207 |
array.insertElementAt(data,endIndex+1); //insert after the last record
|
|
|
208 |
} else if (startIndex == endIndex-1) { //if there are no more records in between
|
|
|
209 |
array.insertElementAt(data,startIndex+1); //insert after the first record
|
|
|
210 |
} else {
|
|
|
211 |
//recursive sort
|
|
|
212 |
int midIndex = (startIndex + endIndex) / 2;
|
|
|
213 |
String midRecordValue = DMSUtils.NVL((String)array.get(midIndex));
|
|
|
214 |
|
|
|
215 |
if (compareToValue(midRecordValue, newValue, isAscending)>0) { //if newValue is less than midRecordValue
|
|
|
216 |
addSortedStringArray(array, isAscending, data, startIndex+1, midIndex);
|
|
|
217 |
} else {
|
|
|
218 |
addSortedStringArray(array, isAscending, data, midIndex, endIndex-1);
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
public static String[] SortArray(String [] data) {
|
|
|
224 |
Vector v = new Vector();
|
|
|
225 |
|
|
|
226 |
for (int i=0; i< data.length; i++) {
|
|
|
227 |
if (i==0) v.add(data[i]);
|
|
|
228 |
else addSortedStringArray(v, true, data[i], 0, i-1);
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
return (String[])v.toArray(new String[0]);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
//generate a properly encoded CSV cell string
|
|
|
235 |
public static String cleanCSV(String value) {
|
|
|
236 |
String ret = DMSUtils.NVL(value);
|
|
|
237 |
try {
|
|
|
238 |
ret = DMSUtils.StrReplace(ret, "\"\"", "\"", true);
|
|
|
239 |
ret = DMSUtils.StrReplace(ret, "\n","\r\n", true);
|
|
|
240 |
ret = "\"" + ret + "\"";
|
|
|
241 |
} catch (Exception e) {
|
|
|
242 |
e.printStackTrace();
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
return ret;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
public static String TimestampToString(Timestamp value, String format) {
|
|
|
249 |
SimpleDateFormat df = new SimpleDateFormat(format);
|
|
|
250 |
return df.format(value);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
public static int StringToInt(String value) {
|
|
|
254 |
return Integer.valueOf(value).intValue();
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
public static Integer StringToInteger(String value) {
|
|
|
258 |
Integer ret = null;
|
|
|
259 |
try {
|
|
|
260 |
ret = new Integer(value);
|
|
|
261 |
} catch (Exception e){e.printStackTrace();}
|
|
|
262 |
return ret;
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
public static Timestamp StringToTimestampEndOfDay(String year, String month, String day) {
|
|
|
267 |
return StringToTimestamp(year, month, day, "23", "59", "59");
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
public static Timestamp StringToTimestamp(String year, String month, String day, String hour, String minute, String second) {
|
|
|
271 |
Timestamp data = null;
|
|
|
272 |
|
|
|
273 |
try {
|
|
|
274 |
int iYear = StringToInt(year);
|
|
|
275 |
if (iYear>1900) iYear = iYear-1900;
|
|
|
276 |
int iMonth = StringToInt(month);
|
|
|
277 |
int iDay = StringToInt(day);
|
|
|
278 |
int iHour = StringToInt(hour);
|
|
|
279 |
int iMinute = StringToInt(minute);
|
|
|
280 |
int iSecond = StringToInt(second);
|
|
|
281 |
|
|
|
282 |
data = new Timestamp(iYear, iMonth, iDay, iHour, iMinute, iSecond,0);
|
|
|
283 |
} catch (Exception e) {e.printStackTrace();}
|
|
|
284 |
|
|
|
285 |
return data;
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
public static Timestamp StringToTimestamp(String year, String month, String day) {
|
|
|
289 |
return StringToTimestamp(year, month, day, "0", "0", "0");
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
public static Timestamp StringToTimestamp(String value, String format) {
|
|
|
293 |
Timestamp data = null;
|
|
|
294 |
|
|
|
295 |
SimpleDateFormat df = new SimpleDateFormat(format);
|
|
|
296 |
|
|
|
297 |
try {
|
|
|
298 |
java.util.Date d = df.parse(value);
|
|
|
299 |
|
|
|
300 |
data = new Timestamp(d.getYear(), d.getMonth(), d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds(),0);
|
|
|
301 |
} catch (Exception e) {e.printStackTrace();}
|
|
|
302 |
|
|
|
303 |
return data;
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
|
|
|
307 |
}
|