Search in sources :

Example 21 with WatchListVO

use of com.serotonin.m2m2.watchlist.WatchListVO in project ma-modules-public by infiniteautomation.

the class WatchListDwr method updateWatchListName.

@DwrPermission(user = true)
public void updateWatchListName(String name) {
    User user = Common.getUser();
    WatchListVO watchList = getWatchList(user);
    WatchListCommon.ensureWatchListEditPermission(user, watchList);
    watchList.setName(name);
    WatchListDao.instance.saveWatchList(watchList);
}
Also used : User(com.serotonin.m2m2.vo.User) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 22 with WatchListVO

use of com.serotonin.m2m2.watchlist.WatchListVO in project ma-modules-public by infiniteautomation.

the class WatchListDwr method deleteWatchList.

@DwrPermission(user = true)
public boolean deleteWatchList(int watchListId) {
    User user = Common.getUser();
    WatchListVO watchList = getWatchList(user);
    if (watchList == null || watchListId != watchList.getId())
        watchList = WatchListDao.instance.get(watchListId);
    if (watchList == null || WatchListDao.instance.getWatchLists(user).size() == 1)
        // Only one watch list left. Leave it.
        return false;
    // Allow the delete if the user is an editor.
    if (watchList.isEditor(user)) {
        WatchListDao.instance.deleteWatchList(watchListId);
        return true;
    }
    return false;
}
Also used : User(com.serotonin.m2m2.vo.User) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 23 with WatchListVO

use of com.serotonin.m2m2.watchlist.WatchListVO 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));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 24 with WatchListVO

use of com.serotonin.m2m2.watchlist.WatchListVO in project ma-modules-public by infiniteautomation.

the class WatchListRestController method get.

@ApiOperation(value = "Get a Watchlist", notes = "", response = WatchListModel.class)
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "/{xid}")
public ResponseEntity<WatchListModel> get(@PathVariable String xid, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<WatchListModel> result = new RestProcessResult<WatchListModel>(HttpStatus.OK);
    try {
        User user = this.checkUser(request, result);
        if (result.isOk()) {
            WatchListVO wl = this.dao.getByXid(xid);
            if (wl == null) {
                result.addRestMessage(getDoesNotExistMessage());
                return result.createResponseEntity();
            }
            if (hasReadPermission(user, wl)) {
                List<WatchListDataPointModel> points = this.dao.getPointSummaries(wl.getId());
                // Filter them on read permission
                ListIterator<WatchListDataPointModel> it = points.listIterator();
                while (it.hasNext()) {
                    if (!Permissions.hasPermission(user, it.next().getReadPermission()))
                        it.remove();
                }
                return result.createResponseEntity(new WatchListModel(wl, points));
            } else {
                result.addRestMessage(getUnauthorizedMessage());
            }
        }
    } catch (Exception e) {
        LOG.warn(e.getMessage(), e);
        result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) WatchListDataPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListDataPointModel) User(com.serotonin.m2m2.vo.User) WatchListModel(com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListModel) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) IOException(java.io.IOException) WatchListVO(com.serotonin.m2m2.watchlist.WatchListVO) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with WatchListVO

use of com.serotonin.m2m2.watchlist.WatchListVO in project ma-modules-public by infiniteautomation.

the class WatchListRestController method delete.

@ApiOperation(value = "Delete a WatchList ", notes = "Only the owner or an admin can delete", response = WatchListModel.class)
@RequestMapping(method = RequestMethod.DELETE, consumes = { "application/*" }, produces = { "*/*" }, value = "/{xid}")
public ResponseEntity<Void> delete(@PathVariable String xid, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<Void> result = new RestProcessResult<Void>(HttpStatus.OK);
    try {
        User user = this.checkUser(request, result);
        if (result.isOk()) {
            WatchListVO wl = this.dao.getByXid(xid);
            if (wl == null) {
                result.addRestMessage(getDoesNotExistMessage());
                return result.createResponseEntity();
            }
            if (isOwner(user, wl)) {
                String initiatorId = request.getHeader("initiatorId");
                this.dao.delete(wl.getId(), initiatorId);
                result.addRestMessage(HttpStatus.NO_CONTENT, new TranslatableMessage("common.deleted"));
                return result.createResponseEntity();
            } else {
                result.addRestMessage(this.getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        }
    } catch (Exception e) {
        LOG.warn(e.getMessage(), e);
        result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) IOException(java.io.IOException) WatchListVO(com.serotonin.m2m2.watchlist.WatchListVO) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

User (com.serotonin.m2m2.vo.User)18 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)13 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)11 WatchListVO (com.serotonin.m2m2.watchlist.WatchListVO)6 ArrayList (java.util.ArrayList)6 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)5 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)5 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 WatchListDataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListDataPointModel)3 WatchListModel (com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListModel)3 ASTNode (net.jazdw.rql.parser.ASTNode)3 IntStringPair (com.serotonin.db.pair.IntStringPair)2 JsonException (com.serotonin.json.JsonException)2