All GCC vendors and employee details for IT department management
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.

393 lines
15 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gcc_manpower.gcctapaldetailsmanagement.controller;
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.http.CacheControl;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.security.authentication.AuthenticationManager;
  11. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  12. import org.springframework.security.core.Authentication;
  13. import org.springframework.security.core.AuthenticationException;
  14. import org.springframework.security.core.context.SecurityContextHolder;
  15. import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.ui.Model;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.PostMapping;
  20. import org.springframework.web.bind.annotation.RequestBody;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. import org.springframework.web.bind.annotation.ResponseBody;
  24. import org.springframework.web.multipart.MultipartFile;
  25. import com.gcc_manpower.gcctapaldetailsmanagement.service.TapalService;
  26. import jakarta.servlet.http.HttpServletRequest;
  27. import jakarta.servlet.http.HttpSession;
  28. @Controller
  29. @RequestMapping("/gcctapaldetailsmanagement")
  30. public class TapalController {
  31. @Autowired
  32. private TapalService tapalService;
  33. @Autowired
  34. private AuthenticationManager authenticationManager;
  35. @Value("${tapal.upload.path:gcctapalfiles}")
  36. private String tapalUploadPathConfig;
  37. @GetMapping({ "", "/", "/dashboard" })
  38. public String viewDashboard(Model model, HttpSession session) {
  39. String username = (String) session.getAttribute("tap_usrName");
  40. if (username == null) {
  41. return "redirect:/gcctapaldetailsmanagement/login";
  42. }
  43. String usrRole = (String) session.getAttribute("tap_usrRole");
  44. boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole);
  45. model.addAttribute("isLoggedIn", true);
  46. model.addAttribute("isAdmin", isAdmin);
  47. model.addAttribute("usrName", username);
  48. model.addAttribute("curPage", "gcctapaldetailsmanagement");
  49. return "gcctapaldetailsmanagement/dashboard";
  50. }
  51. @GetMapping("/details")
  52. public String viewDetails(HttpSession session, Model model) {
  53. String username = (String) session.getAttribute("tap_usrName");
  54. if (username == null) {
  55. return "redirect:/gcctapaldetailsmanagement/login";
  56. }
  57. String usrRole = (String) session.getAttribute("tap_usrRole");
  58. boolean isAdmin = "ADMIN".equalsIgnoreCase(usrRole);
  59. model.addAttribute("isLoggedIn", true);
  60. model.addAttribute("isAdmin", isAdmin);
  61. model.addAttribute("usrName", username);
  62. model.addAttribute("curPage", "gcctapaldetailsmanagement");
  63. return "gcctapaldetailsmanagement/details";
  64. }
  65. @GetMapping("/login")
  66. public String login(HttpSession session) {
  67. return "gcctapaldetailsmanagement/login";
  68. }
  69. @PostMapping("/api/login")
  70. @ResponseBody
  71. public Map<String, Object> validateLogin(
  72. @RequestParam("usrName") String usrName,
  73. @RequestParam("usrPass") String usrPass,
  74. HttpSession session) {
  75. Map<String, Object> response = new HashMap<>();
  76. try {
  77. UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(usrName,
  78. usrPass);
  79. Authentication authentication = authenticationManager.authenticate(authenticationToken);
  80. SecurityContextHolder.getContext().setAuthentication(authentication);
  81. session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
  82. SecurityContextHolder.getContext());
  83. String role = authentication.getAuthorities().stream()
  84. .map(auth -> auth.getAuthority().replace("ROLE_", ""))
  85. .findFirst()
  86. .orElse("USER");
  87. session.setAttribute("tap_usrName", usrName);
  88. session.setAttribute("tap_usrRole", role);
  89. session.setMaxInactiveInterval(1800); // 30 minutes session timeout
  90. response.put("result", true);
  91. response.put("usrName", usrName);
  92. } catch (AuthenticationException e) {
  93. response.put("result", false);
  94. response.put("error", e.getMessage() != null ? e.getMessage() : "Invalid username or password!");
  95. }
  96. return response;
  97. }
  98. @GetMapping("/logout")
  99. public String logout(HttpSession session, HttpServletRequest request) {
  100. SecurityContextHolder.clearContext();
  101. if (session != null) {
  102. session.removeAttribute("tap_usrName");
  103. session.removeAttribute("tap_usrRole");
  104. session.invalidate();
  105. }
  106. request.getSession(true);
  107. return "redirect:/gcctapaldetailsmanagement/login";
  108. }
  109. @GetMapping("/api/counts")
  110. @ResponseBody
  111. public ResponseEntity<Map<String, Integer>> getCounts() {
  112. return ResponseEntity.ok()
  113. .cacheControl(CacheControl.noCache().mustRevalidate())
  114. .body(tapalService.getCountsByCategory());
  115. }
  116. @GetMapping("/api/list")
  117. @ResponseBody
  118. public ResponseEntity<List<Map<String, Object>>> listDetails(
  119. @RequestParam(value = "category", required = false) String category,
  120. @RequestParam(value = "fromDate", required = false) String fromDate,
  121. @RequestParam(value = "toDate", required = false) String toDate,
  122. @RequestParam(value = "status", required = false) String status) {
  123. List<Map<String, Object>> details = tapalService.searchTapals(category, fromDate, toDate, status);
  124. return ResponseEntity.ok()
  125. .cacheControl(CacheControl.noCache().mustRevalidate())
  126. .body(details);
  127. }
  128. @PostMapping("/api/save")
  129. @ResponseBody
  130. public Map<String, Object> saveDetail(
  131. @RequestBody Map<String, Object> payload,
  132. HttpSession session) {
  133. Map<String, Object> response = new HashMap<>();
  134. String username = (String) session.getAttribute("tap_usrName");
  135. if (username == null) {
  136. username = (String) session.getAttribute("usrName");
  137. }
  138. if (username == null) {
  139. response.put("success", false);
  140. response.put("error", "Unauthorized access! Please log in.");
  141. return response;
  142. }
  143. String usrRole = (String) session.getAttribute("tap_usrRole");
  144. if (usrRole == null) {
  145. usrRole = (String) session.getAttribute("usrRole");
  146. }
  147. if (!"ADMIN".equalsIgnoreCase(usrRole)) {
  148. response.put("success", false);
  149. response.put("error", "Access denied! Only admins can save or update records.");
  150. return response;
  151. }
  152. try {
  153. boolean saved = tapalService.saveOrUpdateTapal(payload, username);
  154. response.put("success", saved);
  155. if (!saved) {
  156. response.put("error", "Failed to save Tapal details to database.");
  157. }
  158. } catch (Exception e) {
  159. response.put("success", false);
  160. response.put("error", "Error: " + e.getMessage());
  161. }
  162. return response;
  163. }
  164. @PostMapping("/api/delete")
  165. @ResponseBody
  166. public Map<String, Object> deleteDetail(
  167. @RequestParam("id") int id,
  168. @RequestParam(value = "remarks", required = false, defaultValue = "") String remarks,
  169. HttpSession session) {
  170. Map<String, Object> response = new HashMap<>();
  171. String username = (String) session.getAttribute("tap_usrName");
  172. if (username == null) {
  173. username = (String) session.getAttribute("usrName");
  174. }
  175. if (username == null) {
  176. response.put("success", false);
  177. response.put("error", "Unauthorized access! Please log in.");
  178. return response;
  179. }
  180. String usrRole = (String) session.getAttribute("tap_usrRole");
  181. if (usrRole == null) {
  182. usrRole = (String) session.getAttribute("usrRole");
  183. }
  184. if (!"ADMIN".equalsIgnoreCase(usrRole)) {
  185. response.put("success", false);
  186. response.put("error", "Access denied! Only admins can delete records.");
  187. return response;
  188. }
  189. try {
  190. boolean deleted = tapalService.deleteTapal(id, remarks, username);
  191. response.put("success", deleted);
  192. if (!deleted) {
  193. response.put("error", "Failed to delete record.");
  194. }
  195. } catch (Exception e) {
  196. response.put("success", false);
  197. response.put("error", "Error: " + e.getMessage());
  198. }
  199. return response;
  200. }
  201. @PostMapping("/api/upload-file")
  202. @ResponseBody
  203. public Map<String, Object> uploadFile(
  204. @RequestParam("file") MultipartFile file,
  205. @RequestParam(value = "type", required = false, defaultValue = "tapal") String type,
  206. HttpSession session) {
  207. Map<String, Object> response = new HashMap<>();
  208. String username = (String) session.getAttribute("tap_usrName");
  209. if (username == null) {
  210. username = (String) session.getAttribute("usrName");
  211. }
  212. if (username == null) {
  213. response.put("success", false);
  214. response.put("error", "Unauthorized access! Please log in.");
  215. return response;
  216. }
  217. String usrRole = (String) session.getAttribute("tap_usrRole");
  218. if (usrRole == null) {
  219. usrRole = (String) session.getAttribute("usrRole");
  220. }
  221. if (!"ADMIN".equalsIgnoreCase(usrRole)) {
  222. response.put("success", false);
  223. response.put("error", "Access denied! Only admins can upload files.");
  224. return response;
  225. }
  226. if (file.isEmpty()) {
  227. response.put("success", false);
  228. response.put("error", "Please select a file to upload.");
  229. return response;
  230. }
  231. try {
  232. File parentDir = new File(tapalUploadPathConfig).getAbsoluteFile();
  233. File destDir = parentDir;
  234. String webPathPrefix = "/gcctapalfiles/";
  235. if ("oc".equalsIgnoreCase(type)) {
  236. destDir = new File(parentDir, "oc");
  237. webPathPrefix = "/gcctapalfiles/oc/";
  238. }
  239. if (!destDir.exists()) {
  240. destDir.mkdirs();
  241. }
  242. String originalFilename = file.getOriginalFilename();
  243. String nameWithoutExt = type;
  244. String ext = ".pdf";
  245. if (originalFilename != null && originalFilename.contains(".")) {
  246. int dotIdx = originalFilename.lastIndexOf(".");
  247. nameWithoutExt = originalFilename.substring(0, dotIdx);
  248. ext = originalFilename.substring(dotIdx);
  249. }
  250. nameWithoutExt = nameWithoutExt.replaceAll("[^a-zA-Z0-9-_]", "_");
  251. String savedFilename = nameWithoutExt + "_" + System.currentTimeMillis() + ext;
  252. File destFile = new File(destDir, savedFilename).getAbsoluteFile();
  253. file.transferTo(destFile);
  254. String fileWebUrl = webPathPrefix + savedFilename;
  255. response.put("success", true);
  256. response.put("filePath", fileWebUrl);
  257. } catch (Exception e) {
  258. response.put("success", false);
  259. response.put("error", "File upload failed: " + e.getMessage());
  260. }
  261. return response;
  262. }
  263. @GetMapping("/api/categories")
  264. @ResponseBody
  265. public List<Map<String, Object>> getActiveCategories() {
  266. return tapalService.getActiveCategories();
  267. }
  268. @GetMapping("/api/statuslist")
  269. @ResponseBody
  270. public List<Map<String, Object>> getStatusList() {
  271. return tapalService.getActiveStatuses();
  272. }
  273. @GetMapping("/api/history")
  274. @ResponseBody
  275. public ResponseEntity<List<Map<String, Object>>> getHistory(
  276. @RequestParam("tapalNo") String tapalNo,
  277. HttpSession session) {
  278. String username = (String) session.getAttribute("tap_usrName");
  279. if (username == null) {
  280. username = (String) session.getAttribute("usrName");
  281. }
  282. if (username == null) {
  283. return ResponseEntity.status(401).body(null);
  284. }
  285. return ResponseEntity.ok()
  286. .cacheControl(CacheControl.noCache().mustRevalidate())
  287. .body(tapalService.getHistory(tapalNo));
  288. }
  289. @GetMapping("/api/deleted-list")
  290. @ResponseBody
  291. public ResponseEntity<List<Map<String, Object>>> getDeletedList(HttpSession session) {
  292. String username = (String) session.getAttribute("tap_usrName");
  293. if (username == null) {
  294. username = (String) session.getAttribute("usrName");
  295. }
  296. if (username == null) {
  297. return ResponseEntity.status(401).body(null);
  298. }
  299. return ResponseEntity.ok()
  300. .cacheControl(CacheControl.noCache().mustRevalidate())
  301. .body(tapalService.getDeletedTapals());
  302. }
  303. @PostMapping("/api/restore")
  304. @ResponseBody
  305. public Map<String, Object> restoreDetail(
  306. @RequestParam("id") int id,
  307. HttpSession session) {
  308. Map<String, Object> response = new HashMap<>();
  309. String username = (String) session.getAttribute("tap_usrName");
  310. if (username == null) {
  311. username = (String) session.getAttribute("usrName");
  312. }
  313. if (username == null) {
  314. response.put("success", false);
  315. response.put("error", "Unauthorized access! Please log in.");
  316. return response;
  317. }
  318. String usrRole = (String) session.getAttribute("tap_usrRole");
  319. if (usrRole == null) {
  320. usrRole = (String) session.getAttribute("usrRole");
  321. }
  322. if (!"ADMIN".equalsIgnoreCase(usrRole)) {
  323. response.put("success", false);
  324. response.put("error", "Access denied! Only admins can restore records.");
  325. return response;
  326. }
  327. try {
  328. boolean restored = tapalService.restoreTapal(id, username);
  329. response.put("success", restored);
  330. if (!restored) {
  331. response.put("error", "Failed to restore record.");
  332. }
  333. } catch (Exception e) {
  334. response.put("success", false);
  335. response.put("error", "Error: " + e.getMessage());
  336. }
  337. return response;
  338. }
  339. }