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);
}
}
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;
}
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;
}
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();
}
}
use of com.serotonin.db.pair.IntStringPair in project ma-core-public by infiniteautomation.
the class VarNames method contextToString.
public static String contextToString(List<IntStringPair> context) {
DataPointDao dataPointDao = DataPointDao.instance;
StringBuilder sb = new StringBuilder();
boolean first = true;
for (IntStringPair ivp : context) {
DataPointVO dp = dataPointDao.getDataPoint(ivp.getKey(), false);
if (first)
first = false;
else
sb.append(", ");
if (dp == null)
sb.append("?=");
else
sb.append(dp.getExtendedName()).append("=");
sb.append(ivp.getValue());
}
return sb.toString();
}
Aggregations