Search in sources :

Example 6 with DataSourceDefinition

use of com.serotonin.m2m2.module.DataSourceDefinition in project ma-core-public by infiniteautomation.

the class DataSourceImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ctx.getDataSourceDao().generateUniqueXid();
    DataSourceVO<?> vo = ctx.getDataSourceDao().getDataSource(xid);
    if (vo == null) {
        String typeStr = json.getString("type");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.dataSource.missingType", xid, ModuleRegistry.getDataSourceDefinitionTypes());
        else {
            DataSourceDefinition def = ModuleRegistry.getDataSourceDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.dataSource.invalidType", xid, typeStr, ModuleRegistry.getDataSourceDefinitionTypes());
            else {
                vo = def.baseCreateDataSourceVO();
                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.dataSource.prefix", xid);
            else {
                // Sweet. Save it.
                boolean isnew = vo.isNew();
                if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
                    Common.runtimeManager.saveDataSource(vo);
                    addSuccessMessage(isnew, "emport.dataSource.prefix", xid);
                } else {
                    addFailureMessage(new ProcessMessage("Runtime manager not running, data source with xid: " + xid + "not saved."));
                }
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.dataSource.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.dataSource.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) DataSourceDefinition(com.serotonin.m2m2.module.DataSourceDefinition) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Aggregations

DataSourceDefinition (com.serotonin.m2m2.module.DataSourceDefinition)6 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 User (com.serotonin.m2m2.vo.User)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerialPortConfigException (com.infiniteautomation.mango.io.serial.SerialPortConfigException)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 JsonException (com.serotonin.json.JsonException)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ImageChartRenderer (com.serotonin.m2m2.view.chart.ImageChartRenderer)1 VirtualDataSourceVO (com.serotonin.m2m2.virtual.vo.VirtualDataSourceVO)1 VirtualPointLocatorVO (com.serotonin.m2m2.virtual.vo.VirtualPointLocatorVO)1 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)1 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)1 AbstractDataSourceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel)1