/**
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U <br>
 * This file is part of FI-WARE project.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License.
 * </p>
 * <p>
 * You may obtain a copy of the License at:<br>
 * <br>
 * http://www.apache.org/licenses/LICENSE-2.0
 * </p>
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * </p>
 * <p>
 * See the License for the specific language governing permissions and limitations under the License.
 * </p>
 * <p>
 * For those usages not covered by the Apache version 2.0 License please contact with opensource@tid.es
 * </p>
 */

package com.telefonica.euro_iaas.sdc.util;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;

@Provider
public class MyObjectMapperProvider implements ContextResolver<ObjectMapper> {

    final ObjectMapper defaultObjectMapper;
    final ObjectMapper combinedObjectMapper;

    public MyObjectMapperProvider() {
        defaultObjectMapper = createDefaultMapper();
        combinedObjectMapper = createCombinedObjectMapper();
    }

    @Override
    public ObjectMapper getContext(final Class<?> type) {

        // if (type == Task.class) {
        return combinedObjectMapper;
        // } else {
        // return defaultObjectMapper;
        // }
    }

    private static ObjectMapper createCombinedObjectMapper() {
        return new ObjectMapper().configure(SerializationFeature.WRAP_ROOT_VALUE, true)
                .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
                .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
                .enable(SerializationFeature.WRITE_NULL_MAP_VALUES)
                .configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true)
                .setAnnotationIntrospector(createJaxbJacksonAnnotationIntrospector());
    }

    private static ObjectMapper createDefaultMapper() {
        final ObjectMapper result = new ObjectMapper();
        result.enable(SerializationFeature.INDENT_OUTPUT);

        return result;
    }

    private static AnnotationIntrospector createJaxbJacksonAnnotationIntrospector() {

        final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
        final AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();

        return AnnotationIntrospector.pair(jacksonIntrospector, jaxbIntrospector);
    }
}

@@ -993,39 +993,30 @@
 son.
-databind.AnnotationIntrospector
+annotation.JsonInclude
 ;%0Aim
@@ -1051,38 +1051,38 @@
 atabind.
-DeserializationFeature
+AnnotationIntrospector
 ;%0Aimport
@@ -2141,34 +2141,45 @@
 ature.WR
-AP_ROOT_VALUE, tru
+ITE_DATES_AS_TIMESTAMPS, fals
 e)%0A     
@@ -2194,21 +2194,16 @@
    .
-configure(Des
+enable(S
 eria
@@ -2222,31 +2222,29 @@
 ure.
-UNWRAP_ROOT_VALUE, true
+WRITE_NULL_MAP_VALUES
 )%0A  
@@ -2299,102 +2299,42 @@
 ITE_
-DATES_AS_TIMESTAMPS, false)%0A                .enable(SerializationFeature.WRITE_NULL_MAP_VALUES
+SINGLE_ELEM_ARRAYS_UNWRAPPED, true
 )%0A  
@@ -2405,32 +2405,105 @@
 RTY_NAME, true)%0A
+                .setSerializationInclusion(JsonInclude.Include.NON_NULL)%0A
                 

remove null fields