Search in sources :

Example 6 with JsonDataVO

use of com.serotonin.m2m2.vo.json.JsonDataVO in project ma-core-public by infiniteautomation.

the class JsonDataImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    boolean isNew = false;
    if (StringUtils.isBlank(xid)) {
        xid = JsonDataDao.instance.generateUniqueXid();
        isNew = true;
    }
    JsonDataVO vo = JsonDataDao.instance.getByXid(xid);
    if (vo == null) {
        isNew = true;
        vo = new JsonDataVO();
        vo.setXid(xid);
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // Now validate it. Use a new response object so we can distinguish errors in this vo from
            // other errors.
            ProcessResult voResponse = new ProcessResult();
            vo.validate(voResponse);
            if (voResponse.getHasMessages())
                setValidationMessages(voResponse, "emport.jsondata.prefix", xid);
            else {
                // Sweet. Save it.
                JsonDataDao.instance.save(vo);
                addSuccessMessage(isNew, "emport.jsondata.prefix", xid);
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Aggregations

JsonDataVO (com.serotonin.m2m2.vo.json.JsonDataVO)6 User (com.serotonin.m2m2.vo.User)4 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)4 JsonDataModel (com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel)4 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)2 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)2 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 JsonException (com.serotonin.json.JsonException)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1