fixes bug #AL-2144

master
lucas.taia 2023-02-03 17:56:20 -03:00
parent 671280488c
commit 3078f03ac3
4 changed files with 39 additions and 33 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>br.com.rjconsultores</groupId> <groupId>br.com.rjconsultores</groupId>
<artifactId>ModelWeb</artifactId> <artifactId>ModelWeb</artifactId>
<version>1.0.30</version> <version>1.0.31</version>
<distributionManagement> <distributionManagement>
<repository> <repository>
<id>rj-releases</id> <id>rj-releases</id>

View File

@ -1,6 +1,7 @@
package com.rjconsultores.ventaboletos.constantes; package com.rjconsultores.ventaboletos.constantes;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -45,14 +46,32 @@ public class CustomEnumTypeHibernate implements UserType, ParameterizedType {
return x.hashCode(); return x.hashCode();
} }
@SuppressWarnings("unchecked") @SuppressWarnings({ "unchecked", "rawtypes" })
@Override @Override
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
Object result = null; Object result = null;
try { try {
String name = rs.getString(names[0]); String name = rs.getString(names[0]);
if (!rs.wasNull()) { 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) { } catch (Exception e) {
result = null; result = null;
@ -74,23 +93,29 @@ public class CustomEnumTypeHibernate implements UserType, ParameterizedType {
} }
@Override @Override
@SuppressWarnings("rawtypes")
public Object deepCopy(Object value) throws HibernateException { public Object deepCopy(Object value) throws HibernateException {
return value; if (value == null)
return null;
else {
Enum enumerator = (Enum) value;
return enumerator;
}
} }
@Override @Override
public boolean isMutable() { public boolean isMutable() {
return true; return false;
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value; return (value instanceof Serializable) ? (Serializable) deepCopy(value) : null;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached; return deepCopy(cached);
} }
@Override @Override

View File

@ -82,7 +82,8 @@ public class FormaPago implements Serializable, Auditavel<FormaPago> {
@Parameter(name = "type", value = "com.rjconsultores.ventaboletos.constantes.CustomEnumTypeHibernate"), @Parameter(name = "type", value = "com.rjconsultores.ventaboletos.constantes.CustomEnumTypeHibernate"),
@Parameter(name = "class", value = "com.rjconsultores.ventaboletos.enums.TipoFormapago"), @Parameter(name = "class", value = "com.rjconsultores.ventaboletos.enums.TipoFormapago"),
@Parameter(name = "sqlType", value = "4")/*Types.INTEGER*/, @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") @Column(name = "TIPO_PAGO")
private com.rjconsultores.ventaboletos.enums.TipoFormapago tipoFormapago; private com.rjconsultores.ventaboletos.enums.TipoFormapago tipoFormapago;
@Column(name = "CVESISTEMA") @Column(name = "CVESISTEMA")

View File

@ -46,32 +46,12 @@ public enum TipoFormapago {
} }
public static TipoFormapago getTipoFormapagoByValor(Integer valor) { public static TipoFormapago getTipoFormapagoByValor(Integer valor) {
if(TipoFormapago.DINHEIRO.getValor().equals(valor)) {
return TipoFormapago.DINHEIRO; for (TipoFormapago tipoFormapago : TipoFormapago.values()) {
} else if(TipoFormapago.DEBITO.getValor().equals(valor)) { if (tipoFormapago.getValor().equals(valor)) {
return TipoFormapago.DEBITO; return tipoFormapago;
} 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;
} }
return null; return null;
} }