Search in sources :

Example 26 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-modules-public by infiniteautomation.

the class ScheduledEventEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
    JsonObject scheduledEvent = jsonValue.toJsonObject();
    String xid = scheduledEvent.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ScheduledEventDao.instance.generateUniqueXid();
    ScheduledEventVO vo = ScheduledEventDao.instance.getScheduledEvent(xid);
    if (vo == null) {
        vo = new ScheduledEventVO();
        vo.setXid(xid);
    }
    try {
        importContext.getReader().readInto(vo, scheduledEvent);
        // 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())
            // Too bad. Copy the errors into the actual response.
            importContext.copyValidationMessages(voResponse, "emport.scheduledEvent.prefix", xid);
        else {
            // Sweet. Save it.
            boolean isnew = vo.isNew();
            RTMDefinition.instance.saveScheduledEvent(vo);
            importContext.addSuccessMessage(isnew, "emport.scheduledEvent.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.scheduledEvent.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.scheduledEvent.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 27 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-modules-public by infiniteautomation.

the class WatchListEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
    JsonObject watchListJson = jsonValue.toJsonObject();
    String xid = watchListJson.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = WatchListDao.instance.generateUniqueXid();
    WatchListVO watchList = WatchListDao.instance.getWatchList(xid);
    if (watchList == null) {
        watchList = new WatchListVO();
        watchList.setXid(xid);
    }
    try {
        importContext.getReader().readInto(watchList, watchListJson);
        // Now validate it. Use a new response object so we can distinguish errors in this user from other
        // errors.
        ProcessResult watchListResponse = new ProcessResult();
        watchList.validate(watchListResponse);
        if (watchListResponse.getHasMessages())
            // Too bad. Copy the errors into the actual response.
            importContext.copyValidationMessages(watchListResponse, "emport.watchList.prefix", xid);
        else {
            // Sweet. Save it.
            boolean isnew = watchList.getId() == Common.NEW_ID;
            WatchListDao.instance.save(watchList);
            importContext.addSuccessMessage(isnew, "emport.watchList.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 28 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-modules-public by infiniteautomation.

the class VirtualPointLocatorVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    Integer value = readDataType(jsonObject, DataTypes.IMAGE);
    if (value != null)
        dataTypeId = value;
    JsonObject ctjson = jsonObject.getJsonObject("changeType");
    if (ctjson == null)
        throw new TranslatableJsonException("emport.error.missingObject", "changeType");
    String text = ctjson.getString("type");
    if (text == null)
        throw new TranslatableJsonException("emport.error.missing", "type", ChangeTypeVO.CHANGE_TYPE_CODES.getCodeList());
    changeTypeId = ChangeTypeVO.CHANGE_TYPE_CODES.getId(text);
    if (changeTypeId == -1)
        throw new TranslatableJsonException("emport.error.invalid", "changeType", text, ChangeTypeVO.CHANGE_TYPE_CODES.getCodeList());
    reader.readInto(getChangeType(), ctjson);
}
Also used : JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 29 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-modules-public by infiniteautomation.

the class WatchListVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    String username = jsonObject.getString("user");
    if (StringUtils.isBlank(username))
        throw new TranslatableJsonException("emport.error.missingValue", "user");
    User user = UserDao.instance.getUser(username);
    if (user == null)
        throw new TranslatableJsonException("emport.error.missingUser", username);
    userId = user.getId();
    JsonArray jsonDataPoints = jsonObject.getJsonArray("dataPoints");
    if (jsonDataPoints != null) {
        pointList.clear();
        DataPointDao dataPointDao = DataPointDao.instance;
        for (JsonValue jv : jsonDataPoints) {
            String xid = jv.toString();
            DataPointVO dpVO = dataPointDao.getDataPoint(xid);
            if (dpVO == null)
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            pointList.add(dpVO);
        }
    }
    JsonObject o = jsonObject.getJsonObject("data");
    if (o != null)
        this.data = o.toMap();
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) User(com.serotonin.m2m2.vo.User) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject)

Example 30 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-core-public by infiniteautomation.

the class EmportDwr method importData.

@DwrPermission(admin = true)
public ProcessResult importData(String data) {
    ProcessResult response = new ProcessResult();
    Translations translations = getTranslations();
    User user = Common.getHttpUser();
    JsonTypeReader reader = new JsonTypeReader(data);
    try {
        JsonValue value = reader.read();
        if (value instanceof JsonObject) {
            JsonObject root = value.toJsonObject();
            ImportTask importTask = new ImportTask(root, translations, user, true);
            user.setImportTask(importTask);
            response.addData("importStarted", true);
        } else {
            response.addGenericMessage("emport.invalidImportData");
        }
    } catch (ClassCastException e) {
        response.addGenericMessage("emport.parseError", e.getMessage());
    } catch (TranslatableJsonException e) {
        response.addMessage(e.getMsg());
    } catch (IOException e) {
        response.addGenericMessage("emport.parseError", e.getMessage());
    } catch (JsonException e) {
        response.addGenericMessage("emport.parseError", e.getMessage());
    }
    return response;
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) User(com.serotonin.m2m2.vo.User) ImportTask(com.serotonin.m2m2.web.dwr.emport.ImportTask) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) IOException(java.io.IOException) Translations(com.serotonin.m2m2.i18n.Translations) JsonTypeReader(com.serotonin.json.type.JsonTypeReader) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

JsonObject (com.serotonin.json.type.JsonObject)44 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)24 JsonValue (com.serotonin.json.type.JsonValue)20 JsonArray (com.serotonin.json.type.JsonArray)17 JsonException (com.serotonin.json.JsonException)15 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)11 JsonString (com.serotonin.json.type.JsonString)7 IOException (java.io.IOException)7 IntStringPair (com.serotonin.db.pair.IntStringPair)6 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)6 JsonReader (com.serotonin.json.JsonReader)5 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)5 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)4 ArrayList (java.util.ArrayList)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 User (com.serotonin.m2m2.vo.User)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)2 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)2