Search in sources :

Example 11 with JsonArray

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();
}
Also used : User(com.serotonin.m2m2.vo.User) UnknownHostException(java.net.UnknownHostException) JsonValue(com.serotonin.json.type.JsonValue) ArrayList(java.util.ArrayList) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) FileNotFoundException(java.io.FileNotFoundException) ModuleRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.ModuleRestV2Exception) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JsonArray(com.serotonin.json.type.JsonArray) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ModuleUpgradeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.modules.ModuleUpgradeModel) SocketTimeoutException(java.net.SocketTimeoutException) ModuleUpgradesModel(com.serotonin.m2m2.web.mvc.rest.v1.model.modules.ModuleUpgradesModel) JsonString(com.serotonin.json.type.JsonString) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Module(com.serotonin.m2m2.module.Module) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with JsonArray

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(","));
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject)

Example 13 with JsonArray

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);
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) User(com.serotonin.m2m2.vo.User) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException) RecipientListEntryBean(com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean)

Example 14 with JsonArray

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();
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) User(com.serotonin.m2m2.vo.User) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject)

Example 15 with JsonArray

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();
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) JsonNumber(com.serotonin.json.type.JsonNumber) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) List(java.util.List) JsonBoolean(com.serotonin.json.type.JsonBoolean) RecipientListEntryBean(com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Aggregations

JsonArray (com.serotonin.json.type.JsonArray)39 JsonValue (com.serotonin.json.type.JsonValue)26 JsonObject (com.serotonin.json.type.JsonObject)17 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)14 JsonWriter (com.serotonin.json.JsonWriter)10 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)10 StringWriter (java.io.StringWriter)10 HttpPost (org.apache.http.client.methods.HttpPost)10 StringEntity (org.apache.http.entity.StringEntity)10 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)8 IntStringPair (com.serotonin.db.pair.IntStringPair)6 JsonString (com.serotonin.json.type.JsonString)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 ArrayList (java.util.ArrayList)5 JsonException (com.serotonin.json.JsonException)4 User (com.serotonin.m2m2.vo.User)4 JsonBoolean (com.serotonin.json.type.JsonBoolean)3 IOException (java.io.IOException)3 TypeDefinition (com.serotonin.json.util.TypeDefinition)2 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)2