Search in sources :

Example 1 with GraphicalViewDao

use of com.serotonin.m2m2.gviews.GraphicalViewDao in project ma-modules-public by infiniteautomation.

the class GraphicalViewDwr method saveView.

@DwrPermission(user = true)
public ProcessResult saveView(String name, String xid, int anonymousAccess, String readPermission, String setPermission, String editPermission) {
    ProcessResult result = new ProcessResult();
    User user = Common.getUser();
    GraphicalView view = GraphicalViewsCommon.getUserEditView(user);
    view.setName(name);
    view.setXid(xid);
    view.setAnonymousAccess(anonymousAccess);
    view.setReadPermission(readPermission);
    view.setSetPermission(setPermission);
    view.setEditPermission(editPermission);
    view.validate(result);
    if (!result.getHasMessages()) {
        view.setUserId(user.getId());
        new GraphicalViewDao().saveView(view);
        result.addData("view", view);
    }
    return result;
}
Also used : ShareUser(com.serotonin.m2m2.view.ShareUser) AnonymousUser(com.serotonin.m2m2.vo.AnonymousUser) User(com.serotonin.m2m2.vo.User) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 2 with GraphicalViewDao

use of com.serotonin.m2m2.gviews.GraphicalViewDao in project ma-modules-public by infiniteautomation.

the class GraphicalViewHandler method handleRequest.

@Override
public View handleRequest(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {
    GraphicalViewDao viewDao = new GraphicalViewDao();
    User user = Common.getUser(request);
    List<IntStringPair> views = viewDao.getViewNames(user);
    model.put("views", views);
    try {
        GraphicalViewsCommon.ensureCanCreate(user);
        model.put("canCreateViews", true);
    } catch (Exception e) {
        model.put("canCreateViews", false);
    }
    // Set the current view.
    GraphicalView currentView = null;
    String vid = request.getParameter("viewId");
    if (StringUtils.isBlank(vid)) {
        String xid = request.getParameter("xid");
        if (xid != null)
            currentView = viewDao.getViewByXid(xid);
    } else {
        try {
            currentView = viewDao.getView(Integer.parseInt(vid));
        } catch (NumberFormatException e) {
        // no op
        }
    }
    if (currentView == null && views.size() > 0)
        currentView = viewDao.getView(views.get(0).getKey());
    if (currentView != null) {
        GraphicalViewsCommon.ensureViewPermission(user, currentView);
        // Make sure the owner still has permission to all of the points in the view, and that components are
        // otherwise valid.
        currentView.validateViewComponents(false);
        // Add the view to the session for the dwr access stuff.
        model.put("currentView", currentView);
        if (currentView.isOwner(user))
            model.put("owner", true);
        else
            model.put("owner", false);
        if (currentView.isEditor(user))
            model.put("canEditCurrentView", true);
        else
            model.put("canEditCurrentView", false);
        GraphicalViewsCommon.setUserView(user, currentView);
    }
    return null;
}
Also used : User(com.serotonin.m2m2.vo.User) IntStringPair(com.serotonin.db.pair.IntStringPair)

Example 3 with GraphicalViewDao

use of com.serotonin.m2m2.gviews.GraphicalViewDao in project ma-modules-public by infiniteautomation.

the class GraphicalView method validate.

public void validate(ProcessResult response) {
    if (StringUtils.isBlank(name))
        response.addMessage("name", new TranslatableMessage("validate.required"));
    else if (StringValidation.isLengthGreaterThan(name, 100))
        response.addMessage("name", new TranslatableMessage("validate.notLongerThan", 100));
    if (StringUtils.isBlank(xid))
        response.addMessage("xid", new TranslatableMessage("validate.required"));
    else if (StringValidation.isLengthGreaterThan(xid, 50))
        response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
    else if (!new GraphicalViewDao().isXidUnique(xid, id))
        response.addMessage("xid", new TranslatableMessage("validate.xidUsed"));
    for (ViewComponent vc : viewComponents) vc.validate(response);
    // Validate the permissions
    User user = Common.getUser();
    GraphicalView existingView = null;
    if (this.id != Common.NEW_ID) {
        existingView = new GraphicalViewDao().getView(id);
    }
    if (existingView == null) {
        Permissions.validateAddedPermissions(this.readPermission, user, response, "readPermission");
        Permissions.validateAddedPermissions(this.setPermission, user, response, "setPermission");
        Permissions.validateAddedPermissions(this.editPermission, user, response, "editPermission");
    } else {
        // We are updating a view so only validate the new permissions, allow existing ones to remain and don't let
        // the user remove permissions they do not have
        this.readPermission = trimPermission(this.readPermission);
        validateUpdatedPermissions(existingView.readPermission, this.readPermission, user, response, "readPermission");
        this.setPermission = trimPermission(this.setPermission);
        validateUpdatedPermissions(existingView.setPermission, this.setPermission, user, response, "setPermission");
        this.editPermission = trimPermission(this.editPermission);
        validateUpdatedPermissions(existingView.editPermission, this.editPermission, user, response, "editPermission");
    }
}
Also used : ShareUser(com.serotonin.m2m2.view.ShareUser) User(com.serotonin.m2m2.vo.User) ViewComponent(com.serotonin.m2m2.gviews.component.ViewComponent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 4 with GraphicalViewDao

use of com.serotonin.m2m2.gviews.GraphicalViewDao in project ma-modules-public by infiniteautomation.

the class GraphicalViewEditHandler method handleRequest.

@Override
public View handleRequest(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) throws Exception {
    GraphicalView view;
    User user = Common.getUser(request);
    // Fresh hit. Get the id.
    String viewIdStr = request.getParameter("viewId");
    if (viewIdStr != null) {
        // An existing view.
        view = new GraphicalViewDao().getView(Integer.parseInt(viewIdStr));
        if (view == null)
            // Doesn't exist. Redirect back to the views page.
            return new RedirectView("/views.shtm");
        GraphicalViewsCommon.ensureViewEditPermission(user, view);
        if ("true".equals(request.getParameter("copy"))) {
            // Make a copy
            GraphicalView copy = new GraphicalView();
            copy.setId(Common.NEW_ID);
            copy.setUserId(user.getId());
            copy.setXid(new GraphicalViewDao().generateUniqueXid());
            copy.setName(StringUtils.abbreviate(TranslatableMessage.translate(ControllerUtils.getTranslations(request), "common.copyPrefix", view.getName()), 100));
            copy.setBackgroundFilename(GraphicalViewsCommon.copyImage(view.getBackgroundFilename()));
            for (ViewComponent vc : view.getViewComponents()) copy.addViewComponent(vc);
            view = copy;
        }
    } else {
        // A new view.
        GraphicalViewsCommon.ensureCanCreate(user);
        view = new GraphicalView();
        view.setId(Common.NEW_ID);
        view.setUserId(user.getId());
        view.setXid(new GraphicalViewDao().generateUniqueXid());
    }
    GraphicalViewsCommon.setUserEditView(user, view);
    view.validateViewComponents(false);
    model.put("imageSets", Common.imageSets);
    model.put("dynamicImages", Common.dynamicImages);
    model.put("view", view);
    return null;
}
Also used : User(com.serotonin.m2m2.vo.User) GraphicalView(com.serotonin.m2m2.gviews.GraphicalView) GraphicalViewDao(com.serotonin.m2m2.gviews.GraphicalViewDao) RedirectView(org.springframework.web.servlet.view.RedirectView) ViewComponent(com.serotonin.m2m2.gviews.component.ViewComponent)

Aggregations

User (com.serotonin.m2m2.vo.User)4 ViewComponent (com.serotonin.m2m2.gviews.component.ViewComponent)2 ShareUser (com.serotonin.m2m2.view.ShareUser)2 IntStringPair (com.serotonin.db.pair.IntStringPair)1 GraphicalView (com.serotonin.m2m2.gviews.GraphicalView)1 GraphicalViewDao (com.serotonin.m2m2.gviews.GraphicalViewDao)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 AnonymousUser (com.serotonin.m2m2.vo.AnonymousUser)1 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)1 RedirectView (org.springframework.web.servlet.view.RedirectView)1