Search in sources :

Example 1 with TypeDefinition

use of com.serotonin.json.util.TypeDefinition in project ma-core-public by infiniteautomation.

the class EmailEventHandlerVO method jsonRead.

@SuppressWarnings("unchecked")
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    String text = null;
    TypeDefinition recipType = new TypeDefinition(List.class, RecipientListEntryBean.class);
    JsonArray jsonActiveRecipients = jsonObject.getJsonArray("activeRecipients");
    if (jsonActiveRecipients != null)
        activeRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonActiveRecipients);
    JsonBoolean b = jsonObject.getJsonBoolean("sendEscalation");
    if (b != null)
        sendEscalation = b.booleanValue();
    if (sendEscalation) {
        text = jsonObject.getString("escalationDelayType");
        if (text != null) {
            escalationDelayType = Common.TIME_PERIOD_CODES.getId(text);
            if (escalationDelayType == -1)
                throw new TranslatableJsonException("emport.error.invalid", "escalationDelayType", text, Common.TIME_PERIOD_CODES.getCodeList());
        }
        Integer i = jsonObject.getInt("escalationDelay", 1);
        if (i != null)
            escalationDelay = i;
        JsonArray jsonEscalationRecipients = jsonObject.getJsonArray("escalationRecipients");
        if (jsonEscalationRecipients != null)
            escalationRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonEscalationRecipients);
        b = jsonObject.getJsonBoolean("keepSendingEscalations");
        if (b != null)
            repeatEscalations = b.booleanValue();
    }
    b = jsonObject.getJsonBoolean("sendInactive");
    if (b != null)
        sendInactive = b.booleanValue();
    if (sendInactive) {
        b = jsonObject.getJsonBoolean("inactiveOverride");
        if (b != null)
            inactiveOverride = b.booleanValue();
        if (inactiveOverride) {
            JsonArray jsonInactiveRecipients = jsonObject.getJsonArray("inactiveRecipients");
            if (jsonInactiveRecipients != null)
                inactiveRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonInactiveRecipients);
        }
    }
    b = jsonObject.getJsonBoolean("includeSystemInformation");
    if (b != null)
        includeSystemInfo = b.booleanValue();
    includePointValueCount = jsonObject.getInt("includePointValueCount", 0);
    b = jsonObject.getJsonBoolean("includeLogfile");
    if (b != null)
        includeSystemInfo = b.booleanValue();
    customTemplate = jsonObject.getString("customTemplate");
    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");
            DataPointVO dpvo = DataPointDao.instance.getByXid(dataPointXid);
            if (dpvo == 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(dpvo.getId(), contextKey));
        }
        this.additionalContext = additionalContext;
    } else
        this.additionalContext = new ArrayList<>();
    script = jsonObject.getString("script");
    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 : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) 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) TypeDefinition(com.serotonin.json.util.TypeDefinition) JsonArray(com.serotonin.json.type.JsonArray) ArrayList(java.util.ArrayList) List(java.util.List) JsonBoolean(com.serotonin.json.type.JsonBoolean)

Example 2 with TypeDefinition

use of com.serotonin.json.util.TypeDefinition in project ma-core-public by infiniteautomation.

the class PointHierarchyImporter method importImpl.

@Override
protected void importImpl() {
    try {
        PointHierarchy hierarchy = ctx.getDataPointDao().getPointHierarchy(false);
        @SuppressWarnings("unchecked") List<PointFolder> subfolders = (List<PointFolder>) ctx.getReader().read(new TypeDefinition(List.class, PointFolder.class), json);
        // Merge the new subfolders into the existing point heirarchy.
        hierarchy.mergeFolders(subfolders);
        ph = hierarchy;
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.pointHierarchy.prefix", e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.pointHierarchy.prefix", getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) PointHierarchy(com.serotonin.m2m2.vo.hierarchy.PointHierarchy) PointFolder(com.serotonin.m2m2.vo.hierarchy.PointFolder) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) List(java.util.List) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Example 3 with TypeDefinition

use of com.serotonin.json.util.TypeDefinition in project ma-core-public by infiniteautomation.

the class EventHandlerVO method jsonRead.

@SuppressWarnings("unchecked")
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    DataPointDao dataPointDao = DataPointDao.instance;
    String text = jsonObject.getString("handlerType");
    if (text != null) {
        handlerType = TYPE_CODES.getId(text);
        if (!TYPE_CODES.isValidId(handlerType))
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "handlerType", text, TYPE_CODES.getCodeList());
    }
    if (handlerType == TYPE_SET_POINT) {
        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
        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;
        }
        // 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 (handlerType == TYPE_EMAIL) {
        TypeDefinition recipType = new TypeDefinition(List.class, RecipientListEntryBean.class);
        JsonArray jsonActiveRecipients = jsonObject.getJsonArray("activeRecipients");
        if (jsonActiveRecipients != null)
            activeRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonActiveRecipients);
        JsonBoolean b = jsonObject.getJsonBoolean("sendEscalation");
        if (b != null)
            sendEscalation = b.booleanValue();
        if (sendEscalation) {
            text = jsonObject.getString("escalationDelayType");
            if (text != null) {
                escalationDelayType = Common.TIME_PERIOD_CODES.getId(text);
                if (escalationDelayType == -1)
                    throw new TranslatableJsonException("emport.error.invalid", "escalationDelayType", text, Common.TIME_PERIOD_CODES.getCodeList());
            }
            JsonNumber i = jsonObject.getJsonNumber("escalationDelay");
            if (i != null)
                escalationDelay = i.intValue();
            JsonArray jsonEscalationRecipients = jsonObject.getJsonArray("escalationRecipients");
            if (jsonEscalationRecipients != null)
                escalationRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonEscalationRecipients);
        }
        b = jsonObject.getJsonBoolean("sendInactive");
        if (b != null)
            sendInactive = b.booleanValue();
        if (sendInactive) {
            b = jsonObject.getJsonBoolean("inactiveOverride");
            if (b != null)
                inactiveOverride = b.booleanValue();
            if (inactiveOverride) {
                JsonArray jsonInactiveRecipients = jsonObject.getJsonArray("inactiveRecipients");
                if (jsonInactiveRecipients != null)
                    inactiveRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonInactiveRecipients);
            }
        }
        b = jsonObject.getJsonBoolean("includeSystemInformation");
        if (b != null) {
            includeSystemInfo = b.booleanValue();
        }
        includePointValueCount = jsonObject.getInt("includePointValueCount", 0);
        b = jsonObject.getJsonBoolean("includeLogfile");
        if (b != null) {
            includeSystemInfo = b.booleanValue();
        }
    } else if (handlerType == TYPE_PROCESS) {
        text = jsonObject.getString("activeProcessCommand");
        if (text != null)
            activeProcessCommand = text;
        JsonNumber i = jsonObject.getJsonNumber("activeProcessTimeout");
        if (i != null)
            activeProcessTimeout = i.intValue();
        text = jsonObject.getString("inactiveProcessCommand");
        if (text != null)
            inactiveProcessCommand = text;
        i = jsonObject.getJsonNumber("inactiveProcessTimeout");
        if (i != null)
            inactiveProcessTimeout = i.intValue();
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) JsonNumber(com.serotonin.json.type.JsonNumber) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) List(java.util.List) JsonBoolean(com.serotonin.json.type.JsonBoolean) RecipientListEntryBean(com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Example 4 with TypeDefinition

use of com.serotonin.json.util.TypeDefinition in project ma-core-public by infiniteautomation.

the class GenericTest method main.

public static void main(String[] args) throws Exception {
    Class<?> varClass = GenObject.class;
    Method method = varClass.getDeclaredMethod("getValue");
    ParameterizedType paramType = new TypeDefinition(List.class, String.class);
    ParameterizedType type = new TypeDefinition(varClass, paramType);
    // System.out.println(TypeUtils.determineTypeArguments(varClass,
    // (ParameterizedType) varClass.getGenericSuperclass()));
    System.out.println(TypeUtils.resolveTypeVariable(type, method.getGenericReturnType()));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Method(java.lang.reflect.Method) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Example 5 with TypeDefinition

use of com.serotonin.json.util.TypeDefinition in project ma-core-public by infiniteautomation.

the class SubclassTest method main.

public static void main(String[] args) throws Exception {
    JsonContext context = new JsonContext();
    context.addResolver(new TypeResolver() {

        @Override
        public Class<?> resolve(JsonValue jsonValue) throws JsonException {
            if (jsonValue.toJsonObject().containsKey("sub1Value"))
                return Subclass1.class;
            if (jsonValue.toJsonObject().containsKey("sub2Value"))
                return Subclass2.class;
            throw new JsonException("Unknown BaseClass: " + jsonValue);
        }
    }, BaseClass.class);
    // context.addFactory(new ObjectFactory() {
    // @Override
    // public Object create(JsonValue jsonValue) throws JsonException {
    // if (jsonValue.toJsonObject().hasProperty("sub1Value"))
    // return new Subclass1();
    // if (jsonValue.toJsonObject().hasProperty("sub2Value"))
    // return new Subclass2();
    // throw new JsonException("Unknown BaseClass: " + jsonValue);
    // }
    // }, BaseClass.class);
    // List<BaseClass> list = new ArrayList<BaseClass>();
    // list.add(new Subclass1());
    // list.add(new Subclass2());
    // 
    // String json = JsonWriter.writeToString(context, list);
    // 
    // System.out.println(json);
    String json = "[{\"id\":\"Subclass1\",\"sub1Value\":\"a\",\"baseValue\":\"b\"},{\"myId\":\"Subclass2\",\"sub2Value\":\"c\",\"baseValue\":\"d\"}]";
    JsonReader reader = new JsonReader(context, json);
    TypeDefinition type = new TypeDefinition(List.class, BaseClass.class);
    Object read = reader.read(type);
    System.out.println(read);
}
Also used : JsonException(com.serotonin.json.JsonException) JsonContext(com.serotonin.json.JsonContext) TypeResolver(com.serotonin.json.spi.TypeResolver) JsonValue(com.serotonin.json.type.JsonValue) JsonReader(com.serotonin.json.JsonReader) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Aggregations

TypeDefinition (com.serotonin.json.util.TypeDefinition)8 List (java.util.List)4 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 ArrayList (java.util.ArrayList)3 JsonException (com.serotonin.json.JsonException)2 JsonReader (com.serotonin.json.JsonReader)2 JsonArray (com.serotonin.json.type.JsonArray)2 JsonBoolean (com.serotonin.json.type.JsonBoolean)2 JsonValue (com.serotonin.json.type.JsonValue)2 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 IntStringPair (com.serotonin.db.pair.IntStringPair)1 JsonContext (com.serotonin.json.JsonContext)1 TypeResolver (com.serotonin.json.spi.TypeResolver)1 JsonNumber (com.serotonin.json.type.JsonNumber)1 JsonObject (com.serotonin.json.type.JsonObject)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 PointFolder (com.serotonin.m2m2.vo.hierarchy.PointFolder)1