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);
}
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);
}
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);
}
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;
}
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);
}
Aggregations