Search in sources :

Example 11 with WatchListVO

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

the class WatchListHandler method prepareModel.

protected void prepareModel(HttpServletRequest request, Map<String, Object> model, User user) {
    // The user's permissions may have changed since the last session, so make sure the watch lists are correct.
    List<WatchListVO> watchLists = WatchListDao.instance.getWatchLists(user);
    if (watchLists.size() == 0) {
        // Add a default watch list if none exist.
        WatchListVO watchList = new WatchListVO();
        watchList.setName(ControllerUtils.getTranslations(request).translate("common.newName"));
        watchLists.add(WatchListDao.instance.createNewWatchList(watchList, user.getId()));
    }
    int selected = 0;
    WatchListVO selectedWatchList = WatchListDao.instance.getSelectedWatchList(user.getId());
    if (selectedWatchList != null)
        selected = selectedWatchList.getId();
    // Check if a parameter was provided.
    String wlid = request.getParameter("wlid");
    if (!StringUtils.isBlank(wlid)) {
        try {
            selected = Integer.parseInt(wlid);
        } catch (NumberFormatException e) {
        // ignore
        }
    }
    String wlxid = request.getParameter("wlxid");
    UserDao userDao = UserDao.instance;
    boolean found = false;
    List<Map<String, String>> watchListsData = new ArrayList<Map<String, String>>(watchLists.size());
    List<IntStringPair> watchListUsers = new ArrayList<>(watchLists.size());
    List<IntStringPair> userWatchLists = new ArrayList<>(watchLists.size());
    Set<String> users = new HashSet<String>();
    for (WatchListVO watchList : watchLists) {
        if (!found) {
            if (StringUtils.equals(watchList.getXid(), wlxid)) {
                found = true;
                selected = watchList.getId();
            } else if (watchList.getId() == selected)
                found = true;
        }
        if (watchList.isOwner(user)) {
            // If this is the owner, check that the user still has access to the points. If not, remove the
            // unauthorized points, resave, and continue.
            boolean changed = false;
            List<DataPointVO> list = watchList.getPointList();
            List<DataPointVO> copy = new ArrayList<>(list);
            for (DataPointVO point : copy) {
                if (point == null || !Permissions.hasDataPointReadPermission(user, point)) {
                    list.remove(point);
                    changed = true;
                }
            }
            if (changed)
                WatchListDao.instance.saveWatchList(watchList);
        }
        User watchListUser = userDao.getUser(watchList.getUserId());
        String username;
        if (watchListUser == null) {
            username = Common.translate("watchlist.userDNE");
        } else {
            username = watchListUser.getUsername();
            users.add(watchListUser.getUsername());
        }
        watchListUsers.add(new IntStringPair(watchList.getId(), username));
        // Add the Username to the name to know who's it is
        userWatchLists.add(new IntStringPair(watchList.getId(), watchList.getName() + " (" + username + ")"));
        Map<String, String> wlData = new HashMap<String, String>();
        wlData.put("id", Integer.toString(watchList.getId()));
        wlData.put("name", watchList.getName());
        wlData.put("username", username);
        watchListsData.add(wlData);
    }
    if (!found) {
        // The user's default watch list was not found. It was either deleted or unshared from them. Find a new one.
        // The list will always contain at least one, so just use the id of the first in the list.
        selected = watchLists.get(0).getId();
        WatchListDao.instance.saveSelectedWatchList(user.getId(), selected);
    }
    Collections.sort(watchListsData, new Comparator<Map<String, String>>() {

        @Override
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            return o1.get("name").compareTo(o2.get("name"));
        }
    });
    model.put(KEY_WATCHLISTS, watchListsData);
    model.put(KEY_SELECTED_WATCHLIST, selected);
    model.put(KEY_WATCHLIST_USERS, watchListUsers);
    model.put(KEY_USER_WATCHLISTS, userWatchLists);
    model.put(KEY_USERNAME, user.getUsername());
    List<String> sortedUsers = new ArrayList<String>(users);
    Collections.sort(sortedUsers);
    model.put("usernames", sortedUsers);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) User(com.serotonin.m2m2.vo.User) IntStringPair(com.serotonin.db.pair.IntStringPair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserDao(com.serotonin.m2m2.db.dao.UserDao) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 12 with WatchListVO

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

the class WatchListDwr method removeFromWatchList.

@DwrPermission(user = true)
public void removeFromWatchList(int pointId) {
    // Remove the point from the user's list.
    User user = Common.getUser();
    WatchListVO watchList = getWatchList(user);
    WatchListCommon.ensureWatchListEditPermission(user, watchList);
    for (DataPointVO point : watchList.getPointList()) {
        if (point.getId() == pointId) {
            watchList.getPointList().remove(point);
            break;
        }
    }
    WatchListDao.instance.saveWatchList(watchList);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) User(com.serotonin.m2m2.vo.User) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 13 with WatchListVO

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

the class WatchListDwr method moveDown.

@DwrPermission(user = true)
public void moveDown(int pointId) {
    User user = Common.getUser();
    WatchListVO watchList = getWatchList(user);
    WatchListCommon.ensureWatchListEditPermission(user, watchList);
    List<DataPointVO> points = watchList.getPointList();
    DataPointVO point;
    for (int i = 0; i < points.size(); i++) {
        point = points.get(i);
        if (point.getId() == pointId) {
            points.set(i, points.get(i + 1));
            points.set(i + 1, point);
            break;
        }
    }
    WatchListDao.instance.saveWatchList(watchList);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) User(com.serotonin.m2m2.vo.User) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 14 with WatchListVO

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

the class WatchListDwr method savePermissions.

@DwrPermission(user = true)
public ProcessResult savePermissions(String readPermission, String editPermission) {
    WatchListVO wl = getWatchList();
    wl.setReadPermission(readPermission);
    wl.setEditPermission(editPermission);
    ProcessResult response = new ProcessResult();
    wl.validate(response);
    if (!response.getHasMessages())
        WatchListDao.instance.saveWatchList(wl);
    return response;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 15 with WatchListVO

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

the class WatchListDwr method addToWatchList.

@DwrPermission(user = true)
public WatchListState addToWatchList(int pointId) {
    HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
    User user = Common.getUser();
    DataPointVO point = DataPointDao.instance.getDataPoint(pointId);
    if (point == null)
        return null;
    WatchListVO watchList = getWatchList(user);
    // Check permissions.
    Permissions.ensureDataPointReadPermission(user, point);
    WatchListCommon.ensureWatchListEditPermission(user, watchList);
    // Add it to the watch list.
    watchList.getPointList().add(point);
    WatchListDao.instance.saveWatchList(watchList);
    updateSetPermission(point, user);
    // Return the watch list state for it.
    return createWatchListState(request, point, Common.runtimeManager, new HashMap<String, Object>(), user);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) User(com.serotonin.m2m2.vo.User) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

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