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.
SIPRP/trunk/SiprpWebFichasClinicas/src/shst/medicina/fichasclinicas/logic/DocumentosLogic.java

244 lines
7.0 KiB

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<Long> getDocumentosCount(EvoUsrUserData user, Integer trabalhadorID)
{
EvoResponseBean<Long> result = new EvoResponseBean<Long>();
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<List<DocumentoBean>> getDocumentosList( EvoUsrUserData user, Integer trabalhadorID)
{
EvoResponseBean<List<DocumentoBean>> result = new EvoResponseBean<List<DocumentoBean>>();
if(user != null)
{
try
{
List<DocumentoBean> outSet = new LinkedList<DocumentoBean>();
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<Boolean> saveDocumento( EvoUsrUserData user, DocumentoUploadBean documentoUploadBean )
{
EvoResponseBean<Boolean> result = new EvoResponseBean<Boolean>();
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<Boolean> responseBean = new EvoResponseBean<Boolean>();
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;
}
}