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 java.util.Set;
025: import org.apache.wicket.ajax.AjaxRequestTarget;
026: import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
027: import org.apache.wicket.behavior.SimpleAttributeModifier;
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.model.BoundCompoundPropertyModel;
038: import org.apache.wicket.model.LoadableDetachableModel;
039: import org.apache.wicket.model.PropertyModel;
040:
041: /**
042: * space allocate page
043: */
044: public class SpaceAllocatePage extends BasePage {
045:
046: private WebPage previous;
047: private long spaceId;
048: private long selectedUserId;
049:
050: public void setSelectedUserId(long selectedUserId) {
051: this .selectedUserId = selectedUserId;
052: }
053:
054: public long getSpaceId() {
055: return spaceId;
056: }
057:
058: public WebPage getPrevious() {
059: return previous;
060: }
061:
062: public SpaceAllocatePage(long spaceId, WebPage previous) {
063: this .spaceId = spaceId;
064: this .previous = previous;
065: add(new SpaceAllocateForm("form"));
066: }
067:
068: public SpaceAllocatePage(long spaceId, WebPage previous,
069: long selectedUserId) {
070: this .spaceId = spaceId;
071: this .previous = previous;
072: this .selectedUserId = selectedUserId;
073: add(new SpaceAllocateForm("form"));
074: }
075:
076: /**
077: * wicket form
078: */
079: private class SpaceAllocateForm extends Form {
080:
081: private Space space;
082: private User user;
083: private String roleKey;
084:
085: private DropDownChoice roleKeyChoice;
086: private Button allocateButton;
087:
088: /**
089: * function that attempts to pre-select roleKey for convenience
090: * used on form init and also on Ajax onChange event for User choice
091: */
092: private void initRoleChoice(User u) {
093: List<String> roleKeys = new ArrayList(space.getMetadata()
094: .getRoles().keySet());
095: for (String s : u.getRoleKeys(space)) {
096: roleKeys.remove(s);
097: }
098: if (roleKeys.size() == 1) {
099: // pre select role for convenience
100: roleKey = roleKeys.get(0);
101: }
102: roleKeyChoice.setChoices(roleKeys);
103: roleKeyChoice.setEnabled(true);
104: allocateButton.setEnabled(true);
105: }
106:
107: public SpaceAllocateForm(String id) {
108:
109: super (id);
110:
111: if (selectedUserId > 0) {
112: // pre-select newly created user for convenience
113: user = getJtrac().loadUser(selectedUserId);
114: }
115:
116: final BoundCompoundPropertyModel model = new BoundCompoundPropertyModel(
117: this );
118: setModel(model);
119:
120: space = getJtrac().loadSpace(spaceId);
121:
122: add(new Label("label", space.getName() + " ("
123: + space.getPrefixCode() + ")"));
124:
125: LoadableDetachableModel usrsModel = new LoadableDetachableModel() {
126: protected Object load() {
127: logger
128: .debug("loading user space roles list from database");
129: return getJtrac().findUserRolesForSpace(spaceId);
130: }
131: };
132:
133: final SimpleAttributeModifier sam = new SimpleAttributeModifier(
134: "class", "alt");
135:
136: add(new ListView("usrs", usrsModel) {
137: protected void populateItem(ListItem listItem) {
138: final UserSpaceRole usr = (UserSpaceRole) listItem
139: .getModelObject();
140: if (selectedUserId == usr.getUser().getId()) {
141: listItem.add(new SimpleAttributeModifier(
142: "class", "selected"));
143: } else if (listItem.getIndex() % 2 == 1) {
144: listItem.add(sam);
145: }
146: listItem.add(new Link("loginName") {
147: public void onClick() {
148: if (previous instanceof UserAllocatePage) { // prevent recursive stack buildup
149: previous = null;
150: }
151: setResponsePage(new UserAllocatePage(usr
152: .getUser().getId(),
153: SpaceAllocatePage.this , spaceId));
154: }
155: }.add(new Label("loginName", new PropertyModel(usr,
156: "user.loginName"))));
157: listItem.add(new Label("name", new PropertyModel(
158: usr, "user.name")));
159: listItem.add(new Label("roleKey",
160: new PropertyModel(usr, "roleKey")));
161: listItem.add(new Button("deallocate") {
162: @Override
163: public void onSubmit() {
164: // avoid lazy loading problem
165: UserSpaceRole temp = getJtrac()
166: .loadUserSpaceRole(usr.getId());
167: getJtrac().removeUserSpaceRole(temp);
168: refreshPrincipal(temp.getUser());
169: setResponsePage(new SpaceAllocatePage(
170: spaceId, previous));
171: }
172: });
173: }
174: });
175:
176: add(new Link("createNewUser") {
177: @Override
178: public void onClick() {
179: UserFormPage page = new UserFormPage();
180: page.setPrevious(SpaceAllocatePage.this );
181: setResponsePage(page);
182: }
183: });
184:
185: List<User> users = getJtrac().findUnallocatedUsersForSpace(
186: spaceId);
187:
188: DropDownChoice userChoice = new DropDownChoice("user",
189: users, new IChoiceRenderer() {
190: public Object getDisplayValue(Object o) {
191: User u = (User) o;
192: return u.getName() + " ("
193: + u.getLoginName() + ")";
194: }
195:
196: public String getIdValue(Object o, int i) {
197: return ((User) o).getId() + "";
198: }
199: });
200: userChoice.setNullValid(true);
201:
202: add(userChoice);
203:
204: userChoice.add(new AjaxFormComponentUpdatingBehavior(
205: "onChange") {
206: protected void onUpdate(AjaxRequestTarget target) {
207: User u = (User) getFormComponent()
208: .getConvertedInput();
209: if (u == null) {
210: roleKeyChoice.setEnabled(false);
211: allocateButton.setEnabled(false);
212: } else {
213: User temp = getJtrac().loadUser(u.getId());
214: // populate choice, enable button etc
215: initRoleChoice(temp);
216: }
217: target.addComponent(roleKeyChoice);
218: target.addComponent(allocateButton);
219: }
220: });
221:
222: roleKeyChoice = new DropDownChoice("roleKey");
223: roleKeyChoice.setOutputMarkupId(true);
224: roleKeyChoice.setEnabled(false);
225: roleKeyChoice.setNullValid(true);
226: add(roleKeyChoice);
227:
228: allocateButton = new Button("allocate") {
229: @Override
230: public void onSubmit() {
231: if (user == null || roleKey == null) {
232: return;
233: }
234: // avoid lazy init problem
235: User temp = getJtrac().loadUser(user.getId());
236: getJtrac().storeUserSpaceRole(temp, space, roleKey);
237: refreshPrincipal(temp);
238: setResponsePage(new SpaceAllocatePage(spaceId,
239: previous, user.getId()));
240: }
241: };
242:
243: allocateButton.setOutputMarkupId(true);
244: allocateButton.setEnabled(false);
245: add(allocateButton);
246:
247: if (users.size() == 1) {
248: // pre select the user for convenience
249: user = users.get(0);
250: }
251:
252: if (user != null) {
253: initRoleChoice(user);
254: }
255:
256: // cancel ==========================================================
257: add(new Link("cancel") {
258: public void onClick() {
259: if (previous == null) {
260: setResponsePage(SpaceListPage.class);
261: return;
262: }
263: if (previous instanceof SpaceListPage) {
264: ((SpaceListPage) previous)
265: .setSelectedSpaceId(spaceId);
266: }
267: if (previous instanceof UserAllocatePage) {
268: ((UserAllocatePage) previous)
269: .setSelectedSpaceId(spaceId);
270: }
271: setResponsePage(previous);
272: }
273: });
274: }
275:
276: }
277:
278: }
|