Search in sources :

Example 26 with Attribute

use of cz.metacentrum.perun.webgui.model.Attribute in project perun by CESNET.

the class FindCandidatesOrUsersToAddToVo method getExtSourceNameOrOrganizationColumn.

public static String getExtSourceNameOrOrganizationColumn(Candidate candidate) {
    if (candidate.getObjectType().equalsIgnoreCase("RichUser")) {
        User user = candidate.cast();
        Attribute at = user.getAttribute("urn:perun:user:attribute-def:def:organization");
        if (at != null && at.getValue() != null && !"null".equalsIgnoreCase(at.getValue())) {
            return at.getValue();
        }
        return "";
    }
    if (candidate.getUserExtSource().getExtSource().getType().equalsIgnoreCase("cz.metacentrum.perun.core.impl.ExtSourceX509")) {
        return Utils.convertCertCN(candidate.getUserExtSource().getExtSource().getName());
    } else if (candidate.getUserExtSource().getExtSource().getType().equals("cz.metacentrum.perun.core.impl.ExtSourceIdp")) {
        return Utils.translateIdp(candidate.getUserExtSource().getExtSource().getName());
    } else {
        return candidate.getUserExtSource().getExtSource().getName();
    }
}
Also used : User(cz.metacentrum.perun.webgui.model.User) Attribute(cz.metacentrum.perun.webgui.model.Attribute)

Example 27 with Attribute

use of cz.metacentrum.perun.webgui.model.Attribute in project perun by CESNET.

the class FindCandidatesOrUsersToAddToVo method getEmailColumnValue.

public static String getEmailColumnValue(Candidate candidate) {
    if (candidate.getObjectType().equalsIgnoreCase("RichUser")) {
        User user = candidate.cast();
        Attribute at = user.getAttribute("urn:perun:user:attribute-def:def:preferredMail");
        if (at != null && at.getValue() != null && !"null".equalsIgnoreCase(at.getValue())) {
            return at.getValue().replace(",", " ");
        }
        return "";
    }
    if (candidate.getEmail() == null) {
        return "";
    } else {
        return candidate.getEmail();
    }
}
Also used : User(cz.metacentrum.perun.webgui.model.User) Attribute(cz.metacentrum.perun.webgui.model.Attribute)

Example 28 with Attribute

use of cz.metacentrum.perun.webgui.model.Attribute in project perun by CESNET.

the class SelfAuthenticationsTabItem method draw.

public Widget draw() {
    this.titleWidget.setText(Utils.getStrippedStringWithEllipsis(user.getFullNameWithTitles().trim()) + ": Authentication");
    final String notSet = "<i>N/A</i>";
    // content
    ScrollPanel vp = new ScrollPanel();
    vp.setSize("100%", "100%");
    final VerticalPanel innerVp = new VerticalPanel();
    innerVp.setSize("100%", "100%");
    final TabMenu menu = new TabMenu();
    innerVp.add(menu);
    innerVp.setCellHeight(menu, "30px");
    menu.addWidget(UiElements.getRefreshButton(this));
    final FlexTable layout = new FlexTable();
    layout.setSize("100%", "100%");
    vp.add(innerVp);
    innerVp.add(layout);
    layout.setStyleName("perun-table");
    vp.setStyleName("perun-tableScrollPanel");
    session.getUiElements().resizeSmallTabPanel(vp, 350, this);
    FlexTable loginsHeader = new FlexTable();
    loginsHeader.setWidget(0, 0, new Image(LargeIcons.INSTANCE.keyIcon()));
    loginsHeader.setHTML(0, 1, "<p>Logins");
    loginsHeader.getFlexCellFormatter().setStyleName(0, 1, "subsection-heading");
    layout.setWidget(0, 0, loginsHeader);
    FlexTable certHeader = new FlexTable();
    certHeader.setWidget(0, 0, new Image(LargeIcons.INSTANCE.sslCertificatesIcon()));
    certHeader.setHTML(0, 1, "<p>Certificates");
    certHeader.getFlexCellFormatter().setStyleName(0, 1, "subsection-heading");
    layout.setWidget(3, 0, certHeader);
    layout.setHTML(4, 0, "To <strong>add certificate</strong> please visit <a href=\"" + Utils.getIdentityConsolidatorLink(false) + "\" target=\"_blank\">identity consolidator &gt;&gt;</a> and select \"Using personal certificate\" option.<br />&nbsp;");
    FlexTable sshHeader = new FlexTable();
    sshHeader.setWidget(0, 0, new Image(LargeIcons.INSTANCE.serverKeyIcon()));
    sshHeader.setHTML(0, 1, "<p>Kerberos & SSH keys");
    sshHeader.getFlexCellFormatter().setStyleName(0, 1, "subsection-heading");
    layout.setWidget(6, 0, sshHeader);
    // login table
    final FlexTable loginsTable = new FlexTable();
    loginsTable.setStyleName("inputFormFlexTableDark");
    final GetLogins logins = new GetLogins(userId, new JsonCallbackEvents() {

        @Override
        public void onFinished(JavaScriptObject jso) {
            final ArrayList<Attribute> list = JsonUtils.jsoAsList(jso);
            layout.setWidget(1, 0, loginsTable);
            int row = 0;
            if (user.isServiceUser() || user.isSponsoredUser()) {
                CustomButton addLogin = TabMenu.getPredefinedButton(ButtonType.ADD, "Add new login (only for supported namespaces)");
                addLogin.addClickHandler(new ClickHandler() {

                    @Override
                    public void onClick(ClickEvent event) {
                        session.getTabManager().addTabToCurrentTab(new AddLoginTabItem(user, list));
                    }
                });
                loginsTable.setWidget(row, 0, addLogin);
                row++;
            }
            if (list != null && !list.isEmpty()) {
                for (final Attribute a : list) {
                    loginsTable.setHTML(row, 0, "Login in " + a.getFriendlyNameParameter().toUpperCase() + ":");
                    loginsTable.getFlexCellFormatter().setWidth(row, 0, "150px");
                    loginsTable.getFlexCellFormatter().addStyleName(row, 0, "itemName");
                    loginsTable.setHTML(row, 1, a.getValue());
                    loginsTable.getFlexCellFormatter().setWidth(row, 1, "150px");
                    // change password if possible
                    if (Utils.getSupportedPasswordNamespaces().contains(a.getFriendlyNameParameter())) {
                        FlexTable fw = new FlexTable();
                        fw.addStyleName("padding-vertical");
                        CustomButton cb = new CustomButton("Change password…", SmallIcons.INSTANCE.keyIcon(), new ClickHandler() {

                            public void onClick(ClickEvent event) {
                                session.getTabManager().addTabToCurrentTab(new SelfPasswordTabItem(user, a.getFriendlyNameParameter(), a.getValue(), SelfPasswordTabItem.Actions.CHANGE));
                            }
                        });
                        CustomButton cb2 = new CustomButton("Reset password…", SmallIcons.INSTANCE.keyIcon(), new ClickHandler() {

                            public void onClick(ClickEvent event) {
                                // OPEN PASSWORD RESET APPLICATION ON SAME SERVER
                                Window.open("" + Utils.getPasswordResetLink(a.getFriendlyNameParameter()), "_blank", "");
                            }
                        });
                        fw.setWidget(0, 0, cb);
                        if (!user.isServiceUser()) {
                            fw.setWidget(0, 1, cb2);
                        } else {
                            cb.setText("Reset password…");
                        }
                        loginsTable.setWidget(row, 2, fw);
                    }
                    row++;
                }
            } else {
                loginsTable.setHTML(row, 0, "You don't have any login in supported namespaces.");
                loginsTable.getFlexCellFormatter().setStyleName(0, 0, "inputFormInlineComment");
            }
        }

        @Override
        public void onLoadingStart() {
            layout.setWidget(1, 0, new AjaxLoaderImage().loadingStart());
        }

        @Override
        public void onError(PerunError error) {
            layout.setHTML(1, 0, "Error while loading logins.");
            layout.getFlexCellFormatter().setStyleName(1, 0, "serverResponseLabelError");
        }
    });
    logins.retrieveData();
    // certificates table
    final FlexTable certTable = new FlexTable();
    certTable.addStyleName("inputFormFlexTableDark");
    certTable.setHTML(0, 0, "Certificates: ");
    certTable.getFlexCellFormatter().addStyleName(0, 0, "itemName");
    certTable.getFlexCellFormatter().setWidth(0, 0, "150px");
    layout.setWidget(5, 0, certTable);
    final GetUserExtSources ueses = new GetUserExtSources(userId);
    ueses.setEvents(new JsonCallbackEvents() {

        @Override
        public void onFinished(JavaScriptObject jso) {
            ArrayList<UserExtSource> list = JsonUtils.jsoAsList(jso);
            if (list != null && !list.isEmpty()) {
                boolean found = false;
                FlexTable tab = new FlexTable();
                // rowcounter
                int i = 0;
                for (final UserExtSource a : list) {
                    if (a.getExtSource().getType().equals("cz.metacentrum.perun.core.impl.ExtSourceX509")) {
                        found = true;
                        tab.setHTML(i++, 0, "<strong>" + a.getLogin() + "</strong>");
                        tab.setHTML(i++, 0, "Issuer: " + a.getExtSource().getName());
                        if (!a.isPersistent()) {
                            CustomButton removeButton = new CustomButton("Remove", SmallIcons.INSTANCE.deleteIcon(), new ClickHandler() {

                                @Override
                                public void onClick(ClickEvent event) {
                                    RemoveUserExtSource remove = new RemoveUserExtSource(new JsonCallbackEvents() {

                                        @Override
                                        public void onFinished(JavaScriptObject jso) {
                                            // reload whole tab
                                            ueses.retrieveData();
                                        }
                                    });
                                    remove.removeUserExtSource(userId, a.getId());
                                }
                            });
                            // add button to table
                            tab.getFlexCellFormatter().setRowSpan(i - 2, 1, 2);
                            tab.setWidget(i - 2, 1, removeButton);
                        }
                    }
                }
                if (found) {
                    certTable.setWidget(0, 1, tab);
                } else {
                    certTable.setHTML(0, 1, notSet);
                }
            } else {
                certTable.setHTML(0, 1, notSet);
            }
        }

        @Override
        public void onError(PerunError error) {
            certTable.setHTML(0, 1, "Error while loading certificates. Refresh page to retry.");
            certTable.getFlexCellFormatter().setStyleName(0, 1, "serverResponseLabelError");
        }

        @Override
        public void onLoadingStart() {
            certTable.setWidget(0, 1, new Image(AjaxLoaderImage.SMALL_IMAGE_URL));
        }
    });
    ueses.retrieveData();
    // Kerberos and SSH table
    Map<String, Integer> ids = new HashMap<>();
    ids.put("user", userId);
    final PerunAttributeTableWidget table = new PerunAttributeTableWidget(ids);
    table.setDark(true);
    table.setDescriptionShown(true);
    final GetListOfAttributes attrs2 = new GetListOfAttributes(new JsonCallbackEvents() {

        @Override
        public void onFinished(JavaScriptObject jso) {
            table.add(JsonUtils.<Attribute>jsoAsList(jso));
            layout.setWidget(7, 0, table);
        }

        @Override
        public void onError(PerunError error) {
            layout.setHTML(7, 0, "Error while loading Kerberos and SSH settings. Refresh page to retry.");
            layout.getFlexCellFormatter().setStyleName(7, 0, "serverResponseLabelError");
        }

        @Override
        public void onLoadingStart() {
            layout.setWidget(7, 0, new AjaxLoaderImage().loadingStart());
        }
    });
    ArrayList<String> list2 = new ArrayList<String>();
    list2.add("urn:perun:user:attribute-def:def:kerberosAdminPrincipal");
    list2.add("urn:perun:user:attribute-def:def:sshPublicAdminKey");
    list2.add("urn:perun:user:attribute-def:def:sshPublicKey");
    attrs2.getListOfAttributes(ids, list2);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) AjaxLoaderImage(cz.metacentrum.perun.webgui.widgets.AjaxLoaderImage) Attribute(cz.metacentrum.perun.webgui.model.Attribute) HashMap(java.util.HashMap) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ArrayList(java.util.ArrayList) PerunAttributeTableWidget(cz.metacentrum.perun.webgui.widgets.PerunAttributeTableWidget) AjaxLoaderImage(cz.metacentrum.perun.webgui.widgets.AjaxLoaderImage) RemoveUserExtSource(cz.metacentrum.perun.webgui.json.usersManager.RemoveUserExtSource) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) GetLogins(cz.metacentrum.perun.webgui.json.attributesManager.GetLogins) GetListOfAttributes(cz.metacentrum.perun.webgui.json.attributesManager.GetListOfAttributes) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) UserExtSource(cz.metacentrum.perun.webgui.model.UserExtSource) RemoveUserExtSource(cz.metacentrum.perun.webgui.json.usersManager.RemoveUserExtSource) GetUserExtSources(cz.metacentrum.perun.webgui.json.usersManager.GetUserExtSources) PerunError(cz.metacentrum.perun.webgui.model.PerunError)

Example 29 with Attribute

use of cz.metacentrum.perun.webgui.model.Attribute in project perun by CESNET.

the class AddLoginTabItem method draw.

public Widget draw() {
    titleWidget.setText("Add login");
    VerticalPanel vp = new VerticalPanel();
    vp.setSize("100%", "100%");
    final ExtendedTextBox userLogin = new ExtendedTextBox();
    final ListBox namespace = new ListBox();
    final CustomButton createLogin = TabMenu.getPredefinedButton(ButtonType.ADD, "Add login in selected namespace");
    final Label notice = new Label("Your login will be automatically generated.");
    notice.setVisible(false);
    // offer only available namespaces.
    ArrayList<String> logins = new ArrayList<String>();
    for (Attribute a : usersLogins) {
        logins.add(a.getFriendlyNameParameter());
    }
    for (String s : Utils.getSupportedPasswordNamespaces()) {
        if (!logins.contains(s)) {
            namespace.addItem(s.toUpperCase(), s);
        }
    }
    final ExtendedTextBox.TextBoxValidator loginValidator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (userLogin.getTextBox().getValue().trim().isEmpty()) {
                userLogin.setError("Login can't be empty!");
                return false;
            }
            RegExp regExp = RegExp.compile(Utils.LOGIN_VALUE_MATCHER);
            boolean match = regExp.test(userLogin.getTextBox().getValue().trim());
            if (!match) {
                userLogin.setError("Invalid format!");
                return false;
            }
            if (userLogin.isProcessing() || userLogin.isHardError()) {
                return false;
            }
            userLogin.setOk();
            return true;
        }
    };
    userLogin.setValidator(loginValidator);
    final FlexTable layout = new FlexTable();
    layout.addStyleName("inputFormFlexTable");
    layout.setHTML(0, 0, "Namespace:");
    layout.setHTML(1, 0, "Login:");
    for (int i = 0; i < layout.getRowCount(); i++) {
        layout.getFlexCellFormatter().addStyleName(i, 0, "itemName");
    }
    layout.setWidget(0, 1, namespace);
    layout.setWidget(1, 1, userLogin);
    layout.getFlexCellFormatter().setColSpan(2, 0, 2);
    layout.setWidget(2, 0, notice);
    layout.getFlexCellFormatter().addStyleName(2, 0, "inputFormInlineComment");
    TabMenu menu = new TabMenu();
    menu.addWidget(createLogin);
    final TabItem tab = this;
    menu.addWidget(TabMenu.getPredefinedButton(ButtonType.CANCEL, "", new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            session.getTabManager().closeTab(tab, false);
        }
    }));
    // user can't add new login
    if (namespace.getItemCount() == 0) {
        vp.add(new HTML("<p><strong>You already have login in all supported namespaces!</strong></p>"));
        createLogin.setEnabled(false);
    } else {
        // user can add new login
        vp.add(layout);
        // check login availability
        userLogin.getTextBox().addKeyUpHandler(new KeyUpHandler() {

            @Override
            public void onKeyUp(KeyUpEvent keyUpEvent) {
                if (keyUpEvent.isDownArrow() || keyUpEvent.isUpArrow() || keyUpEvent.isLeftArrow() || keyUpEvent.isRightArrow()) {
                    // do not trigger when no text input
                    return;
                }
                final String value = userLogin.getTextBox().getValue().trim();
                // trigger new validation on checked input or if previously was hard error
                if ((!value.isEmpty() && RegExp.compile(Utils.LOGIN_VALUE_MATCHER).test(value)) || userLogin.isHardError()) {
                    new IsLoginAvailable(namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim(), new JsonCallbackEvents() {

                        @Override
                        public void onFinished(JavaScriptObject jso) {
                            if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                BasicOverlayType bo = jso.cast();
                                userLogin.setProcessing(false);
                                if (!bo.getBoolean()) {
                                    userLogin.setHardError("Login is already in use!");
                                } else {
                                    userLogin.removeHardError();
                                    loginValidator.validateTextBox();
                                }
                            }
                        }

                        @Override
                        public void onLoadingStart() {
                            if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                userLogin.removeHardError();
                                userLogin.setProcessing(true);
                            }
                        }

                        @Override
                        public void onError(PerunError error) {
                            if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                userLogin.setProcessing(false);
                                userLogin.setHardError("Unable to check if login is available!");
                            }
                        }
                    }).retrieveData();
                }
            }
        });
        namespace.addChangeHandler(new ChangeHandler() {

            public void onChange(ChangeEvent changeEvent) {
                if (namespace.getSelectedValue().equals("mu")) {
                    userLogin.getTextBox().setValue("");
                    userLogin.removeHardError();
                    userLogin.setOk();
                    userLogin.getTextBox().setEnabled(false);
                    notice.setVisible(true);
                } else {
                    userLogin.getTextBox().setEnabled(true);
                    notice.setVisible(false);
                    final String value = userLogin.getTextBox().getValue().trim();
                    // trigger new validation on checked input or if previously was hard error
                    if ((!value.isEmpty() && RegExp.compile(Utils.LOGIN_VALUE_MATCHER).test(value)) || userLogin.isHardError()) {
                        new IsLoginAvailable(namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim(), new JsonCallbackEvents() {

                            @Override
                            public void onFinished(JavaScriptObject jso) {
                                if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                    BasicOverlayType bo = jso.cast();
                                    userLogin.setProcessing(false);
                                    if (!bo.getBoolean()) {
                                        userLogin.setError("Login is already in use!");
                                    } else {
                                        userLogin.removeHardError();
                                        loginValidator.validateTextBox();
                                    }
                                }
                            }

                            @Override
                            public void onLoadingStart() {
                                if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                    userLogin.removeHardError();
                                    userLogin.setProcessing(true);
                                }
                            }

                            @Override
                            public void onError(PerunError error) {
                                if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                    userLogin.setProcessing(false);
                                    userLogin.setHardError("Error while loading.");
                                }
                            }
                        }).retrieveData();
                    }
                }
            }
        });
        createLogin.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                if (namespace.getSelectedValue().equals("mu")) {
                    session.getTabManager().addTabToCurrentTab(new SelfPasswordTabItem(user, namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim(), SelfPasswordTabItem.Actions.CREATE));
                } else {
                    if (!loginValidator.validateTextBox())
                        return;
                    SetLogin request = new SetLogin(JsonCallbackEvents.disableButtonEvents(createLogin, new JsonCallbackEvents() {

                        @Override
                        public void onFinished(JavaScriptObject jso) {
                            session.getTabManager().addTabToCurrentTab(new SelfPasswordTabItem(user, namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim(), SelfPasswordTabItem.Actions.CREATE));
                        }
                    }));
                    request.setLogin(user, namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim());
                }
            }
        });
    }
    if (namespace.getSelectedValue().equals("mu")) {
        userLogin.getTextBox().setValue("");
        userLogin.removeHardError();
        userLogin.setOk();
        userLogin.getTextBox().setEnabled(false);
        notice.setVisible(true);
    }
    vp.add(menu);
    vp.setCellHorizontalAlignment(menu, HasHorizontalAlignment.ALIGN_RIGHT);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) Attribute(cz.metacentrum.perun.webgui.model.Attribute) BasicOverlayType(cz.metacentrum.perun.webgui.model.BasicOverlayType) ArrayList(java.util.ArrayList) IsLoginAvailable(cz.metacentrum.perun.webgui.json.usersManager.IsLoginAvailable) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) RegExp(com.google.gwt.regexp.shared.RegExp) ExtendedTextBox(cz.metacentrum.perun.webgui.widgets.ExtendedTextBox) SetLogin(cz.metacentrum.perun.webgui.json.usersManager.SetLogin) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) TabItem(cz.metacentrum.perun.webgui.tabs.TabItem) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) PerunError(cz.metacentrum.perun.webgui.model.PerunError)

Example 30 with Attribute

use of cz.metacentrum.perun.webgui.model.Attribute in project perun by CESNET.

the class ResourceSettingsTabItem method draw.

public Widget draw() {
    if (service != null) {
        titleWidget.setText(Utils.getStrippedStringWithEllipsis(resource.getName()) + ": " + service.getName() + " configuration");
    } else {
        titleWidget.setText(Utils.getStrippedStringWithEllipsis(resource.getName()) + ": service configuration");
    }
    vp.setSize("100%", "100%");
    vp.clear();
    // menu
    TabMenu menu = new TabMenu();
    vp.add(menu);
    vp.setCellHeight(menu, "30px");
    menu.addWidget(UiElements.getRefreshButton(this));
    // callback
    final GetRequiredAttributesV2 resAttrs = new GetRequiredAttributesV2();
    final Map<String, Integer> ids = new HashMap<String, Integer>();
    ids.put("resource", resourceId);
    resAttrs.setIds(ids);
    final GetAttributesV2 attrs = new GetAttributesV2();
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        resAttrs.setCheckable(false);
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        attrs.setCheckable(false);
    // puts first table
    final CellTable<Attribute> table = resAttrs.getEmptyTable();
    final CellTable<Attribute> table2 = attrs.getEmptyTable();
    // if for one service only modify empty table message
    if (service != null) {
        AjaxLoaderImage empty = (AjaxLoaderImage) table.getEmptyTableWidget();
        empty.setEmptyResultMessage("Service " + service.getName() + " requires no setting on resource.");
        AjaxLoaderImage empty2 = (AjaxLoaderImage) table2.getEmptyTableWidget();
        empty2.setEmptyResultMessage("Service " + service.getName() + " requires no setting on resource.");
    }
    final ListBoxWithObjects<Service> servList = new ListBoxWithObjects<Service>();
    sp.setWidget(table);
    sp2.setWidget(table2);
    // switch between assigned and all
    final CheckBox chb = new CheckBox();
    chb.setText(WidgetTranslation.INSTANCE.offerAvailableServicesOnly());
    chb.setTitle(WidgetTranslation.INSTANCE.offerAvailableServicesOnlyTitle());
    // default true
    chb.setValue(lastOfferAvailableOnly);
    // event which fills the listbox and call getRequiredAttributes
    JsonCallbackEvents event = new JsonCallbackEvents() {

        @Override
        public void onFinished(JavaScriptObject jso) {
            // clear services list
            servList.clear();
            // process services
            ArrayList<Service> srv = JsonUtils.jsoAsList(jso);
            srv = new TableSorter<Service>().sortByName(srv);
            for (int i = 0; i < srv.size(); i++) {
                // fill listbox
                servList.addItem(srv.get(i));
            }
            if (servList.isEmpty()) {
                servList.addNotSelectedOption();
                lastSelectedService = 0;
                indexInList = 0;
                attrs.retrieveData();
                setTable(false);
                return;
            }
            if (lastSelectedService == 0 && chb.getValue() == true) {
                servList.addNotSelectedOption();
                servList.addAllOption();
                if (indexInList == 0) {
                    servList.setSelectedIndex(0);
                    attrs.retrieveData();
                    setTable(false);
                    return;
                } else if (indexInList == 1) {
                    servList.setSelectedIndex(1);
                }
            } else if (lastSelectedService == 0 && chb.getValue() == false) {
                servList.addNotSelectedOption();
                servList.setSelectedIndex(0);
                attrs.retrieveData();
                setTable(false);
                return;
            } else if (lastSelectedService != 0) {
                if (chb.getValue() == true) {
                    servList.addNotSelectedOption();
                    servList.addAllOption();
                } else {
                    servList.addNotSelectedOption();
                }
                // remove last service, we can't be sure it was loaded again
                ids.remove("service");
                // either all or specific service
                servList.setSelectedIndex(1);
                for (Service s : servList.getAllObjects()) {
                    if (s.getId() == lastSelectedService) {
                        // service selected last time was found in a list
                        servList.setSelected(s, true);
                        ids.put("service", s.getId());
                        break;
                    }
                }
            }
            // make call
            resAttrs.clearTable();
            resAttrs.setIds(ids);
            resAttrs.retrieveData();
            setTable(true);
        }

        ;

        @Override
        public void onError(PerunError error) {
            servList.clear();
            servList.addItem("Error while loading");
            if (required) {
                ((AjaxLoaderImage) table.getEmptyTableWidget()).loadingError(error);
            } else {
                ((AjaxLoaderImage) table2.getEmptyTableWidget()).loadingError(error);
            }
        }

        ;

        @Override
        public void onLoadingStart() {
            servList.removeAllOption();
            servList.removeNotSelectedOption();
            servList.clear();
            servList.addItem("Loading...");
        }
    };
    final GetAssignedServices services = new GetAssignedServices(resourceId, event);
    final GetServices allServices = new GetServices(event);
    // offer services selection
    if (serviceId == 0) {
        // services listbox
        servList.setTitle("Services");
        attrs.setIds(ids);
        // on change of service update table
        servList.addChangeHandler(new ChangeHandler() {

            public void onChange(ChangeEvent event) {
                // if service selected
                if (servList.getSelectedIndex() == 0) {
                    attrs.retrieveData();
                    setTable(false);
                    lastSelectedService = 0;
                    indexInList = 0;
                    return;
                } else if (chb.getValue() == true && servList.getSelectedIndex() == 1) {
                    ids.remove("service");
                    lastSelectedService = 0;
                    indexInList = 1;
                } else if ((chb.getValue() == true && servList.getSelectedIndex() > 1) || (chb.getValue() == false && servList.getSelectedIndex() > 0)) {
                    ids.put("service", servList.getSelectedObject().getId());
                    lastSelectedService = servList.getSelectedObject().getId();
                }
                lastOfferAvailableOnly = chb.getValue();
                resAttrs.clearTable();
                resAttrs.setIds(ids);
                resAttrs.retrieveData();
                setTable(true);
            }
        });
        if (chb.getValue() == false) {
            allServices.retrieveData();
        } else {
            services.retrieveData();
        }
    } else {
        // retrieve data for selected service only
        lastSelectedService = serviceId;
        lastOfferAvailableOnly = chb.getValue();
        ids.put("service", serviceId);
        resAttrs.setIds(ids);
        resAttrs.retrieveData();
        setTable(true);
    }
    // refresh table event - refresh only on finished / on error keep selected
    final JsonCallbackEvents refreshTable = JsonCallbackEvents.refreshTableEvents(resAttrs);
    final JsonCallbackEvents refreshTable2 = JsonCallbackEvents.refreshTableEvents(attrs);
    // 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);
    final JsonCallbackEvents saveChangesButtonEvent2 = JsonCallbackEvents.disableButtonEvents(saveChangesButton, refreshTable2);
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        saveChangesButton.setEnabled(false);
    saveChangesButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            ArrayList<Attribute> list = new ArrayList<Attribute>();
            if (required) {
                list = resAttrs.getTableSelectedList();
            } else {
                list = attrs.getTableSelectedList();
            }
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                Map<String, Integer> ids = new HashMap<String, Integer>();
                ids.put("resource", resourceId);
                SetAttributes request = new SetAttributes((required) ? saveChangesButtonEvent : saveChangesButtonEvent2);
                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) {
            ArrayList<Attribute> list = new ArrayList<Attribute>();
            if (required) {
                list = resAttrs.getList();
            } else {
                list = attrs.getList();
            }
            Map<String, Integer> ids = new HashMap<String, Integer>();
            ids.put("resource", resourceId);
            session.getTabManager().addTabToCurrentTab(new SetNewAttributeTabItem(ids, list), true);
        }
    });
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        setNewAttributeButton.setEnabled(false);
    if (service == null) {
        menu.addWidget(setNewAttributeButton);
    }
    // fill button
    final CustomButton fillDefaultButton = TabMenu.getPredefinedButton(ButtonType.FILL, ButtonTranslation.INSTANCE.fillResourceAttributes());
    menu.addWidget(fillDefaultButton);
    // remove attr button
    final CustomButton removeButton = TabMenu.getPredefinedButton(ButtonType.REMOVE, ButtonTranslation.INSTANCE.removeAttributes());
    // remove button event
    final JsonCallbackEvents removeButtonEvent = JsonCallbackEvents.disableButtonEvents(removeButton, refreshTable);
    final JsonCallbackEvents removeButtonEvent2 = JsonCallbackEvents.disableButtonEvents(removeButton, refreshTable2);
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        removeButton.setEnabled(false);
    removeButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            ArrayList<Attribute> list = new ArrayList<Attribute>();
            if (required) {
                list = resAttrs.getTableSelectedList();
            } else {
                list = attrs.getTableSelectedList();
            }
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                Map<String, Integer> ids = new HashMap<String, Integer>();
                ids.put("resource", resourceId);
                RemoveAttributes request = new RemoveAttributes((required) ? removeButtonEvent : removeButtonEvent2);
                request.removeAttributes(ids, list);
            }
        }
    });
    menu.addWidget(removeButton);
    // add service selection to menu and switcher
    if (serviceId == 0) {
        menu.addWidget(new HTML("<strong>Selected&nbsp;service: </strong>"));
        menu.addWidget(servList);
        menu.addWidget(chb);
    }
    // checkbox switcher on click
    chb.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            if (chb.getValue() == false) {
                lastOfferAvailableOnly = false;
                allServices.retrieveData();
            } else {
                lastOfferAvailableOnly = true;
                services.retrieveData();
            }
        }
    });
    fillDefaultButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            // automatically try to fill all attributes
            ArrayList<Attribute> prepareList = new ArrayList<Attribute>();
            if (required) {
                prepareList = resAttrs.getTableSelectedList();
            } else {
                prepareList = attrs.getTableSelectedList();
            }
            final ArrayList<Attribute> list = prepareList;
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                Map<String, Integer> ids = new HashMap<String, Integer>();
                ids.put("resource", resourceId);
                FillAttributes request = new FillAttributes(JsonCallbackEvents.disableButtonEvents(fillDefaultButton, new JsonCallbackEvents() {

                    boolean wasRequiredTable = required;

                    @Override
                    public void onFinished(JavaScriptObject jso) {
                        // remove attribute from original table and put new ones
                        ArrayList<Attribute> newList = JsonUtils.jsoAsList(jso);
                        for (Attribute a : newList) {
                            for (Attribute oldA : list) {
                                // deselect old
                                if (a.getId() == oldA.getId()) {
                                    if (wasRequiredTable) {
                                        resAttrs.getSelectionModel().setSelected(oldA, false);
                                        resAttrs.removeFromTable(oldA);
                                    } else {
                                        attrs.getSelectionModel().setSelected(oldA, false);
                                        attrs.removeFromTable(oldA);
                                    }
                                }
                            }
                            if (wasRequiredTable) {
                                //add new
                                resAttrs.addToTable(a);
                                // select returned
                                resAttrs.getSelectionModel().setSelected(a, true);
                            } else {
                                //add new
                                attrs.addToTable(a);
                                // select returned
                                attrs.getSelectionModel().setSelected(a, true);
                            }
                        }
                        if (wasRequiredTable) {
                            resAttrs.sortTable();
                        } else {
                            attrs.sortTable();
                        }
                    }
                }));
                request.fillAttributes(ids, list);
            }
        }
    });
    if (!session.isVoAdmin(resource.getVoId()) && !session.isFacilityAdmin(resource.getFacilityId()))
        fillDefaultButton.setEnabled(false);
    /* TODO - not implemented

			 Button checkValuesButton = new CustomButton("Check values", SmallIcons.INSTANCE.scriptGearIcon());
			 menu.addWidget(checkValuesButton);
			 checkValuesButton.setTitle("Checks inserted values against current Perun state - nothing is saved unless you click on 'Save changes'");


			 checkValuesButton.addClickHandler(new ClickHandler(){
			 public void onClick(ClickEvent event) {
			 Window.alert("not yet implemented");
			 }
			 });

*/
    table.addStyleName("perun-table");
    table.setWidth("100%");
    table2.addStyleName("perun-table");
    table2.setWidth("100%");
    sp.addStyleName("perun-tableScrollPanel");
    session.getUiElements().resizePerunTable(sp, 350, this);
    sp2.addStyleName("perun-tableScrollPanel");
    session.getUiElements().resizePerunTable(sp2, 350, this);
    // default is required attributes
    setTable(true);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) AjaxLoaderImage(cz.metacentrum.perun.webgui.widgets.AjaxLoaderImage) HashMap(java.util.HashMap) Attribute(cz.metacentrum.perun.webgui.model.Attribute) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ArrayList(java.util.ArrayList) GetServices(cz.metacentrum.perun.webgui.json.servicesManager.GetServices) 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) Service(cz.metacentrum.perun.webgui.model.Service) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) ChangeEvent(com.google.gwt.event.dom.client.ChangeEvent) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) PerunError(cz.metacentrum.perun.webgui.model.PerunError) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Attribute (cz.metacentrum.perun.webgui.model.Attribute)58 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)17 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)17 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)16 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)16 ArrayList (java.util.ArrayList)16 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)15 TabMenu (cz.metacentrum.perun.webgui.widgets.TabMenu)15 HashMap (java.util.HashMap)14 ListHandler (com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler)12 PerunError (cz.metacentrum.perun.webgui.model.PerunError)10 SetAttributes (cz.metacentrum.perun.webgui.json.attributesManager.SetAttributes)9 PerunAttributeValueCell (cz.metacentrum.perun.webgui.widgets.cells.PerunAttributeValueCell)9 Map (java.util.Map)9 RemoveAttributes (cz.metacentrum.perun.webgui.json.attributesManager.RemoveAttributes)8 CheckboxCell (com.google.gwt.cell.client.CheckboxCell)7 GeneralObject (cz.metacentrum.perun.webgui.model.GeneralObject)7 PerunAttributeDescriptionCell (cz.metacentrum.perun.webgui.widgets.cells.PerunAttributeDescriptionCell)7 PerunAttributeNameCell (cz.metacentrum.perun.webgui.widgets.cells.PerunAttributeNameCell)7 Column (com.google.gwt.user.cellview.client.Column)6