Search in sources :

Example 6 with WatchListVO

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

the class WatchlistEmportDefinitionNoCase 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.saveWatchList(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 7 with WatchListVO

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

the class MobileWatchListHandler method handleRequest.

@Override
public View handleRequest(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {
    User user = Common.getHttpUser();
    WatchListDao watchListDao = WatchListDao.instance;
    // Check for a watchlist id parameter. If given, update the user.
    try {
        int watchListId = Integer.parseInt(request.getParameter("watchListId"));
        WatchListVO watchList = watchListDao.get(watchListId);
        WatchListCommon.ensureWatchListPermission(user, watchList);
        watchListDao.saveSelectedWatchList(user.getId(), watchList.getId());
    } catch (NumberFormatException e) {
    // no op
    }
    prepareModel(request, model, user);
    // Get the selected watchlist.
    int watchListId = (Integer) model.get(KEY_SELECTED_WATCHLIST);
    // Get the point data.
    List<MobileWatchListState> states = new ArrayList<MobileWatchListState>();
    for (DataPointVO pointVO : watchListDao.get(watchListId).getPointList()) {
        MobileWatchListState state = createState(request, pointVO);
        states.add(state);
    }
    model.put(KEY_WATCHLIST_DATA, states);
    return null;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) User(com.serotonin.m2m2.vo.User) WatchListDao(com.serotonin.m2m2.watchlist.WatchListDao) ArrayList(java.util.ArrayList) WatchListVO(com.serotonin.m2m2.watchlist.WatchListVO)

Example 8 with WatchListVO

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

the class WatchListRestController method queryRQL.

@ApiOperation(value = "Query WatchLists", notes = "", response = WatchListSummaryModel.class, responseContainer = "Array")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Ok", response = WatchListSummaryModel.class), @ApiResponse(code = 403, message = "User does not have access", response = ResponseEntity.class) })
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<QueryDataPageStream<WatchListVO>> queryRQL(HttpServletRequest request) {
    RestProcessResult<QueryDataPageStream<WatchListVO>> result = new RestProcessResult<QueryDataPageStream<WatchListVO>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        try {
            ASTNode query = parseRQLtoAST(request.getQueryString());
            if (!user.isAdmin()) {
                // We are going to filter the results, so we need to strip out the limit(limit,offset) or limit(limit) clause.
                WatchListStreamCallback callback = new WatchListStreamCallback(this, user);
                FilteredPageQueryStream<WatchListVO, WatchListSummaryModel, WatchListDao> stream = new FilteredPageQueryStream<WatchListVO, WatchListSummaryModel, WatchListDao>(WatchListDao.instance, this, query, callback);
                stream.setupQuery();
                return result.createResponseEntity(stream);
            } else
                return result.createResponseEntity(getPageStream(query));
        } 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) WatchListDao(com.serotonin.m2m2.watchlist.WatchListDao) WatchListSummaryModel(com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListSummaryModel) ASTNode(net.jazdw.rql.parser.ASTNode) FilteredPageQueryStream(com.serotonin.m2m2.web.mvc.rest.v1.model.FilteredPageQueryStream) WatchListVO(com.serotonin.m2m2.watchlist.WatchListVO) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with WatchListVO

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

the class WatchListRestController method update.

@ApiOperation(value = "Update a WatchList", notes = "", response = WatchListModel.class)
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" }, value = "/{xid}")
public ResponseEntity<WatchListModel> update(@PathVariable String xid, @RequestBody WatchListModel model, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<WatchListModel> result = new RestProcessResult<WatchListModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (!result.isOk()) {
        return result.createResponseEntity();
    }
    WatchListVO wl = this.dao.getByXid(xid);
    if (wl == null) {
        result.addRestMessage(getDoesNotExistMessage());
        return result.createResponseEntity();
    }
    if (!hasEditPermission(user, wl)) {
        result.addRestMessage(getUnauthorizedMessage());
        return result.createResponseEntity();
    }
    WatchListVO update = model.getData();
    // Set the id
    update.setId(wl.getId());
    // Add the user
    update.setUserId(wl.getUserId());
    // Setup the Points
    if (model.getPoints() != null)
        for (WatchListDataPointModel pm : model.getPoints()) update.getPointList().add(pm.getDataPointVO());
    if (!model.validate()) {
        result.addRestMessage(this.getValidationFailedError());
        return result.createResponseEntity(model);
    }
    try {
        String initiatorId = request.getHeader("initiatorId");
        this.dao.save(update, initiatorId);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
    }
    return result.createResponseEntity(model);
}
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 10 with WatchListVO

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

the class WatchListRestController method createNew.

@ApiOperation(value = "Create New WatchList", notes = "", response = WatchListModel.class)
@ApiResponses({ @ApiResponse(code = 201, message = "User Created", response = WatchListModel.class), @ApiResponse(code = 401, message = "Unauthorized Access", response = ResponseEntity.class), @ApiResponse(code = 409, message = "WatchList Already Exists") })
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" })
public ResponseEntity<WatchListModel> createNew(@ApiParam(value = "Watchlist to save", required = true) @RequestBody WatchListModel model, UriComponentsBuilder builder, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<WatchListModel> result = new RestProcessResult<WatchListModel>(HttpStatus.CREATED);
    User user = this.checkUser(request, result);
    if (!result.isOk()) {
        return result.createResponseEntity();
    }
    WatchListVO wl = model.getData();
    // Check XID if blank and generate one
    if (StringUtils.isBlank(wl.getXid())) {
        wl.setXid(this.dao.generateUniqueXid());
    }
    // Add the user
    wl.setUserId(user.getId());
    // Setup the Points
    if (model.getPoints() != null)
        for (WatchListDataPointModel pm : model.getPoints()) wl.getPointList().add(pm.getDataPointVO());
    // Ready to validate and then save
    if (!model.validate()) {
        result.addRestMessage(this.getValidationFailedError());
        return result.createResponseEntity(model);
    }
    try {
        String initiatorId = request.getHeader("initiatorId");
        this.dao.save(wl, initiatorId);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
    }
    return result.createResponseEntity(new WatchListModel(wl, this.dao.getPointSummaries(wl.getId())));
}
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) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) 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