package io.liveoak.container.tenancy;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.liveoak.common.util.ObjectMapperFactory;
import org.jboss.logging.Logger;

/**
 * @author Bob McWhirter
 */
public class ApplicationConfigurationManager {

    public ApplicationConfigurationManager(File file) {
        this.file = file;
    }

    public synchronized ObjectNode read() throws IOException {
        ObjectMapper mapper = ObjectMapperFactory.create();
        return (ObjectNode) mapper.readTree(this.file);
    }

    public synchronized void updateResource(String id, String type, ObjectNode config) throws IOException {
        ObjectNode tree = read();
        ObjectNode resourcesTree = (ObjectNode) tree.get("resources");
        if (resourcesTree == null) {
            resourcesTree = JsonNodeFactory.instance.objectNode();
            tree.put("resources", resourcesTree);
        }

        ObjectNode resourceTree = (ObjectNode) resourcesTree.get(id);
        if (resourceTree == null) {
            resourceTree = JsonNodeFactory.instance.objectNode();
            resourceTree.put("type", type);
            resourcesTree.put(id, resourceTree);
        }

        resourceTree.put("config", config);

        ObjectMapper mapper = new ObjectMapper();
        ObjectWriter writer = mapper.writer();
        writer = writer.with(new DefaultPrettyPrinter("\\n"));
        writer.writeValue(this.file, tree);
    }

    public synchronized void removeResource(String id) throws IOException {
        ObjectNode tree = read();
        ObjectNode resourcesTree = (ObjectNode) tree.get("resources");
        if (resourcesTree == null) {
            return;
        }

        resourcesTree.remove(id);

        ObjectMapper mapper = ObjectMapperFactory.create();
        ObjectWriter writer = mapper.writer();
        writer = writer.with(new DefaultPrettyPrinter("\\n"));
        log.debug("writing " + tree + " to " + this.file);
        writer.writeValue(this.file, tree);
        log.debug("write complete");
    }

    private final File file;

    private static final Logger log = Logger.getLogger(ApplicationConfigurationManager.class);
}

@@ -480,12 +480,36 @@
 ter%0A
+ * @author Ken Finnigan%0A
  */%0A
-
 publ
@@ -819,32 +819,317 @@
 s.file);%0A    %7D%0A%0A
+    public synchronized void updateApplication(String name, Boolean visible, ObjectNode config) throws IOException %7B%0A        ObjectNode tree = read();%0A        tree.put(%22name%22, name);%0A        tree.put(%22visible%22, visible);%0A        tree.put(%22config%22, config);%0A        write(tree);%0A    %7D%0A%0A
     public synch
@@ -1810,25 +1810,24 @@
 onfig);%0A
-%0A
         
 ObjectMa
@@ -1822,197 +1822,14 @@
     
-ObjectMapper mapper = new ObjectMapper();%0A        ObjectWriter writer = mapper.writer();%0A        writer = writer.with(new DefaultPrettyPrinter(%22%5C%5Cn%22));%0A        writer.writeValue(this.file, 
+write(
 tree
@@ -2121,16 +2121,104 @@
 ve(id);%0A
+        write(tree);%0A    %7D%0A%0A    private void write(ObjectNode tree) throws IOException %7B
 %0A       

Update ConfigurationManager to save application level config