package dk.lessismore.nojpa.rest;

import dk.lessismore.nojpa.reflection.db.model.ModelObjectInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Created by niakoi on 12/3/15.
 */
public class NoJpaConverter implements GenericConverter {

    protected Logger log = LoggerFactory.getLogger(getClass());

    private NoJpaFormatter formatter;

    public NoJpaConverter(NoJpaFormatter formatter) {
        this.formatter = formatter;
    }

    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        Set<ConvertiblePair> pairs = new LinkedHashSet<ConvertiblePair>(2);
        pairs.add(new ConvertiblePair(formatter.getClazz(), String.class));
        pairs.add(new ConvertiblePair(String.class, formatter.getClazz()));
        return pairs;
    }

    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        if (sourceType.getType().equals(String.class)) {
            try {
                log.debug("convert(source("+ source +"), sourceType("+ sourceType +"), targetType("+ targetType +"))");
                return formatter.parse((String) source, null);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            } catch (RuntimeException re) {
                // if @RequestParam(required=false)
                if (targetType.hasAnnotation(RequestParam.class) && !targetType.getAnnotation(RequestParam.class).required()) {
                    return null;
                }
                throw re;
            }

        } else {
            return formatter.print((ModelObjectInterface) source, null);
        }
    }
}

@@ -154,16 +154,83 @@
 actory;%0A
+import org.springframework.core.convert.ConversionFailedException;%0A
 import o
@@ -341,24 +341,85 @@
 cConverter;%0A
+import org.springframework.web.bind.annotation.PathVariable;%0A
 import org.s
@@ -1356,30 +1356,8 @@
 ) %7B%0A
-            try %7B%0A    
     
@@ -1375,17 +1375,17 @@
 .debug(%22
-c
+C
 onvert(s
@@ -1484,462 +1484,680 @@
     
-    return formatter.parse((String) source, null);%0A            %7D catch (ParseException e) %7B%0A                throw new RuntimeException(e);%0A            %7D catch (RuntimeException re) %7B%0A                // if @RequestParam(required=false)%0A                if (targetType.hasAnnotation(RequestParam.class) && !targetType.getAnnotation(RequestParam.class).required()) %7B%0A                    return null;%0A                %7D%0A                throw re;%0A            %7D%0A
+boolean required = (targetType.hasAnnotation(RequestParam.class) && targetType.getAnnotation(RequestParam.class).required())%0A                    %7C%7C (targetType.hasAnnotation(PathVariable.class) && targetType.getAnnotation(PathVariable.class).required());%0A%0A            ModelObjectInterface moi = null;%0A            try %7B%0A                moi = formatter.parse((String) source, null);%0A            %7D catch (ParseException e) %7B%0A                throw new RuntimeException(e);%0A            %7D%0A            if (required && moi == null) %7B%0A                throw new ConversionFailedException(sourceType, targetType, source, new RuntimeException());%0A            %7D%0A            return moi;
 %0A   

fix required pathvariables & request params