use of cz.metacentrum.perun.webgui.json.resourcesManager.GetAssignedResources in project perun by CESNET.
the class UserDetailTabItem method loadMemberSubContent.
private void loadMemberSubContent(final SimplePanel subContent, final Hyperlink voLabel, final ListBoxWithObjects<VirtualOrganization> listbox) {
subContent.setWidget(new AjaxLoaderImage());
final GetMemberByUser gmbu = new GetMemberByUser(listbox.getSelectedObject().getId(), user.getId());
JsonCallbackEvents loadEvent = new JsonCallbackEvents() {
@Override
public void onFinished(JavaScriptObject jso) {
// get member
final Member member = jso.cast();
// create content panel and put it on page
final VerticalPanel entryPanel = new VerticalPanel();
entryPanel.setStyleName("perun-table");
entryPanel.setSize("100%", "100%");
subContent.setWidget(entryPanel);
voLabel.setHTML(SafeHtmlUtils.fromSafeConstant("<h2>" + listbox.getSelectedObject().getName() + "</h2>"));
voLabel.setTargetHistoryToken(session.getTabManager().getLinkForTab(new VoDetailTabItem(listbox.getSelectedObject())));
// detail header
Widget memberHeader = new HTML("<h2>" + "Member details" + "</h2>");
entryPanel.add(memberHeader);
entryPanel.setCellHeight(memberHeader, "30px");
// detail content
FlexTable layout = new FlexTable();
layout.setCellSpacing(6);
// Add some standard form options
layout.setHTML(0, 0, "<strong>Member ID:</strong>");
layout.setHTML(0, 1, String.valueOf(member.getId()));
ImageResource ir = null;
// member status
if (member.getStatus().equalsIgnoreCase("VALID")) {
ir = SmallIcons.INSTANCE.acceptIcon();
} else if (member.getStatus().equalsIgnoreCase("INVALID")) {
ir = SmallIcons.INSTANCE.flagRedIcon();
} else if (member.getStatus().equalsIgnoreCase("SUSPENDED")) {
ir = SmallIcons.INSTANCE.stopIcon();
} else if (member.getStatus().equalsIgnoreCase("EXPIRED")) {
ir = SmallIcons.INSTANCE.flagYellowIcon();
} else if (member.getStatus().equalsIgnoreCase("DISABLED")) {
ir = SmallIcons.INSTANCE.binClosedIcon();
}
HTML status = new HTML("<a>" + member.getStatus() + " " + new Image(ir) + "</a>");
layout.setHTML(1, 0, "<strong>Member status: </strong>");
layout.setWidget(1, 1, status);
// member status - on click action
status.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
FlexTable widget = new FlexTable();
final ListBox lb = new ListBox(false);
lb.addItem("VALID", "VALID");
lb.addItem("INVALID", "INVALID");
lb.addItem("SUSPENDED", "SUSPENDED");
lb.addItem("EXPIRED", "EXPIRED");
lb.addItem("DISABLED", "DISABLED");
widget.setHTML(0, 0, "<strong>Status: </strong>");
widget.setWidget(0, 1, lb);
// pick which one is already set
for (int i = 0; i < lb.getItemCount(); i++) {
if (lb.getItemText(i).equalsIgnoreCase(member.getStatus())) {
lb.setSelectedIndex(i);
}
}
Confirm conf = new Confirm("Change member's status", widget, true);
conf.setCancelButtonText("Cancel");
conf.setOkButtonText("Change status");
conf.setOkClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
SetStatus call = new SetStatus(member.getId(), new JsonCallbackEvents() {
@Override
public void onLoadingStart() {
subContent.setWidget(new AjaxLoaderImage());
}
public void onFinished(JavaScriptObject jso) {
subContent.setWidget(entryPanel);
gmbu.retrieveData();
}
public void onError(PerunError error) {
subContent.setWidget(entryPanel);
gmbu.retrieveData();
}
});
call.setStatus(lb.getValue(lb.getSelectedIndex()));
}
});
conf.show();
}
});
final ListBoxWithObjects<Resource> resList = new ListBoxWithObjects<Resource>();
GetAssignedResources res = new GetAssignedResources(member.getId(), PerunEntity.MEMBER, new JsonCallbackEvents() {
@Override
public void onFinished(JavaScriptObject jso) {
resList.clear();
ArrayList<Resource> list = JsonUtils.jsoAsList(jso);
if (list == null || list.isEmpty()) {
resList.addItem("No resources found");
} else {
list = new TableSorter<Resource>().sortByName(list);
resList.addNotSelectedOption();
resList.addAllItems(list);
}
}
@Override
public void onError(PerunError error) {
resList.clear();
resList.addItem("Error while loading");
}
@Override
public void onLoadingStart() {
resList.clear();
resList.addItem("Loading...");
}
});
res.retrieveData();
// link to member's detail
Hyperlink link = new Hyperlink();
link.setText("View detail");
layout.setHTML(2, 0, "<strong>Member's detail page:</strong>");
layout.setWidget(2, 1, link);
link.setTargetHistoryToken(session.getTabManager().getLinkForTab(new MemberDetailTabItem(member.getId(), 0)));
// wrap the content in a DecoratorPanel
DecoratorPanel decPanel = new DecoratorPanel();
decPanel.setWidget(layout);
entryPanel.add(decPanel);
entryPanel.setCellHeight(decPanel, "50px");
// tables
// detail header
Widget groupHeader = new HTML("<h2>" + "Member groups" + "</h2>");
entryPanel.add(groupHeader);
entryPanel.setCellHeight(groupHeader, "30px");
final GetMemberGroups groups = new GetMemberGroups(member.getId());
groups.setCheckable(false);
groups.setEditable(false);
CellTable<Group> table = groups.getTable();
table.addStyleName("perun-table");
table.setWidth("100%");
entryPanel.add(table);
// detail header
Widget attrHeader = new HTML("<h2>" + "Member / Member-resource attributes" + "</h2>");
entryPanel.add(attrHeader);
entryPanel.setCellHeight(attrHeader, "30px");
final GetAttributesV2 attributes = new GetAttributesV2();
attributes.getMemberAttributes(member.getId());
resList.addChangeHandler(new ChangeHandler() {
@Override
public void onChange(ChangeEvent event) {
if (resList.getSelectedIndex() == 0) {
attributes.getMemberAttributes(member.getId());
attributes.retrieveData();
} else {
attributes.getMemberResourceAttributes(member.getId(), resList.getSelectedObject().getId());
attributes.retrieveData();
}
}
});
TabMenu menu = new TabMenu();
final CustomButton saveAttrButton = TabMenu.getPredefinedButton(ButtonType.SAVE, "Save changes in attributes for member");
saveAttrButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
ArrayList<Attribute> list = attributes.getTableSelectedList();
if (UiElements.cantSaveEmptyListDialogBox(list)) {
Map<String, Integer> ids = new HashMap<String, Integer>();
ids.put("member", member.getId());
if (resList.getSelectedIndex() > 0) {
ids.put("resource", resList.getSelectedObject().getId());
}
SetAttributes request = new SetAttributes(JsonCallbackEvents.disableButtonEvents(saveAttrButton, JsonCallbackEvents.refreshTableEvents(attributes)));
request.setAttributes(ids, list);
}
}
});
menu.addWidget(saveAttrButton);
menu.addWidget(TabMenu.getPredefinedButton(ButtonType.ADD, true, "Set new attributes for member", new ClickHandler() {
public void onClick(ClickEvent event) {
Map<String, Integer> ids = new HashMap<String, Integer>();
ids.put("member", member.getId());
if (resList.getSelectedIndex() > 0) {
ids.put("resource", resList.getSelectedObject().getId());
}
session.getTabManager().addTabToCurrentTab(new SetNewAttributeTabItem(ids, attributes.getList()), true);
}
}));
final CustomButton removeAttrButton = TabMenu.getPredefinedButton(ButtonType.REMOVE, "Remove attributes from member");
removeAttrButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
ArrayList<Attribute> list = attributes.getTableSelectedList();
if (UiElements.cantSaveEmptyListDialogBox(list)) {
Map<String, Integer> ids = new HashMap<String, Integer>();
ids.put("member", member.getId());
if (resList.getSelectedIndex() > 0) {
ids.put("resource", resList.getSelectedObject().getId());
}
RemoveAttributes request = new RemoveAttributes(JsonCallbackEvents.disableButtonEvents(removeAttrButton, JsonCallbackEvents.refreshTableEvents(attributes)));
request.removeAttributes(ids, list);
}
}
});
menu.addWidget(removeAttrButton);
menu.addWidget(new HTML("<strong>Resource:</strong>"));
menu.addWidget(resList);
entryPanel.add(menu);
CellTable<Attribute> attrTable = attributes.getTable();
attrTable.addStyleName("perun-table");
attrTable.setWidth("100%");
entryPanel.add(attrTable);
}
@Override
public void onError(PerunError error) {
subContent.setWidget(new AjaxLoaderImage().loadingError(error));
}
};
// set events & load data
gmbu.setEvents(loadEvent);
gmbu.retrieveData();
}
use of cz.metacentrum.perun.webgui.json.resourcesManager.GetAssignedResources in project perun by CESNET.
the class MemberSettingsTabItem method draw.
public Widget draw() {
titleWidget.setText(Utils.getStrippedStringWithEllipsis(member.getUser().getFullNameWithTitles().trim()) + ": Settings");
// CONTENT WIDGET
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));
// callbacks
final GetAttributesV2 callback = new GetAttributesV2(true);
if (!session.isVoAdmin(member.getVoId()) && !session.isGroupAdmin(groupId)) {
callback.setCheckable(false);
}
// member & user attrs
callback.getMemberAttributes(member.getId(), 1);
final CellTable<Attribute> table = callback.getEmptyTable();
// others callbacks
final Map<String, Integer> ids = new HashMap<String, Integer>();
ids.put("member", member.getId());
// work with user
ids.put("workWithUserAttributes", 1);
final GetResourceRequiredAttributesV2 resourceRequired = new GetResourceRequiredAttributesV2(ids, JsonCallbackEvents.passDataToAnotherCallback(callback));
final GetRequiredAttributes required = new GetRequiredAttributes(ids, JsonCallbackEvents.passDataToAnotherCallback(callback));
if (lastSelectedFilterIndex == 1 || lastSelectedResourceId == 0) {
// load required by default
required.retrieveData();
}
// listbox with resources
final ListBoxWithObjects<Resource> listbox = new ListBoxWithObjects<Resource>();
// listbox for filtering attributes callbacks
final ListBox filter = new ListBox();
filter.addItem("All filled attributes");
filter.addItem("Required");
filter.addItem("Resource required");
// required as default
filter.setSelectedIndex(lastSelectedFilterIndex);
// change table on selection one of the listboxs
final ChangeHandler changehandler = new ChangeHandler() {
public void onChange(ChangeEvent event) {
// if resource selected
if (listbox.getSelectedIndex() > 0) {
// if all attributes
if (filter.getSelectedIndex() == 0) {
callback.clearTable();
// with user
callback.getMemberAttributes(member.getId(), 1);
callback.retrieveData();
callback.getMemberResourceAttributes(member.getId(), listbox.getSelectedObject().getId());
callback.retrieveData();
callback.getUserFacilityAttributes(listbox.getSelectedObject().getFacilityId(), member.getUser().getId());
callback.retrieveData();
// if required attributes
} else if (filter.getSelectedIndex() == 1) {
callback.clearTable();
ids.clear();
UiElements.generateInfo("Not valid option", "Skipping to \"Resource required\" option.");
filter.setSelectedIndex(2);
lastSelectedFilterIndex = 2;
ids.put("member", member.getId());
ids.put("resource", listbox.getSelectedObject().getId());
ids.put("resourceToGetServicesFrom", listbox.getSelectedObject().getId());
ids.put("workWithUserAttributes", 1);
resourceRequired.retrieveData();
// if resource required
} else if (filter.getSelectedIndex() == 2) {
callback.clearTable();
ids.clear();
ids.put("member", member.getId());
ids.put("resource", listbox.getSelectedObject().getId());
ids.put("resourceToGetServicesFrom", listbox.getSelectedObject().getId());
ids.put("workWithUserAttributes", 1);
resourceRequired.retrieveData();
}
// if no resource selected
} else {
// if all attributes
if (filter.getSelectedIndex() == 0) {
callback.clearTable();
callback.getMemberAttributes(member.getId(), 1);
callback.retrieveData();
// if required attributes
} else if (filter.getSelectedIndex() == 1) {
callback.clearTable();
ids.clear();
ids.put("member", member.getId());
ids.put("workWithUserAttributes", 1);
required.retrieveData();
// if resource required
} else if (filter.getSelectedIndex() == 2) {
callback.clearTable();
((AjaxLoaderImage) table.getEmptyTableWidget()).loadingFinished();
UiElements.generateInfo("Not valid option", "You must select resource first.");
}
}
}
};
ChangeHandler resourceChangeHandler = new ChangeHandler() {
public void onChange(ChangeEvent event) {
if (listbox.getSelectedIndex() > 0) {
// if resource selected
// load resource-required (by default)
filter.setSelectedIndex(2);
lastSelectedFilterIndex = 2;
} else {
// else load required by default
filter.setSelectedIndex(1);
lastSelectedFilterIndex = 1;
}
if (listbox.getSelectedObject() != null) {
lastSelectedResourceId = listbox.getSelectedObject().getId();
} else {
lastSelectedResourceId = 0;
}
changehandler.onChange(event);
}
};
ChangeHandler filterChangeHandler = new ChangeHandler() {
public void onChange(ChangeEvent event) {
// if required and resource selected
if (filter.getSelectedIndex() == 1 && listbox.getSelectedIndex() > 0) {
// cance resource selection
listbox.setSelectedIndex(0);
lastSelectedResourceId = 0;
}
lastSelectedFilterIndex = filter.getSelectedIndex();
changehandler.onChange(event);
}
};
// refresh table on listbox change
listbox.addChangeHandler(resourceChangeHandler);
filter.addChangeHandler(filterChangeHandler);
// fill listbox with member's resources
JsonCallbackEvents events = new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
listbox.clear();
ArrayList<Resource> res = JsonUtils.jsoAsList(jso);
res = new TableSorter<Resource>().sortByName(res);
if (res != null && !res.isEmpty()) {
listbox.addNotSelectedOption();
} else {
listbox.addItem("No resource available");
}
for (int i = 0; i < res.size(); i++) {
listbox.addItem(res.get(i));
// select last selected
if (res.get(i).getId() == lastSelectedResourceId) {
listbox.setSelected(res.get(i), true);
}
}
if (lastSelectedFilterIndex != 1 && lastSelectedResourceId != 0) {
DomEvent.fireNativeEvent(Document.get().createChangeEvent(), filter);
}
}
public void onLoadingStart() {
listbox.clear();
listbox.addItem("Loading...");
}
public void onError(PerunError error) {
listbox.clear();
listbox.addItem("Error while loading");
}
};
// get member resources
GetAssignedResources res = new GetAssignedResources(member.getId(), PerunEntity.MEMBER, events);
res.retrieveData();
table.addStyleName("perun-table");
ScrollPanel sp = new ScrollPanel(table);
sp.addStyleName("perun-tableScrollPanel");
session.getUiElements().resizePerunTable(sp, 350, this);
// save changes in attributes
final CustomButton saveChangesButton = TabMenu.getPredefinedButton(ButtonType.SAVE, ButtonTranslation.INSTANCE.saveChangesInAttributes());
if (!session.isVoAdmin(member.getVoId()) && !session.isGroupAdmin(groupId))
saveChangesButton.setEnabled(false);
saveChangesButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (UiElements.cantSaveEmptyListDialogBox(callback.getTableSelectedList())) {
// if resource selected
if (listbox.getSelectedIndex() > 0) {
// if all attributes
SetAttributes request;
if (filter.getSelectedIndex() == 0) {
request = new SetAttributes(JsonCallbackEvents.disableButtonEvents(saveChangesButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
callback.clearTable();
// with user
callback.getMemberAttributes(member.getId(), 1);
callback.retrieveData();
callback.getMemberResourceAttributes(member.getId(), listbox.getSelectedObject().getId());
callback.retrieveData();
callback.getUserFacilityAttributes(listbox.getSelectedObject().getFacilityId(), member.getUser().getId());
callback.retrieveData();
}
}));
} else {
// if required or resource-required
request = new SetAttributes(JsonCallbackEvents.disableButtonEvents(saveChangesButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
callback.clearTable();
ids.clear();
ids.put("member", member.getId());
ids.put("resource", listbox.getSelectedObject().getId());
ids.put("resourceToGetServicesFrom", listbox.getSelectedObject().getId());
ids.put("workWithUserAttributes", 1);
resourceRequired.retrieveData();
}
}));
}
// make setAttributesCall
ids.clear();
ids.put("member", member.getId());
ids.put("resource", listbox.getSelectedObject().getId());
ids.put("facility", listbox.getSelectedObject().getFacilityId());
ids.put("user", member.getUserId());
request.setAttributes(ids, callback.getTableSelectedList());
} else {
// if resource not selected
SetAttributes request;
if (filter.getSelectedIndex() == 1) {
// if required attr option - refresh different callback
request = new SetAttributes(JsonCallbackEvents.disableButtonEvents(saveChangesButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
callback.clearTable();
required.retrieveData();
}
}));
} else {
request = new SetAttributes(JsonCallbackEvents.disableButtonEvents(saveChangesButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
callback.clearTable();
callback.retrieveData();
}
}));
}
// make setAttributes call
ids.clear();
ids.put("member", member.getId());
ids.put("workWithUserAttributes", 1);
request.setAttributes(ids, callback.getTableSelectedList());
}
}
}
});
menu.addWidget(saveChangesButton);
// buttons
CustomButton setNewMemberAttributeButton = 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("member", member.getId());
ids.put("user", member.getUser().getId());
if (listbox.getSelectedIndex() > 0) {
ids.put("resource", listbox.getSelectedObject().getId());
ids.put("facility", listbox.getSelectedObject().getFacilityId());
}
session.getTabManager().addTabToCurrentTab(new SetNewAttributeTabItem(ids), true);
}
});
if (!session.isVoAdmin(member.getVoId()) && !session.isGroupAdmin(groupId))
setNewMemberAttributeButton.setEnabled(false);
menu.addWidget(setNewMemberAttributeButton);
// REMOVE ATTRIBUTES BUTTON
final CustomButton removeButton = TabMenu.getPredefinedButton(ButtonType.REMOVE, ButtonTranslation.INSTANCE.removeAttributes());
removeButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// if selected
if (UiElements.cantSaveEmptyListDialogBox(callback.getTableSelectedList())) {
// if resource selected
if (listbox.getSelectedIndex() > 0) {
// if all attributes
RemoveAttributes request;
if (filter.getSelectedIndex() == 0) {
request = new RemoveAttributes(JsonCallbackEvents.disableButtonEvents(removeButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
callback.clearTable();
// with user
callback.getMemberAttributes(member.getId(), 1);
callback.retrieveData();
callback.getMemberResourceAttributes(member.getId(), listbox.getSelectedObject().getId());
callback.retrieveData();
callback.getUserFacilityAttributes(listbox.getSelectedObject().getFacilityId(), member.getUser().getId());
callback.retrieveData();
}
}));
} else {
// if required or resource-required
request = new RemoveAttributes(JsonCallbackEvents.disableButtonEvents(removeButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
callback.clearTable();
ids.clear();
ids.put("member", member.getId());
ids.put("resource", listbox.getSelectedObject().getId());
ids.put("resourceToGetServicesFrom", listbox.getSelectedObject().getId());
ids.put("workWithUserAttributes", 1);
resourceRequired.retrieveData();
}
}));
}
// make removeAttributesCall
ids.clear();
ids.put("member", member.getId());
ids.put("resource", listbox.getSelectedObject().getId());
ids.put("facility", listbox.getSelectedObject().getFacilityId());
ids.put("user", member.getUserId());
request.removeAttributes(ids, callback.getTableSelectedList());
} else {
// if resource not selected
RemoveAttributes request;
if (filter.getSelectedIndex() == 1) {
// if required attr option - refresh different callback
request = new RemoveAttributes(JsonCallbackEvents.disableButtonEvents(removeButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
callback.clearTable();
required.retrieveData();
}
}));
} else {
request = new RemoveAttributes(JsonCallbackEvents.disableButtonEvents(removeButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
callback.clearTable();
callback.retrieveData();
}
}));
}
// make removeAttributes call
ids.clear();
ids.put("member", member.getId());
ids.put("workWithUserAttributes", 1);
request.removeAttributes(ids, callback.getTableSelectedList());
}
}
}
});
removeButton.setEnabled(false);
if (session.isVoAdmin(member.getVoId()) || session.isGroupAdmin(groupId))
JsonUtils.addTableManagedButton(callback, table, removeButton);
menu.addWidget(removeButton);
// add listbox to menu
menu.addWidget(new HTML("<strong>Assigned resources: </strong>"));
menu.addWidget(listbox);
menu.addWidget(new HTML("<strong>Filter: </strong>"));
menu.addWidget(filter);
// ATTRIBUTES TABLE
vp.add(sp);
vp.setCellHeight(sp, "100%");
this.contentWidget.setWidget(vp);
return getWidget();
}
use of cz.metacentrum.perun.webgui.json.resourcesManager.GetAssignedResources in project perun by CESNET.
the class GroupSettingsTabItem method draw.
public Widget draw() {
titleWidget.setText(Utils.getStrippedStringWithEllipsis(group.getName()) + ": settings");
// Main panel
final VerticalPanel vp = new VerticalPanel();
vp.setSize("100%", "100%");
final TabMenu menu = new TabMenu();
// Adds menu to the main panel
vp.add(menu);
vp.setCellHeight(menu, "30px");
// IDS
final Map<String, Integer> ids = new HashMap<String, Integer>();
ids.put("group", groupId);
// define GET ATTRIBUTES callback
final GetAttributesV2 jsonCallback = new GetAttributesV2(true);
jsonCallback.setIds(ids);
if (!session.isGroupAdmin(groupId) && !session.isVoAdmin(group.getVoId()))
jsonCallback.setCheckable(false);
final CellTable<Attribute> table = jsonCallback.getEmptyTable();
table.setWidth("100%");
table.addStyleName("perun-table");
ScrollPanel sp = new ScrollPanel(table);
sp.addStyleName("perun-tableScrollPanel");
session.getUiElements().resizePerunTable(sp, 350, this);
vp.add(sp);
// define GET RES. REQ ATTRIBUTES callback
final GetResourceRequiredAttributesV2 reqAttrs = new GetResourceRequiredAttributesV2(null, JsonCallbackEvents.passDataToAnotherCallback(jsonCallback));
// GROUP RESOURCES LISTBOX
final ListBoxWithObjects<Resource> resourceDropDown = new ListBoxWithObjects<Resource>();
resourceDropDown.setTitle("By selecting a resource you will switch to either group-resource or member-resource setting (you must pick member too)");
// fill table with group attributes on page load
if (lastSelectedMemberId == 0 && lastSelectedResourceId == 0) {
jsonCallback.retrieveData();
} else {
// load attrs by listbox selection after calls are done
Scheduler.get().scheduleFixedPeriod(new Scheduler.RepeatingCommand() {
@Override
public boolean execute() {
// load proper table if something was selected
if (resCallDone && memCallDone) {
// trigger change event
DomEvent.fireNativeEvent(Document.get().createChangeEvent(), resourceDropDown);
return false;
}
return true;
}
}, 200);
}
// fill resources listbox
if (resources == null) {
// get assigned resources
GetAssignedResources resources = new GetAssignedResources(groupId, PerunEntity.GROUP, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
ArrayList<Resource> resources = JsonUtils.jsoAsList(jso);
resourceDropDown.clear();
if (resources != null && !resources.isEmpty()) {
resources = new TableSorter<Resource>().sortByName(resources);
resourceDropDown.addNotSelectedOption();
for (int i = 0; i < resources.size(); i++) {
resourceDropDown.addItem(resources.get(i));
if (lastSelectedResourceId == resources.get(i).getId()) {
resourceDropDown.setSelected(resources.get(i), true);
}
}
} else {
resourceDropDown.addItem("No resources available");
}
resCallDone = true;
}
public void onError(PerunError error) {
resourceDropDown.clear();
resourceDropDown.addItem("Error while loading");
resCallDone = true;
}
public void onLoadingStart() {
resCallDone = false;
resourceDropDown.clear();
resourceDropDown.addItem("Loading...");
}
});
resources.retrieveData();
} else {
// use predefined resources
resourceDropDown.addNotSelectedOption();
resources = new TableSorter<Resource>().sortByName(resources);
resourceDropDown.addAllItems(resources);
}
// GROUP MEMBERS LISTBOX
final ListBoxWithObjects<RichMember> membersDropDown = new ListBoxWithObjects<RichMember>();
membersDropDown.setTitle("By selecting a member you will switch to either member or member-resource settings (you must pick resource too)");
// fill members listbox
GetGroupRichMembers members = new GetGroupRichMembers(groupId, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
ArrayList<RichMember> mems = JsonUtils.jsoAsList(jso);
membersDropDown.clear();
if (mems != null && !mems.isEmpty()) {
mems = new TableSorter<RichMember>().sortByName(mems);
membersDropDown.addNotSelectedOption();
for (int i = 0; i < mems.size(); i++) {
membersDropDown.addItem(mems.get(i));
if (mems.get(i).getId() == lastSelectedMemberId) {
membersDropDown.setSelected(mems.get(i), true);
}
}
} else {
membersDropDown.addItem("No members available");
}
memCallDone = true;
}
public void onError(PerunError error) {
membersDropDown.clear();
membersDropDown.addItem("Error while loading");
memCallDone = true;
}
public void onLoadingStart() {
memCallDone = false;
membersDropDown.clear();
membersDropDown.addItem("Loading...");
}
});
members.retrieveData();
// CHECKBOXES "FOR ALL MEMBERS"
final CheckBox memChb = new CheckBox();
memChb.setText("To all members");
memChb.setVisible(false);
memChb.setTitle("Uses this setting for all members of this group.");
memChb.setValue(false);
// SAVE CHANGES BUTTON
final CustomButton saveChangesButton = TabMenu.getPredefinedButton(ButtonType.SAVE, ButtonTranslation.INSTANCE.saveChangesInAttributes());
if (!session.isGroupAdmin(groupId) && !session.isVoAdmin(group.getVoId()))
saveChangesButton.setEnabled(false);
// click handler to save group and member-user attributes
final ClickHandler saveAttrsClickHandler = new ClickHandler() {
public void onClick(ClickEvent event) {
final ArrayList<Attribute> list = jsonCallback.getTableSelectedList();
if (UiElements.cantSaveEmptyListDialogBox(list)) {
// refresh table and disable button a event for GetAttributes
final JsonCallbackEvents events = JsonCallbackEvents.disableButtonEvents(saveChangesButton, JsonCallbackEvents.refreshTableEvents(jsonCallback));
if (memChb.getValue() == true) {
// to all members selected
UiElements.generateInfo(WidgetTranslation.INSTANCE.saveConfirmTitle(), "Same value(s) for selected attribute(s) will be set to ALL members of group.", new ClickHandler() {
public void onClick(ClickEvent event) {
ArrayList<RichMember> memList = membersDropDown.getAllObjects();
for (int i = 0; i < memList.size(); i++) {
SetAttributes request = new SetAttributes();
if (i == memList.size() - 1) {
request.setEvents(events);
} else {
request.setEvents(JsonCallbackEvents.disableButtonEvents(saveChangesButton));
}
Map<String, Integer> ids = new HashMap<String, Integer>();
ids.put("member", memList.get(i).getId());
ids.put("workWithUserAttributes", 1);
request.setAttributes(ids, list);
}
}
});
} else {
// just one group / memeber
SetAttributes request = new SetAttributes(events);
request.setAttributes(jsonCallback.getIds(), list);
}
}
}
};
// click handler to save required attributes
final ClickHandler saveReqAttrsClickHandler = new ClickHandler() {
public void onClick(ClickEvent event) {
final ArrayList<Attribute> list = jsonCallback.getTableSelectedList();
// check if not empty
if (UiElements.cantSaveEmptyListDialogBox(list)) {
// refresh table and disable button a event for GetResourceRequiredAttributes
final JsonCallbackEvents events = JsonCallbackEvents.disableButtonEvents(saveChangesButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
jsonCallback.clearTable();
// set back resourceToGetServicesFrom
Map<String, Integer> ids = reqAttrs.getIds();
if (ids.containsKey("resource")) {
ids.put("resourceToGetServicesFrom", ids.get("resource"));
}
if (ids.containsKey("user")) {
ids.put("workWithUserAttributes", 1);
}
if (ids.containsKey("resource") && ids.containsKey("group")) {
ids.put("workWithGroupAttributes", 1);
}
reqAttrs.setIds(ids);
reqAttrs.retrieveData();
}
});
final SetAttributes request = new SetAttributes();
if (memChb.getValue() == true) {
// to all members selected
UiElements.generateInfo(WidgetTranslation.INSTANCE.saveConfirmTitle(), "Same value(s) for selected attribute(s) will be set to ALL members of group.", new ClickHandler() {
public void onClick(ClickEvent event) {
ArrayList<RichMember> memList = membersDropDown.getAllObjects();
for (int i = 0; i < memList.size(); i++) {
if (i == 0) {
events.onLoadingStart();
}
// trigger disable button if first
Map<String, Integer> ids = new HashMap<String, Integer>();
ids.put("member", memList.get(i).getId());
ids.put("user", memList.get(i).getUserId());
ids.put("resource", resourceDropDown.getSelectedObject().getId());
ids.put("facility", resourceDropDown.getSelectedObject().getFacilityId());
if (i == memList.size() - 1) {
request.setEvents(events);
}
// set events to last to get refresh and button enable
request.setAttributes(ids, list);
}
}
});
} else {
// group or member
// disable button events
request.setEvents(events);
request.setAttributes(reqAttrs.getIds(), list);
}
}
}
};
// REMOVE BUTTON
final CustomButton removeButton = TabMenu.getPredefinedButton(ButtonType.REMOVE, ButtonTranslation.INSTANCE.removeAttributes());
if (!session.isGroupAdmin(groupId) && !session.isVoAdmin(group.getVoId()))
removeButton.setEnabled(false);
// click handler to remove group and member-user attributes
final ClickHandler removeAttrsClickHandler = new ClickHandler() {
public void onClick(ClickEvent event) {
final ArrayList<Attribute> list = jsonCallback.getTableSelectedList();
if (UiElements.cantSaveEmptyListDialogBox(list)) {
// refresh table and disable button a event for GetAttributes
final JsonCallbackEvents events = JsonCallbackEvents.disableButtonEvents(removeButton, JsonCallbackEvents.refreshTableEvents(jsonCallback));
final RemoveAttributes request = new RemoveAttributes();
if (memChb.getValue() == true) {
// to all members selected
UiElements.generateInfo(WidgetTranslation.INSTANCE.deleteConfirmTitle(), "Selected attribute(s) will be removed from ALL members of group.", new ClickHandler() {
public void onClick(ClickEvent event) {
ArrayList<RichMember> memList = membersDropDown.getAllObjects();
for (int i = 0; i < memList.size(); i++) {
// trigger disable button if first
if (i == 0) {
events.onLoadingStart();
}
Map<String, Integer> ids = new HashMap<String, Integer>();
ids.put("member", memList.get(i).getId());
ids.put("workWithUserAttributes", 1);
// set events to last to get refresh and button enable
if (i == memList.size() - 1) {
request.setEvents(events);
}
request.removeAttributes(ids, list);
}
}
});
} else {
// just one group / memeber
// disable button events
request.setEvents(events);
request.removeAttributes(jsonCallback.getIds(), list);
}
}
}
};
// click handler to remove ResourceRequiredAttributes
final ClickHandler removeReqAttrsClickHandler = new ClickHandler() {
public void onClick(ClickEvent event) {
final ArrayList<Attribute> list = jsonCallback.getTableSelectedList();
if (UiElements.cantSaveEmptyListDialogBox(list)) {
// refresh table and disable button a event for GetAttributes
final JsonCallbackEvents events = JsonCallbackEvents.disableButtonEvents(removeButton, new JsonCallbackEvents() {
public void onFinished(JavaScriptObject jso) {
jsonCallback.clearTable();
// set back resourceToGetServicesFrom
Map<String, Integer> ids = reqAttrs.getIds();
if (ids.containsKey("resource")) {
ids.put("resourceToGetServicesFrom", ids.get("resource"));
}
if (ids.containsKey("user")) {
ids.put("workWithUserAttributes", 1);
}
if (ids.containsKey("resource") && ids.containsKey("group")) {
ids.put("workWithGroupAttributes", 1);
}
reqAttrs.setIds(ids);
reqAttrs.retrieveData();
}
});
final RemoveAttributes request = new RemoveAttributes();
if (memChb.getValue() == true) {
// to all members selected
UiElements.generateInfo(WidgetTranslation.INSTANCE.deleteConfirmTitle(), "Selected attribute(s) will be removed from ALL members of group.", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
ArrayList<RichMember> memList = membersDropDown.getAllObjects();
for (int i = 0; i < memList.size(); i++) {
// trigger disable button if first
if (i == 0) {
events.onLoadingStart();
}
Map<String, Integer> ids = new HashMap<String, Integer>();
ids.put("member", memList.get(i).getId());
ids.put("user", memList.get(i).getUserId());
ids.put("resource", resourceDropDown.getSelectedObject().getId());
ids.put("facility", resourceDropDown.getSelectedObject().getFacilityId());
// set events to last to get refresh and button enable
if (i == memList.size() - 1) {
request.setEvents(events);
}
request.removeAttributes(ids, list);
}
}
});
} else {
// just one group / member
// disable button events
request.setEvents(events);
request.removeAttributes(reqAttrs.getIds(), list);
}
}
}
};
// SET NEW ATTR BUTTON
final CustomButton setNewAttributeButton = TabMenu.getPredefinedButton(ButtonType.ADD, true, ButtonTranslation.INSTANCE.setNewAttributes());
if (!session.isGroupAdmin(groupId) && !session.isVoAdmin(group.getVoId()))
setNewAttributeButton.setEnabled(false);
// click handler
setNewAttributeButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
ids.clear();
if (resourceDropDown.getSelectedIndex() > 0) {
if (membersDropDown.getSelectedIndex() > 0) {
ids.put("member", membersDropDown.getSelectedObject().getId());
ids.put("user", membersDropDown.getSelectedObject().getUserId());
ids.put("resource", resourceDropDown.getSelectedObject().getId());
ids.put("facility", resourceDropDown.getSelectedObject().getFacilityId());
} else {
ids.put("resource", resourceDropDown.getSelectedObject().getId());
ids.put("group", groupId);
ids.put("workWithGroupAttributes", 1);
}
} else {
if (membersDropDown.getSelectedIndex() > 0) {
ids.put("member", membersDropDown.getSelectedObject().getId());
ids.put("user", membersDropDown.getSelectedObject().getUserId());
} else {
ids.put("group", groupId);
}
}
session.getTabManager().addTabToCurrentTab(new SetNewAttributeTabItem(ids, jsonCallback.getList()), true);
}
});
// ClickHandlersRegistration - save onClicks for GetAttributes
final ArrayList<HandlerRegistration> clickHandlers = new ArrayList<HandlerRegistration>();
clickHandlers.add(saveChangesButton.addClickHandler(saveAttrsClickHandler));
clickHandlers.add(removeButton.addClickHandler(removeAttrsClickHandler));
// DEFINE CHANGE HANDLER FOR ALL DROP DOWNS
ChangeHandler changeHandler = new ChangeHandler() {
public void onChange(ChangeEvent event) {
int resIndex = resourceDropDown.getSelectedIndex();
int memIndex = membersDropDown.getSelectedIndex();
// no resource or member selected
if (resIndex == 0 && memIndex == 0) {
lastSelectedMemberId = 0;
lastSelectedResourceId = 0;
memChb.setValue(false);
memChb.setVisible(false);
jsonCallback.clearTable();
ids.clear();
ids.put("group", groupId);
jsonCallback.setIds(ids);
jsonCallback.retrieveData();
for (HandlerRegistration handler : clickHandlers) {
if (handler != null) {
handler.removeHandler();
}
}
clickHandlers.add(saveChangesButton.addClickHandler(saveAttrsClickHandler));
clickHandlers.add(removeButton.addClickHandler(removeAttrsClickHandler));
}
// no resource but member selected
if (resIndex == 0 && memIndex > 0) {
lastSelectedMemberId = membersDropDown.getSelectedObject().getId();
lastSelectedResourceId = 0;
memChb.setValue(false);
memChb.setVisible(true);
jsonCallback.clearTable();
ids.clear();
ids.put("member", membersDropDown.getSelectedObject().getId());
ids.put("workWithUserAttributes", 1);
jsonCallback.setIds(ids);
jsonCallback.retrieveData();
for (HandlerRegistration handler : clickHandlers) {
if (handler != null) {
handler.removeHandler();
}
}
clickHandlers.add(saveChangesButton.addClickHandler(saveAttrsClickHandler));
clickHandlers.add(removeButton.addClickHandler(removeAttrsClickHandler));
}
// resource and no member selected
if (resIndex > 0 && memIndex == 0) {
lastSelectedMemberId = 0;
lastSelectedResourceId = resourceDropDown.getSelectedObject().getId();
memChb.setValue(false);
memChb.setVisible(false);
jsonCallback.clearTable();
ids.clear();
ids.put("group", groupId);
ids.put("resource", resourceDropDown.getSelectedObject().getId());
ids.put("resourceToGetServicesFrom", ids.get("resource"));
ids.put("workWithGroupAttributes", 1);
reqAttrs.setIds(ids);
reqAttrs.retrieveData();
// set ids back to make saveChanges work
ids.remove("resourceToGetServicesFrom");
reqAttrs.setIds(ids);
for (HandlerRegistration handler : clickHandlers) {
if (handler != null) {
handler.removeHandler();
}
}
clickHandlers.add(saveChangesButton.addClickHandler(saveReqAttrsClickHandler));
clickHandlers.add(removeButton.addClickHandler(removeReqAttrsClickHandler));
}
// resource and member selected
if (resIndex > 0 && memIndex > 0) {
lastSelectedMemberId = membersDropDown.getSelectedObject().getId();
lastSelectedResourceId = resourceDropDown.getSelectedObject().getId();
memChb.setValue(false);
memChb.setVisible(true);
jsonCallback.clearTable();
ids.clear();
ids.put("resource", resourceDropDown.getSelectedObject().getId());
ids.put("resourceToGetServicesFrom", ids.get("resource"));
ids.put("member", membersDropDown.getSelectedObject().getId());
ids.put("workWithUserAttributes", 1);
reqAttrs.setIds(ids);
reqAttrs.retrieveData();
// set ids back to make saveChanges work
ids.clear();
ids.put("member", membersDropDown.getSelectedObject().getId());
ids.put("user", membersDropDown.getSelectedObject().getUser().getId());
ids.put("facility", resourceDropDown.getSelectedObject().getFacilityId());
ids.put("resource", resourceDropDown.getSelectedObject().getId());
reqAttrs.setIds(ids);
for (HandlerRegistration handler : clickHandlers) {
if (handler != null) {
handler.removeHandler();
}
}
clickHandlers.add(saveChangesButton.addClickHandler(saveReqAttrsClickHandler));
clickHandlers.add(removeButton.addClickHandler(removeReqAttrsClickHandler));
}
}
};
// ADD CHANGE HANDLERS TO DROP-DOWN MENUS
resourceDropDown.addChangeHandler(changeHandler);
membersDropDown.addChangeHandler(changeHandler);
// PUT ELEMENTS IN MENU
menu.addWidget(UiElements.getRefreshButton(this));
menu.addWidget(saveChangesButton);
//menu.addWidget(resChb);
menu.addWidget(memChb);
menu.addWidget(setNewAttributeButton);
menu.addWidget(removeButton);
menu.addWidget(new HTML("<strong>Resources:</strong>"));
menu.addWidget(resourceDropDown);
menu.addWidget(new HTML("<strong>Members:</strong>"));
menu.addWidget(membersDropDown);
this.contentWidget.setWidget(vp);
return getWidget();
}
Aggregations