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));
}
}
}
Aggregations