Search in sources :

Example 11 with JsonException

use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.

the class DataPointImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    DataPointVO vo = null;
    DataSourceVO<?> dsvo = null;
    if (StringUtils.isBlank(xid))
        xid = ctx.getDataPointDao().generateUniqueXid();
    else
        vo = ctx.getDataPointDao().getDataPoint(xid);
    if (vo == null) {
        // Locate the data source for the point.
        String dsxid = json.getString("dataSourceXid");
        dsvo = ctx.getDataSourceDao().getDataSource(dsxid);
        if (dsvo == null)
            addFailureMessage("emport.dataPoint.badReference", xid);
        else {
            vo = new DataPointVO();
            vo.setXid(xid);
            vo.setDataSourceId(dsvo.getId());
            vo.setDataSourceXid(dsxid);
            vo.setPointLocator(dsvo.createPointLocator());
            vo.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>(0));
        // Not needed as it will be set via the template or JSON or it exists in the DB already: vo.setTextRenderer(new PlainRenderer());
        }
    }
    if (vo != null) {
        try {
            DataPointPropertiesTemplateVO template = null;
            if (json.containsKey("templateXid")) {
                String templateXid = json.getString("templateXid");
                if (!StringUtils.isEmpty(templateXid))
                    template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(templateXid);
            }
            // Read into the VO to get all properties
            ctx.getReader().readInto(vo, json);
            // Override the settings if we need to
            if (template != null) {
                template.updateDataPointVO(vo);
            }
            // If the name is not provided, default to the XID
            if (StringUtils.isBlank(vo.getName()))
                vo.setName(xid);
            // If the chart colour is null provide default of '' to handle legacy code that sets colour to null
            if (vo.getChartColour() == null)
                vo.setChartColour("");
            // Now validate it. Use a new response object so we can distinguish errors in this vo from
            // other errors.
            ProcessResult voResponse = new ProcessResult();
            vo.validate(voResponse);
            if (voResponse.getHasMessages())
                setValidationMessages(voResponse, "emport.dataPoint.prefix", xid);
            else {
                // We will always override the DS Info with the one from the XID Lookup
                dsvo = ctx.getDataSourceDao().getDataSource(vo.getDataSourceXid());
                if (dsvo == null)
                    addFailureMessage("emport.dataPoint.badReference", xid);
                else {
                    // Compare this point to the existing point in DB to ensure
                    // that we aren't moving a point to a different type of Data Source
                    DataPointVO oldPoint = ctx.getDataPointDao().getDataPoint(vo.getId(), false);
                    // Does the old point have a different data source?
                    if (oldPoint != null && (oldPoint.getDataSourceId() != dsvo.getId())) {
                        vo.setDataSourceId(dsvo.getId());
                        vo.setDataSourceName(dsvo.getName());
                    }
                }
                boolean isNew = vo.isNew();
                try {
                    if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
                        Common.runtimeManager.saveDataPoint(vo);
                        if (hierarchyList != null && json.containsKey(PATH)) {
                            String path = json.getString(PATH);
                            if (StringUtils.isNotEmpty(path))
                                hierarchyList.add(new DataPointSummaryPathPair(new DataPointSummary(vo), path));
                        }
                        addSuccessMessage(isNew, "emport.dataPoint.prefix", xid);
                    } else {
                        addFailureMessage(new ProcessMessage("Runtime Manager not running point with xid: " + xid + " not saved."));
                    }
                } catch (LicenseViolatedException e) {
                    addFailureMessage(new ProcessMessage(e.getErrorMessage()));
                }
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.dataPoint.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.dataPoint.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) DataPointSummary(com.serotonin.m2m2.vo.DataPointSummary) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) LicenseViolatedException(com.serotonin.m2m2.LicenseViolatedException) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Example 12 with JsonException

use of com.serotonin.json.JsonException 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));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonArray(com.serotonin.json.type.JsonArray) Iterator(java.util.Iterator)

Example 13 with JsonException

use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.

the class PointHierarchyImporter method importImpl.

@Override
protected void importImpl() {
    try {
        PointHierarchy hierarchy = ctx.getDataPointDao().getPointHierarchy(false);
        @SuppressWarnings("unchecked") List<PointFolder> subfolders = (List<PointFolder>) ctx.getReader().read(new TypeDefinition(List.class, PointFolder.class), json);
        // Merge the new subfolders into the existing point heirarchy.
        hierarchy.mergeFolders(subfolders);
        ph = hierarchy;
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.pointHierarchy.prefix", e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.pointHierarchy.prefix", getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) PointHierarchy(com.serotonin.m2m2.vo.hierarchy.PointHierarchy) PointFolder(com.serotonin.m2m2.vo.hierarchy.PointFolder) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) List(java.util.List) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Example 14 with JsonException

use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.

the class TemplateImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = TemplateDao.instance.generateUniqueXid();
    BaseTemplateVO<?> vo = TemplateDao.instance.getByXid(xid);
    if (vo == null) {
        String typeStr = json.getString("templateType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.template.missingType", xid, ModuleRegistry.getTemplateDefinitionTypes());
        else {
            TemplateDefinition def = ModuleRegistry.getTemplateDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.template.invalidType", xid, typeStr, ModuleRegistry.getTemplateDefinitionTypes());
            else {
                vo = def.baseCreateTemplateVO();
                vo.setXid(xid);
            }
        }
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // Now validate it. Use a new response object so we can distinguish errors in this vo from
            // other errors.
            ProcessResult voResponse = new ProcessResult();
            vo.validate(voResponse);
            if (voResponse.getHasMessages())
                setValidationMessages(voResponse, "emport.template.prefix", xid);
            else {
                // Sweet. Save it.
                boolean isnew = vo.isNew();
                TemplateDao.instance.save(vo);
                addSuccessMessage(isnew, "emport.template.prefix", xid);
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.template.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.template.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) TemplateDefinition(com.serotonin.m2m2.module.TemplateDefinition)

Example 15 with JsonException

use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.

the class VirtualSerialPortImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    boolean isNew = false;
    if (StringUtils.isBlank(xid)) {
        xid = VirtualSerialPortConfigDao.instance.generateUniqueXid();
        isNew = true;
    }
    VirtualSerialPortConfig vo = VirtualSerialPortConfigDao.instance.getByXid(xid);
    if (vo == null) {
        isNew = true;
        String typeStr = json.getString("type");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.virtualserialport.missingType", xid, VirtualSerialPortConfig.PORT_TYPE_CODES);
        else {
            try {
                Class<?> virtualPortClass = VirtualSerialPortConfigResolver.findClass(typeStr);
                vo = (VirtualSerialPortConfig) virtualPortClass.newInstance();
                vo.setXid(xid);
            } catch (TranslatableJsonException ex) {
                addFailureMessage("emport.virtualserialport.prefix", xid, ex.getMsg());
            } catch (InstantiationException e) {
                addFailureMessage("emport.virtualserialport.prefix", xid, e.getMessage());
            } catch (IllegalAccessException e) {
                addFailureMessage("emport.virtualserialport.prefix", xid, e.getMessage());
            }
        }
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // Now validate it. Use a new response object so we can distinguish errors in this vo from
            // other errors.
            ProcessResult voResponse = new ProcessResult();
            vo.validate(voResponse);
            if (voResponse.getHasMessages())
                setValidationMessages(voResponse, "emport.virtualserialport.prefix", xid);
            else {
                // Sweet. Save it.
                VirtualSerialPortConfigDao.instance.save(vo);
                addSuccessMessage(isNew, "emport.virtualserialport.prefix", xid);
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.virtualserialport.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.virtualserialport.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) VirtualSerialPortConfig(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig)

Aggregations

JsonException (com.serotonin.json.JsonException)40 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)22 JsonObject (com.serotonin.json.type.JsonObject)18 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)17 IOException (java.io.IOException)17 JsonValue (com.serotonin.json.type.JsonValue)6 JsonWriter (com.serotonin.json.JsonWriter)5 JsonReader (com.serotonin.json.JsonReader)4 HashMap (java.util.HashMap)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 JsonArray (com.serotonin.json.type.JsonArray)3 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)3 SerializableProperty (com.serotonin.json.util.SerializableProperty)3 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)3 JsonSerializableUtility (com.serotonin.m2m2.util.JsonSerializableUtility)3 StringWriter (java.io.StringWriter)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 JsonContext (com.serotonin.json.JsonContext)2 TypeDefinition (com.serotonin.json.util.TypeDefinition)2