Search in sources :

Example 1 with GetRequiredAttributes

use of cz.metacentrum.perun.webgui.json.attributesManager.GetRequiredAttributes in project perun by CESNET.

the class SelfResourcesSettingsTabItem method loadContent.

private void loadContent(final FlexTable layoutx, final Resource resource, final boolean disableOptOut) {
    // get member
    final GetMemberByUser callMember = new GetMemberByUser(resource.getVoId(), userId, new JsonCallbackEvents() {

        public void onFinished(JavaScriptObject jso) {
            // member
            final Member mem = jso.cast();
            // set ids
            Map<String, Integer> ids = new HashMap<String, Integer>();
            ids.put("member", mem.getId());
            ids.put("resource", resource.getId());
            // to filter empty values
            ids.put("resourceToGetServicesFrom", resource.getId());
            ids.put("workWithUserAttributes", 1);
            // get req. attrs - to filter eg. if quota is used or not
            final GetRequiredAttributes attributes = new GetRequiredAttributes(ids);
            final JsonCallbackEvents refreshEvent = new JsonCallbackEvents() {

                @Override
                public void onFinished(JavaScriptObject jso) {
                    // / reload resource settings data
                    attributes.retrieveData();
                }
            };
            attributes.setEvents(new JsonCallbackEvents() {

                public void onLoadingStart() {
                    AjaxLoaderImage loader = new AjaxLoaderImage(true);
                    layoutx.setWidget(0, 0, loader.loadingStart());
                }

                public void onError(PerunError error) {
                    AjaxLoaderImage loader = new AjaxLoaderImage(true);
                    layoutx.setWidget(0, 0, loader.loadingError(error));
                }

                public void onFinished(JavaScriptObject jso) {
                    // fill layout
                    ArrayList<Attribute> attrs = JsonUtils.jsoAsList(jso);
                    // check if any attributes displayed
                    boolean empty = true;
                    // remove loading image
                    layoutx.getWidget(0, 0).removeFromParent();
                    int row = 0;
                    // process shell
                    for (final Attribute a : attrs) {
                        // user's shell on facility (DEF is exception from default VIRT so display them)
                        if (a.getFriendlyName().equalsIgnoreCase("shell") && !a.getDefinition().equalsIgnoreCase("virt")) {
                            layoutx.setHTML(row, 0, "<strong>Shell: </strong>");
                            empty = false;
                            // change button
                            CustomButton cb = new CustomButton("Change…", SmallIcons.INSTANCE.cogIcon());
                            layoutx.setWidget(row, 2, cb);
                            // click handler
                            cb.addClickHandler(new ClickHandler() {

                                public void onClick(ClickEvent event) {
                                    session.getTabManager().addTabToCurrentTab(new ShellChangeTabItem(resource, userId, a, refreshEvent));
                                }
                            });
                            if (!a.getValue().equalsIgnoreCase("null")) {
                                // FIXME - we can't offer what default would be, since virt value is always same as def value
                                layoutx.setHTML(row, 1, SafeHtmlUtils.fromString((a.getValue() != null) ? a.getValue() : "").asString());
                                row++;
                                layoutx.setHTML(row, 1, "You are using specific shell for this resource overriding your global preferences.<br />To get back to default use change button.");
                                layoutx.getFlexCellFormatter().setStyleName(row, 1, "inputFormInlineComment");
                                layoutx.getFlexCellFormatter().setColSpan(row, 1, 2);
                            }
                            for (Attribute ia : attrs) {
                                if (ia.getFriendlyName().equalsIgnoreCase("shell") && ia.getDefinition().equalsIgnoreCase("virt")) {
                                    if (a.getValue().equalsIgnoreCase("null") && ia.getValue().equalsIgnoreCase("null")) {
                                        layoutx.setHTML(row, 1, "Using default (default: Not set)");
                                    } else if (a.getValue().equalsIgnoreCase("null")) {
                                        layoutx.setHTML(row, 1, "Using default (default: " + SafeHtmlUtils.fromString((a.getValue() != null) ? a.getValue() : "").asString() + ")");
                                        row++;
                                        layoutx.setHTML(row, 1, "You are using default shell taken from your global preferences.<br />Use change button to set specific shell for this resource.");
                                        layoutx.getFlexCellFormatter().setStyleName(row, 1, "inputFormInlineComment");
                                        layoutx.getFlexCellFormatter().setColSpan(row, 1, 2);
                                    }
                                    break;
                                }
                            }
                            row++;
                            break;
                        }
                    }
                    for (final Attribute a : attrs) {
                        if (a.getFriendlyName().equalsIgnoreCase("dataQuotas") && !a.getValueAsMap().isEmpty()) {
                            final int rowDataLimit = row;
                            final CustomButton quotaChangeButton = new CustomButton("Request change…", SmallIcons.INSTANCE.databaseIcon());
                            // display value
                            layoutx.setHTML(row, 0, "<strong>Data quota: </strong>");
                            // get default
                            Map<String, Integer> ids = new HashMap<String, Integer>();
                            ids.put("resource", resource.getId());
                            GetAttributes defaultAttr = new GetAttributes(new JsonCallbackEvents() {

                                public void onError(PerunError error) {
                                    if (a.getValueAsMap().isEmpty()) {
                                        layoutx.setHTML(rowDataLimit, 1, "Using default (default: error while loading)");
                                    } else {
                                        layoutx.setHTML(rowDataLimit, 1, SafeHtmlUtils.fromString(convertMapToNiceString(a.getValueAsMap())).asString() + " (default: error while loading)");
                                    }
                                    quotaChangeButton.addClickHandler(new ClickHandler() {

                                        public void onClick(ClickEvent event) {
                                            session.getTabManager().addTabToCurrentTab(new RequestQuotaChangeTabItem(resource, user, QuotaType.DATA, convertMapToNiceString(a.getValueAsMap()), convertMapToNiceString(new HashMap<String, JSONValue>())));
                                        }
                                    });
                                }

                                public void onFinished(JavaScriptObject jso) {
                                    ArrayList<Attribute> attrs = JsonUtils.jsoAsList(jso);
                                    boolean empty = true;
                                    for (final Attribute resAttr : attrs) {
                                        if (resAttr.getFriendlyName().equalsIgnoreCase("defaultDataQuotas")) {
                                            if (a.getValueAsMap().isEmpty()) {
                                                // null private + default
                                                layoutx.setHTML(rowDataLimit, 1, "Using default (default: " + SafeHtmlUtils.fromString(convertMapToNiceString(resAttr.getValueAsMap())).asString() + ")");
                                            } else {
                                                // private - default
                                                layoutx.setHTML(rowDataLimit, 1, SafeHtmlUtils.fromString(convertMapToNiceString(a.getValueAsMap())).asString() + " (default: " + SafeHtmlUtils.fromString(convertMapToNiceString(resAttr.getValueAsMap())).asString() + ")");
                                            }
                                            empty = false;
                                            quotaChangeButton.addClickHandler(new ClickHandler() {

                                                public void onClick(ClickEvent event) {
                                                    session.getTabManager().addTabToCurrentTab(new RequestQuotaChangeTabItem(resource, user, QuotaType.DATA, convertMapToNiceString(a.getValueAsMap()), convertMapToNiceString(resAttr.getValueAsMap())));
                                                }
                                            });
                                        }
                                    }
                                    // if no default found, write down at least private
                                    if (empty) {
                                        if (a.getValueAsMap().isEmpty()) {
                                            layoutx.setHTML(rowDataLimit, 1, "Using default (default: Not set)");
                                        } else {
                                            layoutx.setHTML(rowDataLimit, 1, SafeHtmlUtils.fromString(convertMapToNiceString(a.getValueAsMap())).asString() + " (default: Not set)");
                                        }
                                        quotaChangeButton.addClickHandler(new ClickHandler() {

                                            public void onClick(ClickEvent event) {
                                                session.getTabManager().addTabToCurrentTab(new RequestQuotaChangeTabItem(resource, user, QuotaType.DATA, convertMapToNiceString(a.getValueAsMap()), convertMapToNiceString(new HashMap<String, JSONValue>())));
                                            }
                                        });
                                    }
                                }
                            });
                            defaultAttr.setIds(ids);
                            defaultAttr.retrieveData();
                            empty = false;
                            layoutx.setWidget(row, 2, quotaChangeButton);
                            row++;
                            layoutx.setHTML(row, 1, "Quotas are using \"soft limit : hard limit\" notation and are bound to specific volume.");
                            layoutx.getFlexCellFormatter().setStyleName(row, 1, "inputFormInlineComment");
                            layoutx.getFlexCellFormatter().setColSpan(row, 1, 2);
                            row++;
                            break;
                        }
                    }
                    for (final Attribute a : attrs) {
                        if (a.getFriendlyName().equalsIgnoreCase("filesQuotas") && !a.getValueAsMap().isEmpty()) {
                            layoutx.setHTML(row, 0, "<strong>Files quota: </strong>");
                            final int rowFilesQuota = row;
                            // get default
                            Map<String, Integer> ids = new HashMap<String, Integer>();
                            ids.put("resource", resource.getId());
                            final CustomButton quotaChangeButton = new CustomButton("Request change…", SmallIcons.INSTANCE.databaseIcon());
                            GetAttributes defaultAttr = new GetAttributes(new JsonCallbackEvents() {

                                public void onError(PerunError error) {
                                    if (a.getValueAsMap().isEmpty()) {
                                        layoutx.setHTML(rowFilesQuota, 1, "Using default (default: error while loading)");
                                    } else {
                                        layoutx.setHTML(rowFilesQuota, 1, SafeHtmlUtils.fromString(convertMapToNiceString(a.getValueAsMap())).asString() + " (default: error while loading)");
                                    }
                                    quotaChangeButton.addClickHandler(new ClickHandler() {

                                        public void onClick(ClickEvent event) {
                                            session.getTabManager().addTabToCurrentTab(new RequestQuotaChangeTabItem(resource, user, QuotaType.FILES, convertMapToNiceString(a.getValueAsMap()), convertMapToNiceString(new HashMap<String, JSONValue>())));
                                        }
                                    });
                                }

                                public void onFinished(JavaScriptObject jso) {
                                    ArrayList<Attribute> attrs = JsonUtils.jsoAsList(jso);
                                    boolean empty = true;
                                    for (final Attribute resAttr : attrs) {
                                        if (resAttr.getFriendlyName().equalsIgnoreCase("defaultFilesQuotas")) {
                                            if (a.getValueAsMap().isEmpty()) {
                                                // null private + default
                                                layoutx.setHTML(rowFilesQuota, 1, "Using default (default: " + SafeHtmlUtils.fromString(convertMapToNiceString(resAttr.getValueAsMap())).asString() + ")");
                                            } else {
                                                // private + default
                                                layoutx.setHTML(rowFilesQuota, 1, SafeHtmlUtils.fromString(convertMapToNiceString(a.getValueAsMap())).asString() + " (default: " + SafeHtmlUtils.fromString(convertMapToNiceString(resAttr.getValueAsMap())).asString() + ")");
                                            }
                                            empty = false;
                                            quotaChangeButton.addClickHandler(new ClickHandler() {

                                                public void onClick(ClickEvent event) {
                                                    session.getTabManager().addTabToCurrentTab(new RequestQuotaChangeTabItem(resource, user, QuotaType.FILES, convertMapToNiceString(a.getValueAsMap()), convertMapToNiceString(resAttr.getValueAsMap())));
                                                }
                                            });
                                        }
                                    }
                                    // if no default found, write down at least private
                                    if (empty) {
                                        if (a.getValueAsMap().isEmpty()) {
                                            layoutx.setHTML(rowFilesQuota, 1, "Using default (default: Not set)");
                                        } else {
                                            layoutx.setHTML(rowFilesQuota, 1, SafeHtmlUtils.fromString(convertMapToNiceString(a.getValueAsMap())).asString() + " (default: Not set)");
                                        }
                                        quotaChangeButton.addClickHandler(new ClickHandler() {

                                            public void onClick(ClickEvent event) {
                                                session.getTabManager().addTabToCurrentTab(new RequestQuotaChangeTabItem(resource, user, QuotaType.FILES, convertMapToNiceString(a.getValueAsMap()), convertMapToNiceString(new HashMap<String, JSONValue>())));
                                            }
                                        });
                                    }
                                    layoutx.setWidget(rowFilesQuota, 2, quotaChangeButton);
                                }
                            });
                            defaultAttr.setIds(ids);
                            defaultAttr.retrieveData();
                            empty = false;
                            row++;
                            layoutx.setHTML(row, 1, "Quotas are using \"soft limit : hard limit\" notation and are bound to specific volume.");
                            layoutx.getFlexCellFormatter().setStyleName(row, 1, "inputFormInlineComment");
                            layoutx.getFlexCellFormatter().setColSpan(row, 1, 2);
                            row++;
                            break;
                        }
                    }
                    for (final Attribute a : attrs) {
                        if (a.getFriendlyName().equalsIgnoreCase("optOutMailingList")) {
                            layoutx.setHTML(row, 0, "<strong>Mailing: </strong>");
                            // find attribute with settings
                            final CheckBox exclude = new CheckBox("Exclude me from this mailing list");
                            exclude.setValue(Boolean.parseBoolean(a.getValue()));
                            final int rowMail = row;
                            exclude.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

                                @Override
                                public void onValueChange(ValueChangeEvent<Boolean> event) {
                                    if (exclude.getValue()) {
                                        a.setValue("true");
                                    } else {
                                        a.setValue(null);
                                    }
                                    Map<String, Integer> ids = new HashMap<String, Integer>();
                                    ids.put("resource", resource.getId());
                                    ids.put("member", mem.getId());
                                    ArrayList<Attribute> ls = new ArrayList<Attribute>();
                                    ls.add(a);
                                    SetAttributes set = new SetAttributes(new JsonCallbackEvents() {

                                        @Override
                                        public void onFinished(JavaScriptObject jso) {
                                            layoutx.setWidget(rowMail, 1, exclude);
                                        }

                                        @Override
                                        public void onLoadingStart() {
                                            layoutx.setWidget(rowMail, 1, new AjaxLoaderImage(true));
                                        }

                                        @Override
                                        public void onError(PerunError error) {
                                            layoutx.setWidget(rowMail, 1, exclude);
                                            // change back since we were not able to change value in Perun
                                            exclude.setValue(!exclude.getValue());
                                        }
                                    });
                                    set.setAttributes(ids, ls);
                                }
                            });
                            // enable only if opt-out is allowed
                            if (disableOptOut) {
                                layoutx.setHTML(row, 1, "Your membership in this mailing list is mandatory.");
                            } else {
                                layoutx.setWidget(row, 1, exclude);
                            }
                            empty = false;
                        }
                    }
                    if (empty) {
                        layoutx.setHTML(0, 0, "<strong>No settings available for this resource.</strong>");
                        layoutx.getFlexCellFormatter().setColSpan(0, 0, 3);
                    }
                }
            });
            attributes.retrieveData();
        }

        public void onLoadingStart() {
            AjaxLoaderImage loader = new AjaxLoaderImage(true);
            layoutx.setWidget(0, 0, loader.loadingStart());
        }

        public void onError(PerunError error) {
            AjaxLoaderImage loader = new AjaxLoaderImage(true);
            layoutx.setWidget(0, 0, loader.loadingError(error));
        }
    });
    callMember.retrieveData();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) AjaxLoaderImage(cz.metacentrum.perun.webgui.widgets.AjaxLoaderImage) HashMap(java.util.HashMap) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ArrayList(java.util.ArrayList) SetAttributes(cz.metacentrum.perun.webgui.json.attributesManager.SetAttributes) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) GetVosWhereUserIsMember(cz.metacentrum.perun.webgui.json.usersManager.GetVosWhereUserIsMember) GetAttributes(cz.metacentrum.perun.webgui.json.attributesManager.GetAttributes) JSONValue(com.google.gwt.json.client.JSONValue) GetRequiredAttributes(cz.metacentrum.perun.webgui.json.attributesManager.GetRequiredAttributes) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) GetMemberByUser(cz.metacentrum.perun.webgui.json.membersManager.GetMemberByUser) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1 JSONValue (com.google.gwt.json.client.JSONValue)1 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)1 GetAttributes (cz.metacentrum.perun.webgui.json.attributesManager.GetAttributes)1 GetRequiredAttributes (cz.metacentrum.perun.webgui.json.attributesManager.GetRequiredAttributes)1 SetAttributes (cz.metacentrum.perun.webgui.json.attributesManager.SetAttributes)1 GetMemberByUser (cz.metacentrum.perun.webgui.json.membersManager.GetMemberByUser)1 GetVosWhereUserIsMember (cz.metacentrum.perun.webgui.json.usersManager.GetVosWhereUserIsMember)1 AjaxLoaderImage (cz.metacentrum.perun.webgui.widgets.AjaxLoaderImage)1 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1