001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.wicket;
018:
019: import info.jtrac.domain.Space;
020: import info.jtrac.domain.User;
021: import info.jtrac.domain.UserSpaceRole;
022: import java.util.ArrayList;
023: import java.util.List;
024: import org.apache.wicket.ajax.AjaxRequestTarget;
025: import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
026: import org.apache.wicket.behavior.SimpleAttributeModifier;
027: import org.apache.wicket.markup.html.WebMarkupContainer;
028: import org.apache.wicket.markup.html.WebPage;
029: import org.apache.wicket.markup.html.basic.Label;
030: import org.apache.wicket.markup.html.form.Button;
031: import org.apache.wicket.markup.html.form.DropDownChoice;
032: import org.apache.wicket.markup.html.form.Form;
033: import org.apache.wicket.markup.html.form.IChoiceRenderer;
034: import org.apache.wicket.markup.html.link.Link;
035: import org.apache.wicket.markup.html.list.ListItem;
036: import org.apache.wicket.markup.html.list.ListView;
037: import org.apache.wicket.markup.html.panel.FeedbackPanel;
038: import org.apache.wicket.model.BoundCompoundPropertyModel;
039: import org.apache.wicket.model.PropertyModel;
040:
041: /**
042: * user allocate page
043: */
044: public class UserAllocatePage extends BasePage {
045:
046: private WebPage previous;
047: private long userId;
048: private long selectedSpaceId;
049:
050: public void setSelectedSpaceId(long selectedSpaceId) {
051: this .selectedSpaceId = selectedSpaceId;
052: }
053:
054: public UserAllocatePage(long userId, WebPage previous) {
055: this .userId = userId;
056: this .previous = previous;
057: add(new UserAllocateForm("form"));
058: }
059:
060: public UserAllocatePage(long userId, WebPage previous,
061: long selectedSpaceId) {
062: this .userId = userId;
063: this .previous = previous;
064: this .selectedSpaceId = selectedSpaceId;
065: add(new UserAllocateForm("form"));
066: }
067:
068: /**
069: * wicket form
070: */
071: private class UserAllocateForm extends Form {
072:
073: private User user;
074: private Space space;
075: private String roleKey;
076:
077: public Space getSpace() {
078: return space;
079: }
080:
081: public void setSpace(Space space) {
082: this .space = space;
083: }
084:
085: public String getRoleKey() {
086: return roleKey;
087: }
088:
089: public void setRoleKey(String roleKey) {
090: this .roleKey = roleKey;
091: }
092:
093: private DropDownChoice roleKeyChoice;
094: private Button allocateButton;
095:
096: /**
097: * function that attempts to pre-select roleKey for convenience
098: * used on form init and also on Ajax onChange event for Space choice
099: */
100: private void initRoleChoice(Space s) {
101: List<String> roleKeys = new ArrayList(s.getMetadata()
102: .getRoles().keySet());
103: roleKeys.removeAll(user.getRoleKeys(s));
104: if (roleKeys.size() == 1) {
105: // pre select role for convenience
106: roleKey = roleKeys.get(0);
107: }
108: roleKeyChoice.setChoices(roleKeys);
109: roleKeyChoice.setEnabled(true);
110: allocateButton.setEnabled(true);
111: }
112:
113: public UserAllocateForm(String id) {
114:
115: super (id);
116: final BoundCompoundPropertyModel model = new BoundCompoundPropertyModel(
117: this );
118: setModel(model);
119:
120: add(new FeedbackPanel("feedback"));
121:
122: user = getJtrac().loadUser(userId);
123:
124: add(new Label("label", user.getName() + " ("
125: + user.getLoginName() + ")"));
126:
127: List<UserSpaceRole> usrs = new ArrayList(user
128: .getUserSpaceRoles());
129:
130: final SimpleAttributeModifier sam = new SimpleAttributeModifier(
131: "class", "alt");
132:
133: add(new ListView("usrs", usrs) {
134: protected void populateItem(ListItem listItem) {
135: final UserSpaceRole usr = (UserSpaceRole) listItem
136: .getModelObject();
137: if (usr.getSpace() != null
138: && usr.getSpace().getId() == selectedSpaceId) {
139: listItem.add(new SimpleAttributeModifier(
140: "class", "selected"));
141: } else if (listItem.getIndex() % 2 == 1) {
142: listItem.add(sam);
143: }
144: WebMarkupContainer spaceSpan = new WebMarkupContainer(
145: "space");
146: listItem.add(spaceSpan);
147: if (usr.getSpace() == null) {
148: spaceSpan.setVisible(false);
149: } else {
150: spaceSpan.add(new Label("name", usr.getSpace()
151: .getName()));
152: spaceSpan.add(new Link("prefixCode") {
153: public void onClick() {
154: if (previous instanceof SpaceAllocatePage) { // prevent recursive stack buildup
155: previous = null;
156: }
157: setResponsePage(new SpaceAllocatePage(
158: usr.getSpace().getId(),
159: UserAllocatePage.this , userId));
160: }
161: }.add(new Label("prefixCode", usr.getSpace()
162: .getPrefixCode())));
163: }
164: listItem.add(new Label("roleKey",
165: new PropertyModel(usr, "roleKey")));
166: Button deallocate = new Button("deallocate") {
167: @Override
168: public void onSubmit() {
169: getJtrac().removeUserSpaceRole(usr);
170: refreshPrincipal(usr.getUser());
171: setResponsePage(new UserAllocatePage(
172: userId, previous));
173: }
174: };
175: // make it impossible to remove the first user ensuring there is always an admin
176: if (usr.getUser().getId() == 1
177: && "ROLE_ADMIN".equals(usr.getRoleKey())) {
178: deallocate.setVisible(false);
179: }
180: listItem.add(deallocate);
181: }
182: });
183:
184: List<Space> spaces = getJtrac()
185: .findUnallocatedSpacesForUser(user.getId());
186:
187: DropDownChoice spaceChoice = new DropDownChoice("space",
188: spaces, new IChoiceRenderer() {
189: public Object getDisplayValue(Object o) {
190: return ((Space) o).getName();
191: }
192:
193: public String getIdValue(Object o, int i) {
194: return ((Space) o).getId() + "";
195: }
196: });
197: spaceChoice.setNullValid(true);
198:
199: add(spaceChoice);
200:
201: spaceChoice.add(new AjaxFormComponentUpdatingBehavior(
202: "onChange") {
203: protected void onUpdate(AjaxRequestTarget target) {
204: Space s = (Space) getFormComponent()
205: .getConvertedInput();
206: if (s == null) {
207: roleKeyChoice.setEnabled(false);
208: allocateButton.setEnabled(false);
209: } else {
210: Space temp = getJtrac().loadSpace(s.getId());
211: // populate choice, enable button etc
212: initRoleChoice(temp);
213: }
214: target.addComponent(roleKeyChoice);
215: target.addComponent(allocateButton);
216: }
217: });
218:
219: roleKeyChoice = new DropDownChoice("roleKey");
220: roleKeyChoice.setOutputMarkupId(true);
221: roleKeyChoice.setEnabled(false);
222: roleKeyChoice.setRequired(true);
223: roleKeyChoice.setNullValid(true);
224: roleKeyChoice.add(new ErrorHighlighter());
225: add(roleKeyChoice);
226:
227: allocateButton = new Button("allocate") {
228: @Override
229: public void onSubmit() {
230: if (space == null || roleKey == null) {
231: return;
232: }
233: getJtrac().storeUserSpaceRole(user, space, roleKey);
234: refreshPrincipal(user);
235: setResponsePage(new UserAllocatePage(userId,
236: previous, space.getId()));
237: }
238: };
239: allocateButton.setOutputMarkupId(true);
240: allocateButton.setEnabled(false);
241: add(allocateButton);
242:
243: if (spaces.size() == 1) {
244: // pre select space for convenience
245: space = spaces.get(0);
246: // see if the role can be pre selected also at least populate choice, enable button etc
247: initRoleChoice(space);
248: }
249:
250: // make admin ======================================================
251:
252: WebMarkupContainer makeAdmin = new WebMarkupContainer(
253: "makeAdmin");
254: if (user.isAdminForAllSpaces()) {
255: makeAdmin.setVisible(false);
256: } else {
257: makeAdmin.add(new Button("makeAdmin") {
258: @Override
259: public void onSubmit() {
260: getJtrac().storeUserSpaceRole(user, null,
261: "ROLE_ADMIN");
262: refreshPrincipal(user);
263: setResponsePage(new UserAllocatePage(userId,
264: previous));
265: }
266: });
267: }
268: add(makeAdmin);
269:
270: // cancel ==========================================================
271: add(new Link("cancel") {
272: public void onClick() {
273: if (previous == null) {
274: setResponsePage(UserListPage.class);
275: return;
276: }
277: if (previous instanceof UserListPage) {
278: ((UserListPage) previous)
279: .setSelectedUserId(userId);
280: }
281: if (previous instanceof SpaceAllocatePage) {
282: ((SpaceAllocatePage) previous)
283: .setSelectedUserId(userId);
284: }
285: setResponsePage(previous);
286: }
287: });
288: }
289:
290: }
291:
292: }
|