package shst.medicina.fichasclinicas.logic; import java.sql.Timestamp; import java.util.LinkedList; import java.util.List; import java.util.logging.Level; import javax.ws.rs.core.Response; import org.springframework.beans.factory.annotation.Autowired; import shst.medicina.fichasclinicas.beans.DocumentoBean; import shst.medicina.fichasclinicas.beans.DocumentoUploadBean; import shst.medicina.fichasclinicas.beans.EvoMessageBean; import shst.medicina.fichasclinicas.beans.EvoMessageTypeBean; import shst.medicina.fichasclinicas.beans.EvoResponseBean; import shst.medicina.fichasclinicas.data.shst.FilFileData; import shst.medicina.fichasclinicas.data.shst.FilFileTrabalhadorData; import shst.medicina.fichasclinicas.provider.DocumentosDataProvider; import com.evolute.module.utilizadores.data.EvoUsrUserData; import com.evolute.utils.error.ErrorLogger; public class DocumentosLogic { @Autowired(required = true) DocumentosDataProvider documentosProvider; public EvoResponseBean getDocumentosCount(EvoUsrUserData user, Integer trabalhadorID) { EvoResponseBean result = new EvoResponseBean(); if(user != null) { try { Long rst = documentosProvider.getDocumentosCount(trabalhadorID); result.setSuccess(true); result.setData(rst); } catch(Exception ex) { result.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType(EvoMessageTypeBean.ERROR); msg.setMessageData("Erro ao comunicar com a Base de dados da SIPRP!"); result.addMessage(msg); ErrorLogger.logException(ex, Level.SEVERE); } } else { result.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType(EvoMessageTypeBean.SESSIONTIMEOUT); msg.setMessageData("Utilizador asssociado a sess\u00e3o inv\u00e1lida/inexistente!"); result.addMessage(msg); } return result; } public EvoResponseBean> getDocumentosList( EvoUsrUserData user, Integer trabalhadorID) { EvoResponseBean> result = new EvoResponseBean>(); if(user != null) { try { List outSet = new LinkedList(); for(FilFileTrabalhadorData in : documentosProvider.getDocumentosList(trabalhadorID)) { DocumentoBean out = null; if(in.toFile_id() != null) { out = new DocumentoBean(); outSet.add(out); FilFileData doc = in.toFile_id(); out.setId(doc.getId()); out.setDetalhe(doc.getDetails()); out.setTipo(doc.getMime_type()); } if(out != null && in.toCategoria_id() != null) { out.setCategoria(in.toCategoria_id().getDescricao()); } } result.setSuccess(true); result.setData(outSet); } catch(Exception ex) { result.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType(EvoMessageTypeBean.ERROR); msg.setMessageData("Erro ao comunicar com a Base de dados da SIPRP!"); result.addMessage(msg); ErrorLogger.logException(ex, Level.SEVERE); } } else { result.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType(EvoMessageTypeBean.SESSIONTIMEOUT); msg.setMessageData("Utilizador asssociado a sess\u00e3o inv\u00e1lida/inexistente!"); result.addMessage(msg); } return result; } public Response getDocumentoAsResponse(EvoUsrUserData user, Integer documentoID) { Response response; if(user != null) { try { FilFileData doc = documentosProvider.getDocumento(documentoID); if(doc != null) { response = Response.ok(doc.getFile_data(), doc.getMime_type()).header("Content-Disposition", "attachment; filename=\""+doc.getName()+"\"").build(); } else { response = Response.status(Response.Status.NOT_FOUND).build(); } } catch(Exception ex) { response = Response.serverError().build(); ErrorLogger.logException(ex, Level.SEVERE); } } else { response = Response.status(Response.Status.UNAUTHORIZED).build(); } return response; } public EvoResponseBean saveDocumento( EvoUsrUserData user, DocumentoUploadBean documentoUploadBean ) { EvoResponseBean result = new EvoResponseBean(); if( user != null ) { try { System.out.println(documentoUploadBean.getTrabalhadorId()); if( documentosProvider.trabalhadorExists( documentoUploadBean.getTrabalhadorId() ) ) { documentosProvider.saveDocumento( documentoUploadBean ); result.setData( true ); result.setSuccess(true); EvoMessageBean msg = new EvoMessageBean(); msg.setType( EvoMessageTypeBean.INFO ); msg.setMessageData( "Upload bem sucedido" ); result.addMessage( msg ); } else { result.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType( EvoMessageTypeBean.ERROR ); msg.setMessageData( "Erro ao realizar a opera\u00e7\u00e3o!" ); result.addMessage( msg ); } } catch( Exception ex ) { result.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType(EvoMessageTypeBean.ERROR); msg.setMessageData("Erro ao comunicar com a Base de dados da SIPRP!"); result.addMessage(msg); ErrorLogger.logException(ex, Level.SEVERE); } } else { result.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType(EvoMessageTypeBean.SESSIONTIMEOUT); msg.setMessageData("Utilizador asssociado a sess\u00e3o inv\u00e1lida/inexistente!"); result.addMessage(msg); } return result; } public EvoResponseBean< Boolean > removerDocumento( EvoUsrUserData user, Integer documentoID ) { EvoResponseBean responseBean = new EvoResponseBean(); if( user != null ) { try { FilFileData documento = documentosProvider.getDocumento( documentoID ); if( documento != null ) { documento.setDeleted_stamp( new Timestamp( System.currentTimeMillis() ) ); documento.save(); responseBean.setData( true ); responseBean.setSuccess(true); EvoMessageBean msg = new EvoMessageBean(); msg.setType( EvoMessageTypeBean.INFO ); msg.setMessageData( "Remo\u00e7\u00e3o do documento bem sucedida" ); responseBean.addMessage( msg ); } else { responseBean.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType(EvoMessageTypeBean.ERROR); msg.setMessageData("O documento dado n\u00e3o \u00e9 v\u00e1lido."); responseBean.addMessage(msg); } } catch( Exception ex ) { responseBean.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType(EvoMessageTypeBean.ERROR); msg.setMessageData("Erro ao comunicar com a Base de dados da SIPRP!"); responseBean.addMessage(msg); ErrorLogger.logException(ex, Level.SEVERE); } } else { responseBean.setSuccess(false); EvoMessageBean msg = new EvoMessageBean(); msg.setType(EvoMessageTypeBean.SESSIONTIMEOUT); msg.setMessageData("Utilizador asssociado a sess\u00e3o inv\u00e1lida/inexistente!"); responseBean.addMessage(msg); } return responseBean; } }