1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
| import vo.CaseLegalAdvice; import vo.Chapter; import vo.Enactment; import vo.EnactmentView; import vo.LegalAdviceUpload; import vo.OffenseQuote;
import java.io.File; import java.io.FileInputStream; import java.io.Serializable; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.oro.text.regex.MatchResult; import org.apache.oro.text.regex.Pattern; import org.apache.oro.text.regex.PatternCompiler; import org.apache.oro.text.regex.PatternMatcher; import org.apache.oro.text.regex.Perl5Compiler; import org.apache.oro.text.regex.Perl5Matcher; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction;
import xx.utils.DbManager;
public class LegalAdviceAction extends DispatchAction { final ResourceBundle uploadSetting = ResourceBundle .getBundle("xx.properties.uploadSettings");
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
final SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
final String savePath = uploadSetting .getString("upload.dir.prosecutions");
String fileNo = ""; String subject = ""; String legalAdviceStatus = ""; String legalAdviceRemarks = ""; String legalAdviceDate = ""; String authRefInd = ""; String authRef = ""; String defendant = ""; String defendantEmployer = ""; String defendantEmployee = ""; String ordinance = ""; String keepFileNos = ""; List fileContent = new ArrayList();
List keepFileNosList = new ArrayList(); List uploadFileList = new ArrayList(); String reqCode = ""; String caseNo = "";
final Timestamp now = new Timestamp(new java.util.Date().getTime()); String login_id = (String) request.getSession() .getAttribute("login_id");
final DiskFileUpload upload = new DiskFileUpload(); final List list = upload.parseRequest(request); final Iterator it = list.iterator(); while (it.hasNext()) { final FileItem item = (FileItem) it.next(); if (item.isFormField()) { if (getParamter(item, "subject")) { subject = item.getString(); } else if (getParamter(item, "ordinance")) { ordinance = item.getString(); } else if (getParamter(item, "legalAdviceDate")) { legalAdviceDate = item.getString(); } else if (getParamter(item, "legalAdviceStatus")) { if ("a".equals(item.getString())) { legalAdviceStatus = "Accepted"; } else if ("na".equals(item.getString())) { legalAdviceStatus = "Not Accepted"; } else { legalAdviceStatus = "Not Mentioned"; } } else if (getParamter(item, "legalAdviceRemarks")) { legalAdviceRemarks = item.getString(); } else if (getParamter(item, "authRefInd")) { authRefInd = item.getString(); } else if (getParamter(item, "defendantEmployer")) { defendantEmployer = item.getString(); } else if (getParamter(item, "defendantEmployee")) { defendantEmployee = item.getString(); } else if (getParamter(item, "fileNo")) { fileNo = item.getString(); } else if (getParamter(item, "authRef")) { authRef = item.getString(); } else if (getParamter(item, "reqCode")) { reqCode = item.getString(); } else if (getParamter(item, "caseNo")) { caseNo = item.getString(); } else if (getParamter(item, "keepFileNos")) { keepFileNos = item.getString(); } } else { String fileName = item.getName(); if (fileName != null && fileName.length() > 0) { if (fileName.indexOf("\\") != -1) { PatternCompiler compiler = new Perl5Compiler(); PatternMatcher matcher = new Perl5Matcher(); Pattern pattern = compiler.compile("^.+\\\\(.+)$"); if (matcher.matches(fileName, pattern)) { MatchResult ms = matcher.getMatch(); System.out .println("fileName full path " + fileName); fileName = ms.group(1); System.out.println("fileName " + fileName); } }
uploadFileList.add(fileName); fileContent.add(item); } } }
if (defendantEmployer.length() > 0 && defendantEmployee.length() > 0) { defendant = defendantEmployer + "/" + defendantEmployee; } else if (defendantEmployer.length() > 0) { defendant = defendantEmployer; } else { defendant = defendantEmployee; }
System.out.println("caseNo " + caseNo + " , subject " + subject + " ,legalAdviceDate " + legalAdviceDate + " , ordinance " + ordinance + " , keepFileNos " + keepFileNos);
final Connection conn = DbManager.getConnection();
if ("edit".equals(reqCode)) { System.out.println("edit legail advice"); String sql = "update case_legal_advice set offense_code = ?, file_no = ?, subject = ?, " + "legal_advice_date = ?, legal_advice_status = ?, legal_advice_remarks = ?, " + "defendant = ?, auth_ref_ind = ?, auth_ref = ?," + "last_update = ?, update_by = ? where case_seq_no = ?"; PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, ordinance); ps.setString(2, fileNo); ps.setString(3, subject); ps.setDate(4, new Date(sf.parse(legalAdviceDate).getTime())); ps.setString(5, legalAdviceStatus); ps.setString(6, legalAdviceRemarks); ps.setString(7, defendant); ps.setString(8, authRefInd); ps.setString(9, authRef);
ps.setTimestamp(10, now); ps.setString(11, login_id); ps.setInt(12, Integer.parseInt(caseNo));
ps.executeUpdate(); ps.close();
StringTokenizer st = new StringTokenizer(keepFileNos, "|"); while (st.hasMoreTokens()) { String token = st.nextToken(); System.out.println("keep file No. " + token); keepFileNosList.add(token); }
String fileSql = "select upload_seq_no, file_name from legal_advice_upload where case_seq_no = ? order by upload_seq_no"; System.out.println("fileSql " + fileSql); ps = conn.prepareStatement(fileSql); ps.setString(1, caseNo); ResultSet rs = ps.executeQuery();
List toDeleteFilesNo = new ArrayList(); while (rs.next()) { String upload_seq_no = rs.getString("upload_seq_no"); String file = rs.getString("file_name");
if (!keepFileNosList.contains(upload_seq_no)) { toDeleteFilesNo.add(upload_seq_no); if (new File(savePath + file).delete()) { System.out.println("file " + file + " is deleted."); } } } rs.close(); ps.close();
Iterator delItr = toDeleteFilesNo.iterator(); String deleteFileSql = "delete legal_advice_upload where upload_seq_no = ?"; ps = conn.prepareStatement(deleteFileSql); while (delItr.hasNext()) { String uploadNo = (String) delItr.next(); ps.setString(1, uploadNo); ps.executeUpdate(); } ps.close();
} else { System.out.println("add legail advice."); int seq = 0; Statement stmt = conn.createStatement(); ResultSet rs = stmt .executeQuery("select case_legal_advice_SQ.nextval as seq from dual");
while (rs.next()) { seq = rs.getInt("seq"); } rs.close(); stmt.close();
System.out.println("case_legal_advice_SQ " + seq); caseNo = String.valueOf(seq); String sql = "insert into case_legal_advice(case_seq_no, offense_code, file_no, subject, legal_advice_date, " + "legal_advice_status, legal_advice_remarks, defendant, auth_ref_ind, auth_ref, " + "last_update, update_by) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; final PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, seq); ps.setString(2, ordinance); ps.setString(3, fileNo); ps.setString(4, subject); ps.setDate(5, new Date(sf.parse(legalAdviceDate).getTime())); ps.setString(6, legalAdviceStatus); ps.setString(7, legalAdviceRemarks); ps.setString(8, defendant); ps.setString(9, authRefInd); ps.setString(10, authRef);
ps.setTimestamp(11, now); ps.setString(12, login_id);
ps.executeUpdate(); ps.close(); }
Iterator itr = uploadFileList.iterator(); int i = 0; while (itr.hasNext()) { String fileName = (String) itr.next(); int uploadReq = 0; Statement stmt2 = conn.createStatement(); ResultSet rs2 = stmt2 .executeQuery("select legal_advice_upload_SQ.nextval as seq from dual");
while (rs2.next()) { uploadReq = rs2.getInt("seq"); } rs2.close(); stmt2.close(); System.out.println("legal_advice_upload_SQ " + uploadReq);
String sql2 = "insert into legal_advice_upload (upload_seq_no, case_seq_no, file_name, last_update, update_by) values(?, ?, ?, ?, ?)"; PreparedStatement ps2 = conn.prepareStatement(sql2); ps2.setInt(1, uploadReq); ps2.setInt(2, Integer.parseInt(caseNo)); ps2.setString(3, fileName); ps2.setTimestamp(4, now); ps2.setString(5, login_id); ps2.executeUpdate(); ps2.close();
FileItem item = (FileItem) fileContent.get(i);
System.out.println("save file " + fileName + " => " + savePath + String.valueOf(uploadReq)); item.write(new File(savePath + String.valueOf(uploadReq))); }
return mapping.findForward("redirectList"); }
private boolean getParamter(FileItem item, String paramName) { if (item.getString() != null && item.getFieldName().equals(paramName)) { return true; } return false; }
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String orderBy = request.getParameter("orderBy"); System.out.println("orderBy " + orderBy);
Object lastFieldSort = request.getSession().getAttribute( "lastFieldSort"); FieldSort fieldSort = null;
if (lastFieldSort == null) { fieldSort = new FieldSort(); fieldSort.setSortBy(orderBy); fieldSort.setAsc(Boolean.TRUE); } else { fieldSort = (FieldSort) lastFieldSort;
if (fieldSort.getSortBy() != null && fieldSort.getSortBy().equals(orderBy)) { if (fieldSort.getAsc().equals(Boolean.TRUE)) { fieldSort.setAsc(Boolean.FALSE); } else { fieldSort.setAsc(Boolean.TRUE); } } else { fieldSort.setSortBy(orderBy); fieldSort.setAsc(Boolean.TRUE); } }
final Connection conn = DbManager.getConnection();
Statement stmt = conn.createStatement(); String sql = "select e.enact_Chap, e.title, c.chapter, o.sect_reg, o.description, cla.case_seq_no, cla.file_no, cla.subject, " + "cla.legal_advice_date, cla.legal_advice_status, cla.defendant, cla.auth_ref_ind, cla.auth_ref, u.upload_seq_no, u.file_name " + "from Enactment e, Chapter c, offense_quote o, case_legal_advice cla, legal_advice_upload u " + "where e.enact_chap = c.enact_chap and c.Chapter = o.Chapter and o.offense_code = cla.offense_code(+) and " + "cla.case_seq_no = u.case_seq_no(+)"; if (orderBy != null && orderBy.length() > 0) { sql += " order by " + fieldSort.getSortBy() + (fieldSort.getAsc().equals(Boolean.TRUE) ? " asc" : " desc") + ", cla.file_no"; } else { sql += " order by c.Chapter, cla.file_no"; orderBy = "c.chapter"; fieldSort = new FieldSort(); fieldSort.setSortBy(orderBy); fieldSort.setAsc(Boolean.TRUE); }
request.getSession().setAttribute("lastFieldSort", fieldSort);
System.out.println("sql = " + sql); ResultSet rs = stmt.executeQuery(sql); List list = new ArrayList(); while (rs.next()) { EnactmentView view = new EnactmentView(); Enactment enactment = new Enactment(); Chapter chapter = new Chapter(); ... String enactChap = rs.getString("enact_Chap"); String title = rs.getString("title"); String chapterValue = rs.getString("chapter"); ...
list.add(view); } rs.close(); stmt.close();
request.setAttribute("list", list); request.setAttribute("sortBy", fieldSort.getSortBy()); if (fieldSort.getAsc().equals(Boolean.FALSE)) { request.setAttribute("order", "down"); } else { request.setAttribute("order", "up"); }
String savePath = uploadSetting.getString("upload.dir.prosecutions"); request.setAttribute("savePath", savePath);
return mapping.findForward("list"); }
public ActionForward download(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String id = request.getParameter("id"); String fileName = "";
final Connection conn = DbManager.getConnection(); String sql = "select file_name from legal_advice_upload where upload_seq_no = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, id); ResultSet rs = ps.executeQuery();
if (rs.next()) { fileName = rs.getString("file_name"); } rs.close(); ps.close();
System.out.println("download file : " + fileName); String savePath = uploadSetting.getString("upload.dir.prosecutions"); FileInputStream in = new FileInputStream(savePath + "/" + id); byte[] buffer = new byte[8092]; int i = 0; response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("UTF-8"), "ISO_8859_1") + "\"");
while ((i = in.read(buffer)) != -1) { response.getOutputStream().write(buffer, 0, i); }
response.getOutputStream().flush(); return null; } }
class FieldSort implements Serializable { private String sortBy; private Boolean asc;
public String getSortBy() { return sortBy; }
public void setSortBy(String sortBy) { this.sortBy = sortBy; }
public Boolean getAsc() { return asc; }
public void setAsc(Boolean asc) { this.asc = asc; } }
|