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.util.ValidationUtils;
022: import java.io.Serializable;
023: import java.util.List;
024: import org.apache.wicket.markup.html.WebPage;
025: import org.apache.wicket.markup.html.basic.Label;
026: import org.apache.wicket.markup.html.form.Button;
027: import org.apache.wicket.markup.html.form.Form;
028: import org.apache.wicket.markup.html.form.TextField;
029: import org.apache.wicket.markup.html.link.Link;
030: import org.apache.wicket.markup.html.panel.FeedbackPanel;
031: import org.apache.wicket.model.BoundCompoundPropertyModel;
032: import org.apache.wicket.validation.IValidatable;
033: import org.apache.wicket.validation.validator.AbstractValidator;
034:
035: /**
036: * space role add / edit form
037: */
038: public class SpaceRolePage extends BasePage {
039:
040: private WebPage previous;
041: private Space space;
042:
043: public SpaceRolePage(Space space, String roleKey, WebPage previous) {
044: this .space = space;
045: this .previous = previous;
046: add(new SpaceRoleForm("form", roleKey));
047: }
048:
049: /**
050: * wicket form
051: */
052: private class SpaceRoleForm extends Form {
053:
054: private String roleKey;
055:
056: public SpaceRoleForm(String id, final String roleKey) {
057:
058: super (id);
059: add(new FeedbackPanel("feedback"));
060: this .roleKey = roleKey;
061:
062: SpaceRoleModel modelObject = new SpaceRoleModel();
063: modelObject.setRoleKey(roleKey);
064: final BoundCompoundPropertyModel model = new BoundCompoundPropertyModel(
065: modelObject);
066: setModel(model);
067:
068: add(new Label("label", roleKey));
069:
070: // delete ==========================================================
071: Button delete = new Button("delete") {
072: @Override
073: public void onSubmit() {
074: List<User> users = getJtrac()
075: .findUsersWithRoleForSpace(space.getId(),
076: roleKey);
077: int affectedCount = users.size();
078: if (affectedCount > 0) {
079: String heading = localize("space_role_delete.confirm")
080: + " : " + roleKey;
081: String warning = localize("space_role_delete.line3");
082: String line1 = localize(
083: "space_role_delete.line1", space
084: .getName());
085: String line2 = localize("space_role_delete.line2");
086: ConfirmPage confirm = new ConfirmPage(
087: SpaceRolePage.this , heading, warning,
088: new String[] { line1, line2 }) {
089: public void onConfirm() {
090: getJtrac().bulkUpdateDeleteSpaceRole(
091: space, roleKey);
092: space.getMetadata().removeRole(roleKey);
093: getJtrac().storeSpace(space);
094: // synchronize metadata else when we save again we get Stale Object Exception
095: space.setMetadata(getJtrac()
096: .loadMetadata(
097: space.getMetadata()
098: .getId()));
099: // current user may be allocated to this space with this role - refresh
100: refreshPrincipal();
101: setResponsePage(new SpacePermissionsPage(
102: space, previous));
103: }
104: };
105: setResponsePage(confirm);
106: } else {
107: // this is an unsaved space / field or there are no impacted items
108: space.getMetadata().removeRole(roleKey);
109: setResponsePage(new SpacePermissionsPage(space,
110: previous));
111: }
112: }
113: };
114: delete.setDefaultFormProcessing(false);
115: if (space.getMetadata().getRoleCount() <= 1) {
116: delete.setEnabled(false);
117: }
118: add(delete);
119: // option ===========================================================
120: final TextField field = new TextField("roleKey");
121: field.setRequired(true);
122: field.add(new ErrorHighlighter());
123: // validation: format ok?
124: field.add(new AbstractValidator() {
125: protected void onValidate(IValidatable v) {
126: String s = (String) v.getValue();
127: if (!ValidationUtils.isAllUpperCase(s)) {
128: error(v);
129: }
130: }
131:
132: @Override
133: protected String resourceKey() {
134: return "space_role_form.error.role.invalid";
135: }
136: });
137: // validation: already exists?
138: field.add(new AbstractValidator() {
139: protected void onValidate(IValidatable v) {
140: String s = (String) v.getValue();
141: if (space.getMetadata().getRoles().containsKey(s)) {
142: error(v);
143: }
144: }
145:
146: @Override
147: protected String resourceKey() {
148: return "space_role_form.error.role.exists";
149: }
150: });
151: add(field);
152: // cancel ==========================================================
153: add(new Link("cancel") {
154: public void onClick() {
155: setResponsePage(new SpacePermissionsPage(space,
156: previous));
157: }
158: });
159: }
160:
161: @Override
162: protected void onSubmit() {
163: final SpaceRoleModel model = (SpaceRoleModel) getModelObject();
164: if (roleKey == null) {
165: space.getMetadata().addRole(model.getRoleKey());
166: setResponsePage(new SpacePermissionsPage(space,
167: previous));
168: } else if (!roleKey.equals(model.getRoleKey())) {
169: if (space.getId() > 0) {
170: String heading = localize(
171: "space_role_form_confirm.confirm", roleKey,
172: model.getRoleKey());
173: String warning = localize("space_role_form_confirm.line2");
174: String line1 = localize("space_role_form_confirm.line1");
175: ConfirmPage confirm = new ConfirmPage(
176: SpaceRolePage.this , heading, warning,
177: new String[] { line1 }) {
178: public void onConfirm() {
179: getJtrac().bulkUpdateRenameSpaceRole(space,
180: roleKey, model.getRoleKey());
181: space.getMetadata().renameRole(roleKey,
182: model.getRoleKey());
183: getJtrac().storeSpace(space);
184: // synchronize metadata else when we save again we get Stale Object Exception
185: space.setMetadata(getJtrac().loadMetadata(
186: space.getMetadata().getId()));
187: // current user may be allocated to this space with this role - refresh
188: refreshPrincipal();
189: setResponsePage(new SpacePermissionsPage(
190: space, previous));
191: }
192: };
193: setResponsePage(confirm);
194: } else {
195: space.getMetadata().renameRole(roleKey,
196: model.getRoleKey());
197: setResponsePage(new SpacePermissionsPage(space,
198: previous));
199: }
200: } else {
201: setResponsePage(new SpacePermissionsPage(space,
202: previous));
203: }
204:
205: }
206:
207: }
208:
209: /**
210: * custom form backing object that wraps role key
211: * required for the create / edit use case
212: */
213: private class SpaceRoleModel implements Serializable {
214:
215: private String roleKey;
216:
217: public String getRoleKey() {
218: return roleKey;
219: }
220:
221: public void setRoleKey(String roleKey) {
222: this.roleKey = roleKey;
223: }
224:
225: }
226:
227: }
|