package org.gluu.oxtrust.service.scim2.serialization;

import org.apache.logging.log4j.LogManager;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.gluu.oxtrust.model.scim2.BaseScimResource;
import org.gluu.oxtrust.model.scim2.ListResponse;
import org.apache.logging.log4j.Logger;

import java.io.IOException;

import static org.gluu.oxtrust.model.scim2.Constants.LIST_RESPONSE_SCHEMA_ID;

/**
 * Created by jgomer on 2017-10-01.
 */
public class ListResponseJsonSerializer extends JsonSerializer<ListResponse> {

    private Logger log = LogManager.getLogger(getClass());
    private ScimResourceSerializer resourceSerializer;
    private ObjectMapper mapper = new ObjectMapper();

    public ListResponseJsonSerializer(ScimResourceSerializer serializer){
        resourceSerializer=serializer;
    }

    @Override
    public void serialize(ListResponse listResponse, JsonGenerator jGen, SerializerProvider provider) throws IOException {

        jGen.writeStartObject();
        jGen.writeNumberField("totalResults", listResponse.getTotalResults());
        jGen.writeNumberField("startIndex", listResponse.getStartIndex());
        jGen.writeNumberField("itemsPerPage", listResponse.getItemsPerPage());

        jGen.writeArrayFieldStart("schemas");
        jGen.writeString(LIST_RESPONSE_SCHEMA_ID);
        jGen.writeEndArray();

        jGen.writeArrayFieldStart("Resources");
        for (BaseScimResource resource : listResponse.getResources()){
            try {
                log.debug("oh margoth");
                JsonNode jsonResource=mapper.readTree(resourceSerializer.serialize(resource));
                jGen.writeTree(jsonResource);
                //jGen.writeString(resourceSerializer.serialize(resource));
            }
            catch (Exception e){
                throw new IOException(e);
            }
        }
        jGen.writeEndArray();

        jGen.writeEndObject();

    }

}

@@ -874,114 +874,549 @@
    p
-ublic ListResponseJsonSerializer(ScimResourceSerializer serializer)%7B%0A        resourceSerializer=serializer
+rivate String attributes;%0A    private String excludeAttributes;%0A%0A    //why not to inject the resource serializer instead of passing it as parameter? weld simply does not like it!%0A    public ListResponseJsonSerializer(ScimResourceSerializer serializer)%7B%0A        resourceSerializer=serializer;%0A    %7D%0A%0A    public ListResponseJsonSerializer(ScimResourceSerializer serializer, String attributes, String excludeAttributes)%7B%0A        resourceSerializer=serializer;%0A        this.attributes=attributes;%0A        this.excludeAttributes=excludeAttributes
 ;%0A  
@@ -1666,24 +1666,76 @@
 Results());%0A
+        if (listResponse.getTotalResults()%3E0) %7B%0A    
         jGen
@@ -1793,24 +1793,28 @@
 rtIndex());%0A
+    
         jGen
@@ -1879,16 +1879,26 @@
 Page());
+%0A        %7D
 %0A%0A      
@@ -2160,49 +2160,8 @@
 y %7B%0A
-                log.debug(%22oh margoth%22);%0A
     
@@ -2239,32 +2239,63 @@
 rialize(resource
+, attributes, excludeAttributes
 ));%0A            

Add attributes/excludedAttribues member to serializer