Search in sources :

Example 1 with AbstractEventHandlerVO

use of com.serotonin.m2m2.vo.event.AbstractEventHandlerVO in project ma-core-public by infiniteautomation.

the class DataPointVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    // Not reading XID so can't do this: super.jsonRead(reader, jsonObject);
    name = jsonObject.getString("name");
    enabled = jsonObject.getBoolean("enabled");
    String text = jsonObject.getString("loggingType");
    if (text != null) {
        loggingType = LOGGING_TYPE_CODES.getId(text);
        if (loggingType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "loggingType", text, LOGGING_TYPE_CODES.getCodeList());
    }
    text = jsonObject.getString("intervalLoggingPeriodType");
    if (text != null) {
        intervalLoggingPeriodType = Common.TIME_PERIOD_CODES.getId(text);
        if (intervalLoggingPeriodType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "intervalLoggingPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
    }
    text = jsonObject.getString("intervalLoggingType");
    if (text != null) {
        intervalLoggingType = INTERVAL_LOGGING_TYPE_CODES.getId(text);
        if (intervalLoggingType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "intervalLoggingType", text, INTERVAL_LOGGING_TYPE_CODES.getCodeList());
    }
    text = jsonObject.getString("purgeType");
    if (text != null) {
        purgeType = Common.TIME_PERIOD_CODES.getId(text);
        if (purgeType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "purgeType", text, Common.TIME_PERIOD_CODES.getCodeList(TimePeriods.MILLISECONDS, TimePeriods.SECONDS, TimePeriods.MINUTES, TimePeriods.HOURS));
    }
    JsonObject locatorJson = jsonObject.getJsonObject("pointLocator");
    if (locatorJson != null)
        reader.readInto(pointLocator, locatorJson);
    JsonArray pedArray = jsonObject.getJsonArray("eventDetectors");
    if (pedArray != null) {
        for (JsonValue jv : pedArray) {
            JsonObject pedObject = jv.toJsonObject();
            String pedXid = pedObject.getString("xid");
            if (StringUtils.isBlank(pedXid))
                throw new TranslatableJsonException("emport.error.ped.missingAttr", "xid");
            // Use the ped xid to lookup an existing ped.
            AbstractEventDetectorVO<?> ped = null;
            for (AbstractPointEventDetectorVO<?> existing : eventDetectors) {
                if (StringUtils.equals(pedXid, existing.getXid())) {
                    ped = existing;
                    break;
                }
            }
            JsonArray handlerXids = pedObject.getJsonArray("handlers");
            if (handlerXids != null)
                for (int k = 0; k < handlerXids.size(); k += 1) {
                    AbstractEventHandlerVO<?> eh = EventHandlerDao.instance.getByXid(handlerXids.getString(k));
                    if (eh == null) {
                        throw new TranslatableJsonException("emport.eventHandler.missing", handlerXids.getString(k));
                    }
                }
            if (ped == null) {
                String typeStr = pedObject.getString("type");
                if (typeStr == null)
                    throw new TranslatableJsonException("emport.error.ped.missingAttr", "type");
                EventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeStr);
                if (def == null)
                    throw new TranslatableJsonException("emport.error.ped.invalid", "type", typeStr, ModuleRegistry.getEventDetectorDefinitionTypes());
                else {
                    ped = def.baseCreateEventDetectorVO();
                    ped.setDefinition(def);
                }
                // Create a new one
                ped.setId(Common.NEW_ID);
                ped.setXid(pedXid);
                AbstractPointEventDetectorVO<?> dped = (AbstractPointEventDetectorVO<?>) ped;
                dped.njbSetDataPoint(this);
                eventDetectors.add(dped);
            }
            reader.readInto(ped, pedObject);
        }
    }
    text = jsonObject.getString("unit");
    if (text != null) {
        unit = parseUnitString(text, "unit");
        unitString = UnitUtil.formatUcum(unit);
    }
    text = jsonObject.getString("integralUnit");
    if (text != null) {
        useIntegralUnit = true;
        integralUnit = parseUnitString(text, "integralUnit");
        integralUnitString = UnitUtil.formatUcum(integralUnit);
    }
    text = jsonObject.getString("renderedUnit");
    if (text != null) {
        useRenderedUnit = true;
        renderedUnit = parseUnitString(text, "renderedUnit");
        renderedUnitString = UnitUtil.formatUcum(renderedUnit);
    }
    text = jsonObject.getString("plotType");
    if (text != null) {
        plotType = PLOT_TYPE_CODES.getId(text);
        if (plotType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "plotType", text, PLOT_TYPE_CODES.getCodeList());
    }
    // Rollup
    text = jsonObject.getString("rollup");
    if (text != null) {
        rollup = Common.ROLLUP_CODES.getId(text);
        if (rollup == -1)
            throw new TranslatableJsonException("emport.error.chart.invalid", "rollup", text, Common.ROLLUP_CODES.getCodeList());
    }
    // Simplify
    text = jsonObject.getString("simplifyType");
    if (text != null) {
        simplifyType = SIMPLIFY_TYPE_CODES.getId(text);
        if (simplifyType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "simplifyType", text, SIMPLIFY_TYPE_CODES.getCodeList());
    }
    int simplifyTarget = jsonObject.getInt("simplifyTarget", Integer.MIN_VALUE);
    if (simplifyTarget != Integer.MIN_VALUE)
        this.simplifyTarget = simplifyTarget;
    double simplifyTolerance = jsonObject.getDouble("simplifyTolerance", Double.NaN);
    if (simplifyTolerance != Double.NaN)
        this.simplifyTolerance = simplifyTolerance;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)

Example 2 with AbstractEventHandlerVO

use of com.serotonin.m2m2.vo.event.AbstractEventHandlerVO in project ma-core-public by infiniteautomation.

the class EventHandlerImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ctx.getEventHandlerDao().generateUniqueXid();
    AbstractEventHandlerVO<?> handler = ctx.getEventHandlerDao().getEventHandler(xid);
    if (handler == null) {
        String typeStr = json.getString("handlerType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.eventHandler.missingType", xid, ModuleRegistry.getEventHandlerDefinitionTypes());
        else {
            EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.eventHandler.invalidType", xid, typeStr, ModuleRegistry.getEventHandlerDefinitionTypes());
            else {
                handler = def.baseCreateEventHandlerVO();
                handler.setXid(xid);
            }
        }
    }
    JsonObject et = json.getJsonObject("eventType");
    JsonArray ets = json.getJsonArray("eventTypes");
    EventType eventType = null;
    List<EventType> eventTypes = null;
    try {
        // Find the event type.
        if (et != null)
            eventType = ctx.getReader().read(EventType.class, et);
        else if (ets != null) {
            eventTypes = new ArrayList<EventType>(ets.size());
            Iterator<JsonValue> iter = ets.iterator();
            while (iter.hasNext()) eventTypes.add(ctx.getReader().read(EventType.class, iter.next()));
        }
        ctx.getReader().readInto(handler, json);
        // Now validate it. Use a new response object so we can distinguish errors in this vo from other errors.
        ProcessResult voResponse = new ProcessResult();
        handler.validate(voResponse);
        if (voResponse.getHasMessages())
            setValidationMessages(voResponse, "emport.eventHandler.prefix", xid);
        else {
            // Sweet.
            boolean isnew = handler.getId() == Common.NEW_ID;
            // Save it.
            if (et != null)
                ctx.getEventHandlerDao().saveEventHandler(eventType, handler);
            else
                ctx.getEventHandlerDao().saveEventHandler(handler);
            if (eventTypes != null)
                for (EventType type : eventTypes) ctx.getEventHandlerDao().addEventHandlerMappingIfMissing(handler.getId(), type);
            addSuccessMessage(isnew, "emport.eventHandler.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonArray(com.serotonin.json.type.JsonArray) Iterator(java.util.Iterator)

Example 3 with AbstractEventHandlerVO

use of com.serotonin.m2m2.vo.event.AbstractEventHandlerVO in project ma-core-public by infiniteautomation.

the class EventHandlerDao method saveEventHandler.

private AbstractEventHandlerVO<?> saveEventHandler(final String typeName, final String subtypeName, final int typeRef1, final int typeRef2, final AbstractEventHandlerVO<?> handler) {
    getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            if (handler.getId() == Common.NEW_ID)
                insertEventHandler(typeName, subtypeName, typeRef1, typeRef2, handler);
            else
                updateEventHandler(handler);
            if (handler.getAddedEventTypes() != null)
                for (EventType et : handler.getAddedEventTypes()) EventHandlerDao.instance.addEventHandlerMappingIfMissing(handler.getId(), et);
        }
    });
    return getEventHandler(handler.getId());
}
Also used : AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 4 with AbstractEventHandlerVO

use of com.serotonin.m2m2.vo.event.AbstractEventHandlerVO in project ma-modules-public by infiniteautomation.

the class EventHandlerRestController method get.

@ApiOperation(value = "Get EventHandler by XID", notes = "EventType permission required")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "application/sero-json" }, value = "/{xid}")
public ResponseEntity<AbstractEventHandlerModel<?>> get(@ApiParam(value = "Valid Eventh Handler XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<AbstractEventHandlerModel<?>> result = new RestProcessResult<AbstractEventHandlerModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        AbstractEventHandlerVO<?> vo = EventHandlerDao.instance.getByXid(xid);
        if (vo == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        } else {
            // Check Permissions
            if (Permissions.hasAdmin(user))
                return result.createResponseEntity(vo.asModel());
            else
                result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("permissions.accessDenied", user.getUsername(), SuperadminPermissionDefinition.GROUP_NAME));
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) AbstractEventHandlerModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with AbstractEventHandlerVO

use of com.serotonin.m2m2.vo.event.AbstractEventHandlerVO in project ma-modules-public by infiniteautomation.

the class EventHandlerRestController method delete.

@ApiOperation(value = "Delete an event handler", notes = "The user must have event type permission")
@RequestMapping(method = RequestMethod.DELETE, value = "/{xid}", produces = { "application/json" })
public ResponseEntity<AbstractEventHandlerModel<?>> delete(@PathVariable String xid, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<AbstractEventHandlerModel<?>> result = new RestProcessResult<AbstractEventHandlerModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        AbstractEventHandlerVO<?> existing = EventHandlerDao.instance.getByXid(xid);
        if (existing == null) {
            result.addRestMessage(this.getDoesNotExistMessage());
            return result.createResponseEntity();
        } else {
            // Check Event Type Permission
            if (!Permissions.hasAdmin(user)) {
                result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("permissions.accessDenied", user.getUsername(), SuperadminPermissionDefinition.GROUP_NAME));
                return result.createResponseEntity();
            }
            // All Good Delete It
            EventHandlerDao.instance.delete(existing.getId());
            return result.createResponseEntity(existing.asModel());
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) AbstractEventHandlerModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

User (com.serotonin.m2m2.vo.User)5 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)5 AbstractEventHandlerModel (com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 JsonArray (com.serotonin.json.type.JsonArray)3 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 AbstractEventHandlerVO (com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)3 JsonException (com.serotonin.json.JsonException)2 JsonObject (com.serotonin.json.type.JsonObject)2 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)2 EventType (com.serotonin.m2m2.rt.event.type.EventType)2 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)1 JsonValue (com.serotonin.json.type.JsonValue)1 EventHandlerDao (com.serotonin.m2m2.db.dao.EventHandlerDao)1 AuditEventType (com.serotonin.m2m2.rt.event.type.AuditEventType)1