package fr.synchrotron.soleil.ica.ci.app.mavenrepoimporter.service.mongodb;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.BasicDBObject;
import fr.synchrotron.soleil.ica.ci.lib.mongodb.domainobjects.artifact.ArtifactDocument;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Map;

/**
 * @author Gregory Boissinot
 */
public class MongoInsertion {

    private MongoTemplate mongoTemplate;

    public MongoInsertion(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    public void insert(ArtifactDocument artifactDocument) {

        Query query = new Query();
        query.addCriteria(Criteria.where("org").is(artifactDocument.getOrg()))
                .addCriteria(Criteria.where("name").is(artifactDocument.getName()))
                .addCriteria(Criteria.where("status").is(artifactDocument.getStatus()))
                .addCriteria(Criteria.where("version").is(artifactDocument.getVersion()));

        final BasicDBObject basicDBObject = new BasicDBObject();

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        Map<String, Object> map = objectMapper.convertValue(artifactDocument, Map.class);
        basicDBObject.putAll(map);

        Update update = Update.fromDBObject(basicDBObject);
        mongoTemplate.upsert(query, update, "artifacts");


    }
}

@@ -784,16 +784,56 @@
 plate;%0A%0A
+    private ObjectMapper objectMapper;%0A%0A
     publ
@@ -925,16 +925,324 @@
 mplate;%0A
+        this.objectMapper = new ObjectMapper();%0A        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);%0A        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);%0A        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);%0A%0A
     %7D%0A%0A 
@@ -1748,408 +1748,8 @@
 ();%0A
-%0A        ObjectMapper objectMapper = new ObjectMapper();%0A        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);%0A        //objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);%0A        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);%0A        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);%0A%0A
     

Migrate objectMapper to variable instance