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.History;
020: import info.jtrac.domain.Item;
021: import info.jtrac.domain.ItemSearch;
022: import info.jtrac.domain.ItemUser;
023: import info.jtrac.domain.Space;
024: import info.jtrac.domain.State;
025: import info.jtrac.domain.User;
026: import info.jtrac.domain.UserSpaceRole;
027: import info.jtrac.util.UserUtils;
028: import java.util.ArrayList;
029: import java.util.List;
030: import java.util.Map;
031: import org.apache.wicket.PageParameters;
032: import org.apache.wicket.ajax.AjaxRequestTarget;
033: import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
034: import org.apache.wicket.markup.html.WebMarkupContainer;
035: import org.apache.wicket.markup.html.form.CheckBox;
036: import org.apache.wicket.markup.html.form.DropDownChoice;
037: import org.apache.wicket.markup.html.form.Form;
038: import org.apache.wicket.markup.html.form.FormComponent;
039: import org.apache.wicket.markup.html.form.IChoiceRenderer;
040: import org.apache.wicket.markup.html.form.ListMultipleChoice;
041: import org.apache.wicket.markup.html.form.TextArea;
042: import org.apache.wicket.markup.html.form.upload.FileUpload;
043: import org.apache.wicket.markup.html.form.upload.FileUploadField;
044: import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
045: import org.apache.wicket.markup.html.panel.FeedbackPanel;
046: import org.apache.wicket.model.BoundCompoundPropertyModel;
047: import org.apache.wicket.util.lang.Bytes;
048:
049: /**
050: * Form to update history for item
051: */
052: public class ItemViewFormPanel extends BasePanel {
053:
054: private JtracFeedbackMessageFilter filter;
055: private ItemSearch itemSearch;
056:
057: public ItemViewFormPanel(String id, Item item, ItemSearch itemSearch) {
058: super (id);
059: this .itemSearch = itemSearch;
060: FeedbackPanel feedback = new FeedbackPanel("feedback");
061: filter = new JtracFeedbackMessageFilter();
062: feedback.setFilter(filter);
063: add(feedback);
064: add(new ItemViewForm("form", item));
065: }
066:
067: /**
068: * wicket form
069: */
070: private class ItemViewForm extends Form {
071:
072: private FileUploadField fileUploadField;
073: private long itemId;
074: private DropDownChoice assignedToChoice;
075: private DropDownChoice statusChoice;
076:
077: public ItemViewForm(String id, final Item item) {
078: super (id);
079: setMultiPart(true);
080: this .itemId = item.getId();
081: final History history = new History();
082: history.setItemUsers(item.getItemUsers());
083: final BoundCompoundPropertyModel model = new BoundCompoundPropertyModel(
084: history);
085: setModel(model);
086: add(new TextArea("comment").setRequired(true).add(
087: new ErrorHighlighter()));
088: // custom fields ===================================================
089: User user = getPrincipal();
090: add(new CustomFieldsFormPanel("fields", model, item, user));
091: // =================================================================
092: final Space space = item.getSpace();
093: final List<UserSpaceRole> userSpaceRoles = getJtrac()
094: .findUserRolesForSpace(space.getId());
095: // assigned to =====================================================
096: final WebMarkupContainer border = new WebMarkupContainer(
097: "border");
098: border.setOutputMarkupId(true);
099: final WebMarkupContainer hide = new WebMarkupContainer(
100: "hide");
101: border.add(hide);
102: final List<User> emptyList = new ArrayList<User>(0); // will be populated over Ajax
103: assignedToChoice = new DropDownChoice("assignedTo",
104: emptyList, new IChoiceRenderer() {
105: public Object getDisplayValue(Object o) {
106: return ((User) o).getName();
107: }
108:
109: public String getIdValue(Object o, int i) {
110: return ((User) o).getId() + "";
111: }
112: });
113: assignedToChoice.setOutputMarkupId(true);
114: assignedToChoice.setVisible(false);
115: assignedToChoice.setNullValid(true);
116: border.add(new ErrorHighlighter(assignedToChoice));
117: border.add(assignedToChoice);
118: add(border);
119: // status ==========================================================
120: final Map<Integer, String> statesMap = item
121: .getPermittedTransitions(user);
122: List<Integer> states = new ArrayList(statesMap.keySet());
123: statusChoice = new IndicatingDropDownChoice("status",
124: states, new IChoiceRenderer() {
125: public Object getDisplayValue(Object o) {
126: return statesMap.get(o);
127: }
128:
129: public String getIdValue(Object o, int i) {
130: return o.toString();
131: }
132: });
133: statusChoice.setNullValid(true);
134: statusChoice.add(new ErrorHighlighter());
135: statusChoice.add(new AjaxFormComponentUpdatingBehavior(
136: "onChange") {
137: protected void onUpdate(AjaxRequestTarget target) {
138: Integer selectedStatus = (Integer) getFormComponent()
139: .getConvertedInput();
140: if (selectedStatus == null) {
141: assignedToChoice.setVisible(false);
142: hide.setVisible(true);
143: } else {
144: List<User> assignable = UserUtils
145: .filterUsersAbleToTransitionFrom(
146: userSpaceRoles, space,
147: selectedStatus);
148: assignedToChoice.setChoices(assignable);
149: assignedToChoice.setVisible(true);
150: hide.setVisible(false);
151: }
152: target.addComponent(border);
153: }
154: });
155: add(statusChoice);
156: // notify list =====================================================
157: List<ItemUser> choices = UserUtils
158: .convertToItemUserList(userSpaceRoles);
159: ListMultipleChoice itemUsers = new JtracCheckBoxMultipleChoice(
160: "itemUsers", choices, new IChoiceRenderer() {
161: public Object getDisplayValue(Object o) {
162: return ((ItemUser) o).getUser().getName();
163: }
164:
165: public String getIdValue(Object o, int i) {
166: return ((ItemUser) o).getUser().getId()
167: + "";
168: }
169: }, true);
170: add(itemUsers);
171: // attachment ======================================================
172: fileUploadField = new FileUploadField("file");
173: add(fileUploadField);
174: setMaxSize(Bytes.megabytes(getJtrac()
175: .getAttachmentMaxSizeInMb()));
176: // send notifications===============================================
177: add(new CheckBox("sendNotifications"));
178: // validation that assignedTo is not null if status is not null and not CLOSED
179: // have to use FormValidator because this is conditional validation across two FormComponents
180: add(new AbstractFormValidator() {
181: public FormComponent[] getDependentFormComponents() {
182: // actually we depend on assignedToChoice also, but wicket logs a warning when the
183: // component is not visible but we are doing ajax. anyway we use assignedToChoice.getInput()
184: // not assignedToChoice.convertedInput() so no danger there
185: return new FormComponent[] { statusChoice };
186: }
187:
188: public void validate(Form unused) {
189: if (assignedToChoice.getInput() == null
190: || assignedToChoice.getInput().trim()
191: .length() == 0) {
192: Integer i = (Integer) statusChoice
193: .getConvertedInput();
194: if (i != null && i != State.CLOSED) {
195: // user may have customized the name of the CLOSED State e.g. for i18n
196: // so when reporting the error, use the display name
197: String closedDisplayName = space
198: .getMetadata().getStatusValue(
199: State.CLOSED);
200: assignedToChoice.error(localize(
201: "item_view_form.assignedTo.error",
202: closedDisplayName));
203: }
204: }
205: }
206: });
207: }
208:
209: @Override
210: protected void validate() {
211: filter.reset();
212: super .validate();
213: }
214:
215: @Override
216: protected void onSubmit() {
217: final FileUpload fileUpload = fileUploadField
218: .getFileUpload();
219: History history = (History) getModelObject();
220: User user = ((JtracSession) getSession()).getUser();
221: history.setLoggedBy(user);
222: getJtrac().storeHistoryForItem(itemId, history, fileUpload);
223: setResponsePage(ItemViewPage.class, new PageParameters("0="
224: + history.getRefId()));
225: }
226:
227: }
228:
229: }
|