You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
226 lines
8.5 KiB
226 lines
8.5 KiB
package com.gcc_manpower.gcctonermanagement.controller;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
|
import com.gcc_manpower.gcctonermanagement.service.TonerService;
|
|
import com.gcc_manpower.service.AppUserService;
|
|
|
|
import jakarta.servlet.http.HttpSession;
|
|
|
|
@Controller
|
|
@RequestMapping("/gcctonermanagement")
|
|
public class TonerController {
|
|
|
|
@Autowired
|
|
private AppUserService appUserService;
|
|
|
|
@Autowired
|
|
private TonerService tonerService;
|
|
|
|
@Value("${toner.upload.path:gcctonermanagementfiles}")
|
|
private String tonerUploadPathConfig;
|
|
|
|
@GetMapping({ "", "/", "/upload" })
|
|
public String viewDashboard(Model model, HttpSession session) {
|
|
String username = (String) session.getAttribute("toner_usrName");
|
|
if (username == null) {
|
|
return "redirect:/gcctonermanagement/login";
|
|
}
|
|
|
|
String usrRole = (String) session.getAttribute("toner_usrRole");
|
|
boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole);
|
|
|
|
// List<Map<String, Object>> vendorDetails =
|
|
// appUserService.getVendorDetailsWithCounts();
|
|
|
|
model.addAttribute("isLoggedIn", true);
|
|
model.addAttribute("isAdmin", isAdmin);
|
|
model.addAttribute("usrName", username);
|
|
model.addAttribute("curPage", "gcctonermanagement");
|
|
// model.addAttribute("vendorDetails", vendorDetails);
|
|
|
|
return "gcctonermanagement/upload";
|
|
}
|
|
|
|
@GetMapping("/login")
|
|
public String login(HttpSession session) {
|
|
return "gcctonermanagement/login";
|
|
}
|
|
|
|
@PostMapping("/api/login")
|
|
@ResponseBody
|
|
public Map<String, Object> validateLogin(
|
|
@RequestParam("usrName") String usrName,
|
|
@RequestParam("usrPass") String usrPass,
|
|
HttpSession session) {
|
|
|
|
Map<String, Object> authResult = appUserService.authenticate(usrName, usrPass);
|
|
Map<String, Object> response = new HashMap<>();
|
|
|
|
if (Boolean.TRUE.equals(authResult.get("success"))) {
|
|
session.setAttribute("toner_usrName", authResult.get("usrName"));
|
|
session.setAttribute("toner_usrRole", authResult.get("usrRole"));
|
|
session.setMaxInactiveInterval(1800); // 30 minutes session timeout
|
|
response.put("result", true);
|
|
response.put("usrName", authResult.get("usrName"));
|
|
} else {
|
|
response.put("result", false);
|
|
response.put("error", authResult.get("error"));
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
@GetMapping("/logout")
|
|
public String logoutGet(HttpSession session) {
|
|
session.removeAttribute("toner_usrName");
|
|
session.removeAttribute("toner_usrRole");
|
|
return "redirect:/gcctonermanagement/login";
|
|
}
|
|
|
|
@GetMapping("/api/printers")
|
|
@ResponseBody
|
|
public List<Map<String, Object>> getPrinters(HttpSession session) {
|
|
if (session.getAttribute("toner_usrName") == null) {
|
|
return java.util.Collections.emptyList();
|
|
}
|
|
return tonerService.getActivePrinters();
|
|
}
|
|
|
|
@GetMapping("/api/toners")
|
|
@ResponseBody
|
|
public List<Map<String, Object>> getToners(@RequestParam("pid") int pid, HttpSession session) {
|
|
if (session.getAttribute("toner_usrName") == null) {
|
|
return java.util.Collections.emptyList();
|
|
}
|
|
return tonerService.getActiveTonersByPrinter(pid);
|
|
}
|
|
|
|
@PostMapping("/api/upload")
|
|
@ResponseBody
|
|
public Map<String, Object> uploadToner(
|
|
@RequestParam("companyName") String companyName,
|
|
@RequestParam("pid") int pid,
|
|
@RequestParam("tid") int tid,
|
|
@RequestParam("qty") String qty,
|
|
@RequestParam(value = "dcFile", required = false) MultipartFile dcFile,
|
|
@RequestParam(value = "userRequestFile", required = false) MultipartFile userRequestFile,
|
|
@RequestParam(value = "finalDeliveredFile", required = false) MultipartFile finalDeliveredFile,
|
|
HttpSession session) {
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
String username = (String) session.getAttribute("toner_usrName");
|
|
if (username == null) {
|
|
response.put("success", false);
|
|
response.put("error", "Unauthorized access. Please log in.");
|
|
return response;
|
|
}
|
|
|
|
// Validate PDF extension on non-empty files
|
|
if (dcFile != null && !dcFile.isEmpty() && !isPdfFile(dcFile)) {
|
|
response.put("success", false);
|
|
response.put("error", "Delivery Challan file must be a PDF.");
|
|
return response;
|
|
}
|
|
if (userRequestFile != null && !userRequestFile.isEmpty() && !isPdfFile(userRequestFile)) {
|
|
response.put("success", false);
|
|
response.put("error", "User Request file must be a PDF.");
|
|
return response;
|
|
}
|
|
if (finalDeliveredFile != null && !finalDeliveredFile.isEmpty() && !isPdfFile(finalDeliveredFile)) {
|
|
response.put("success", false);
|
|
response.put("error", "Final Delivered file must be a PDF.");
|
|
return response;
|
|
}
|
|
|
|
try {
|
|
File uploadDir = new File(tonerUploadPathConfig).getAbsoluteFile();
|
|
if (!uploadDir.exists()) {
|
|
uploadDir.mkdirs();
|
|
}
|
|
|
|
// Save files
|
|
String dcPath = saveUploadedFile(dcFile, "dc", uploadDir);
|
|
String userRequestPath = saveUploadedFile(userRequestFile, "user_request", uploadDir);
|
|
String finalDeliveredPath = saveUploadedFile(finalDeliveredFile, "final_delivered", uploadDir);
|
|
|
|
boolean saved = tonerService.saveInventoryDetails(
|
|
companyName.trim(),
|
|
pid,
|
|
tid,
|
|
qty.trim(),
|
|
dcPath,
|
|
userRequestPath,
|
|
finalDeliveredPath,
|
|
username);
|
|
|
|
if (saved) {
|
|
response.put("success", true);
|
|
response.put("message", "Toner details saved successfully.");
|
|
} else {
|
|
// Delete newly saved files on DB insert failure
|
|
deleteFileIfExists(dcPath, uploadDir);
|
|
deleteFileIfExists(userRequestPath, uploadDir);
|
|
deleteFileIfExists(finalDeliveredPath, uploadDir);
|
|
response.put("success", false);
|
|
response.put("error", "Failed to save registration details in the database.");
|
|
}
|
|
} catch (Exception e) {
|
|
response.put("success", false);
|
|
response.put("error", "An error occurred during saving: " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
private boolean isPdfFile(MultipartFile file) {
|
|
String originalFilename = file.getOriginalFilename();
|
|
if (originalFilename == null)
|
|
return false;
|
|
return originalFilename.toLowerCase().endsWith(".pdf");
|
|
}
|
|
|
|
private String saveUploadedFile(MultipartFile file, String prefix, File uploadDir) throws Exception {
|
|
if (file == null || file.isEmpty()) {
|
|
return null;
|
|
}
|
|
String originalFilename = file.getOriginalFilename();
|
|
String ext = ".pdf"; // Since we only allow PDFs
|
|
|
|
// Clean filename, make it unique
|
|
String savedFilename = prefix + "_" + System.currentTimeMillis() + ext;
|
|
File destFile = new File(uploadDir, savedFilename).getAbsoluteFile();
|
|
file.transferTo(destFile);
|
|
|
|
return "/gcctonermanagementfiles/" + savedFilename;
|
|
}
|
|
|
|
private void deleteFileIfExists(String webPath, File uploadDir) {
|
|
if (webPath == null || webPath.isEmpty())
|
|
return;
|
|
try {
|
|
String filename = webPath.substring(webPath.lastIndexOf('/') + 1);
|
|
File file = new File(uploadDir, filename).getAbsoluteFile();
|
|
if (file.exists()) {
|
|
file.delete();
|
|
}
|
|
} catch (Exception e) {
|
|
System.err.println("Error deleting file: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|