use of com.serotonin.json.JsonException in project ma-modules-public by infiniteautomation.
the class ScheduledEventEmportDefinition method doImport.
@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
JsonObject scheduledEvent = jsonValue.toJsonObject();
String xid = scheduledEvent.getString("xid");
if (StringUtils.isBlank(xid))
xid = ScheduledEventDao.instance.generateUniqueXid();
ScheduledEventVO vo = ScheduledEventDao.instance.getScheduledEvent(xid);
if (vo == null) {
vo = new ScheduledEventVO();
vo.setXid(xid);
}
try {
importContext.getReader().readInto(vo, scheduledEvent);
// 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())
// Too bad. Copy the errors into the actual response.
importContext.copyValidationMessages(voResponse, "emport.scheduledEvent.prefix", xid);
else {
// Sweet. Save it.
boolean isnew = vo.isNew();
RTMDefinition.instance.saveScheduledEvent(vo);
importContext.addSuccessMessage(isnew, "emport.scheduledEvent.prefix", xid);
}
} catch (TranslatableJsonException e) {
importContext.getResult().addGenericMessage("emport.scheduledEvent.prefix", xid, e.getMsg());
} catch (JsonException e) {
importContext.getResult().addGenericMessage("emport.scheduledEvent.prefix", xid, importContext.getJsonExceptionMessage(e));
}
}
use of com.serotonin.json.JsonException in project ma-modules-public by infiniteautomation.
the class WatchListEmportDefinition method doImport.
@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
JsonObject watchListJson = jsonValue.toJsonObject();
String xid = watchListJson.getString("xid");
if (StringUtils.isBlank(xid))
xid = WatchListDao.instance.generateUniqueXid();
WatchListVO watchList = WatchListDao.instance.getWatchList(xid);
if (watchList == null) {
watchList = new WatchListVO();
watchList.setXid(xid);
}
try {
importContext.getReader().readInto(watchList, watchListJson);
// Now validate it. Use a new response object so we can distinguish errors in this user from other
// errors.
ProcessResult watchListResponse = new ProcessResult();
watchList.validate(watchListResponse);
if (watchListResponse.getHasMessages())
// Too bad. Copy the errors into the actual response.
importContext.copyValidationMessages(watchListResponse, "emport.watchList.prefix", xid);
else {
// Sweet. Save it.
boolean isnew = watchList.getId() == Common.NEW_ID;
WatchListDao.instance.save(watchList);
importContext.addSuccessMessage(isnew, "emport.watchList.prefix", xid);
}
} catch (TranslatableJsonException e) {
importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, e.getMsg());
} catch (JsonException e) {
importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, importContext.getJsonExceptionMessage(e));
}
}
use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.
the class EmportDwr method importData.
@DwrPermission(admin = true)
public ProcessResult importData(String data) {
ProcessResult response = new ProcessResult();
Translations translations = getTranslations();
User user = Common.getHttpUser();
JsonTypeReader reader = new JsonTypeReader(data);
try {
JsonValue value = reader.read();
if (value instanceof JsonObject) {
JsonObject root = value.toJsonObject();
ImportTask importTask = new ImportTask(root, translations, user, true);
user.setImportTask(importTask);
response.addData("importStarted", true);
} else {
response.addGenericMessage("emport.invalidImportData");
}
} catch (ClassCastException e) {
response.addGenericMessage("emport.parseError", e.getMessage());
} catch (TranslatableJsonException e) {
response.addMessage(e.getMsg());
} catch (IOException e) {
response.addGenericMessage("emport.parseError", e.getMessage());
} catch (JsonException e) {
response.addGenericMessage("emport.parseError", e.getMessage());
}
return response;
}
use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.
the class Restorer method restore.
/**
* Restore the object based on the Audit Trail
* @return
*/
public T restore() {
T vo = null;
try {
// Follow the trail
for (AuditEventInstanceVO audit : trail) {
JsonObject context = audit.getContext();
JsonReader reader = new JsonReader(Common.JSON_CONTEXT, context);
if (audit.getChangeType() == AuditEventInstanceVO.CHANGE_TYPE_CREATE) {
vo = this.build(audit.getObjectId(), context, reader);
} else if (audit.getChangeType() == AuditEventInstanceVO.CHANGE_TYPE_MODIFY) {
if (vo == null)
vo = getExisting(audit.getObjectId());
vo = this.build(vo, context, reader);
}
}
ProcessResult voResponse = new ProcessResult();
vo.validate(voResponse);
if (voResponse.getHasMessages())
copyValidationMessages(voResponse, "restore.prefix", vo.getXid());
else {
addSuccessMessage(vo.isNew(), "restore.prefix", vo.getXid());
}
} catch (TranslatableJsonException e) {
addFailureMessage("restore.prefix", "need-to-fill-in", e.getMsg());
} catch (JsonException e) {
addFailureMessage("restoring.prefix", "need-to-fill-in", getJsonExceptionMessage(e));
} catch (Exception e) {
addFailureMessage("restoring.prefix", "need-to-fill-in", e.getMessage());
}
return vo;
}
use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.
the class SerotoninJsonMessageConverter method writeInternal.
/* (non-Javadoc)
* @see org.springframework.http.converter.AbstractHttpMessageConverter#writeInternal(java.lang.Object, org.springframework.http.HttpOutputMessage)
*/
@Override
protected void writeInternal(Object t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
OutputStreamWriter osWriter = new OutputStreamWriter(outputMessage.getBody());
JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, osWriter);
try {
writer.writeObject(t);
writer.flush();
} catch (JsonException e) {
throw new IOException(e);
}
}
Aggregations