1
2
3
4
5 package comum.util;
6
7 import java.util.Calendar;
8 import java.util.Iterator;
9
10 import org.apache.log4j.Logger;
11 import org.hibernate.HibernateException;
12 import org.hibernate.cfg.Configuration;
13 import org.hibernate.cfg.Mappings;
14 import org.hibernate.mapping.Column;
15 import org.hibernate.mapping.PrimaryKey;
16 import org.hibernate.mapping.Property;
17 import org.hibernate.mapping.Table;
18 import org.hibernate.type.Type;
19
20 import comum.database.HibernateUtil;
21
22 import ecar.pojo.UsuarioUsu;
23
24
25
26
27
28
29 public class LogBean {
30 public static final String SEPARADOR_CAMPOS_LOG = "#";
31 public static final String SEPARADOR_CAMPOS_DADOS = ",";
32
33 private Logger logger = null;
34
35 private Object obj = null;
36 private UsuarioUsu usuario = null;
37 private String iPUsuario = null;
38 private String operacao = null;
39 private String codigoTransacao = null;
40 private String codigoSessao = null;
41
42
43
44
45
46
47
48
49
50 public String getIPUsuario() {
51 return iPUsuario;
52 }
53
54
55
56
57
58
59
60
61
62 public void setIPUsuario(String usuario) {
63 iPUsuario = usuario;
64 }
65
66
67
68
69
70
71
72
73
74 public Object getObj() {
75 return obj;
76 }
77
78
79
80
81
82
83
84
85
86 public void setObj(Object obj) {
87 this.obj = obj;
88 }
89
90
91
92
93
94
95
96
97
98 public UsuarioUsu getUsuario() {
99 return usuario;
100 }
101
102
103
104
105
106
107
108
109
110 public void setUsuario(UsuarioUsu usuario) {
111 this.usuario = usuario;
112 }
113
114
115
116
117
118
119
120
121 public LogBean() {
122 logger = Logger.getLogger(this.getClass());
123 }
124
125
126
127
128
129
130
131
132
133 public String getOperacao() {
134 return operacao;
135 }
136
137
138
139
140
141
142
143
144
145 public void setOperacao(String operacao) {
146 this.operacao = operacao;
147 }
148
149
150
151
152
153
154
155
156
157 public String getCodigoSessao() {
158 return codigoSessao;
159 }
160
161
162
163
164
165
166
167
168
169 public void setCodigoSessao(String codigoSessao) {
170 this.codigoSessao = codigoSessao;
171 }
172
173
174
175
176
177
178
179
180
181 public String getCodigoTransacao() {
182 return codigoTransacao;
183 }
184
185
186
187
188
189
190
191
192
193 public void setCodigoTransacao(String codigoTransacao) {
194 this.codigoTransacao = codigoTransacao;
195 }
196
197
198
199
200
201
202
203
204
205
206 public String toString() {
207
208 StringBuffer retorno = new StringBuffer();
209 retorno.append(getUsuario().getCodUsu());
210 retorno.append(SEPARADOR_CAMPOS_LOG);
211 retorno.append(getIPUsuario());
212 retorno.append(SEPARADOR_CAMPOS_LOG);
213 retorno.append(getCodigoTransacao());
214 retorno.append(SEPARADOR_CAMPOS_LOG);
215 retorno.append(getCodigoSessao());
216 retorno.append(SEPARADOR_CAMPOS_LOG);
217 retorno.append(getOperacao());
218 retorno.append(SEPARADOR_CAMPOS_LOG);
219 retorno.append(getTableName(getObj()));
220 retorno.append(SEPARADOR_CAMPOS_LOG);
221 retorno.append(getDados());
222 retorno.append(SEPARADOR_CAMPOS_LOG);
223 retorno.append(Data.parseDate(Data.getDataAtual()));
224
225 return retorno.toString();
226 }
227
228
229
230
231
232
233
234
235
236 private String getDados() {
237 return getColunas(getObj());
238 }
239
240
241
242
243
244
245
246
247
248
249 public static String getHoraAtual(boolean comSeparador) {
250 Calendar cal = Calendar.getInstance();
251
252 StringBuffer hh = new StringBuffer();
253 StringBuffer mm = new StringBuffer();
254 StringBuffer ss = new StringBuffer();
255 hh.append(cal.get(Calendar.HOUR_OF_DAY));
256 mm.append(cal.get(Calendar.MINUTE));
257 ss.append(cal.get(Calendar.SECOND));
258
259 if(mm.length() < 2)
260 mm.insert(0, "0");
261 if(hh.length() < 2)
262 hh.insert(0, "0");
263 if(ss.length() < 2)
264 ss.insert(0, "0");
265
266 if(comSeparador) {
267 return (hh.append(":").append(mm).append(":").append(ss)).toString();
268 }
269 else {
270 return (hh.append(mm).append(ss)).toString();
271 }
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286 private String getColunas(Object o) {
287 StringBuffer r = new StringBuffer();
288
289 r.append(getPkName(o)).append(SEPARADOR_CAMPOS_DADOS);
290
291
292 Configuration conf = HibernateUtil.getConfiguration();
293 Mappings map = conf.createMappings();
294
295 Iterator<?> itr = map.getClass(o.getClass().getName()).getPropertyIterator();
296 while (itr.hasNext()) {
297 Property prop = (Property) itr.next();
298
299 String colname = prop.getName();
300
301 String colvalue = null;
302 Type t = prop.getType();
303
304 if("java.util.Set".equals(t.getReturnedClass().getName())) {
305 continue;
306 }
307
308 try{
309 Object v = Util.invocaGet(o, colname);
310 boolean mostrarIgual = true;
311
312 if(v != null) {
313
314 if(v instanceof java.util.Date) {
315 colvalue = Data.parseDate((java.util.Date)v);
316 }
317
318 else if(t.getReturnedClass().getName().indexOf("ecar.pojo.") > -1) {
319 colvalue = getPkName(v);
320 mostrarIgual = false;
321 }
322 else {
323 colvalue = v.toString();
324 }
325 }
326
327 if(mostrarIgual) {
328 r.append(colname.toUpperCase()).append("=").append(colvalue);
329 }
330 else {
331 r.append(colvalue);
332 }
333 r.append(SEPARADOR_CAMPOS_DADOS);
334
335 }catch(Exception e){
336 logger.error(e);
337 }
338 }
339 r.delete(r.length() - SEPARADOR_CAMPOS_DADOS.length(), r.length());
340
341 return r.toString();
342 }
343
344
345
346
347
348
349
350
351
352
353 private String getPkName(Object o) {
354 StringBuffer r = new StringBuffer();
355 Configuration conf = HibernateUtil.getConfiguration();
356 Mappings map = conf.createMappings();
357 Table tab = map.getClass(o.getClass().getName()).getTable();
358 PrimaryKey pk = tab.getPrimaryKey();
359 Iterator<?> itr = pk.getColumnIterator();
360 while (itr.hasNext()) {
361 Column col = (Column) itr.next();
362 String colname = col.getName();
363 String value = "";
364 try {
365 value = conf.getClassMapping(o.getClass().getName())
366 .getIdentifierProperty().getGetter(o.getClass()).get(o)
367 .toString();
368 } catch (HibernateException e) {
369 logger.error(e);
370 }
371 r.append(colname.toUpperCase()).append("=").append(value);
372 r.append(SEPARADOR_CAMPOS_DADOS);
373 }
374 r.delete(r.length() - SEPARADOR_CAMPOS_DADOS.length(), r.length());
375
376 return r.toString();
377 }
378
379
380
381
382
383
384
385
386
387
388 private String getTableName(Object o) {
389 Configuration conf = HibernateUtil.getConfiguration();
390 Mappings map = conf.createMappings();
391 Table tab = map.getClass(o.getClass().getName()).getTable();
392 String tabela2 = tab.getName();
393
394 return tabela2;
395 }
396 }