use of com.serotonin.json.type.JsonObject in project ma-core-public by infiniteautomation.
the class ModulesDwr method versionCheck.
@DwrPermission(admin = true)
public ProcessResult versionCheck() {
ProcessResult result = new ProcessResult();
if (UPGRADE_DOWNLOADER != null) {
result.addData("error", Common.translate("modules.versionCheck.occupied"));
return result;
}
try {
JsonValue jsonResponse = getAvailableUpgrades();
if (jsonResponse instanceof JsonString)
result.addData("error", jsonResponse.toString());
else {
JsonObject root = jsonResponse.toJsonObject();
result.addData("upgrades", root.get("upgrades").toNative());
result.addData("newInstalls", root.get("newInstalls").toNative());
if (root.containsKey("upgradesError"))
result.addData("upgradesError", root.getString("upgradesError"));
if (root.containsKey("updates")) {
result.addData("updates", root.get("updates").toNative());
result.addData("newInstalls-oldCore", root.get("newInstalls-oldCore").toNative());
}
if (root.containsKey("missingModules"))
result.addData("missingModules", root.getJsonArray("missingModules").toNative());
}
} catch (UnknownHostException e) {
LOG.error("", e);
result.addData("unknownHost", e.getMessage());
} catch (Exception e) {
LOG.error("", e);
result.addData("error", e.getMessage());
}
return result;
}
use of com.serotonin.json.type.JsonObject 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.JsonObject in project ma-core-public by infiniteautomation.
the class MangoTestBase method loadConfiguration.
protected void loadConfiguration(File jsonFile) throws JsonException, IOException, URISyntaxException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile), Common.UTF8_CS));
JsonReader jr = new JsonReader(reader);
JsonObject jo = jr.read(JsonObject.class);
ImportTask task = new ImportTask(jo, Common.getTranslations(), null, false);
task.run(Common.timer.currentTimeMillis());
if (task.getResponse().getHasMessages()) {
for (ProcessMessage message : task.getResponse().getMessages()) {
switch(message.getLevel()) {
case error:
case warning:
fail(message.toString(Common.getTranslations()));
case info:
LOG.info(message.toString(Common.getTranslations()));
}
}
}
}
use of com.serotonin.json.type.JsonObject 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.JsonObject in project ma-core-public by infiniteautomation.
the class MapConverter method jsonWrite.
@Override
public JsonValue jsonWrite(JsonTypeWriter writer, Object value) throws JsonException {
Map<?, ?> map = (Map<?, ?>) value;
JsonObject jsonObject = new JsonObject();
for (Map.Entry<?, ?> entry : map.entrySet()) jsonObject.put(entry.getKey().toString(), writer.writeObject(entry.getValue()));
return jsonObject;
}
Aggregations