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);
}
}
}
}
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));
}
}
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));
}
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());
}
}
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);
}
}
}
Aggregations