1

So i posted this question, Using enum property as ormlite column value instead of ordinal. is it possible?, about how to use anum property value as the value passed to the database instead of the name, and it worked. I created the persistclass with the methods javaToSqlArg( for inserting in the Database) and the sqlArgoToJava for retrieving data from the database to the objects.

The problem seems that when doing the latter. The javaToSqlArg method is called before the sqlArgToJava which was the one I expected to be called directly when making select queries. Once again here is the persist class:

public class MyEnumPersister extends EnumStringType{

    private static final MyEnumPersister singleTon = new MyEnumPersister();

    protected MyEnumPersister() {
        super(SqlType.STRING, new Class<?>[] { Enum.class });
    }

    public static MyEnumPersister getSingleton() {
        return singleTon;
    }

    @Override
    public Object javaToSqlArg(FieldType fieldType, Object obj) {
        if(obj instanceof EstadoPedido){
            return ((EstadoPedido)obj).getEstadoValue();
        }else if(obj instanceof TipoPedido){
            return ((TipoPedido)obj).getTipoValue();
        }
        return obj;
    }               

    @Override
    public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) throws SQLException {
        if(fieldType.getColumnName().equals(PedidoDTO.ESTADO_FIELD_NAME)){  
            return PedidoDTO.siglaEstadoPedidoComparator((String)sqlArg);
        }else{
            return PedidoDTO.siglaTipoPedidoComparator((String)sqlArg);
        }
    }
}

And the dto class:

public class PedidoDTO extends IpdmsMobileDTO {

    private static final long serialVersionUID = 268620648774735484L;

    public final static String SIGLA_ESTADO_ENVIADO="E";
    public final static String SIGLA_ESTADO_PENDENTE="P";
    public final static String SIGLA_ESTADO_CANCELADO="C";
    public final static String SIGLA_ESTADO_CANCELADOALTERADO="CAP";

    public final static String SIGLA_TIPO_REGISTARNOTIFICATIORESPOSTA="RNR";

    //ormlime column names
    public final static String ESTADO_FIELD_NAME = "estado";
    public final static String TIPO_FIELD_NAME = "tipo";
    public final static String DATAENVIO_FIELD_NAME = "data_envio";
    public final static String ENTIDADE_ID_FIELD_NAME = "entidade_id";

    @DatabaseField(columnName=ESTADO_FIELD_NAME, persisterClass=MyEnumPersister.class)
    private EstadoPedido estado;
    @DatabaseField(columnName=TIPO_FIELD_NAME,  persisterClass=MyEnumPersister.class)
    private TipoPedido tipo;
    @DatabaseField(columnName=DATAENVIO_FIELD_NAME)
    private Date dataEnvio;
    @DatabaseField(columnName=ENTIDADE_ID_FIELD_NAME)
    private int idEntidade;

    public PedidoDTO() {
        //for ormlite
    }   

    public EstadoPedido getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado=siglaEstadoPedidoComparator(estado);
    }

    public static EstadoPedido siglaEstadoPedidoComparator(String estado){
        if (estado.equals(SIGLA_ESTADO_PENDENTE))
            return EstadoPedido.PENDENTE;
        else if (estado.equals(SIGLA_ESTADO_ENVIADO))
            return EstadoPedido.ENVIADO;
        else if(estado.equals(SIGLA_ESTADO_CANCELADO))
            return EstadoPedido.CANCELADO;
        else
            return EstadoPedido.CANCELADOALTERADOPREV;
    }

    public TipoPedido getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        //set tipo do pedido
        this.tipo =siglaTipoPedidoComparator(tipo);
    }

    public static TipoPedido siglaTipoPedidoComparator(String tipo){
        return TipoPedido.REGISTARNOTIFICACAORESPOSTA;
    }

    public enum EstadoPedido{
        ENVIADO  ("Enviado"  ,SIGLA_ESTADO_ENVIADO), 
        PENDENTE ("Pendente" ,SIGLA_ESTADO_PENDENTE),
        CANCELADO("Cancelado",SIGLA_ESTADO_CANCELADO),
        CANCELADOALTERADOPREV("Cancelado/Alterado Previamente",SIGLA_ESTADO_CANCELADO);

        private String estadoName;  
        private String estadoValue;

        EstadoPedido(String estadoName,String estadoValue){
            this.estadoName=estadoName;
            this.estadoValue=estadoValue;
        }

        public String getEstadoName(){
            return estadoName;
        }

        public String getEstadoValue(){
            return estadoValue;
        }
    }

    public enum TipoPedido {
        REGISTARNOTIFICACAORESPOSTA("Registar Notificação Resposta",SIGLA_TIPO_REGISTARNOTIFICATIORESPOSTA);

        private String tipoName;    
        private String tipoValue;

        TipoPedido(String tipoName,String tipoValue){
            this.tipoName=tipoName;
            this.tipoValue=tipoValue;
        }

        public String getTipoName(){
            return tipoName;
        }

        public String getTipoValue(){
            return tipoValue;
        }
    }
}

and the query:

public List<PedidoDTO> getPendingRequests(){

    List<PedidoDTO> pendingReq=null;
    QueryBuilder<PedidoDTO, Integer> queryBuild=null;

    try {
        queryBuild=getHelper().getPedidosDao().queryBuilder();
        pendingReq=queryBuild.where().eq(PedidoDTO.ESTADO_FIELD_NAME, PedidoDTO.SIGLA_ESTADO_PENDENTE).query();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return pendingReq;
}
Community
  • 1
  • 1
Maxrunner
  • 1,955
  • 4
  • 24
  • 41

1 Answers1

1

The javaToSqlArg method is called before the sqlArgToJava which was the one I expected to be called directly when making select queries. Once again here is the persist class:

ORMLite tries to be consistant about this @Max. You are passing in a Java value to the eq comparison method and it tries to perform the same conversion on it that happens when you store it to the database. In this case, since the Java field is a EstadoPedido or TipoPedido, the value you pass to the eq method should be that as well.

So your QueryBuilder eq call should use an enum value not a String and should be changed to something like:

where().eq(PedidoDTO.ESTADO_FIELD_NAME, EstadoPedido.PENDENTE)

I think I got that right.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Sorry Gray been on vacations, only saw this now. Let me check this later and i'll be back to you. – Maxrunner Sep 04 '12 at 16:02
  • Ok, so i've beem looking into this but i have some questions. Why changing from String to the enum change the way this works? its still java, so it will still call the javaToSqlArg when making the query, or am i wrong? – Maxrunner Sep 05 '12 at 09:32
  • Ok seems this is working as you said but i'm not following why. Can you elaborate more on it. – Maxrunner Sep 05 '12 at 10:53
  • You are querying for an enum field value @Max so you should provide an enum. Just because it happens to be stored in the database as a `String` doesn't change that. You should be able to change your persister class to store it as an integer _without_ having to change all of your query code. That's the whole point. – Gray Sep 05 '12 at 12:16
  • Ok i understand, one more thing it seems i can make equal statements using the eq method where if the column is a foreign key id i can compare directly with the object without the need to refer the object id, is this correct? – Maxrunner Sep 05 '12 at 12:51
  • Right @Max. Both the id and the object itself should be handled. – Gray Sep 05 '12 at 12:58