5aee716d |
1 | /* |
2 | * To change this template, choose Tools | Templates |
3 | * and open the template in the editor. |
4 | */ |
5 | package org.ufcspa.simdecs.comunicacao; |
6 | |
5aee716d |
7 | import java.io.PrintWriter; |
2f5d1c39 |
8 | import java.util.Date; |
63846778 |
9 | import java.util.Iterator; |
f6b96b40 |
10 | import javax.persistence.EntityManager; |
5aee716d |
11 | import javax.servlet.http.HttpServletRequest; |
12 | import javax.servlet.http.HttpServletResponse; |
2f5d1c39 |
13 | import org.ufcspa.simdecs.entities.*; |
5aee716d |
14 | |
15 | /** |
16 | * |
17 | * @author maroni |
18 | */ |
cff9bc95 |
19 | public class MetodoGravaLog extends BaseComunicacao { |
2f5d1c39 |
20 | |
21 | private long getIdFromUID(String pUID, String prefixo) { |
22 | String id = pUID.replaceAll(prefixo, ""); |
23 | id = id.substring(0, id.indexOf(":")); |
24 | return Long.parseLong(id); |
25 | } |
26 | |
27 | private long getIdPacienteFromUID(String pUID) { |
28 | String id = pUID.substring(pUID.indexOf(":paciente-") + ":paciente-".length()); |
29 | return Long.parseLong(id); |
30 | } |
31 | |
63846778 |
32 | |
33 | private void registraLog(EntityManager em, SessaoUsuario sessaoUsuario, String etapa, Nodo nodo, Paciente paciente, boolean desmarcado, String tipoEvento) { |
34 | |
2f5d1c39 |
35 | |
63846778 |
36 | if (etapa.equals(Log.ETAPA_INVESTIGACAO)) { |
37 | Iterator itLogs = em.createQuery("From Log where sessaoUsuario.id=:pIdSessao and paciente.id=:pIdPaciente and etapa=:pEtapa and tipo.id=:pTipo and (nodo.id=:pIdNodo or :pIdNodo is null) and desmarcado=:pDesmarcado") |
38 | .setParameter("pIdSessao", sessaoUsuario.getId()) |
39 | .setParameter("pIdPaciente", paciente.getId()) |
40 | .setParameter("pEtapa", etapa) |
41 | .setParameter("pIdNodo", nodo.getId()) |
42 | .setParameter("pDesmarcado", desmarcado) |
43 | .setParameter("pTipo", tipoEvento) |
44 | .getResultList().iterator(); |
45 | |
46 | while (itLogs.hasNext()) { |
47 | return; |
48 | } |
49 | } |
2f5d1c39 |
50 | |
51 | if (!em.getTransaction().isActive()) |
52 | em.getTransaction().begin(); |
53 | |
54 | Log log = new Log(); |
55 | log.setSessaoUsuario(sessaoUsuario); |
56 | log.setCaso(sessaoUsuario.getCaso()); |
57 | log.setData(new Date()); |
63846778 |
58 | log.setEtapa(etapa); |
59 | log.setNodo(nodo); |
2f5d1c39 |
60 | log.setDesmarcado(desmarcado); |
63846778 |
61 | log.setPaciente(paciente); |
62 | log.setRede(paciente.getRede()); |
63 | log.setTipo(em.find(TipoEventoLog.class, tipoEvento)); |
2f5d1c39 |
64 | em.persist(log); |
65 | |
66 | em.getTransaction().commit(); |
63846778 |
67 | |
68 | } |
69 | |
70 | private String registraLog(EntityManager em, SessaoUsuario sessaoUsuario, String pUID, boolean desmarcado) { |
71 | |
72 | String taticaPedagogica=null; |
73 | |
74 | // Sintoma vindo de NodoPaciente |
75 | if (pUID.toLowerCase().startsWith("sintoma-np-")) { |
76 | NodoPaciente nodoPaciente = em.find(NodoPaciente.class, getIdFromUID(pUID, "sintoma-np-")); |
77 | |
78 | registraLog(em, sessaoUsuario, Log.ETAPA_INVESTIGACAO, nodoPaciente.getNodo(), nodoPaciente.getPaciente(), desmarcado, TipoEventoLog.PERGUNTA_SINAL_SINTOMA); |
79 | |
25aea581 |
80 | if (nodoPaciente.getNodo().getTaticaPedagogica() != null && nodoPaciente.getNodo().getTaticaPedagogica() != "") |
81 | taticaPedagogica = nodoPaciente.getNodo().getTaticaPedagogica(); |
2f5d1c39 |
82 | } |
83 | |
84 | // Sintoma vindo de Nodo |
85 | if (pUID.toLowerCase().startsWith("sintoma-nd-")) { |
86 | Nodo nodo = em.find(Nodo.class, getIdFromUID(pUID, "sintoma-nd-")); |
87 | Paciente paciente = em.find(Paciente.class, getIdPacienteFromUID(pUID)); |
88 | |
63846778 |
89 | registraLog(em, sessaoUsuario, Log.ETAPA_INVESTIGACAO, nodo, paciente, desmarcado, TipoEventoLog.PERGUNTA_SINAL_SINTOMA); |
2f5d1c39 |
90 | |
25aea581 |
91 | if (nodo.getTaticaPedagogica() != null && nodo.getTaticaPedagogica() != "") |
92 | taticaPedagogica = nodo.getTaticaPedagogica(); |
2f5d1c39 |
93 | } |
94 | |
95 | // Exame físico vindo de NodoPaciente |
96 | if (pUID.toLowerCase().startsWith("exame-fisico-np-")) { |
97 | NodoPaciente nodoPaciente = em.find(NodoPaciente.class, getIdFromUID(pUID, "exame-fisico-np-")); |
98 | |
63846778 |
99 | registraLog(em, sessaoUsuario, Log.ETAPA_INVESTIGACAO, nodoPaciente.getNodo(), nodoPaciente.getPaciente(), desmarcado, TipoEventoLog.SOLICITA_EXAME_FISICO); |
25aea581 |
100 | |
101 | if (nodoPaciente.getNodo().getTaticaPedagogica() != null && nodoPaciente.getNodo().getTaticaPedagogica() != "") |
102 | taticaPedagogica = nodoPaciente.getNodo().getTaticaPedagogica(); |
2f5d1c39 |
103 | |
104 | } |
105 | |
106 | // Exame complementar vindo de NodoPaciente |
107 | if (pUID.toLowerCase().startsWith("exame-complementar-np-")) { |
108 | NodoPaciente nodoPaciente = em.find(NodoPaciente.class, getIdFromUID(pUID, "exame-complementar-np-")); |
109 | |
63846778 |
110 | registraLog(em, sessaoUsuario, Log.ETAPA_INVESTIGACAO, nodoPaciente.getNodo(), nodoPaciente.getPaciente(), desmarcado, TipoEventoLog.SOLICITA_EXAME_COMPLEMENTAR); |
25aea581 |
111 | |
112 | if (nodoPaciente.getNodo().getTaticaPedagogica() != null && nodoPaciente.getNodo().getTaticaPedagogica() != "") |
113 | taticaPedagogica = nodoPaciente.getNodo().getTaticaPedagogica(); |
2f5d1c39 |
114 | } |
2f5d1c39 |
115 | // Historico médico |
116 | if (pUID.toLowerCase().startsWith("historico-medico-")) { |
117 | Paciente paciente = em.find(Paciente.class, getIdPacienteFromUID(pUID)); |
118 | |
63846778 |
119 | registraLog(em, sessaoUsuario, Log.ETAPA_INVESTIGACAO, null, paciente, desmarcado, TipoEventoLog.PERGUNTA_HISTORICO); |
2f5d1c39 |
120 | } |
121 | |
122 | // Diagnostico |
123 | if (pUID.toLowerCase().startsWith("diagnostico-")) { |
cff9bc95 |
124 | Nodo nodo = em.find(Nodo.class, getIdFromUID(pUID, "diagnostico-")); |
125 | Paciente paciente = em.find(Paciente.class, getIdPacienteFromUID(pUID)); |
2f5d1c39 |
126 | |
63846778 |
127 | registraLog(em, sessaoUsuario, Log.ETAPA_DIAGNOSTICO, nodo, paciente, desmarcado, TipoEventoLog.SELECIONA_DIAGNOSTICO); |
2f5d1c39 |
128 | |
25aea581 |
129 | if (nodo.getTaticaPedagogica() != null && nodo.getTaticaPedagogica() != "") |
130 | taticaPedagogica = nodo.getTaticaPedagogica(); |
2f5d1c39 |
131 | } |
132 | |
133 | // Conduta |
134 | if (pUID.toLowerCase().startsWith("conduta-")) { |
cff9bc95 |
135 | Nodo nodo = em.find(Nodo.class, getIdFromUID(pUID, "conduta-")); |
136 | Paciente paciente = em.find(Paciente.class, getIdPacienteFromUID(pUID)); |
2f5d1c39 |
137 | |
63846778 |
138 | registraLog(em, sessaoUsuario, Log.ETAPA_CONDUTA, nodo, paciente, desmarcado, TipoEventoLog.SELECIONA_CONDUTA); |
2f5d1c39 |
139 | |
25aea581 |
140 | if (nodo.getTaticaPedagogica() != null && nodo.getTaticaPedagogica() != "") |
141 | taticaPedagogica = nodo.getTaticaPedagogica(); |
2f5d1c39 |
142 | } |
143 | |
25aea581 |
144 | return taticaPedagogica; |
2f5d1c39 |
145 | } |
cff9bc95 |
146 | |
147 | @Override |
148 | protected void executaServico(EntityManager em, HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws Exception |
149 | { |
5aee716d |
150 | response.setContentType("text/html;charset=UTF-8"); |
cff9bc95 |
151 | response.setContentType("text/xml"); |
152 | response.setHeader("Cache-Control", "no-cache"); |
f6b96b40 |
153 | |
cff9bc95 |
154 | long pIdSessao = Long.parseLong(request.getParameter("idsessao")); |
155 | String pUID = request.getParameter("uid"); |
156 | String pDesmarcado = request.getParameter("desmarcado"); |
157 | boolean desmarcado = false; |
f6b96b40 |
158 | |
25aea581 |
159 | if (pDesmarcado != null && pDesmarcado.equals("true")) { |
cff9bc95 |
160 | desmarcado = true; |
161 | } |
2f5d1c39 |
162 | |
cff9bc95 |
163 | SessaoUsuario sessaoUsuario = em.find(SessaoUsuario.class, pIdSessao); |
164 | if (sessaoUsuario == null) { |
165 | return; |
166 | } |
f6b96b40 |
167 | |
25aea581 |
168 | String taticaPedagocica = registraLog(em, sessaoUsuario, pUID, desmarcado); |
88cced02 |
169 | |
cff9bc95 |
170 | out.println("<xml>"); |
171 | |
172 | boolean isPar; |
173 | if (Integer.parseInt(pUID.substring(pUID.length() - 1)) % 2 == 0) { |
174 | isPar = true; |
175 | } else { |
176 | isPar = false; |
5aee716d |
177 | } |
5aee716d |
178 | |
5aee716d |
179 | |
cff9bc95 |
180 | //if (isPar) |
181 | // out.println(" <resposta_acao_mediador acao=\"sim\" problema=\"PROBLEMA ORIGINADO PELO MEDIADOR\" mensagem=\"MENSAGEM A CADA ACAO VINDA DO MEDIADOR\" />"); |
182 | //else |
25aea581 |
183 | if (taticaPedagocica != null && !taticaPedagocica.equals("")) |
e1005ea9 |
184 | out.println(" <resposta_acao_mediador acao=\"sim\" problema=\"Problema\" mensagem=\"" + clear(taticaPedagocica) + "\" />"); |
25aea581 |
185 | else |
186 | out.println(" <resposta_acao_mediador acao=\"nao\" />"); |
187 | |
28042ed9 |
188 | out.println(" <execucao sucesso=\"sim\" />" ); |
5aee716d |
189 | |
cff9bc95 |
190 | out.println("</xml>"); |
191 | } |
5aee716d |
192 | } |