Search in sources :

Example 6 with JsonArray

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

the class PublisherVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    // Not reading XID so can't do this: super.jsonRead(reader, jsonObject);
    name = jsonObject.getString("name");
    enabled = jsonObject.getBoolean("enabled");
    // Legacy conversion for publishType
    if (jsonObject.containsKey("publishType")) {
        String publishTypeCode = jsonObject.getString("publishType");
        int publishTypeId = PUBLISH_TYPE_CODES.getId(publishTypeCode);
        if (publishTypeId == -1)
            throw new TranslatableJsonException("emport.error.invalid", "publishType", publishTypeCode, PUBLISH_TYPE_CODES.getCodeList());
        publishType = publishTypeId;
    } else if (jsonObject.containsKey("changesOnly")) {
        boolean changesOnly = jsonObject.getBoolean("changesOnly");
        if (changesOnly) {
            this.publishType = PublishType.CHANGES_ONLY;
        } else {
            this.publishType = PublishType.ALL;
        }
    }
    // Could wrap the readInto with a try-catch in case one dataPointId entry is null,
    // however this would be a silent suppression of the issue, so we have elected not to.
    // infiniteautomation/ma-core-public#948
    JsonArray arr = jsonObject.getJsonArray("points");
    if (arr != null) {
        points.clear();
        for (JsonValue jv : arr) {
            T point = createPublishedPointInstance();
            reader.readInto(point, jv.toJsonObject());
            points.add(point);
        }
    }
    String text = jsonObject.getString("snapshotSendPeriodType");
    if (text != null) {
        snapshotSendPeriodType = Common.TIME_PERIOD_CODES.getId(text);
        if (snapshotSendPeriodType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "snapshotSendPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
    }
    JsonObject alarmCodeLevels = jsonObject.getJsonObject("alarmLevels");
    if (alarmCodeLevels != null) {
        ExportCodes eventCodes = getEventCodes();
        if (eventCodes != null && eventCodes.size() > 0) {
            for (String code : alarmCodeLevels.keySet()) {
                int eventId = eventCodes.getId(code);
                if (!eventCodes.isValidId(eventId))
                    throw new TranslatableJsonException("emport.error.eventCode", code, eventCodes.getCodeList());
                text = alarmCodeLevels.getString(code);
                int level = AlarmLevels.CODES.getId(text);
                if (!AlarmLevels.CODES.isValidId(level))
                    throw new TranslatableJsonException("emport.error.alarmLevel", text, code, AlarmLevels.CODES.getCodeList());
                setAlarmLevel(eventId, level);
            }
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) ExportCodes(com.serotonin.m2m2.util.ExportCodes) PublisherRT(com.serotonin.m2m2.rt.publish.PublisherRT) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject)

Example 7 with JsonArray

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

the class EventHandlerImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ctx.getEventHandlerDao().generateUniqueXid();
    AbstractEventHandlerVO<?> handler = ctx.getEventHandlerDao().getEventHandler(xid);
    if (handler == null) {
        String typeStr = json.getString("handlerType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.eventHandler.missingType", xid, ModuleRegistry.getEventHandlerDefinitionTypes());
        else {
            EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.eventHandler.invalidType", xid, typeStr, ModuleRegistry.getEventHandlerDefinitionTypes());
            else {
                handler = def.baseCreateEventHandlerVO();
                handler.setXid(xid);
            }
        }
    }
    JsonObject et = json.getJsonObject("eventType");
    JsonArray ets = json.getJsonArray("eventTypes");
    EventType eventType = null;
    List<EventType> eventTypes = null;
    try {
        // Find the event type.
        if (et != null)
            eventType = ctx.getReader().read(EventType.class, et);
        else if (ets != null) {
            eventTypes = new ArrayList<EventType>(ets.size());
            Iterator<JsonValue> iter = ets.iterator();
            while (iter.hasNext()) eventTypes.add(ctx.getReader().read(EventType.class, iter.next()));
        }
        ctx.getReader().readInto(handler, json);
        // Now validate it. Use a new response object so we can distinguish errors in this vo from other errors.
        ProcessResult voResponse = new ProcessResult();
        handler.validate(voResponse);
        if (voResponse.getHasMessages())
            setValidationMessages(voResponse, "emport.eventHandler.prefix", xid);
        else {
            // Sweet.
            boolean isnew = handler.getId() == Common.NEW_ID;
            // Save it.
            if (et != null)
                ctx.getEventHandlerDao().saveEventHandler(eventType, handler);
            else
                ctx.getEventHandlerDao().saveEventHandler(handler);
            if (eventTypes != null)
                for (EventType type : eventTypes) ctx.getEventHandlerDao().addEventHandlerMappingIfMissing(handler.getId(), type);
            addSuccessMessage(isnew, "emport.eventHandler.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonArray(com.serotonin.json.type.JsonArray) Iterator(java.util.Iterator)

Example 8 with JsonArray

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

the class CollectionConverter method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
    JsonArray jsonArray = (JsonArray) jsonValue;
    @SuppressWarnings("unchecked") Collection<Object> collection = (Collection<Object>) obj;
    Type innerType = TypeUtils.getActualTypeArgument(type, 0);
    for (JsonValue element : jsonArray) collection.add(reader.read(innerType, element));
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) Type(java.lang.reflect.Type) JsonValue(com.serotonin.json.type.JsonValue) Collection(java.util.Collection)

Example 9 with JsonArray

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

the class GraphicalView method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    if (isNew()) {
        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 components = jsonObject.getJsonArray("viewComponents");
    if (components != null) {
        viewComponents.clear();
        for (JsonValue jv : components) addViewComponent(reader.read(ViewComponent.class, jv));
    }
    String text = jsonObject.getString("anonymousAccess");
    if (text != null) {
        anonymousAccess = ShareUser.ACCESS_CODES.getId(text);
        if (anonymousAccess == -1)
            throw new TranslatableJsonException("emport.error.invalid", "anonymousAccess", text, ShareUser.ACCESS_CODES.getCodeList());
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) ShareUser(com.serotonin.m2m2.view.ShareUser) User(com.serotonin.m2m2.vo.User) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 10 with JsonArray

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

the class MultistateGraphicComponent method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    JsonArray jsonStateList = jsonObject.getJsonArray("stateImageMappings");
    if (jsonStateList != null) {
        stateImageMap.clear();
        for (JsonValue jv : jsonStateList) {
            JsonObject jsonMapping = jv.toJsonObject();
            Integer state = jsonMapping.getInt("state");
            if (state == null)
                throw new TranslatableJsonException("emport.error.missingValue", "state");
            Integer index = jsonMapping.getInt("imageIndex");
            if (index == null)
                throw new TranslatableJsonException("emport.error.missingValue", "index");
            stateImageMap.put(state, index);
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Aggregations

JsonArray (com.serotonin.json.type.JsonArray)39 JsonValue (com.serotonin.json.type.JsonValue)26 JsonObject (com.serotonin.json.type.JsonObject)17 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)14 JsonWriter (com.serotonin.json.JsonWriter)10 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)10 StringWriter (java.io.StringWriter)10 HttpPost (org.apache.http.client.methods.HttpPost)10 StringEntity (org.apache.http.entity.StringEntity)10 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)8 IntStringPair (com.serotonin.db.pair.IntStringPair)6 JsonString (com.serotonin.json.type.JsonString)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 ArrayList (java.util.ArrayList)5 JsonException (com.serotonin.json.JsonException)4 User (com.serotonin.m2m2.vo.User)4 JsonBoolean (com.serotonin.json.type.JsonBoolean)3 IOException (java.io.IOException)3 TypeDefinition (com.serotonin.json.util.TypeDefinition)2 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)2