use of com.serotonin.m2m2.module.PublisherDefinition in project ma-core-public by infiniteautomation.
the class PublishedPointModelDeserializer method deserialize.
/* (non-Javadoc)
* @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
*/
@Override
public AbstractPublishedPointModel<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode tree = jp.readValueAsTree();
// Get the model type which is of the form PUB-POINT-{publisher type name}
String typeName = tree.get("modelType").asText();
if (typeName != null)
typeName = typeName.split("PUB-POINT-")[1];
PublisherDefinition definition = ModuleRegistry.getPublisherDefinition(typeName);
if (definition == null)
throw new ModelNotFoundException("PUB-POINT-" + typeName);
return (AbstractPublishedPointModel<?>) mapper.treeToValue(tree, definition.getPublishedPointModelClass());
}
use of com.serotonin.m2m2.module.PublisherDefinition in project ma-core-public by infiniteautomation.
the class PublisherModelDeserializer method deserialize.
/* (non-Javadoc)
* @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
*/
@Override
public AbstractPublisherModel<?, ?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode tree = jp.readValueAsTree();
String typeName = tree.get("modelType").asText();
PublisherDefinition definition = ModuleRegistry.getPublisherDefinition(typeName);
if (definition == null)
throw new ModelNotFoundException(typeName);
AbstractPublisherModel<?, ?> model = (AbstractPublisherModel<?, ?>) mapper.treeToValue(tree, definition.getPublisherModelClass());
// Set the definition here
model.setDefinition(definition);
return model;
}
use of com.serotonin.m2m2.module.PublisherDefinition in project ma-core-public by infiniteautomation.
the class PublisherEditController method handleRequestInternal.
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
User user = Common.getUser(request);
Permissions.ensureAdmin(user);
PublisherVO<? extends PublishedPointVO> publisherVO;
// Get the id.
String idStr = request.getParameter("pid");
if (idStr == null) {
// Adding a new data source? Get the type id.
String typeId = request.getParameter("typeId");
if (StringUtils.isBlank(typeId))
return new ModelAndView(new RedirectView(errorViewName));
// A new publisher
PublisherDefinition def = ModuleRegistry.getPublisherDefinition(typeId);
if (def == null)
return new ModelAndView(new RedirectView(errorViewName));
publisherVO = def.baseCreatePublisherVO();
publisherVO.setXid(PublisherDao.instance.generateUniqueXid());
} else {
// An existing configuration.
int id = Integer.parseInt(idStr);
publisherVO = Common.runtimeManager.getPublisher(id);
if (publisherVO == null)
return new ModelAndView(new RedirectView(errorViewName));
}
// Set the id of the data source in the user object for the DWR.
user.setEditPublisher(publisherVO);
// Create the model.
Map<String, Object> model = new HashMap<>();
model.put("publisher", publisherVO);
if (publisherVO.getId() != Common.NEW_ID) {
List<EventInstance> events = EventDao.instance.getPendingEventsForPublisher(publisherVO.getId(), user.getId());
List<EventInstanceBean> beans = new ArrayList<>();
if (events != null) {
Translations translations = ControllerUtils.getTranslations(request);
for (EventInstance event : events) beans.add(new EventInstanceBean(event.isActive(), event.getAlarmLevel(), Functions.getTime(event.getActiveTimestamp()), event.getMessage().translate(translations)));
}
model.put("publisherEvents", beans);
}
publisherVO.addEditContext(model);
return new ModelAndView(getViewName(), model);
}
use of com.serotonin.m2m2.module.PublisherDefinition in project ma-core-public by infiniteautomation.
the class PublisherImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
if (StringUtils.isBlank(xid))
xid = ctx.getPublisherDao().generateUniqueXid();
PublisherVO<?> vo = ctx.getPublisherDao().getPublisher(xid);
if (vo == null) {
String typeStr = json.getString("type");
if (StringUtils.isBlank(typeStr))
addFailureMessage("emport.publisher.missingType", xid, ModuleRegistry.getPublisherDefinitionTypes());
else {
PublisherDefinition def = ModuleRegistry.getPublisherDefinition(typeStr);
if (def == null)
addFailureMessage("emport.publisher.invalidType", xid, typeStr, ModuleRegistry.getPublisherDefinitionTypes());
else {
vo = def.baseCreatePublisherVO();
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.publisher.prefix", xid);
else {
// Sweet. Save it.
boolean isnew = vo.isNew();
if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
Common.runtimeManager.savePublisher(vo);
addSuccessMessage(isnew, "emport.publisher.prefix", xid);
} else {
addFailureMessage(new ProcessMessage("Runtime manager not running publisher with xid : " + vo.getXid() + " not saved."));
}
}
} catch (TranslatableJsonException e) {
addFailureMessage("emport.publisher.prefix", xid, e.getMsg());
} catch (JsonException e) {
addFailureMessage("emport.publisher.prefix", xid, getJsonExceptionMessage(e));
}
}
}
Aggregations