use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.
the class SystemSettingsImporter method importImpl.
/* (non-Javadoc)
* @see com.serotonin.m2m2.web.dwr.emport.Importer#importImpl()
*/
@Override
protected void importImpl() {
try {
Map<String, Object> settings = new HashMap<String, Object>();
// Finish reading it in.
for (String key : json.keySet()) {
JsonValue value = json.get(key);
// Don't import null values or database schemas
if ((value != null) && (!key.startsWith(SystemSettingsDao.DATABASE_SCHEMA_VERSION))) {
Object o = value.toNative();
if (o instanceof String) {
// Could be an export code so try and convert it
Integer id = SystemSettingsDao.instance.convertToValueFromCode(key, (String) o);
if (id != null)
settings.put(key, id);
else
settings.put(key, o);
} else {
settings.put(key, o);
}
}
}
// Now validate it. Use a new response object so we can distinguish errors in this vo from
// other errors.
ProcessResult voResponse = new ProcessResult();
SystemSettingsDao.instance.validate(settings, voResponse);
if (voResponse.getHasMessages())
setValidationMessages(voResponse, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
else {
SystemSettingsDao.instance.updateSettings(settings);
addSuccessMessage(false, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
}
} catch (Exception e) {
addFailureMessage("emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()), e.getMessage());
}
}
use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.
the class JsonReader method next.
private JsonValue next() throws JsonException, IOException {
JsonValue jsonValue = null;
if (reader != null) {
if (!reader.isEos())
jsonValue = reader.read();
} else {
jsonValue = this.jsonValue;
this.jsonValue = null;
}
return jsonValue;
}
use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.
the class JsonPropertyConverter method jsonRead.
@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
JsonObject jsonObject = (JsonObject) jsonValue;
if (jsonSerializable)
((JsonSerializable) obj).jsonRead(reader, jsonObject);
if (properties != null) {
for (SerializableProperty prop : properties) {
// Check whether the property should be included
if (!prop.include(reader.getIncludeHint()))
continue;
Method writeMethod = prop.getWriteMethod();
if (writeMethod == null)
continue;
String name = prop.getNameToUse();
JsonValue propJsonValue = jsonObject.get(name);
if (propJsonValue == null)
continue;
Type propType = writeMethod.getGenericParameterTypes()[0];
propType = TypeUtils.resolveTypeVariable(type, propType);
Class<?> propClass = TypeUtils.getRawClass(propType);
try {
Object propValue = reader.read(propType, propJsonValue);
if (propClass.isPrimitive() && propValue == null) {
if (propClass == Boolean.TYPE)
propValue = false;
else
propValue = 0;
}
prop.getWriteMethod().invoke(obj, propValue);
} catch (Exception e) {
throw new JsonException("JsonException writing property '" + prop.getName() + "' of class " + propClass.getName(), e);
}
}
}
}
use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.
the class ScriptContextVariable method jsonReadVarContext.
/**
* Read in context,
* @param json
* @param context
* @return if my XID is in the context, return the name it has to map into the VO otherwise return null
* @throws JsonException
*/
public static String jsonReadVarContext(JsonObject json, List<ScriptContextVariable> context, boolean isContextUpdate) throws JsonException {
JsonArray jsonContext = json.getJsonArray("context");
if (jsonContext != null) {
context.clear();
DataPointDao dataPointDao = DataPointDao.instance;
for (JsonValue jv : jsonContext) {
JsonObject jo = jv.toJsonObject();
String xid = jo.getString("dataPointXid");
if (xid == null)
throw new TranslatableJsonException("emport.error.context.missing", "dataPointXid");
Integer dpid = dataPointDao.getIdByXid(xid);
if (dpid == null) {
// This can also happen if the point is in its own context (Bug from legacy systems).
throw new TranslatableJsonException("emport.error.missingPoint", xid);
}
// For compatibility with varName and variableName json types
String var = jo.getString("varName");
if (var == null) {
var = jo.getString("variableName");
if (var == null)
throw new TranslatableJsonException("emport.error.context.missing", "varName");
}
// Default for legacy systems
if (jo.containsKey("updateContext"))
isContextUpdate = jo.getBoolean("updateContext");
context.add(new ScriptContextVariable(dpid, var, isContextUpdate));
}
}
return json.getString("variableName");
}
use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.
the class EventDetectorRowMapper method mapRow.
/* (non-Javadoc)
* @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
*/
@Override
public AbstractEventDetectorVO<?> mapRow(ResultSet rs, int rowNum) throws SQLException {
EventDetectorDefinition<?> definition = ModuleRegistry.getEventDetectorDefinition(rs.getString(this.firstColumn + 3));
if (definition == null)
throw new ShouldNeverHappenException("Event Detector defintion of type: " + rs.getString(this.firstColumn + 3) + " not found.");
AbstractEventDetectorVO<?> vo = definition.baseCreateEventDetectorVO();
vo.setId(rs.getInt(this.firstColumn));
vo.setXid(rs.getString(this.firstColumn + 1));
vo.setDefinition(definition);
// Extract the source id
int sourceIdColumnIndex;
if (this.sourceIdColumnOffset < 0)
sourceIdColumnIndex = this.firstColumn + 5 + EventDetectorDao.instance.getSourceIdIndex(definition.getSourceTypeName());
else
sourceIdColumnIndex = this.firstColumn + this.sourceIdColumnOffset;
vo.setSourceId(rs.getInt(sourceIdColumnIndex));
// Read Into Detector
JsonTypeReader typeReader = new JsonTypeReader(rs.getString(this.firstColumn + 4));
try {
JsonValue value = typeReader.read();
JsonObject root = value.toJsonObject();
JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
root.remove("handlers");
reader.readInto(vo);
} catch (ClassCastException | IOException | JsonException e) {
LOG.error(e.getMessage(), e);
}
return vo;
}
Aggregations