2121*/
2222package com .iemr .admin .controller .employeemaster ;
2323
24+ import java .net .URLEncoder ;
25+ import java .nio .charset .StandardCharsets ;
2426import java .util .Base64 ;
2527
2628import org .slf4j .Logger ;
2729import org .slf4j .LoggerFactory ;
2830import org .springframework .beans .factory .annotation .Autowired ;
2931import org .springframework .context .annotation .PropertySource ;
32+ import org .springframework .http .ContentDisposition ;
3033import org .springframework .http .HttpHeaders ;
34+ import org .springframework .http .InvalidMediaTypeException ;
3135import org .springframework .http .MediaType ;
3236import org .springframework .http .ResponseEntity ;
33-
37+ import org . springframework . web . bind . annotation . GetMapping ;
3438import org .springframework .web .bind .annotation .PathVariable ;
39+ import org .springframework .web .bind .annotation .PostMapping ;
3540import org .springframework .web .bind .annotation .RequestBody ;
3641import org .springframework .web .bind .annotation .RequestMapping ;
3742import org .springframework .web .bind .annotation .RequestMethod ;
3843import org .springframework .web .bind .annotation .RestController ;
3944
45+ import com .google .gson .JsonObject ;
4046import com .iemr .admin .data .employeemaster .EmployeeSignature ;
4147import com .iemr .admin .service .employeemaster .EmployeeSignatureServiceImpl ;
42- import com .iemr .admin .utils .mapper .InputMapper ;
4348import com .iemr .admin .utils .response .OutputResponse ;
4449
4550import io .swagger .v3 .oas .annotations .Operation ;
51+ import jakarta .servlet .http .HttpServletRequest ;
4652
4753
4854@ PropertySource ("classpath:application.properties" )
@@ -54,12 +60,10 @@ public class EmployeeSignatureController {
5460 @ Autowired
5561 EmployeeSignatureServiceImpl employeeSignatureServiceImpl ;
5662
57- private InputMapper inputMapper = new InputMapper ();
58-
5963 private Logger logger = LoggerFactory .getLogger (this .getClass ().getSimpleName ());
6064
6165 @ Operation (summary = "Upload" )
62- @ RequestMapping (value = "/upload" , headers = "Authorization" , method = { RequestMethod . POST } , produces = {
66+ @ PostMapping (value = "/upload" , headers = "Authorization" , produces = {
6367 "application/json" })
6468 public String uploadFile (@ RequestBody EmployeeSignature emp ) {
6569 OutputResponse response = new OutputResponse ();
@@ -83,22 +87,27 @@ public String uploadFile(@RequestBody EmployeeSignature emp) {
8387 }
8488
8589 @ Operation (summary = "User id" )
86- @ RequestMapping (value = "/{userID}" , headers = "Authorization" , method = { RequestMethod . GET } )
90+ @ GetMapping (value = "/{userID}" , headers = "Authorization" )
8791 public ResponseEntity <byte []> fetchFile (@ PathVariable ("userID" ) Long userID ) throws Exception {
88- OutputResponse response = new OutputResponse ();
8992 logger .debug ("File download for userID" + userID );
9093
9194 try {
9295
9396 EmployeeSignature userSignID = employeeSignatureServiceImpl .fetchSignature (userID );
9497 HttpHeaders responseHeaders = new HttpHeaders ();
95- responseHeaders .set (HttpHeaders .CONTENT_DISPOSITION ,
96- "inline; filename=\" " + userSignID .getFileName () + "\" " );
97- responseHeaders .set ("filename" , userSignID .getFileName ());
98-
99- return ResponseEntity .ok ().contentType (MediaType .parseMediaType (userSignID .getFileType ()))
100- .headers (responseHeaders ).body (userSignID .getSignature ());
101-
98+ ContentDisposition cd = ContentDisposition .attachment ()
99+ .filename (userSignID .getFileName (), StandardCharsets .UTF_8 ).build ();
100+ responseHeaders .setContentDisposition (cd );
101+
102+ MediaType mediaType ;
103+ try {
104+ mediaType = MediaType .parseMediaType (userSignID .getFileType ());
105+ } catch (InvalidMediaTypeException | NullPointerException e ) {
106+ mediaType = MediaType .APPLICATION_OCTET_STREAM ;
107+ }
108+ byte [] fileBytes = userSignID .getSignature (); // MUST be byte[]
109+ return ResponseEntity .ok ().headers (responseHeaders ).contentType (mediaType ).contentLength (fileBytes .length )
110+ .body (fileBytes );
102111 } catch (Exception e ) {
103112 logger .error ("Unexpected error:" , e );
104113 logger .error ("File download for userID failed with exception " + e .getMessage (), e );
@@ -117,7 +126,15 @@ public String existFile(@PathVariable("userID") Long userID) throws Exception {
117126 try {
118127
119128 Boolean userSignID = employeeSignatureServiceImpl .existSignature (userID );
120- response .setResponse (userSignID .toString ());
129+ Boolean signatureActive = employeeSignatureServiceImpl .isSignatureActive (userID );
130+
131+ // Create JSON response with both fields
132+ JsonObject responseData = new JsonObject ();
133+ responseData .addProperty ("response" , userSignID .toString ());
134+ responseData .addProperty ("signStatus" , signatureActive .toString ());
135+
136+ // Set the response (existing setResponse method will handle it)
137+ response .setResponse (responseData .toString ());
121138
122139 } catch (Exception e ) {
123140 logger .error ("Unexpected error:" , e );
@@ -128,4 +145,19 @@ public String existFile(@PathVariable("userID") Long userID) throws Exception {
128145 logger .debug ("response" + response );
129146 return response .toString ();
130147 }
148+
149+ @ Operation (summary = "Active or DeActive user Signature" )
150+ @ PostMapping (value = "/activateOrdeActivateSignature" , headers = "Authorization" , produces = { "application/json" })
151+ public String ActivateUser (@ RequestBody String activateUser , HttpServletRequest request ) {
152+ OutputResponse response = new OutputResponse ();
153+ try {
154+ EmployeeSignature empSignature = employeeSignatureServiceImpl .updateUserSignatureStatus (activateUser );
155+ boolean active = empSignature .getDeleted () == null ? false : !empSignature .getDeleted ();
156+ response .setResponse ("{\" userID\" :" + empSignature .getUserID () + ",\" active\" :" + active + "}" );
157+ } catch (Exception e ) {
158+ logger .error ("Active or Deactivate User Signature failed with exception " + e .getMessage (), e );
159+ response .setError (e );
160+ }
161+ return response .toString ();
162+ }
131163}
0 commit comments