Search in sources :

Example 6 with AbstractEventHandlerVO

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

the class EventHandlerRestController method update.

@ApiOperation(value = "Update an existing event handler", notes = "")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }, value = "/{xid}")
public ResponseEntity<AbstractEventHandlerModel<?>> update(@PathVariable String xid, @ApiParam(value = "Updated model", required = true) @RequestBody(required = true) AbstractEventHandlerModel<?> model, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<AbstractEventHandlerModel<?>> result = new RestProcessResult<AbstractEventHandlerModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        AbstractEventHandlerVO<?> vo = model.getData();
        AbstractEventHandlerVO<?> existing = EventHandlerDao.instance.getByXid(xid);
        if (existing == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // Check Event Type Permission
        if (!Permissions.hasAdmin(user)) {
            result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("permissions.accessDenied", user.getUsername(), SuperadminPermissionDefinition.GROUP_NAME));
            return result.createResponseEntity();
        }
        // Ensure we keep the same ID
        vo.setId(existing.getId());
        if (!model.validate()) {
            result.addRestMessage(this.getValidationFailedError());
            return result.createResponseEntity(model);
        } else {
            String initiatorId = request.getHeader("initiatorId");
            EventHandlerDao.instance.save(vo, initiatorId);
        }
        // Put a link to the updated data in the header?
        URI location = builder.path("/v1/event-handlers/{xid}").buildAndExpand(vo.getXid()).toUri();
        result.addRestMessage(getResourceUpdatedMessage(location));
        return result.createResponseEntity(model);
    }
    // Not logged in
    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) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with AbstractEventHandlerVO

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

the class EventHandlerRestController method save.

@ApiOperation(value = "Save a new event handler", notes = "User must have event type permission")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json" }, produces = { "application/json" })
public ResponseEntity<AbstractEventHandlerModel<?>> save(@RequestBody(required = true) AbstractEventHandlerModel<?> model, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<AbstractEventHandlerModel<?>> result = new RestProcessResult<AbstractEventHandlerModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        // Check Event Type Permission
        if (!Permissions.hasAdmin(user)) {
            result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("permissions.accessDenied", user.getUsername(), SuperadminPermissionDefinition.GROUP_NAME));
            return result.createResponseEntity();
        }
        // Set XID if required
        if (StringUtils.isEmpty(model.getXid())) {
            model.setXid(EventHandlerDao.instance.generateUniqueXid());
        }
        if (!model.validate()) {
            result.addRestMessage(this.getValidationFailedError());
            return result.createResponseEntity(model);
        } else {
            AbstractEventHandlerVO<?> vo = model.getData();
            String initiatorId = request.getHeader("initiatorId");
            EventHandlerDao.instance.save(vo, initiatorId);
        }
        // Put a link to the updated data in the header?
        URI location = builder.path("/v1/event-handlers/{xid}").buildAndExpand(model.getXid()).toUri();
        result.addRestMessage(getResourceCreatedMessage(location));
        return result.createResponseEntity(model);
    }
    // Not logged in
    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) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with AbstractEventHandlerVO

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

the class EventHandlerRestController method queryRQL.

@ApiOperation(value = "Query Event Handlers", notes = "Use RQL formatted query", response = AbstractEventHandlerModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<QueryDataPageStream<AbstractEventHandlerVO<?>>> queryRQL(HttpServletRequest request) {
    RestProcessResult<QueryDataPageStream<AbstractEventHandlerVO<?>>> result = new RestProcessResult<QueryDataPageStream<AbstractEventHandlerVO<?>>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        try {
            ASTNode node = parseRQLtoAST(request.getQueryString());
            EventHandlerStreamCallback callback = new EventHandlerStreamCallback(this, user);
            FilteredPageQueryStream<AbstractEventHandlerVO<?>, AbstractEventHandlerModel<?>, EventHandlerDao> stream = new FilteredPageQueryStream<AbstractEventHandlerVO<?>, AbstractEventHandlerModel<?>, EventHandlerDao>(EventHandlerDao.instance, this, node, callback);
            stream.setupQuery();
            return result.createResponseEntity(stream);
        } catch (InvalidRQLRestException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) QueryDataPageStream(com.serotonin.m2m2.web.mvc.rest.v1.model.QueryDataPageStream) User(com.serotonin.m2m2.vo.User) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) AbstractEventHandlerModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel) ASTNode(net.jazdw.rql.parser.ASTNode) EventHandlerStreamCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.eventHandler.EventHandlerStreamCallback) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) EventHandlerDao(com.serotonin.m2m2.db.dao.EventHandlerDao) FilteredPageQueryStream(com.serotonin.m2m2.web.mvc.rest.v1.model.FilteredPageQueryStream) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with AbstractEventHandlerVO

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

the class EventHandlersDwr method save.

private ProcessResult save(String eventType, String eventSubtype, int eventTypeRef1, int eventTypeRef2, AbstractEventHandlerVO<?> vo, int handlerId, String xid, String alias, boolean disabled) {
    EventTypeVO type = new EventTypeVO(eventType, eventSubtype, eventTypeRef1, eventTypeRef2);
    Permissions.ensureEventTypePermission(Common.getHttpUser(), type);
    vo.setId(handlerId);
    vo.setXid(StringUtils.isBlank(xid) ? EventHandlerDao.instance.generateUniqueXid() : xid);
    vo.setAlias(alias);
    vo.setDisabled(disabled);
    ProcessResult response = new ProcessResult();
    vo.validate(response);
    if (!response.getHasMessages()) {
        EventHandlerDao.instance.saveEventHandler(type, vo);
        response.addData("handler", vo);
    }
    return response;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO)

Example 10 with AbstractEventHandlerVO

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

the class EventDetectorImporter method importImpl.

@Override
protected void importImpl() {
    String dataPointXid = json.getString("dataPointXid");
    DataPointVO dpvo;
    // Everyone is in the same thread so no synchronization on dataPointMap required.
    if (dataPointMap.containsKey(dataPointXid))
        dpvo = dataPointMap.get(dataPointXid);
    else if (StringUtils.isEmpty(dataPointXid) || (dpvo = DataPointDao.instance.getByXid(dataPointXid)) == null) {
        addFailureMessage("emport.error.missingPoint", dataPointXid);
        return;
    } else {
        dataPointMap.put(dataPointXid, dpvo);
        // We're only going to use this to house event detectors imported in the eventDetectors object.
        dpvo.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>());
    }
    String typeStr = json.getString("type");
    if (typeStr == null)
        addFailureMessage("emport.error.ped.missingAttr", "type");
    EventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeStr);
    if (def == null) {
        addFailureMessage("emport.error.ped.invalid", "type", typeStr, ModuleRegistry.getEventDetectorDefinitionTypes());
        return;
    }
    JsonArray handlerXids = json.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) {
                addFailureMessage("emport.eventHandler.missing", handlerXids.getString(k));
                return;
            }
        }
    AbstractEventDetectorVO<?> importing = def.baseCreateEventDetectorVO();
    importing.setDefinition(def);
    String xid = json.getString("xid");
    // Create a new one
    importing.setId(Common.NEW_ID);
    importing.setXid(xid);
    AbstractPointEventDetectorVO<?> dped = (AbstractPointEventDetectorVO<?>) importing;
    dped.njbSetDataPoint(dpvo);
    dpvo.getEventDetectors().add(dped);
    try {
        ctx.getReader().readInto(importing, json);
    // try {
    // if(Common.runtimeManager.getState() == RuntimeManager.RUNNING){
    // Common.runtimeManager.saveDataPoint(dpvo);
    // addSuccessMessage(isNew, "emport.eventDetector.prefix", xid);
    // }else{
    // addFailureMessage(new ProcessMessage("Runtime Manager not running point with xid: " + xid + " not saved."));
    // }
    // } catch(LicenseViolatedException e) {
    // addFailureMessage(new ProcessMessage(e.getErrorMessage()));
    // }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.eventDetector.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.eventDetector.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) JsonArray(com.serotonin.json.type.JsonArray) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ArrayList(java.util.ArrayList) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)

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