Search in sources :

Example 31 with JsonObject

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

the class SetPointEventHandlerVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    DataPointDao dataPointDao = DataPointDao.instance;
    String xid = jsonObject.getString("targetPointId");
    if (xid != null) {
        Integer id = dataPointDao.getIdByXid(xid);
        if (id == null)
            throw new TranslatableJsonException("emport.error.missingPoint", xid);
        targetPointId = id;
    }
    // Active
    String text = jsonObject.getString("activeAction");
    if (text != null) {
        activeAction = SET_ACTION_CODES.getId(text);
        if (!SET_ACTION_CODES.isValidId(activeAction))
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "activeAction", text, SET_ACTION_CODES.getCodeList());
    }
    if (activeAction == SET_ACTION_POINT_VALUE) {
        xid = jsonObject.getString("activePointId");
        if (xid != null) {
            Integer id = dataPointDao.getIdByXid(xid);
            if (id == null)
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            activePointId = id;
        }
    } else if (activeAction == SET_ACTION_STATIC_VALUE) {
        text = jsonObject.getString("activeValueToSet");
        if (text != null)
            activeValueToSet = text;
    } else if (activeAction == SET_ACTION_SCRIPT_VALUE) {
        text = jsonObject.getString("activeScript");
        if (text == null)
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "inactiveScript");
        activeValueToSet = text;
    }
    // Inactive
    text = jsonObject.getString("inactiveAction");
    if (text != null) {
        inactiveAction = SET_ACTION_CODES.getId(text);
        if (!SET_ACTION_CODES.isValidId(inactiveAction))
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "inactiveAction", text, SET_ACTION_CODES.getCodeList());
    }
    if (inactiveAction == SET_ACTION_POINT_VALUE) {
        xid = jsonObject.getString("inactivePointId");
        if (xid != null) {
            Integer id = dataPointDao.getIdByXid(xid);
            if (id == null)
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            inactivePointId = id;
        }
    } else if (inactiveAction == SET_ACTION_STATIC_VALUE) {
        text = jsonObject.getString("inactiveValueToSet");
        if (text != null)
            inactiveValueToSet = text;
    } else if (inactiveAction == SET_ACTION_SCRIPT_VALUE) {
        text = jsonObject.getString("inactiveScript");
        if (text == null)
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "inactiveScript");
        inactiveValueToSet = text;
    }
    JsonArray context = jsonObject.getJsonArray("additionalContext");
    if (context != null) {
        List<IntStringPair> additionalContext = new ArrayList<>();
        for (JsonValue jv : context) {
            JsonObject jo = jv.toJsonObject();
            String dataPointXid = jo.getString("dataPointXid");
            if (dataPointXid == null)
                throw new TranslatableJsonException("emport.error.context.missing", "dataPointXid");
            Integer id = DataPointDao.instance.getIdByXid(dataPointXid);
            if (id == null)
                throw new TranslatableJsonException("emport.error.missingPoint", dataPointXid);
            String contextKey = jo.getString("contextKey");
            if (contextKey == null)
                throw new TranslatableJsonException("emport.error.context.missing", "contextKey");
            additionalContext.add(new IntStringPair(id, contextKey));
        }
        this.additionalContext = additionalContext;
    } else
        this.additionalContext = new ArrayList<>();
    JsonObject permissions = jsonObject.getJsonObject("scriptPermissions");
    ScriptPermissions scriptPermissions = new ScriptPermissions();
    if (permissions != null) {
        String perm = permissions.getString(ScriptPermissions.DATA_SOURCE);
        if (perm != null)
            scriptPermissions.setDataSourcePermissions(perm);
        perm = permissions.getString(ScriptPermissions.DATA_POINT_READ);
        if (perm != null)
            scriptPermissions.setDataPointReadPermissions(perm);
        perm = permissions.getString(ScriptPermissions.DATA_POINT_SET);
        if (perm != null)
            scriptPermissions.setDataPointSetPermissions(perm);
    }
    this.scriptPermissions = scriptPermissions;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) ScriptPermissions(com.serotonin.m2m2.rt.script.ScriptPermissions)

Example 32 with JsonObject

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

the class SetPointEventHandlerVO method jsonWrite.

@Override
public void jsonWrite(ObjectWriter writer) throws IOException, JsonException {
    super.jsonWrite(writer);
    String dpXid = DataPointDao.instance.getXidById(targetPointId);
    writer.writeEntry("targetPointId", dpXid);
    // Active
    writer.writeEntry("activeAction", SET_ACTION_CODES.getCode(activeAction));
    if (activeAction == SET_ACTION_POINT_VALUE) {
        dpXid = DataPointDao.instance.getXidById(activePointId);
        writer.writeEntry("activePointId", dpXid);
    } else if (activeAction == SET_ACTION_STATIC_VALUE)
        writer.writeEntry("activeValueToSet", activeValueToSet);
    else if (activeAction == SET_ACTION_SCRIPT_VALUE)
        writer.writeEntry("activeScript", activeScript);
    // Inactive
    writer.writeEntry("inactiveAction", SET_ACTION_CODES.getCode(inactiveAction));
    if (inactiveAction == SET_ACTION_POINT_VALUE) {
        dpXid = DataPointDao.instance.getXidById(inactivePointId);
        writer.writeEntry("inactivePointId", dpXid);
    } else if (inactiveAction == SET_ACTION_STATIC_VALUE)
        writer.writeEntry("inactiveValueToSet", inactiveValueToSet);
    else if (inactiveAction == SET_ACTION_SCRIPT_VALUE)
        writer.writeEntry("inactiveScript", inactiveScript);
    JsonArray context = new JsonArray();
    for (IntStringPair pnt : additionalContext) {
        DataPointVO dpvo = DataPointDao.instance.getDataPoint(pnt.getKey(), false);
        if (dpvo != null) {
            JsonObject point = new JsonObject();
            point.put("dataPointXid", dpvo.getXid());
            point.put("contextKey", pnt.getValue());
            context.add(point);
        }
    }
    writer.writeEntry("additionalContext", context);
    if (scriptPermissions != null) {
        JsonObject permissions = new JsonObject();
        permissions.put(ScriptPermissions.DATA_SOURCE, scriptPermissions.getDataSourcePermissions());
        permissions.put(ScriptPermissions.DATA_POINT_READ, scriptPermissions.getDataPointReadPermissions());
        permissions.put(ScriptPermissions.DATA_POINT_SET, scriptPermissions.getDataPointSetPermissions());
        writer.writeEntry("scriptPermissions", permissions);
    } else {
        writer.writeEntry("scriptPermissions", null);
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) IntStringPair(com.serotonin.db.pair.IntStringPair) JsonObject(com.serotonin.json.type.JsonObject)

Example 33 with JsonObject

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

the class EmailRecipientResolver method resolve.

@Override
public Type resolve(JsonValue jsonValue) throws JsonException {
    if (jsonValue == null)
        return null;
    JsonObject json = jsonValue.toJsonObject();
    String text = json.getString("recipientType");
    if (text == null)
        throw new TranslatableJsonException("emport.error.recipient.missing", "recipientType", EmailRecipient.TYPE_CODES);
    int type = EmailRecipient.TYPE_CODES.getId(text);
    if (!EmailRecipient.TYPE_CODES.isValidId(type))
        throw new TranslatableJsonException("emport.error.recipient.invalid", "recipientType", text, EmailRecipient.TYPE_CODES.getCodeList());
    if (type == EmailRecipient.TYPE_MAILING_LIST)
        return MailingList.class;
    if (type == EmailRecipient.TYPE_USER)
        return UserEntry.class;
    return AddressEntry.class;
}
Also used : JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 34 with JsonObject

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

the class Restorer method restore.

/**
 * Restore the object based on the Audit Trail
 * @return
 */
public T restore() {
    T vo = null;
    try {
        // Follow the trail
        for (AuditEventInstanceVO audit : trail) {
            JsonObject context = audit.getContext();
            JsonReader reader = new JsonReader(Common.JSON_CONTEXT, context);
            if (audit.getChangeType() == AuditEventInstanceVO.CHANGE_TYPE_CREATE) {
                vo = this.build(audit.getObjectId(), context, reader);
            } else if (audit.getChangeType() == AuditEventInstanceVO.CHANGE_TYPE_MODIFY) {
                if (vo == null)
                    vo = getExisting(audit.getObjectId());
                vo = this.build(vo, context, reader);
            }
        }
        ProcessResult voResponse = new ProcessResult();
        vo.validate(voResponse);
        if (voResponse.getHasMessages())
            copyValidationMessages(voResponse, "restore.prefix", vo.getXid());
        else {
            addSuccessMessage(vo.isNew(), "restore.prefix", vo.getXid());
        }
    } catch (TranslatableJsonException e) {
        addFailureMessage("restore.prefix", "need-to-fill-in", e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("restoring.prefix", "need-to-fill-in", getJsonExceptionMessage(e));
    } catch (Exception e) {
        addFailureMessage("restoring.prefix", "need-to-fill-in", e.getMessage());
    }
    return vo;
}
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) JsonReader(com.serotonin.json.JsonReader) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) AuditEventInstanceVO(com.serotonin.m2m2.vo.event.audit.AuditEventInstanceVO)

Example 35 with JsonObject

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

the class SerotoninJsonMessageConverter method readInternal.

/* (non-Javadoc)
	 * @see org.springframework.http.converter.AbstractHttpMessageConverter#readInternal(java.lang.Class, org.springframework.http.HttpInputMessage)
	 */
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    InputStreamReader isReader = new InputStreamReader(inputMessage.getBody());
    JsonTypeReader typeReader = new JsonTypeReader(isReader);
    try {
        JsonValue value = typeReader.read();
        if (clazz.equals(JsonValue.class))
            return value;
        // First get the definition for the model so we can create a real object
        ModelDefinition def = findModelDefinition(clazz);
        AbstractRestModel<?> model = def.createModel();
        JsonReader reader = new JsonReader(Common.JSON_CONTEXT, value);
        if (value instanceof JsonObject) {
            // TODO Should do some pre-validation or something to ensure we are
            // importing the right thing?
            JsonObject root = value.toJsonObject();
            if (model != null) {
                Object data = model.getData();
                reader.readInto(data, root);
                return model;
            } else {
                // Catchall
                return root.toNative();
            }
        } else {
            throw new IOException("Huh?");
        }
    } catch (JsonException e) {
        throw new IOException(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) InputStreamReader(java.io.InputStreamReader) JsonValue(com.serotonin.json.type.JsonValue) ModelDefinition(com.serotonin.m2m2.module.ModelDefinition) JsonReader(com.serotonin.json.JsonReader) JsonObject(com.serotonin.json.type.JsonObject) JsonObject(com.serotonin.json.type.JsonObject) IOException(java.io.IOException) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

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