use of com.serotonin.json.type.JsonArray in project ma-modules-public by infiniteautomation.
the class ModulesRestController method getUpgrades.
@ApiOperation(value = "Get Available Upgrades", notes = "Check the store for Upgrades")
@RequestMapping(method = RequestMethod.GET, value = "/upgrades-available", produces = { "application/json" })
public ResponseEntity<ModuleUpgradesModel> getUpgrades(HttpServletRequest request) {
RestProcessResult<ModuleUpgradesModel> result = new RestProcessResult<ModuleUpgradesModel>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
if (Permissions.hasAdmin(user)) {
// Do the check
try {
JsonValue jsonResponse = ModulesDwr.getAvailableUpgrades();
if (jsonResponse instanceof JsonString) {
result.addRestMessage(getInternalServerErrorMessage(jsonResponse.toString()));
} else {
List<ModuleUpgradeModel> upgrades = new ArrayList<>();
List<ModuleUpgradeModel> newInstalls = new ArrayList<>();
ModuleUpgradesModel model = new ModuleUpgradesModel(upgrades, newInstalls);
JsonObject root = jsonResponse.toJsonObject();
JsonValue jsonUpgrades = root.get("upgrades");
JsonArray jsonUpgradesArray = jsonUpgrades.toJsonArray();
Iterator<JsonValue> it = jsonUpgradesArray.iterator();
while (it.hasNext()) {
JsonValue v = it.next();
if (v.getJsonValue("name") == null) {
it.remove();
continue;
}
String name = v.getJsonValue("name").toString();
Module module = "core".equals(name) ? ModuleRegistry.getCoreModule() : ModuleRegistry.getModule(name);
if (module == null) {
it.remove();
continue;
}
upgrades.add(new ModuleUpgradeModel(module, v));
}
JsonValue jsonInstalls = root.get("newInstalls");
JsonArray jsonInstallsArray = jsonInstalls.toJsonArray();
for (JsonValue v : jsonInstallsArray) {
newInstalls.add(new ModuleUpgradeModel(v));
}
return result.createResponseEntity(model);
}
} catch (SocketTimeoutException e) {
result.addRestMessage(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("rest.error.requestTimeout", Common.envProps.getString("store.url")));
} catch (UnknownHostException e) {
result.addRestMessage(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("rest.error.unknownHost", Common.envProps.getString("store.url")));
} catch (Exception e) {
result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
}
} else {
result.addRestMessage(this.getUnauthorizedMessage());
}
}
return result.createResponseEntity();
}
use of com.serotonin.json.type.JsonArray in project ma-modules-public by infiniteautomation.
the class ModuleUpgradeModel method loadFromStoreJson.
public void loadFromStoreJson(JsonValue v) {
this.dependencyVersions = new HashMap<>();
JsonValue sub = v.getJsonValue("name");
if (sub != null)
this.name = sub.toString();
sub = v.getJsonValue("version");
if (sub != null)
this.newVersion = sub.toString();
sub = v.getJsonValue("fullVersion");
if (sub != null)
this.newVersion = sub.toString();
sub = v.getJsonValue("shortDescription");
if (sub != null)
this.description = sub.toString();
sub = v.getJsonValue("longDescription");
if (sub != null)
this.longDescription = sub.toString();
sub = v.getJsonValue("vendorName");
if (sub != null)
this.vendor = sub.toString();
sub = v.getJsonValue("vendorUrl");
if (sub != null)
this.vendorUrl = sub.toString();
sub = v.getJsonValue("releaseNotes");
if (sub != null)
this.releaseNotes = sub.toString();
JsonValue dependencies = v.getJsonValue("dependencies");
JsonValue fullDependencies = v.getJsonValue("fullDependencies");
if (dependencies instanceof JsonArray && !(fullDependencies instanceof JsonObject)) {
dependencies.toList().forEach((obj) -> {
String moduleName = obj.toString();
String versionRange = Common.getVersion().getMajorVersion() + "." + Common.getVersion().getMinorVersion();
this.dependencyVersions.put(moduleName, versionRange);
});
}
if (fullDependencies instanceof JsonObject) {
fullDependencies.toMap().forEach((key, value) -> {
this.dependencyVersions.put(key, value.toString());
});
}
this.dependencies = this.dependencyVersions.entrySet().stream().map((entry) -> {
return entry.getKey() + ":" + entry.getValue();
}).collect(Collectors.joining(","));
}
use of com.serotonin.json.type.JsonArray in project ma-modules-public by infiniteautomation.
the class ReportVO method jsonRead.
/* (non-Javadoc)
* @see com.serotonin.json.spi.JsonSerializable#jsonRead(com.serotonin.json.JsonReader, com.serotonin.json.type.JsonObject)
*/
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
super.jsonRead(reader, jsonObject);
if (jsonObject.containsKey("userId")) {
userId = jsonObject.getInt("userId");
} else if (jsonObject.containsKey("user")) {
String username = jsonObject.getString("user");
if (org.apache.commons.lang3.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();
}
String text = jsonObject.getString("includeEvents");
if (text != null) {
includeEvents = EVENT_CODES.getId(text);
if (includeEvents == -1)
throw new TranslatableJsonException("emport.error.invalid", "includeEvents", text, EVENT_CODES.getCodeList());
}
text = jsonObject.getString("dateRangeType");
if (text != null) {
dateRangeType = DATE_RANGE_TYPES.getId(text);
if (dateRangeType == -1)
throw new TranslatableJsonException("emport.error.invalid", "dateRangeType", text, DATE_RANGE_TYPES.getCodeList());
}
if (dateRangeType == DATE_RANGE_TYPE_RELATIVE) {
text = jsonObject.getString("relativeDateType");
if (text != null) {
relativeDateType = DATE_RELATIVE_TYPES.getId(text);
if (relativeDateType == -1)
throw new TranslatableJsonException("emport.error.invalid", "relativeDateType", text, DATE_RELATIVE_TYPES.getCodeList());
}
if (relativeDateType == RELATIVE_DATE_TYPE_PREVIOUS) {
text = jsonObject.getString("previousPeriodType");
if (text != null) {
previousPeriodType = Common.TIME_PERIOD_CODES.getId(text);
if (previousPeriodType == -1)
throw new TranslatableJsonException("emport.error.invalid", "previousPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
previousPeriodCount = jsonObject.getInt("previousPeriods");
} else {
// FOR legacy bug where previousPeriodType was misspelled
text = jsonObject.getString("perviousPeriodType");
if (text != null) {
previousPeriodType = Common.TIME_PERIOD_CODES.getId(text);
if (previousPeriodType == -1)
throw new TranslatableJsonException("emport.error.invalid", "previousPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
previousPeriodCount = jsonObject.getInt("previousPeriods");
}
}
} else if (relativeDateType == RELATIVE_DATE_TYPE_PREVIOUS) {
text = jsonObject.getString("pastPeriodType");
if (text != null) {
pastPeriodType = Common.TIME_PERIOD_CODES.getId(text);
if (pastPeriodType == -1)
throw new TranslatableJsonException("emport.error.invalid", "pastPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
pastPeriodCount = jsonObject.getInt("pastPeriods");
}
}
} else if (dateRangeType == DATE_RANGE_TYPE_SPECIFIC) {
fromNone = jsonObject.getBoolean("fromInception");
if (!fromNone) {
fromYear = jsonObject.getInt("fromYear");
fromMonth = jsonObject.getInt("fromMonth");
fromDay = jsonObject.getInt("fromDay");
fromHour = jsonObject.getInt("fromHour");
fromMinute = jsonObject.getInt("fromMinute");
}
toNone = jsonObject.getBoolean("toLatest");
if (!toNone) {
toYear = jsonObject.getInt("toYear");
toMonth = jsonObject.getInt("toMonth");
toDay = jsonObject.getInt("toDay");
toHour = jsonObject.getInt("toHour");
toMinute = jsonObject.getInt("toMinute");
}
}
schedule = jsonObject.getBoolean("schedule");
if (schedule) {
text = jsonObject.getString("schedulePeriod");
if (text != null) {
schedulePeriod = SCHEDULE_CODES.getId(text);
if (schedulePeriod == -1)
throw new TranslatableJsonException("emport.error.invalid", "schedulePeriod", text, SCHEDULE_CODES.getCodeList());
if (schedulePeriod == SCHEDULE_CRON) {
scheduleCron = jsonObject.getString("scheduleCron");
try {
new CronTimerTrigger(scheduleCron);
} catch (ParseException e) {
throw new TranslatableJsonException("emport.error.invalid", "scheduleCron", scheduleCron, "cron expressions");
}
}
} else {
throw new TranslatableJsonException("emport.error.invalid", "schedulePeriod", "null", SCHEDULE_CODES.getCodeList());
}
}
email = jsonObject.getBoolean("email");
if (email) {
JsonArray recipientsArray = jsonObject.getJsonArray("recipients");
boolean add = true;
if (recipientsArray != null) {
for (JsonValue jv : recipientsArray) {
RecipientListEntryBean recipient = new RecipientListEntryBean();
reader.readInto(recipient, jv);
for (RecipientListEntryBean existing : recipients) {
if (existing.equals(recipient)) {
reader.readInto(existing, jv);
add = false;
break;
}
}
if (add) {
recipients.add(recipient);
} else {
add = true;
}
}
} else {
throw new TranslatableJsonException("emport.error.invalid", "recipients", "null", "valid users, email addresses or mailing lists");
}
includeData = jsonObject.getBoolean("includeData");
if (includeData)
zipData = jsonObject.getBoolean("zipData");
}
JsonArray pointsArray = jsonObject.getJsonArray("points");
if (pointsArray != null) {
points = new ArrayList<ReportPointVO>();
for (JsonValue jv : pointsArray) {
ReportPointVO reportPoint = new ReportPointVO();
reader.readInto(reportPoint, jv);
// TODO prevent adding the same point twice?
points.add(reportPoint);
}
}
}
use of com.serotonin.json.type.JsonArray in project ma-modules-public by infiniteautomation.
the class WatchListVO method jsonRead.
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
super.jsonRead(reader, jsonObject);
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 jsonDataPoints = jsonObject.getJsonArray("dataPoints");
if (jsonDataPoints != null) {
pointList.clear();
DataPointDao dataPointDao = DataPointDao.instance;
for (JsonValue jv : jsonDataPoints) {
String xid = jv.toString();
DataPointVO dpVO = dataPointDao.getDataPoint(xid);
if (dpVO == null)
throw new TranslatableJsonException("emport.error.missingPoint", xid);
pointList.add(dpVO);
}
}
JsonObject o = jsonObject.getJsonObject("data");
if (o != null)
this.data = o.toMap();
}
use of com.serotonin.json.type.JsonArray 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();
}
}
Aggregations