Search in sources :

Example 6 with IntStringPair

use of com.serotonin.db.pair.IntStringPair in project ma-modules-public by infiniteautomation.

the class MultistateGraphicComponent method setImageStateList.

public void setImageStateList(List<IntStringPair> imageStateList) {
    stateImageMap.clear();
    for (IntStringPair ivp : imageStateList) {
        String[] states = ivp.getValue().split(",");
        for (String stateStr : states) {
            try {
                int state = Integer.parseInt(stateStr.trim());
                stateImageMap.put(state, ivp.getKey());
            } catch (NumberFormatException e) {
            // Ignore
            }
        }
    }
}
Also used : IntStringPair(com.serotonin.db.pair.IntStringPair)

Example 7 with IntStringPair

use of com.serotonin.db.pair.IntStringPair in project ma-core-public by infiniteautomation.

the class EmailEventHandlerVO method jsonWrite.

@Override
public void jsonWrite(ObjectWriter writer) throws IOException, JsonException {
    super.jsonWrite(writer);
    writer.writeEntry("activeRecipients", activeRecipients);
    writer.writeEntry("sendEscalation", sendEscalation);
    if (sendEscalation) {
        writer.writeEntry("escalationDelayType", Common.TIME_PERIOD_CODES.getCode(escalationDelayType));
        writer.writeEntry("escalationDelay", escalationDelay);
        writer.writeEntry("escalationRecipients", escalationRecipients);
        writer.writeEntry("keepSendingEscalations", repeatEscalations);
    }
    writer.writeEntry("sendInactive", sendInactive);
    if (sendInactive) {
        writer.writeEntry("inactiveOverride", inactiveOverride);
        if (inactiveOverride)
            writer.writeEntry("inactiveRecipients", inactiveRecipients);
    }
    writer.writeEntry("includeSystemInformation", includeSystemInfo);
    writer.writeEntry("includePointValueCount", includePointValueCount);
    writer.writeEntry("includeLogfile", includeLogfile);
    writer.writeEntry("customTemplate", customTemplate);
    JsonArray context = new JsonArray();
    for (IntStringPair pnt : additionalContext) {
        DataPointVO dpvo = DataPointDao.instance.get(pnt.getKey());
        if (dpvo != null) {
            JsonObject point = new JsonObject();
            point.put("dataPointXid", dpvo.getXid());
            point.put("contextKey", pnt.getValue());
            context.add(point);
        }
    }
    writer.writeEntry("additionalContext", context);
    writer.writeEntry("script", script);
    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 8 with IntStringPair

use of com.serotonin.db.pair.IntStringPair 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 9 with IntStringPair

use of com.serotonin.db.pair.IntStringPair in project ma-core-public by infiniteautomation.

the class EventHandlersDwr method validateScript.

@DwrPermission(user = true)
public ProcessResult validateScript(String script, Integer targetPointId, int type, List<IntStringPair> additionalContext, ScriptPermissions scriptPermissions) {
    ProcessResult response = new ProcessResult();
    TranslatableMessage message;
    Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
    int targetDataType;
    if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE || type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE) {
        DataPointRT target = targetPointId == null ? null : Common.runtimeManager.getDataPoint(targetPointId.intValue());
        if (target == null) {
            DataPointVO targetVo = targetPointId == null ? null : DataPointDao.instance.getDataPoint(targetPointId.intValue(), false);
            if (targetVo == null) {
                if (// These are passed in the validateScript of eventHandlers.jsp
                type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
                    response.addMessage("activeScript", new TranslatableMessage("eventHandlers.noTargetPoint"));
                else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
                    response.addMessage("inactiveScript", new TranslatableMessage("eventHandlers.noTargetPoint"));
                return response;
            }
            if (targetVo.getDefaultCacheSize() == 0)
                targetVo.setDefaultCacheSize(1);
            target = new DataPointRT(targetVo, targetVo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(targetVo.getDataSourceId()), null);
            target.resetValues();
            context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, target);
        }
        targetDataType = target.getDataTypeId();
    } else {
        targetDataType = DataTypes.ALPHANUMERIC;
    }
    for (IntStringPair cxt : additionalContext) {
        DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
        if (dprt == null) {
            DataPointVO dpvo = DataPointDao.instance.getDataPoint(cxt.getKey(), false);
            if (dpvo == null) {
                if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
                    response.addMessage("activeScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
                else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
                    response.addMessage("inactiveScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
                else if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
                    response.addMessage("emailScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
                return response;
            }
            if (dpvo.getDefaultCacheSize() == 0)
                dpvo.setDefaultCacheSize(1);
            dprt = new DataPointRT(dpvo, dpvo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(dpvo.getDataSourceId()), null);
            dprt.resetValues();
        }
        context.put(cxt.getValue(), dprt);
    }
    Map<String, Object> otherContext = new HashMap<String, Object>();
    otherContext.put(SetPointEventHandlerVO.EVENT_CONTEXT_KEY, getTestEvent());
    if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
        otherContext.put("model", new HashMap<String, Object>());
    final StringWriter scriptOut = new StringWriter();
    final PrintWriter scriptWriter = new PrintWriter(scriptOut);
    final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYY HH:mm:ss");
    ScriptPointValueSetter loggingSetter = new ScriptPointValueSetter(scriptPermissions) {

        @Override
        public void set(IDataPointValueSource point, Object value, long timestamp, String annotation) {
            DataPointRT dprt = (DataPointRT) point;
            if (!dprt.getVO().getPointLocator().isSettable()) {
                scriptOut.append("Point " + dprt.getVO().getExtendedName() + " not settable.");
                return;
            }
            if (!Permissions.hasPermission(dprt.getVO().getSetPermission(), permissions.getDataPointSetPermissions())) {
                scriptOut.write(new TranslatableMessage("pointLinks.setTest.permissionDenied", dprt.getVO().getXid()).translate(Common.getTranslations()));
                return;
            }
            scriptOut.append("Setting point " + dprt.getVO().getName() + " to " + value + " @" + sdf.format(new Date(timestamp)) + "\r\n");
        }

        @Override
        protected void setImpl(IDataPointValueSource point, Object value, long timestamp, String annotation) {
        // not really setting
        }
    };
    try {
        CompiledScript compiledScript = CompiledScriptExecutor.compile(script);
        PointValueTime pvt = CompiledScriptExecutor.execute(compiledScript, context, otherContext, System.currentTimeMillis(), targetDataType, System.currentTimeMillis(), scriptPermissions, scriptWriter, new ScriptLog(SetPointHandlerRT.NULL_WRITER, LogLevel.FATAL), loggingSetter, null, true);
        if (pvt.getValue() == null)
            message = new TranslatableMessage("eventHandlers.script.nullResult");
        else if (CompiledScriptExecutor.UNCHANGED == pvt.getValue())
            message = new TranslatableMessage("eventHandlers.script.successUnchanged");
        else
            message = new TranslatableMessage("eventHandlers.script.success", pvt.getValue());
        // Add the script logging output
        response.addData("out", scriptOut.toString().replaceAll("\n", "<br/>"));
    } catch (ScriptPermissionsException e) {
        message = e.getTranslatableMessage();
    } catch (ScriptException e) {
        message = new TranslatableMessage("eventHandlers.script.failure", e.getMessage());
    } catch (ResultTypeException e) {
        message = e.getTranslatableMessage();
    }
    if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
        response.addMessage("activeScript", message);
    else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
        response.addMessage("inactiveScript", message);
    else if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
        response.addMessage("emailScript", message);
    return response;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) CompiledScript(javax.script.CompiledScript) ScriptPointValueSetter(com.serotonin.m2m2.rt.script.ScriptPointValueSetter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IntStringPair(com.serotonin.db.pair.IntStringPair) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) Date(java.util.Date) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) StringWriter(java.io.StringWriter) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) SimpleDateFormat(java.text.SimpleDateFormat) PrintWriter(java.io.PrintWriter) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 10 with IntStringPair

use of com.serotonin.db.pair.IntStringPair in project ma-core-public by infiniteautomation.

the class SetPointEventHandlerVO method readObject.

@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    int ver = in.readInt();
    // Switch on the version of the class so that version changes can be elegantly handled.
    if (ver == 1) {
        targetPointId = in.readInt();
        activeAction = in.readInt();
        activeValueToSet = SerializationHelper.readSafeUTF(in);
        activePointId = in.readInt();
        inactiveAction = in.readInt();
        inactiveValueToSet = SerializationHelper.readSafeUTF(in);
        inactivePointId = in.readInt();
        activeScript = inactiveScript = null;
        additionalContext = new ArrayList<IntStringPair>();
        scriptPermissions = new ScriptPermissions();
    } else if (ver == 2) {
        targetPointId = in.readInt();
        activeAction = in.readInt();
        activeValueToSet = SerializationHelper.readSafeUTF(in);
        activePointId = in.readInt();
        inactiveAction = in.readInt();
        inactiveValueToSet = SerializationHelper.readSafeUTF(in);
        inactivePointId = in.readInt();
        activeScript = SerializationHelper.readSafeUTF(in);
        inactiveScript = SerializationHelper.readSafeUTF(in);
        additionalContext = new ArrayList<IntStringPair>();
        scriptPermissions = new ScriptPermissions();
    } else if (ver == 3) {
        targetPointId = in.readInt();
        activeAction = in.readInt();
        activeValueToSet = SerializationHelper.readSafeUTF(in);
        activePointId = in.readInt();
        inactiveAction = in.readInt();
        inactiveValueToSet = SerializationHelper.readSafeUTF(in);
        inactivePointId = in.readInt();
        activeScript = SerializationHelper.readSafeUTF(in);
        inactiveScript = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptPermissions = (ScriptPermissions) in.readObject();
    }
}
Also used : IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList) ScriptPermissions(com.serotonin.m2m2.rt.script.ScriptPermissions)

Aggregations

IntStringPair (com.serotonin.db.pair.IntStringPair)31 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)10 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)9 JsonArray (com.serotonin.json.type.JsonArray)6 JsonObject (com.serotonin.json.type.JsonObject)6 LinkedHashMap (java.util.LinkedHashMap)6 ScriptException (javax.script.ScriptException)5 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)4 User (com.serotonin.m2m2.vo.User)4 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 JsonValue (com.serotonin.json.type.JsonValue)3 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)3 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)3 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)3