Example usage for com.google.gwt.query.client.js JsUtils.JsUtilsImpl parseJSON

List of usage examples for com.google.gwt.query.client.js JsUtils.JsUtilsImpl parseJSON

Introduction

In this page you can find the example usage for com.google.gwt.query.client.js JsUtils.JsUtilsImpl parseJSON.

Prototype

public static Properties parseJSON(String json) 

Source Link

Document

Parses a json string returning a Object with useful method to get the content.

Usage

From source file:org.mindinformatics.gwt.domeo.plugins.persistence.annotopia.src.AnnotopiaPersistenceManager.java

License:Apache License

@Override
public void saveAnnotation() {
    _application.getLogger().debug(this, "Saving modified annotation sets...");
    _application.getProgressPanelContainer()
            .setProgressMessage("Saving modified annotation sets to Annotopia...");

    ArrayList<MAnnotationSet> setToSerialize = new ArrayList<MAnnotationSet>();
    for (MAnnotationSet set : ((IDomeo) _application).getAnnotationPersistenceManager()
            .getAllDiscussionSets()) {//from w  w w.jav  a2  s .  com
        if (set.getHasChanged() && set.getAnnotations().size() > 0)
            setToSerialize.add(set);
    }
    for (MAnnotationSet set : ((IDomeo) _application).getAnnotationPersistenceManager().getAllUserSets()) {
        if (set.getHasChanged() && set.getAnnotations().size() > 0)
            setToSerialize.add(set);
    }

    AnnotopiaSerializerManager manager = AnnotopiaSerializerManager.getInstance((IDomeo) _application);
    for (MAnnotationSet annotationSet : setToSerialize) {
        final String operation = (annotationSet.getVersionNumber() == null
                || annotationSet.getVersionNumber().isEmpty()) ? "post" : "put";
        JsUtils.JsUtilsImpl utils = new JsUtils.JsUtilsImpl();
        Properties v = utils.parseJSON("{\"apiKey\":\"" + ApplicationUtils.getAnnotopiaApiKey()
                + "\",\"outCmd\":\"frame\",\"set\":" + manager.serialize(annotationSet).toString() + "}");
        try {
            Ajax.ajax(Ajax.createSettings().setUrl(URL + "s/annotationset").setHeaders(getAnnotopiaOAuthToken())
                    .setDataType("json") // txt, json, jsonp, xml */
                    .setType(operation) // post, get
                    .setData(v) // parameters for the query-string setData(GQuery.$$("apiKey: testkey, set: " + value))
                    .setTimeout(10000).setSuccess(new Function() { // callback to be run if the request success
                        public void f() {
                            IDomeo _domeo = ((IDomeo) _application);
                            JsAnnotopiaSetResultWrapper wrapper = (JsAnnotopiaSetResultWrapper) parseJson(
                                    getDataProperties().toJsonString());
                            AnnotopiaConverter unmarshaller = new AnnotopiaConverter(_domeo);

                            MAnnotationSet set = unmarshaller
                                    .unmarshallAnnotationSet(wrapper.getResult().getSet().get(0), false);
                            if (set == null) {
                                // TODO message no annotation found
                                _application.getLogger().exception(this, "Annotation set not saved correctly");
                                _application.getProgressPanelContainer()
                                        .setErrorMessage("Annotation set not saved correctly");
                            } else {
                                MAnnotationSet currentSet = null;
                                if (operation.equals("post"))
                                    currentSet = _domeo.getPersistenceManager()
                                            .getAnnotationSetById(set.getPreviousVersion());
                                else
                                    currentSet = _domeo.getPersistenceManager()
                                            .getAnnotationSetById(set.getIndividualUri());

                                currentSet.setIndividualUri(set.getIndividualUri());
                                _application.getLogger().info(this,
                                        "Setting Set id to " + currentSet.getIndividualUri());
                                currentSet.setLastSavedOn(set.getLastSavedOn());
                                currentSet.setVersionNumber(set.getVersionNumber());
                                currentSet.setPreviousVersion(set.getPreviousVersion());
                                currentSet.setHasChanged(false);
                                _application.getLogger().info(this, "Set: " + currentSet.getIndividualUri());

                                for (MAnnotation annotation : set.getAnnotations()) {
                                    _application.getLogger().info(this,
                                            "Annotation " + annotation.getPreviousVersion());
                                    for (MAnnotation currentAnnotation : currentSet.getAnnotations()) {
                                        _application.getLogger().info(this,
                                                "Matching " + currentAnnotation.getIndividualUri());
                                        if (currentAnnotation.getIndividualUri()
                                                .equals(annotation.getPreviousVersion())) {

                                            _application.getLogger().info(this, "Matched");
                                            currentAnnotation.setIndividualUri(annotation.getIndividualUri());
                                            currentAnnotation.setLastSavedOn(annotation.getLastSavedOn());
                                            currentAnnotation.setVersionNumber(annotation.getVersionNumber());
                                            currentAnnotation
                                                    .setPreviousVersion(annotation.getPreviousVersion());
                                            currentAnnotation.setHasChanged(false);
                                            // TODO: Assumes one target
                                            currentAnnotation.getSelector()
                                                    .setUri(annotation.getSelector().getUri());
                                            break;
                                        }
                                    }
                                }
                                _application.getProgressPanelContainer().hide();
                                _application.getLogger().debug(this,
                                        "Completed saving of Annotation Set in "
                                                + (System.currentTimeMillis()
                                                        - ((IDomeo) _application).getDocumentPipelineTimer())
                                                + "ms");
                            }
                        }
                    }).setError(new Function() { // callback to be run if the request fails
                        public void f() {
                            Window.alert("There was an error " + getDataObject());

                            _application.getLogger().exception(this,
                                    "Couldn't complete existing annotation sets list saving");
                            _application.getProgressPanelContainer()
                                    .setErrorMessage("Couldn't complete existing annotation sets list saving");
                        }
                    }));
        } catch (Exception e) {
            _application.getLogger().exception(this, "Couldn't complete existing annotation sets list saving");
        }
    }
}