Search in sources :

Example 21 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-modules-public by infiniteautomation.

the class CompoundComponent method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    JsonObject jsonChildren = jsonObject.getJsonObject("children");
    if (jsonChildren != null) {
        for (Map.Entry<String, JsonValue> jsonChild : jsonChildren.entrySet()) {
            CompoundChild child = getChild(jsonChild.getKey());
            if (child == null || !child.getViewComponent().isPointComponent())
                throw new TranslatableJsonException("emport.error.compound.invalidChildId", jsonChild.getKey(), definition().getId(), getPointComponentChildIds());
            jsonReadDataPoint(jsonChild.getValue(), (PointComponent) child.getViewComponent());
        }
    }
}
Also used : JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with JsonObject

use of com.serotonin.json.type.JsonObject 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);
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 23 with JsonObject

use of com.serotonin.json.type.JsonObject 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 24 with JsonObject

use of com.serotonin.json.type.JsonObject 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 25 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-modules-public by infiniteautomation.

the class ReportEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
    JsonObject reportJson = jsonValue.toJsonObject();
    String xid = reportJson.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ReportDao.instance.generateUniqueXid();
    ReportVO report = null;
    try {
        report = ReportDao.instance.getReport(xid);
    } catch (IncorrectResultSizeDataAccessException e) {
        importContext.getResult().addGenericMessage("reports.emport.duplicateXids", xid);
        return;
    }
    if (report == null) {
        report = new ReportVO();
        report.setXid(xid);
    }
    try {
        importContext.getReader().readInto(report, reportJson);
        // Now validate it. Use a new response object so we can distinguish errors in this user from other
        // errors.
        ProcessResult reportResponse = new ProcessResult();
        report.validate(reportResponse);
        if (reportResponse.getHasMessages())
            // Too bad. Copy the errors into the actual response.
            importContext.copyValidationMessages(reportResponse, "emport.report.prefix", xid);
        else {
            // Sweet. Save it.
            boolean isnew = report.getId() == Common.NEW_ID;
            ReportDao.instance.saveReport(report);
            importContext.addSuccessMessage(isnew, "emport.report.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.report.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.report.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) ReportVO(com.serotonin.m2m2.reports.vo.ReportVO)

Aggregations

JsonObject (com.serotonin.json.type.JsonObject)44 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)24 JsonValue (com.serotonin.json.type.JsonValue)20 JsonArray (com.serotonin.json.type.JsonArray)17 JsonException (com.serotonin.json.JsonException)15 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)11 JsonString (com.serotonin.json.type.JsonString)7 IOException (java.io.IOException)7 IntStringPair (com.serotonin.db.pair.IntStringPair)6 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)6 JsonReader (com.serotonin.json.JsonReader)5 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)5 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)4 ArrayList (java.util.ArrayList)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 User (com.serotonin.m2m2.vo.User)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)2 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)2