Search in sources :

Example 1 with PublisherDefinition

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());
}
Also used : PublisherDefinition(com.serotonin.m2m2.module.PublisherDefinition) ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractPublishedPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.publisher.AbstractPublishedPointModel)

Example 2 with PublisherDefinition

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;
}
Also used : PublisherDefinition(com.serotonin.m2m2.module.PublisherDefinition) ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) AbstractPublisherModel(com.serotonin.m2m2.web.mvc.rest.v1.model.publisher.AbstractPublisherModel) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with PublisherDefinition

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);
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) PublisherDefinition(com.serotonin.m2m2.module.PublisherDefinition) EventInstanceBean(com.serotonin.m2m2.web.dwr.beans.EventInstanceBean) RedirectView(org.springframework.web.servlet.view.RedirectView) Translations(com.serotonin.m2m2.i18n.Translations)

Example 4 with PublisherDefinition

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));
        }
    }
}
Also used : PublisherDefinition(com.serotonin.m2m2.module.PublisherDefinition) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Aggregations

PublisherDefinition (com.serotonin.m2m2.module.PublisherDefinition)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)2 JsonException (com.serotonin.json.JsonException)1 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 Translations (com.serotonin.m2m2.i18n.Translations)1 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)1 User (com.serotonin.m2m2.vo.User)1 EventInstanceBean (com.serotonin.m2m2.web.dwr.beans.EventInstanceBean)1 AbstractPublishedPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.publisher.AbstractPublishedPointModel)1 AbstractPublisherModel (com.serotonin.m2m2.web.mvc.rest.v1.model.publisher.AbstractPublisherModel)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 RedirectView (org.springframework.web.servlet.view.RedirectView)1