Search in sources :

Example 1 with GetAssignedRichMembers

use of cz.metacentrum.perun.webgui.json.resourcesManager.GetAssignedRichMembers in project perun by CESNET.

the class ResourceMemberSettingsTabItem method draw.

public Widget draw() {
    titleWidget.setText(Utils.getStrippedStringWithEllipsis(resource.getName()) + ": member settings");
    final VerticalPanel vp = new VerticalPanel();
    vp.setSize("100%", "100%");
    // menu
    TabMenu menu = new TabMenu();
    vp.add(menu);
    vp.setCellHeight(menu, "30px");
    menu.addWidget(UiElements.getRefreshButton(this));
    final ListBoxWithObjects<RichMember> memberListBox = new ListBoxWithObjects<>();
    final ListBoxWithObjects<Service> serviceListBox = new ListBoxWithObjects<>();
    // load
    final Map<String, Integer> ids = new HashMap<String, Integer>();
    // to get attributes for all services
    final GetResourceRequiredAttributesV2 resReqAttrs = new GetResourceRequiredAttributesV2();
    final GetRequiredAttributesV2 reqAttrs = new GetRequiredAttributesV2(ids, JsonCallbackEvents.passDataToAnotherCallback(resReqAttrs));
    // puts first table
    final CellTable<Attribute> table = resReqAttrs.getEmptyTable();
    // get assigned members
    final GetAssignedRichMembers members = new GetAssignedRichMembers(resourceId, new JsonCallbackEvents() {

        @Override
        public void onFinished(JavaScriptObject jso) {
            memberListBox.removeAllOption();
            memberListBox.clear();
            ArrayList<RichMember> grp = JsonUtils.jsoAsList(jso);
            grp = new TableSorter<RichMember>().sortByName(grp);
            if (grp == null || grp.isEmpty()) {
                memberListBox.addItem("No members assigned");
                lastSelectedMember = 0;
                ((AjaxLoaderImage) table.getEmptyTableWidget()).setEmptyResultMessage("No group or service assigned. Please assign them first to see member-resource setting.");
                ((AjaxLoaderImage) table.getEmptyTableWidget()).loadingFinished();
                return;
            }
            memberListBox.addAllItems(grp);
            for (RichMember g : memberListBox.getAllObjects()) {
                if (lastSelectedMember != 0 && g.getId() == lastSelectedMember)
                    memberListBox.setSelected(g, true);
            }
            memberCallDone = true;
            if (lastSelectedService == 0) {
                // load resource-required
                if (servCallDone) {
                    Map<String, Integer> ids = new HashMap<String, Integer>();
                    ids.put("resource", resourceId);
                    ids.put("resourceToGetServicesFrom", resourceId);
                    ids.put("member", memberListBox.getSelectedObject().getId());
                    ids.put("workWithUserAttributes", 1);
                    resReqAttrs.setIds(ids);
                    resReqAttrs.retrieveData();
                }
            } else {
                // load resource-service-member
                if (servCallDone) {
                    Map<String, Integer> ids = new HashMap<String, Integer>();
                    ids.put("resource", resourceId);
                    ids.put("service", lastSelectedService);
                    ids.put("member", memberListBox.getSelectedObject().getId());
                    ids.put("workWithUserAttributes", 1);
                    reqAttrs.setIds(ids);
                    reqAttrs.retrieveData();
                }
            }
        }

        @Override
        public void onError(PerunError error) {
            memberListBox.removeAllOption();
            memberListBox.clear();
            memberListBox.addItem("Error while loading");
            memberCallDone = true;
        }

        @Override
        public void onLoadingStart() {
            memberListBox.removeAllOption();
            memberListBox.clear();
            memberListBox.addItem("Loading...");
            memberCallDone = false;
        }
    });
    members.retrieveData();
    GetAssignedServices services = new GetAssignedServices(resourceId, new JsonCallbackEvents() {

        @Override
        public void onFinished(JavaScriptObject jso) {
            serviceListBox.removeAllOption();
            serviceListBox.clear();
            ArrayList<Service> srv = JsonUtils.jsoAsList(jso);
            srv = new TableSorter<Service>().sortByName(srv);
            if (srv == null || srv.isEmpty()) {
                serviceListBox.addItem("No services assigned");
                lastSelectedService = 0;
                ((AjaxLoaderImage) table.getEmptyTableWidget()).setEmptyResultMessage("No group or service assigned. Please assign them first to see member-resource setting.");
                ((AjaxLoaderImage) table.getEmptyTableWidget()).loadingFinished();
                return;
            }
            serviceListBox.addAllItems(srv);
            serviceListBox.addAllOption();
            if (lastSelectedService == 0)
                serviceListBox.setSelectedIndex(0);
            for (Service s : serviceListBox.getAllObjects()) {
                if (lastSelectedService != 0 && s.getId() == lastSelectedService)
                    serviceListBox.setSelected(s, true);
            }
            servCallDone = true;
            if (lastSelectedService == 0) {
                if (memberCallDone) {
                    // load resource-required
                    Map<String, Integer> ids = new HashMap<String, Integer>();
                    ids.put("resource", resourceId);
                    ids.put("resourceToGetServicesFrom", resourceId);
                    ids.put("member", memberListBox.getSelectedObject().getId());
                    ids.put("workWithUserAttributes", 1);
                    resReqAttrs.setIds(ids);
                    resReqAttrs.retrieveData();
                }
            } else {
                if (memberCallDone) {
                    // load resource-service-member
                    Map<String, Integer> ids = new HashMap<String, Integer>();
                    ids.put("resource", resourceId);
                    ids.put("service", lastSelectedService);
                    ids.put("member", memberListBox.getSelectedObject().getId());
                    ids.put("workWithUserAttributes", 1);
                    reqAttrs.setIds(ids);
                    reqAttrs.retrieveData();
                }
            }
        }

        @Override
        public void onError(PerunError error) {
            serviceListBox.removeAllOption();
            serviceListBox.clear();
            serviceListBox.addItem("Error while loading");
            servCallDone = true;
        }

        @Override
        public void onLoadingStart() {
            serviceListBox.removeAllOption();
            serviceListBox.clear();
            serviceListBox.addItem("Loading...");
            servCallDone = false;
        }
    });
    services.retrieveData();
    ChangeHandler changeHandler = new ChangeHandler() {

        @Override
        public void onChange(ChangeEvent event) {
            if (memberCallDone && servCallDone) {
                if (memberListBox.getSelectedObject() != null) {
                    lastSelectedMember = memberListBox.getSelectedObject().getId();
                } else {
                    lastSelectedMember = 0;
                }
                if (serviceListBox.getSelectedIndex() > 0) {
                    lastSelectedService = serviceListBox.getSelectedObject().getId();
                } else {
                    lastSelectedService = 0;
                }
                if (lastSelectedService == 0) {
                    // load resource-required
                    Map<String, Integer> ids = new HashMap<String, Integer>();
                    ids.put("resource", resourceId);
                    ids.put("resourceToGetServicesFrom", resourceId);
                    ids.put("member", memberListBox.getSelectedObject().getId());
                    ids.put("workWithUserAttributes", 1);
                    resReqAttrs.setIds(ids);
                    resReqAttrs.retrieveData();
                } else {
                    // load resource-service-member
                    Map<String, Integer> ids = new HashMap<String, Integer>();
                    ids.put("resource", resourceId);
                    ids.put("service", lastSelectedService);
                    ids.put("member", memberListBox.getSelectedObject().getId());
                    ids.put("workWithUserAttributes", 1);
                    reqAttrs.setIds(ids);
                    reqAttrs.retrieveData();
                }
            }
        }
    };
    memberListBox.addChangeHandler(changeHandler);
    serviceListBox.addChangeHandler(changeHandler);
    final JsonCallbackEvents refreshTable = JsonCallbackEvents.refreshTableEvents(resReqAttrs);
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        resReqAttrs.setCheckable(false);
    // add save changes to menu
    final CustomButton saveChangesButton = TabMenu.getPredefinedButton(ButtonType.SAVE, ButtonTranslation.INSTANCE.saveChangesInAttributes());
    menu.addWidget(saveChangesButton);
    // set button disable event
    final JsonCallbackEvents saveChangesButtonEvent = JsonCallbackEvents.disableButtonEvents(saveChangesButton, refreshTable);
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        saveChangesButton.setEnabled(false);
    saveChangesButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            ArrayList<Attribute> list = resReqAttrs.getTableSelectedList();
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                Map<String, Integer> ids = new HashMap<String, Integer>();
                ids.put("resource", resourceId);
                ids.put("member", memberListBox.getSelectedObject().getId());
                ids.put("workWithUserAttributes", 1);
                SetAttributes request = new SetAttributes(saveChangesButtonEvent);
                request.setAttributes(ids, list);
            }
        }
    });
    // add set new to menu
    CustomButton setNewAttributeButton = TabMenu.getPredefinedButton(ButtonType.ADD, true, ButtonTranslation.INSTANCE.setNewAttributes(), new ClickHandler() {

        public void onClick(ClickEvent event) {
            Map<String, Integer> ids = new HashMap<String, Integer>();
            ids.put("resource", resourceId);
            ids.put("member", memberListBox.getSelectedObject().getId());
            ids.put("user", memberListBox.getSelectedObject().getUserId());
            ids.put("facility", resource.getFacilityId());
            session.getTabManager().addTabToCurrentTab(new SetNewAttributeTabItem(ids, resReqAttrs.getList()), true);
        }
    });
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        setNewAttributeButton.setEnabled(false);
    menu.addWidget(setNewAttributeButton);
    // remove attr button
    final CustomButton removeButton = TabMenu.getPredefinedButton(ButtonType.REMOVE, ButtonTranslation.INSTANCE.removeAttributes());
    // remove button event
    final JsonCallbackEvents removeButtonEvent = JsonCallbackEvents.disableButtonEvents(removeButton, refreshTable);
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        removeButton.setEnabled(false);
    removeButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            ArrayList<Attribute> list = resReqAttrs.getTableSelectedList();
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                Map<String, Integer> ids = new HashMap<String, Integer>();
                ids.put("resource", resourceId);
                ids.put("member", memberListBox.getSelectedObject().getId());
                ids.put("facility", resource.getFacilityId());
                ids.put("user", memberListBox.getSelectedObject().getUserId());
                RemoveAttributes request = new RemoveAttributes(removeButtonEvent);
                request.removeAttributes(ids, list);
            }
        }
    });
    menu.addWidget(removeButton);
    // add member selection to menu
    menu.addWidget(new HTML("<strong>Selected&nbsp;member: </strong>"));
    menu.addWidget(memberListBox);
    // add service selection to menu
    menu.addWidget(new HTML("<strong>Selected&nbsp;service: </strong>"));
    menu.addWidget(serviceListBox);
    table.addStyleName("perun-table");
    table.setWidth("100%");
    ScrollPanel sp = new ScrollPanel(table);
    sp.addStyleName("perun-tableScrollPanel");
    session.getUiElements().resizePerunTable(sp, 350, this);
    vp.add(sp);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) HashMap(java.util.HashMap) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ArrayList(java.util.ArrayList) ListBoxWithObjects(cz.metacentrum.perun.webgui.widgets.ListBoxWithObjects) ChangeHandler(com.google.gwt.event.dom.client.ChangeHandler) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) SetNewAttributeTabItem(cz.metacentrum.perun.webgui.tabs.attributestabs.SetNewAttributeTabItem) GetAssignedServices(cz.metacentrum.perun.webgui.json.resourcesManager.GetAssignedServices) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) GetAssignedRichMembers(cz.metacentrum.perun.webgui.json.resourcesManager.GetAssignedRichMembers) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) ChangeEvent(com.google.gwt.event.dom.client.ChangeEvent) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 ChangeEvent (com.google.gwt.event.dom.client.ChangeEvent)1 ChangeHandler (com.google.gwt.event.dom.client.ChangeHandler)1 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)1 GetAssignedRichMembers (cz.metacentrum.perun.webgui.json.resourcesManager.GetAssignedRichMembers)1 GetAssignedServices (cz.metacentrum.perun.webgui.json.resourcesManager.GetAssignedServices)1 SetNewAttributeTabItem (cz.metacentrum.perun.webgui.tabs.attributestabs.SetNewAttributeTabItem)1 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)1 ListBoxWithObjects (cz.metacentrum.perun.webgui.widgets.ListBoxWithObjects)1 TabMenu (cz.metacentrum.perun.webgui.widgets.TabMenu)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1