From 3078f03ac3034acb04831fc4e36fa22899753c3d Mon Sep 17 00:00:00 2001 From: "lucas.taia" Date: Fri, 3 Feb 2023 17:56:20 -0300 Subject: [PATCH] fixes bug #AL-2144 --- pom.xml | 2 +- .../constantes/CustomEnumTypeHibernate.java | 37 ++++++++++++++++--- .../ventaboletos/entidad/FormaPago.java | 3 +- .../ventaboletos/enums/TipoFormapago.java | 30 +++------------ 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/pom.xml b/pom.xml index 12e7496d7..14fa97ddf 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 br.com.rjconsultores ModelWeb - 1.0.30 + 1.0.31 rj-releases diff --git a/src/com/rjconsultores/ventaboletos/constantes/CustomEnumTypeHibernate.java b/src/com/rjconsultores/ventaboletos/constantes/CustomEnumTypeHibernate.java index b202c5748..9ad47c0d0 100644 --- a/src/com/rjconsultores/ventaboletos/constantes/CustomEnumTypeHibernate.java +++ b/src/com/rjconsultores/ventaboletos/constantes/CustomEnumTypeHibernate.java @@ -1,6 +1,7 @@ package com.rjconsultores.ventaboletos.constantes; import java.io.Serializable; +import java.lang.reflect.Method; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -45,14 +46,32 @@ public class CustomEnumTypeHibernate implements UserType, ParameterizedType { return x.hashCode(); } - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { Object result = null; try { String name = rs.getString(names[0]); if (!rs.wasNull()) { - result = Enum.valueOf(returnedClass(), name); + boolean enumName = Boolean.valueOf(parameters.get("enumName").toString()); + int sqlType = Integer.valueOf(parameters.get("sqlType").toString()); + if (enumName) { + result = Enum.valueOf(returnedClass(), name); + } else { + Class typeClass = null; + Object parameterType = null; + if (Types.INTEGER == sqlType) { + typeClass = Integer.class; + parameterType = Integer.valueOf(name); + } else if (Types.VARCHAR == sqlType) { + typeClass = String.class; + parameterType = name; + } + Method enumMethod = returnedClass().getMethod(parameters.get("enumMethod").toString(), + new Class[] { typeClass }); + result = enumMethod.invoke(returnedClass(), new Object[] { parameterType }); + } + } } catch (Exception e) { result = null; @@ -74,23 +93,29 @@ public class CustomEnumTypeHibernate implements UserType, ParameterizedType { } @Override + @SuppressWarnings("rawtypes") public Object deepCopy(Object value) throws HibernateException { - return value; + if (value == null) + return null; + else { + Enum enumerator = (Enum) value; + return enumerator; + } } @Override public boolean isMutable() { - return true; + return false; } @Override public Serializable disassemble(Object value) throws HibernateException { - return (Serializable) value; + return (value instanceof Serializable) ? (Serializable) deepCopy(value) : null; } @Override public Object assemble(Serializable cached, Object owner) throws HibernateException { - return cached; + return deepCopy(cached); } @Override diff --git a/src/com/rjconsultores/ventaboletos/entidad/FormaPago.java b/src/com/rjconsultores/ventaboletos/entidad/FormaPago.java index 317f1a3ef..612d3ba5b 100644 --- a/src/com/rjconsultores/ventaboletos/entidad/FormaPago.java +++ b/src/com/rjconsultores/ventaboletos/entidad/FormaPago.java @@ -82,7 +82,8 @@ public class FormaPago implements Serializable, Auditavel { @Parameter(name = "type", value = "com.rjconsultores.ventaboletos.constantes.CustomEnumTypeHibernate"), @Parameter(name = "class", value = "com.rjconsultores.ventaboletos.enums.TipoFormapago"), @Parameter(name = "sqlType", value = "4")/*Types.INTEGER*/, - @Parameter(name = "enumName", value = "false")}) + @Parameter(name = "enumName", value = "false"), + @Parameter(name = "enumMethod", value = "getTipoFormapagoByValor")}) @Column(name = "TIPO_PAGO") private com.rjconsultores.ventaboletos.enums.TipoFormapago tipoFormapago; @Column(name = "CVESISTEMA") diff --git a/src/com/rjconsultores/ventaboletos/enums/TipoFormapago.java b/src/com/rjconsultores/ventaboletos/enums/TipoFormapago.java index a50363eda..02b49ed9f 100644 --- a/src/com/rjconsultores/ventaboletos/enums/TipoFormapago.java +++ b/src/com/rjconsultores/ventaboletos/enums/TipoFormapago.java @@ -46,32 +46,12 @@ public enum TipoFormapago { } public static TipoFormapago getTipoFormapagoByValor(Integer valor) { - if(TipoFormapago.DINHEIRO.getValor().equals(valor)) { - return TipoFormapago.DINHEIRO; - } else if(TipoFormapago.DEBITO.getValor().equals(valor)) { - return TipoFormapago.DEBITO; - } else if(TipoFormapago.CREDITO.getValor().equals(valor)) { - return TipoFormapago.CREDITO; - } else if(TipoFormapago.NOTA_CREDITO.getValor().equals(valor)) { - return TipoFormapago.NOTA_CREDITO; - } else if(TipoFormapago.TROCA_PASSAGEM.getValor().equals(valor)) { - return TipoFormapago.TROCA_PASSAGEM; - } else if(TipoFormapago.IMPRESSAO_PASSAGEM.getValor().equals(valor)) { - return TipoFormapago.IMPRESSAO_PASSAGEM; - } else if(TipoFormapago.ORDEM_SERVICO.getValor().equals(valor)) { - return TipoFormapago.ORDEM_SERVICO; - } else if(TipoFormapago.BOLETO_ABERTO.getValor().equals(valor)) { - return TipoFormapago.BOLETO_ABERTO; - } else if(TipoFormapago.PACOTE.getValor().equals(valor)) { - return TipoFormapago.PACOTE; - } else if(TipoFormapago.RESERVA.getValor().equals(valor)) { - return TipoFormapago.RESERVA; - } else if(TipoFormapago.CHEQUE.getValor().equals(valor)) { - return TipoFormapago.CHEQUE; - } else if(TipoFormapago.DEPOSITO.getValor().equals(valor)) { - return TipoFormapago.DEPOSITO; + + for (TipoFormapago tipoFormapago : TipoFormapago.values()) { + if (tipoFormapago.getValor().equals(valor)) { + return tipoFormapago; + } } - return null; }