Summary Table

Categories Total Count
PII 0
URL 0
DNS 1
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

package gov.va.med.ewv.controller;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletContext;
import javax.validation.Valid;

import org.apache.commons.io.FilenameUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import gov.va.med.ars.constants.ErrorMessages;
import gov.va.med.ars.exceptions.GenericException;
import gov.va.med.ewv.bean.ArchiveFileRequest;
import gov.va.med.ewv.bean.FileUploadRequest;
import gov.va.med.ewv.model.response.EwdPdfReferencesFileArchiveResponse;
import gov.va.med.ewv.model.response.EwdPdfReferencesFileUploadResponse;
import gov.va.med.ewv.model.response.EwvClaimDetailResponse;
import gov.va.med.ewv.model.response.EwvPdfReferencesResponse;
import gov.va.med.ewv.service.IEwvClaimService;
import gov.va.med.ewv.service.IEwvPdfReferenceService;

/**
* RESTFul Web Services Controller that handles the RESTFul Web Services requested pertaining to
* EWV information.
*
* @author
DNS
*
*/
@RestController
@RequestMapping("api/v1")
public class EwvClaimController {


private static final Logger logger = LogManager.getLogger(EwvClaimController.class);

@Autowired
private IEwvClaimService ewvClaimService;

@Autowired
private IEwvPdfReferenceService ewvPdfReferenceService;

@Autowired
ServletContext context;

/**
* RESTFul Web Service that returns back Claim information associated the the given
* Claim "pdiNumber" Number. Request and Response Body is JSON formatted string message.
*
* @param pdiNumber - Claim Number that uniquely identifies a claim
*
* @return - JSON formatted response of an EwvClaimDetailResponse Java class instance
*
* @throws GenericException - thrown if some type of error is encountered
*/
@GetMapping(value = "/ewvClaim/{pdiNumber}")
public ResponseEntity<?> getDetail(@PathVariable("pdiNumber") String pdiNumber) throws GenericException {

logger.info("Invoked the controller to retrieve the claim details for pdiNumber : " + pdiNumber);
EwvClaimDetailResponse ewvClaim = null;

try {
ewvClaim = ewvClaimService.getEwvClaim(pdiNumber);
} catch(GenericException e) {
logger.error("Exception occured for pdiNumber : "+ pdiNumber);
throw new GenericException("ERROR", e.getErrorDescription(), HttpStatus.OK);
}

return ResponseEntity.ok().body(ewvClaim);
}


/**
* RESTful Web Service that returns back all the PDF References information that is in the
* database. Response Body is a JSON formatted string message.
*
* @return - JSON formatted response of an EwvPdfReferencesResponse Java class instance
*
* @throws GenericException - thrown if some type of error is encountered
*/
@GetMapping(value = "/pdfReference/getAllPdfReferences")
public ResponseEntity<?> getAllPdfReferences() throws GenericException {

logger.info("Invoked the controller to retrieve all the PDF References: Inside \"getAllPdfReferences\" method");
EwvPdfReferencesResponse pdfReferencesResponse = null;

try {
pdfReferencesResponse = ewvPdfReferenceService.getAllPdfReferences();
} catch(GenericException e) {
logger.error("Exception occured when calling the \"getAllPdfReferences\" method");
throw new GenericException("ERROR", e.getErrorDescription(), HttpStatus.OK);
}

return ResponseEntity.ok().body(pdfReferencesResponse);
}

/**
* RESTFul Web Service that marks one or more PDF References to the Archived state. Request and Response Body is JSON formatted string message.
*
* @param archiveFileRequest - instance of ArchiveFileRequest Java class that is fed into the Request Body as a JSON formatted string
*
* @return - JSON formatted response of an EwvClaimDetailResponse Java class instance; it will show which PDF References have been placed
* successfully in the Archived state and which have not. In those instances that are unsuccessful, the reason for not being
* successful will be included in the response
*
* @throws GenericException - thrown if some type of error is encountered
*/
@RequestMapping(value = "/filearchive", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<?> uploadArchiveFlag(@RequestBody ArchiveFileRequest archiveFileRequest) throws GenericException {
//String result = null;
// List<ArchivedPdfReference> archivedPdfReferences;
EwdPdfReferencesFileArchiveResponse result = null;

try {
// System.out.println(archiveFileRequest.toString());
result = ewvPdfReferenceService.updateUploadedFiles(archiveFileRequest.getArchivedPdfReferences());
}
// Here Catch IOException only.
// Other Exceptions catch by RestGlobalExceptionHandler class.
catch (Exception e) {
e.printStackTrace();
// return new ResponseEntity<>("Error: " + e.getMessage(), HttpStatus.BAD_REQUEST);
throw new GenericException("ERROR", e.getMessage(), HttpStatus.OK);
}

// return new ResponseEntity<String>("Updated archive flag " , HttpStatus.OK);

// The below cannot be used, because it will not return back a JSON-formatted response!
// return ResponseEntity.ok().body(result.toString().replaceAll("\\\\", ""));

return ResponseEntity.ok().body(result);
}


/**
* RESTFul Web Service that uploads a list of files into the server file system as well as insert PDF References of those files into
* the database. The Input to the RESTFul Web Service will be of Content Type "multipart/*" (or more likely "multipart/form-data")
* where the Body of the Request had multiple parts. The Response Body is a JSON formatted string message.
*
* @param fileUploadRequest - instance of a FileUploadRequest that is fed into the Request Body as a "multipart/*" format
*
* @return - JSON formatted response of an EwdPdfReferencesFileUploadResponse Java class instance; it will show which Files
* have had their contents stored successfully into the server file system as well as their PDF Reference information
* in the database and which have not. In those instances that are unsuccessful, the reason for not being
* successful will be included in the response
*
* @throws GenericException - thrown if some type of error is encountered
*/
@RequestMapping(value = "/fileupload", headers=("content-type=multipart/*"), method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<?> uploadFileMulti(@ModelAttribute FileUploadRequest fileUploadRequest) throws GenericException {

EwdPdfReferencesFileUploadResponse result = null;
//List<ArchivedPdfReference> archivedPdfReferences;

try {
result = ewvPdfReferenceService.saveUploadedFiles(fileUploadRequest.getMultipartFiles());
}
// Here Catch IOException only.
// Other Exceptions catch by RestGlobalExceptionHandler class.
catch (Exception e) {
e.printStackTrace();
// return new ResponseEntity<>("Error: " + e.getMessage(), HttpStatus.BAD_REQUEST);
throw new GenericException("ERROR", e.getMessage(), HttpStatus.BAD_REQUEST);
}

// return new ResponseEntity<String>("Uploaded to: " + result, HttpStatus.OK);

// The below cannot be used, because it will not return back a JSON-formatted response!
// return ResponseEntity.ok().body(result.toString().replaceAll("\\\\", ""));

return ResponseEntity.ok().body(result);
}


@GetMapping("/file/retrieval/{fileId}")
public ResponseEntity<?> fileRetrieval(@Valid @PathVariable("fileId") String fileId) throws GenericException {
String fullPath = ewvPdfReferenceService.getPathForTheAttachment(fileId);
if(fullPath != null) {
File file = new File(fullPath);
InputStreamResource isr = null;
try {
isr = new InputStreamResource(new FileInputStream(file));
} catch (FileNotFoundException e) {
logger.error("Error occured: " , e.getMessage());
}
return ResponseEntity.ok().header("Content-Disposition", FilenameUtils.getName(fullPath)).contentLength(file.length()).contentType(MediaType.valueOf(new MimetypesFileTypeMap().getContentType(file))).body(isr);
} else {
throw new GenericException(ErrorMessages.BAD_REQUEST, "Required Data is missing", HttpStatus.BAD_REQUEST);
}
}

@InitBinder()
public void customizeBinding (WebDataBinder binder) {
binder.setDisallowedFields(new String[]{});
}
}