rasvaan / accurator

Accurator cpack
7 stars 3 forks source link

Save annotation to triple store #209

Closed rasvaan closed 8 years ago

rasvaan commented 8 years ago

Improve the submit annotation code. It currently only saves annotations with a generic target (uri of the object). This should be rewritten to correctly saving fragments. Code is based on submitAnnotatoin in annotation.js:

submitAnnotation : function(motiv, target, body, label, timing, next, graph) {
        this.refocus(next);
        if (!target) return;
        if (!body) return;
        if (!label && body['@value']) label = body['@value'];
        if (!timing) timing = -1;
        if (!motiv) motiv = motiv = Annotation.MOTIVATION.tagging;
        if (!graph) graph = target;

        var bodyString = Y.JSON.stringify(body);

        var targetObject = null;
        if (this._anno && this._anno._deniche.currentShape) {
            var shape = this._anno._deniche.currentShape.geometry;
            var targetImage = this.get('targetImage');
            if (targetImage && target != targetImage) {
            // another annotation on selector with existing id (target id is the selector, not the image)
            targetObject = [ { hasSource: targetImage, hasSelector: { value:shape}}, { '@id':target } ];
            } else {
            // annotation on new selector, id will be generated server-side
            targetObject = [ { hasSource: target, hasSelector: { value:shape}} ];
            }
        } else {
            // annotation without fragment, on entire target image
            targetObject = [{'@id':target}];
        }
        var targetString = Y.JSON.stringify(targetObject);

        var tags = this.tags;
        var myMetaTags = this.get("myMetaTags");
        var oSelf = this;

        var context = "";
        var index = parseInt(localStorage.getItem("itemIndex"));
        var clusterId = parseInt(localStorage.getItem("clusterId"));
        var query = localStorage.getItem("query");

        if(query === "random") {
            context = "random, " + index;
        } else if(query === "expertise") {
            context = "expertise, " + index;
        } else if(query === "expertise values") {
            context = "recommendation, " + index + ", " + clusterId;
        } else if (query !== "") {
            context = "search, " + query + ", " + index + ", " + clusterId;
        } else {
            context = "unknown";
        }

        Y.io(this.get("store.add"), {
            method: "POST",
            data:{
            field:this.get("field"),
            hasTarget:targetString,
            hasBody:bodyString,
            label:label,
            typing_time: timing,
            reached_object_with: context,
            motivatedBy: motiv,
            graph: graph
            },
            on:{success: function(e,o) {
            var response = Y.JSON.parse(o.responseText);
            var r = response.annotation;
            if (motiv == Annotation.MOTIVATION.tagging) {
                tags.add(r);
                oSelf.addTagFragment(r, false); // add but do not update open editor
            } else {
                var values = tags.getValuesByKey('annotation');
                var index = values.indexOf(target);
                if (!myMetaTags[target]) myMetaTags[target] = {}
                if (motiv == Annotation.MOTIVATION.moderating) {
                myMetaTags[target][label] = r;
                } else if (motiv == Annotation.MOTIVATION.commenting) {
                myMetaTags[target][motiv] = r;
                }
                oSelf.set('myMetaTags', myMetaTags);
                var record = tags.getRecordByIndex(index);
                tags.update(record, index);
            }
            }
               }
        });
        },