diff --git a/src/main/java/com/gcc_manpower/gccservicedocuments/controller/GccServiceDocumentsController.java b/src/main/java/com/gcc_manpower/gccservicedocuments/controller/GccServiceDocumentsController.java index 63c666b..0cf9d44 100644 --- a/src/main/java/com/gcc_manpower/gccservicedocuments/controller/GccServiceDocumentsController.java +++ b/src/main/java/com/gcc_manpower/gccservicedocuments/controller/GccServiceDocumentsController.java @@ -33,13 +33,17 @@ public class GccServiceDocumentsController { @GetMapping({ "", "/" }) public String viewPortal(Model model, HttpSession session) { - String usrName = (String) session.getAttribute("usrName"); + String usrName = (String) session.getAttribute("doc_usrName"); if (usrName == null) { return "redirect:/gccservicedocuments/login"; } - String usrRole = (String) session.getAttribute("usrRole"); + String usrRole = (String) session.getAttribute("doc_usrRole"); boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole); + if (!isAdmin) { + return "redirect:/gccservicedocuments/view"; + } + model.addAttribute("isLoggedIn", true); model.addAttribute("isAdmin", isAdmin); model.addAttribute("usrName", usrName); @@ -50,11 +54,11 @@ public class GccServiceDocumentsController { @GetMapping("/view") public String viewReportPage(Model model, HttpSession session) { - String usrName = (String) session.getAttribute("usrName"); + String usrName = (String) session.getAttribute("doc_usrName"); if (usrName == null) { return "redirect:/gccservicedocuments/login"; } - String usrRole = (String) session.getAttribute("usrRole"); + String usrRole = (String) session.getAttribute("doc_usrRole"); boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole); model.addAttribute("isLoggedIn", true); @@ -67,7 +71,7 @@ public class GccServiceDocumentsController { @GetMapping("/login") public String login(HttpSession session) { - if (session.getAttribute("usrName") != null) { + if (session.getAttribute("doc_usrName") != null) { return "redirect:/gccservicedocuments/view"; } return "gccservicedocuments/login"; @@ -84,8 +88,8 @@ public class GccServiceDocumentsController { Map response = new HashMap<>(); if (Boolean.TRUE.equals(authResult.get("success"))) { - session.setAttribute("usrName", authResult.get("usrName")); - session.setAttribute("usrRole", authResult.get("usrRole")); + session.setAttribute("doc_usrName", authResult.get("usrName")); + session.setAttribute("doc_usrRole", authResult.get("usrRole")); session.setMaxInactiveInterval(1800); // 30 minutes session timeout response.put("result", true); response.put("usrName", authResult.get("usrName")); @@ -99,9 +103,8 @@ public class GccServiceDocumentsController { @GetMapping("/logout") public String logout(HttpSession session) { - session.removeAttribute("usrName"); - session.removeAttribute("usrRole"); - session.invalidate(); + session.removeAttribute("doc_usrName"); + session.removeAttribute("doc_usrRole"); return "redirect:/gccservicedocuments/login"; } @@ -150,10 +153,11 @@ public class GccServiceDocumentsController { HttpSession session) { Map response = new HashMap<>(); - String usrName = (String) session.getAttribute("usrName"); - if (usrName == null) { + String usrName = (String) session.getAttribute("doc_usrName"); + String usrRole = (String) session.getAttribute("doc_usrRole"); + if (usrName == null || !"ADMIN".equalsIgnoreCase(usrRole)) { response.put("success", false); - response.put("error", "Unauthorized access! Please log in."); + response.put("error", "Unauthorized access! Admin role required."); return response; } @@ -232,8 +236,8 @@ public class GccServiceDocumentsController { @ResponseBody public Map deleteFile(@RequestParam("id") Long id, HttpSession session) { Map response = new HashMap<>(); - String usrName = (String) session.getAttribute("usrName"); - String usrRole = (String) session.getAttribute("usrRole"); + String usrName = (String) session.getAttribute("doc_usrName"); + String usrRole = (String) session.getAttribute("doc_usrRole"); if (usrName == null || !"ADMIN".equalsIgnoreCase(usrRole)) { response.put("success", false); response.put("error", "Unauthorized access! Admin role required."); @@ -251,4 +255,93 @@ public class GccServiceDocumentsController { return response; } + + @PostMapping("/api/update") + @ResponseBody + public Map updateFile( + @RequestParam("id") Long id, + @RequestParam("title") String title, + @RequestParam("subCategory") String subCategory, + @RequestParam("fileType") String fileType, + @RequestParam(value = "file", required = false) MultipartFile file, + HttpSession session) { + Map response = new HashMap<>(); + String usrName = (String) session.getAttribute("doc_usrName"); + String usrRole = (String) session.getAttribute("doc_usrRole"); + if (usrName == null || !"ADMIN".equalsIgnoreCase(usrRole)) { + response.put("success", false); + response.put("error", "Unauthorized access! Admin role required."); + return response; + } + + if (title == null || title.trim().isEmpty() || subCategory == null || subCategory.trim().isEmpty() + || fileType == null || fileType.trim().isEmpty()) { + response.put("success", false); + response.put("error", "Required fields cannot be empty."); + return response; + } + + String fileWebUrl = null; + String originalFilename = null; + File destFile = null; + + try { + if (file != null && !file.isEmpty()) { + // Save new file + String cleanCategory = subCategory.trim().replaceAll("[^a-zA-Z0-9-_ ]", "").trim(); + if (cleanCategory.isEmpty()) { + cleanCategory = "General"; + } + + File uploadDir = new File(documentUploadPathConfig, cleanCategory).getAbsoluteFile(); + if (!uploadDir.exists()) { + uploadDir.mkdirs(); + } + + originalFilename = file.getOriginalFilename(); + String nameWithoutExt = ""; + String ext = ""; + if (originalFilename != null && originalFilename.contains(".")) { + int dotIdx = originalFilename.lastIndexOf("."); + nameWithoutExt = originalFilename.substring(0, dotIdx); + ext = originalFilename.substring(dotIdx); + } else { + nameWithoutExt = originalFilename != null ? originalFilename : "file"; + } + + nameWithoutExt = nameWithoutExt.replaceAll("[^a-zA-Z0-9-_]", "_"); + String savedFilename = nameWithoutExt + "_" + System.currentTimeMillis() + ext; + + destFile = new File(uploadDir, savedFilename).getAbsoluteFile(); + file.transferTo(destFile); + + fileWebUrl = "/gccservicedocuments/files/" + cleanCategory + "/" + savedFilename; + } + + boolean updated = documentService.updateDocument(id, title.trim(), subCategory.trim(), fileType.trim(), + fileWebUrl, originalFilename); + + if (updated) { + if (file != null && !file.isEmpty() && "video".equalsIgnoreCase(fileType)) { + videoCompressionService.compressVideoAsync(destFile); + } + response.put("success", true); + response.put("message", "Document updated successfully."); + } else { + if (destFile != null && destFile.exists()) { + destFile.delete(); + } + response.put("success", false); + response.put("error", "Failed to update document details in the database."); + } + } catch (Exception e) { + if (destFile != null && destFile.exists()) { + destFile.delete(); + } + response.put("success", false); + response.put("error", "Document update failed: " + e.getMessage()); + } + + return response; + } } diff --git a/src/main/java/com/gcc_manpower/gccservicedocuments/service/GccServiceDocumentsService.java b/src/main/java/com/gcc_manpower/gccservicedocuments/service/GccServiceDocumentsService.java index 4863f1f..2415556 100644 --- a/src/main/java/com/gcc_manpower/gccservicedocuments/service/GccServiceDocumentsService.java +++ b/src/main/java/com/gcc_manpower/gccservicedocuments/service/GccServiceDocumentsService.java @@ -63,7 +63,8 @@ public class GccServiceDocumentsService { try { int id = Integer.parseInt(subCategoryName.trim()); String checkIdSql = "SELECT id FROM sub_category_list WHERE id = ? AND mid = ? AND is_delete = 0"; - List ids = jdbcTemplate.queryForList(checkIdSql, Integer.class, id, String.valueOf(mainCategoryId)); + List ids = jdbcTemplate.queryForList(checkIdSql, Integer.class, id, + String.valueOf(mainCategoryId)); if (!ids.isEmpty()) { return ids.get(0); } @@ -73,7 +74,8 @@ public class GccServiceDocumentsService { String selectSql = "SELECT id FROM sub_category_list WHERE category_name = ? AND mid = ? AND is_delete = 0"; try { - List ids = jdbcTemplate.queryForList(selectSql, Integer.class, subCategoryName.trim(), String.valueOf(mainCategoryId)); + List ids = jdbcTemplate.queryForList(selectSql, Integer.class, subCategoryName.trim(), + String.valueOf(mainCategoryId)); if (!ids.isEmpty()) { return ids.get(0); } @@ -86,7 +88,8 @@ public class GccServiceDocumentsService { try { jdbcTemplate.update(insertSql, subCategoryName.trim(), String.valueOf(mainCategoryId)); // Fetch the generated ID - List ids = jdbcTemplate.queryForList(selectSql, Integer.class, subCategoryName.trim(), String.valueOf(mainCategoryId)); + List ids = jdbcTemplate.queryForList(selectSql, Integer.class, subCategoryName.trim(), + String.valueOf(mainCategoryId)); if (!ids.isEmpty()) { return ids.get(0); } @@ -150,7 +153,8 @@ public class GccServiceDocumentsService { + "VALUES (?, ?, ?, ?, ?, 1, 0)"; try { - int rows = jdbcTemplate.update(sql, title, String.valueOf(subCategoryId), String.valueOf(fileTypeId), filePath, + int rows = jdbcTemplate.update(sql, title, String.valueOf(subCategoryId), String.valueOf(fileTypeId), + filePath, originalFilename); return rows > 0; } catch (Exception e) { @@ -159,7 +163,8 @@ public class GccServiceDocumentsService { } } - public List> searchDocuments(String mainCategory, String subCategory, String fromDate, String toDate) { + public List> searchDocuments(String mainCategory, String subCategory, String fromDate, + String toDate) { StringBuilder sql = new StringBuilder( "SELECT d.id, d.title, s.category_name AS category, m.main_category_name AS main_category, f.file_type_name AS file_type, d.file_path, d.original_filename, d.upload_date " + @@ -208,6 +213,88 @@ public class GccServiceDocumentsService { } } + public Map getDocumentById(Long id) { + String sql = "SELECT * FROM documents_uploads WHERE id = ?"; + try { + List> list = jdbcTemplate.queryForList(sql, id); + if (!list.isEmpty()) { + return list.get(0); + } + } catch (Exception e) { + System.err.println("Error getting document ID " + id + ": " + e.getMessage()); + } + return null; + } + + public boolean updateDocument(Long id, String title, String subCategory, String fileType, String filePath, + String originalFilename) { + // Insert old record into history before update + try { + Map old = getDocumentById(id); + if (old != null) { + String insertHistorySql = "INSERT INTO documents_upload_history (" + + "document_id, title, category, file_type, file_path, original_filename, upload_date, is_active, is_delete" + + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; + jdbcTemplate.update(insertHistorySql, + old.get("id"), + old.get("title"), + old.get("category"), + old.get("file_type"), + old.get("file_path"), + old.get("original_filename"), + old.get("upload_date"), + old.get("is_active"), + old.get("is_delete")); + } + } catch (Exception e) { + System.err.println("Error inserting document history for ID " + id + ": " + e.getMessage()); + } + + // Resolve subCategoryId and fileTypeId + int subCategoryId = 0; + String findSubSql = "SELECT id FROM sub_category_list WHERE category_name = ? AND is_delete = 0 LIMIT 1"; + try { + List ids = jdbcTemplate.queryForList(findSubSql, Integer.class, subCategory.trim()); + if (!ids.isEmpty()) { + subCategoryId = ids.get(0); + } else { + // Find old document's main category ID + String oldMainCatSql = "SELECT s.mid FROM documents_uploads d JOIN sub_category_list s ON d.category = s.id WHERE d.id = ?"; + List mids = jdbcTemplate.queryForList(oldMainCatSql, Integer.class, id); + int mid = mids.isEmpty() ? 1 : mids.get(0); + subCategoryId = getOrCreateSubCategory(subCategory, mid); + } + } catch (Exception e) { + System.err.println("Error resolving subcategory: " + e.getMessage()); + } + + int fileTypeId = getOrCreateFileType(fileType); + + // Build the update query + StringBuilder sql = new StringBuilder("UPDATE documents_uploads SET title = ?, category = ?, file_type = ?"); + List params = new ArrayList<>(); + params.add(title.trim()); + params.add(String.valueOf(subCategoryId)); + params.add(String.valueOf(fileTypeId)); + + if (filePath != null && !filePath.trim().isEmpty()) { + sql.append(", file_path = ?, original_filename = ?"); + params.add(filePath.trim()); + params.add(originalFilename.trim()); + } + + sql.append(" WHERE id = ?"); + params.add(id); + + try { + int rows = jdbcTemplate.update(sql.toString(), params.toArray()); + return rows > 0; + } catch (Exception e) { + System.err.println("Error updating service document ID " + id + ": " + e.getMessage()); + return false; + } + } + public List getAllCategories() { String sql = "SELECT category_name FROM sub_category_list WHERE is_active = 1 AND is_delete = 0 ORDER BY category_name"; try { diff --git a/src/main/java/com/gcc_manpower/gccservicepaymenttracker/controller/GccServicePaymentTrackerController.java b/src/main/java/com/gcc_manpower/gccservicepaymenttracker/controller/GccServicePaymentTrackerController.java index 758acb3..b1e75c7 100644 --- a/src/main/java/com/gcc_manpower/gccservicepaymenttracker/controller/GccServicePaymentTrackerController.java +++ b/src/main/java/com/gcc_manpower/gccservicepaymenttracker/controller/GccServicePaymentTrackerController.java @@ -31,12 +31,12 @@ public class GccServicePaymentTrackerController { @GetMapping({ "", "/", "/dashboard" }) public String viewDashboard(Model model, HttpSession session) { - String username = (String) session.getAttribute("usrName"); + String username = (String) session.getAttribute("pay_usrName"); if (username == null) { return "redirect:/gccservicepaymenttracker/login"; } - String usrRole = (String) session.getAttribute("usrRole"); + String usrRole = (String) session.getAttribute("pay_usrRole"); boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole); List> vendorDetails = appUserService.getVendorDetailsWithCounts(); @@ -52,7 +52,7 @@ public class GccServicePaymentTrackerController { @GetMapping("/login") public String login(HttpSession session) { - if (session.getAttribute("usrName") != null) { + if (session.getAttribute("pay_usrName") != null) { return "redirect:/gccservicepaymenttracker/dashboard"; } return "gccservicepaymenttracker/login"; @@ -69,8 +69,8 @@ public class GccServicePaymentTrackerController { Map response = new HashMap<>(); if (Boolean.TRUE.equals(authResult.get("success"))) { - session.setAttribute("usrName", authResult.get("usrName")); - session.setAttribute("usrRole", authResult.get("usrRole")); + session.setAttribute("pay_usrName", authResult.get("usrName")); + session.setAttribute("pay_usrRole", authResult.get("usrRole")); session.setMaxInactiveInterval(1800); // 30 minutes session timeout response.put("result", true); response.put("usrName", authResult.get("usrName")); @@ -84,17 +84,15 @@ public class GccServicePaymentTrackerController { @PostMapping("/logout") public String logout(HttpSession session) { - session.removeAttribute("usrName"); - session.removeAttribute("usrRole"); - session.invalidate(); + session.removeAttribute("pay_usrName"); + session.removeAttribute("pay_usrRole"); return "redirect:/gccservicepaymenttracker/login"; } @GetMapping("/logout") public String logoutGet(HttpSession session) { - session.removeAttribute("usrName"); - session.removeAttribute("usrRole"); - session.invalidate(); + session.removeAttribute("pay_usrName"); + session.removeAttribute("pay_usrRole"); return "redirect:/gccservicepaymenttracker/login"; } diff --git a/src/main/java/com/gcc_manpower/gccservicepaymenttracker/service/GccServicePaymentTrackerService.java b/src/main/java/com/gcc_manpower/gccservicepaymenttracker/service/GccServicePaymentTrackerService.java index 7ca9c71..217a383 100644 --- a/src/main/java/com/gcc_manpower/gccservicepaymenttracker/service/GccServicePaymentTrackerService.java +++ b/src/main/java/com/gcc_manpower/gccservicepaymenttracker/service/GccServicePaymentTrackerService.java @@ -72,33 +72,39 @@ public class GccServicePaymentTrackerService { public List> searchDetails(String type, String fromDate, String toDate, String status) { StringBuilder sql = new StringBuilder( "SELECT p.*, " - + "t.type_name as resolved_type, " - + "v.vendor_name as resolved_vendor " - + "FROM payment_tracking_details p " - + "LEFT JOIN type_list t ON p.type REGEXP '^[0-9]+$' AND t.id = p.type " - + "LEFT JOIN vendors_list v ON p.vendor_name REGEXP '^[0-9]+$' AND v.id = p.vendor_name " - + "WHERE p.is_active = 1 AND p.is_delete = 0"); + + "t.type_name as resolved_type, " + + "v.vendor_name as resolved_vendor " + + "FROM payment_tracking_details p " + + "LEFT JOIN type_list t ON p.type REGEXP '^[0-9]+$' AND t.id = p.type " + + "LEFT JOIN vendors_list v ON p.vendor_name REGEXP '^[0-9]+$' AND v.id = p.vendor_name " + + "WHERE p.is_active = 1 AND p.is_delete = 0"); List params = new ArrayList<>(); if (type != null && !type.trim().isEmpty()) { String category = type.trim().toLowerCase(); if (category.equals("asd")) { - sql.append(" AND (LOWER(p.type) = 'asd' OR LOWER(p.type) LIKE '%asd%' OR LOWER(t.type_name) LIKE '%asd%' OR LOWER(t.type_name) LIKE '%additional security%')"); + sql.append( + " AND (LOWER(p.type) = 'asd' OR LOWER(p.type) LIKE '%asd%' OR LOWER(t.type_name) LIKE '%asd%' OR LOWER(t.type_name) LIKE '%additional security%')"); } else if (category.equals("agreement")) { sql.append(" AND (LOWER(p.type) LIKE '%agreement%' OR LOWER(t.type_name) LIKE '%agreement%')"); } else if (category.equals("sd")) { - sql.append(" AND ((LOWER(p.type) = 'sd' OR LOWER(p.type) LIKE '%security%' OR LOWER(t.type_name) LIKE '%security%') " - + "AND LOWER(p.type) NOT LIKE '%additional security%' AND LOWER(p.type) NOT LIKE '%asd%' " - + "AND (t.type_name IS NULL OR (LOWER(t.type_name) NOT LIKE '%additional security%' AND LOWER(t.type_name) NOT LIKE '%asd%')))"); + sql.append( + " AND ((LOWER(p.type) = 'sd' OR LOWER(p.type) LIKE '%security%' OR LOWER(t.type_name) LIKE '%security%') " + + "AND LOWER(p.type) NOT LIKE '%additional security%' AND LOWER(p.type) NOT LIKE '%asd%' " + + "AND (t.type_name IS NULL OR (LOWER(t.type_name) NOT LIKE '%additional security%' AND LOWER(t.type_name) NOT LIKE '%asd%')))"); } else if (category.equals("tax invoice")) { - sql.append(" AND (LOWER(p.type) LIKE '%invoice%' OR LOWER(p.type) LIKE '%bill%' OR LOWER(t.type_name) LIKE '%invoice%' OR LOWER(t.type_name) LIKE '%bill%')"); + sql.append( + " AND (LOWER(p.type) LIKE '%invoice%' OR LOWER(p.type) LIKE '%bill%' OR LOWER(t.type_name) LIKE '%invoice%' OR LOWER(t.type_name) LIKE '%bill%')"); } else if (category.equals("emd")) { - sql.append(" AND (LOWER(p.type) = 'emd' OR LOWER(p.type) LIKE '%earnest%' OR LOWER(t.type_name) LIKE '%emd%' OR LOWER(t.type_name) LIKE '%earnest%')"); + sql.append( + " AND (LOWER(p.type) = 'emd' OR LOWER(p.type) LIKE '%earnest%' OR LOWER(t.type_name) LIKE '%emd%' OR LOWER(t.type_name) LIKE '%earnest%')"); } else if (category.equals("bank guarantee")) { - sql.append(" AND (LOWER(p.type) LIKE '%guarantee%' OR LOWER(p.type) = 'bg' OR LOWER(t.type_name) LIKE '%guarantee%' OR LOWER(t.type_name) = 'bg')"); + sql.append( + " AND (LOWER(p.type) LIKE '%guarantee%' OR LOWER(p.type) = 'bg' OR LOWER(t.type_name) LIKE '%guarantee%' OR LOWER(t.type_name) = 'bg')"); } else if (category.equals("delivery chellan")) { - sql.append(" AND (LOWER(p.type) LIKE '%chellan%' OR LOWER(p.type) LIKE '%challan%' OR LOWER(p.type) = 'dc' " - + "OR LOWER(t.type_name) LIKE '%chellan%' OR LOWER(t.type_name) LIKE '%challan%' OR LOWER(t.type_name) = 'dc')"); + sql.append( + " AND (LOWER(p.type) LIKE '%chellan%' OR LOWER(p.type) LIKE '%challan%' OR LOWER(p.type) = 'dc' " + + "OR LOWER(t.type_name) LIKE '%chellan%' OR LOWER(t.type_name) LIKE '%challan%' OR LOWER(t.type_name) = 'dc')"); } else { sql.append(" AND (LOWER(p.type) = ? OR LOWER(t.type_name) = ?)"); params.add(category); @@ -166,6 +172,41 @@ public class GccServicePaymentTrackerService { if (idObj != null && !String.valueOf(idObj).trim().isEmpty()) { // Update int id = Integer.parseInt(String.valueOf(idObj).trim()); + + // Insert old record into history before update + try { + Map old = getDetailById(id); + if (old != null) { + String insertHistorySql = "INSERT INTO payment_tracking_details_history (" + + "detail_id, received_date, vendor_name, subject, amount, invoice_no, receiptno, " + + "invoice_date, type, physical_file_no, remarks, tat, status, followup, myfollowup, " + + "is_active, is_delete, order_by, file_path" + + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + jdbcTemplate.update(insertHistorySql, + old.get("id"), + old.get("received_date"), + old.get("vendor_name"), + old.get("subject"), + old.get("amount"), + old.get("invoice_no"), + old.get("receiptno"), + old.get("invoice_date"), + old.get("type"), + old.get("physical_file_no"), + old.get("remarks"), + old.get("tat"), + old.get("status"), + old.get("followup"), + old.get("myfollowup"), + old.get("is_active"), + old.get("is_delete"), + old.get("order_by"), + old.get("file_path")); + } + } catch (Exception e) { + System.err.println("Error saving history for payment tracking detail ID " + id + ": " + e.getMessage()); + } + String sql = "UPDATE payment_tracking_details SET " + "received_date = ?, vendor_name = ?, subject = ?, amount = ?, invoice_no = ?, " + "receiptno = ?, invoice_date = ?, type = ?, physical_file_no = ?, remarks = ?, " diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4687540..7197b79 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ -#spring.profiles.active=local -spring.profiles.active=prod +spring.profiles.active=local +#spring.profiles.active=prod spring.application.name=gcc-manpower server.servlet.session.timeout=10m \ No newline at end of file diff --git a/src/main/resources/templates/fragments/navbar.html b/src/main/resources/templates/fragments/navbar.html index 33b8bcf..011384c 100644 --- a/src/main/resources/templates/fragments/navbar.html +++ b/src/main/resources/templates/fragments/navbar.html @@ -16,10 +16,10 @@ class=" fa-solid fa-house"> Home - + --> --> - - -