package br.com.centralit.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import br.com.centralit.api.model.AnexoGed;
import br.com.centralit.api.service.AnexoGedService;
import br.com.centralit.framework.controller.GenericController;
import br.com.centralit.framework.service.arquitetura.GedFileService;
/**
*
*
*
*
*
* Company: Central IT - Governança Corporativa -
*
*
*
* Title:
*
*
*
* Description:
*
*
*
* Iniciativa(s): NUMERO_INICIATIVA
*
*
*
* Regra(s) de negócio: NUMERO_REGRA_DE_NEGOCIO
*
*
* @since 16/12/2015 - 15:42:02
*
* @version 1.0.0
*
* @author rogerio.costa
*
*/
@Controller
@RequestMapping("/rest/anexoGed")
public class AnexoGedController extends GenericController {
/** Atributo anexoGedService. */
private AnexoGedService anexoGedService;
@Autowired
private GedFileService gedFileService;
/**
* Responsável pela criação de novas instâncias desta classe.
*
* @param anexoGedService
*/
@Autowired
public AnexoGedController( AnexoGedService anexoGedService ) {
super(anexoGedService);
this.anexoGedService = anexoGedService;
}
/**
* Método responsável por realizar o upload dos anexos que estão ligadas a entidade documento
*
* @author rogerio.costa
*
* @param name
* @param file
* @param idAnexo
*/
@RequestMapping(value = "/uploadAnexo", method = RequestMethod.POST)
public @ResponseBody
void uploadAnexo(@RequestParam(value = "filename", required = false) String name, @RequestParam("file") MultipartFile file, @RequestParam(value = "idDocumento", required = false) Long idDocumento) {
this.anexoGedService.saveAnexo(file, idDocumento);
}
/**
*
* Método responsável por visualizar anexo
*
* @author rogerio.costa
*
* @param idAnexo
* @param response
* @return
*/
@RequestMapping(value = "/visualizar", method = RequestMethod.GET)
public @ResponseBody
ResponseEntity visualizar(@RequestParam(value = "idAnexo") Long idAnexo, HttpServletResponse response) {
AnexoGed anexo = this.anexoGedService.find(idAnexo);
HttpHeaders headers = new HttpHeaders();
headers.add("content-disposition", "inline;filename=" + anexo.getDescricao());
headers.setContentType(MediaType.parseMediaType(anexo.getDominioTipoAnexo().getDescricao()));
ResponseEntity resp = null;
try {
File file = this.gedFileService.restoreFile(anexo);
file.length();
FileInputStream input = new FileInputStream(file);
resp = new ResponseEntity(IOUtils.toByteArray(input), headers, HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
}