package io.corbel.resources.rem.dao;

import io.corbel.lib.queries.DateQueryLiteral;
import io.corbel.lib.queries.LongQueryLiteral;
import io.corbel.lib.queries.QueryNodeImpl;
import io.corbel.lib.queries.request.QueryLiteral;
import io.corbel.lib.queries.request.QueryNode;
import io.corbel.lib.queries.request.QueryOperator;

import java.util.Date;
import java.util.function.Function;

/**
 * @author Rubén Carrasco
 *
 */
public class DateQueryNodeTransformer implements Function<QueryNode, QueryNode> {

    @Override
    public QueryNode apply(QueryNode t) {
        return new QueryNodeImpl(t.getOperator(), t.getField(), tranformDateValue(t.getField(), t.getOperator(), t.getValue()));
    }

    @SuppressWarnings("incomplete-switch")
    private QueryLiteral<?> tranformDateValue(String field, QueryOperator operator, QueryLiteral<?> value) {
        if (field.equals(ReservedFields._UPDATED_AT) || field.equals(ReservedFields._CREATED_AT)) {
            switch (operator) {
                case $EQ:
                case $GT:
                case $GTE:
                case $LT:
                case $LTE:
                case $NE:
                    try {
                        return new DateQueryLiteral(new Date(((LongQueryLiteral) value).getLiteral()));
                    } catch (ClassCastException e) {}
            }
        }
        return value;
    }
}

@@ -31,16 +31,76 @@
 m.dao;%0A%0A
+import java.util.Date;%0Aimport java.util.function.Function;%0A%0A
 import i
@@ -385,68 +385,8 @@
 r;%0A%0A
-import java.util.Date;%0Aimport java.util.function.Function;%0A%0A
 /**%0A
@@ -501,16 +501,85 @@
 ode%3E %7B%0A%0A
+    private enum VALID_OPERATORS %7B $EQ, $GT, $GTE, $LT, $LTE, $NE %7D%0A%0A
     @Ove
@@ -690,24 +690,25 @@
 ield(), tran
+s
 formDateValu
@@ -767,51 +767,8 @@
  %7D%0A%0A
-    @SuppressWarnings(%22incomplete-switch%22)%0A
     
@@ -795,16 +795,17 @@
 %3C?%3E tran
+s
 formDate
@@ -989,190 +989,68 @@
     
-switch (operator) %7B%0A                case $EQ:%0A                case $GT:%0A                case $GTE:%0A                case $LT:%0A                case $LTE:%0A                case $NE:%0A    
+if (VALID_OPERATORS.valueOf(operator.toString()) != null) %7B%0A
     
@@ -1067,20 +1067,16 @@
   try %7B%0A
-    
         
@@ -1183,20 +1183,16 @@
         
-    
 %7D catch 
@@ -1211,17 +1211,23 @@
 ception 
-e
+ignored
 ) %7B%7D%0A   

Refactor DateQueryNodeTransformer.