package com.altamiracorp.lumify.web.routes.vertex;

import com.altamiracorp.lumify.core.model.graph.GraphVertex;
import com.altamiracorp.lumify.core.model.ontology.PropertyName;
import com.altamiracorp.lumify.core.user.User;
import com.altamiracorp.lumify.core.model.graph.GraphRepository;
import com.altamiracorp.lumify.core.model.ontology.OntologyRepository;
import com.altamiracorp.lumify.core.model.ontology.Property;
import com.altamiracorp.lumify.core.model.artifact.ArtifactRepository;
import com.altamiracorp.lumify.web.BaseRequestHandler;
import com.altamiracorp.lumify.web.Messaging;
import com.altamiracorp.miniweb.HandlerChain;
import com.google.inject.Inject;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class VertexSetProperty extends BaseRequestHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(VertexSetProperty.class);

    private final GraphRepository graphRepository;
    private final OntologyRepository ontologyRepository;
    private final ArtifactRepository artifactRepository;

    @Inject
    public VertexSetProperty(final OntologyRepository ontologyRepo, final GraphRepository graphRepo, final ArtifactRepository artifactRepo) {
        ontologyRepository = ontologyRepo;
        graphRepository = graphRepo;
        artifactRepository = artifactRepo;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {
        final String graphVertexId = getAttributeString(request, "graphVertexId");
        final String propertyName = getRequiredParameter(request, "propertyName");
        final String valueStr = getRequiredParameter(request, "value");

        User user = getUser(request);
        Property property = ontologyRepository.getProperty(propertyName, user);
        if (property == null) {
            throw new RuntimeException("Could not find property: " + propertyName);
        }

        Object value;
        try {
            value = property.convertString(valueStr);
        } catch (Exception ex) {
            LOGGER.warn("Validation error propertyName: " + propertyName + ", valueStr: " + valueStr, ex);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
            return;
        }

        GraphVertex graphVertex = graphRepository.findVertex(graphVertexId, user);
        graphVertex.setProperty(propertyName, value);
        if (propertyName.equals(PropertyName.GEO_LOCATION.toString())) {
            graphVertex.setProperty(PropertyName.GEO_LOCATION_DESCRIPTION, "");
        } else if (propertyName.equals(PropertyName.SOURCE.toString())) {
            graphVertex.setProperty(PropertyName.SOURCE, value);
        }
        graphRepository.commit();

        Messaging.broadcastPropertyChange(graphVertexId, propertyName, value, toJson(graphVertex));

        Map<String, String> properties = graphRepository.getVertexProperties(graphVertexId, user);
        JSONObject propertiesJson = VertexProperties.propertiesToJson(properties);
        JSONObject json = new JSONObject();
        json.put("properties", propertiesJson);

        if (toJson(graphVertex) != null) {
            json.put("vertex", toJson(graphVertex));
        }

        respondWithJson(response, json);
    }

    private JSONObject toJson(GraphVertex vertex) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("graphVertexId", vertex.getId());
            for (String propertyKey : vertex.getPropertyKeys()) {
                obj.put(propertyKey, vertex.getProperty(propertyKey));
            }
            return obj;
        } catch (JSONException e) {
            new RuntimeException(e);
        }
        return null;
    }
}

@@ -45,16 +45,81 @@
 ertex;%0A%0A
+import com.altamiracorp.lumify.core.model.audit.AuditRepository;%0A
 import c
@@ -1280,16 +1280,67 @@
 ository;
+%0A    private final AuditRepository auditRepository;
 %0A%0A    @I
@@ -1483,16 +1483,49 @@
 factRepo
+, final AuditRepository auditRepo
 ) %7B%0A    
@@ -1639,24 +1639,61 @@
 tifactRepo;%0A
+        auditRepository = auditRepo;%0A
     %7D%0A%0A    @
@@ -2726,32 +2726,392 @@
 ertexId, user);%0A
+%0A        String message;%0A        if (graphVertex.getProperty(propertyName) != null) %7B%0A            message = %22Changed %22 + propertyName + %22 from %22 + graphVertex.getProperty(propertyName) + %22 to %22 + valueStr;%0A        %7D else %7B%0A            message = %22Set %22 + propertyName + %22 to %22 + valueStr;%0A        %7D%0A        auditRepository.audit(graphVertexId, message, user);%0A%0A
         graphVer

add audit message for vertex set property