James Moger
2014-09-30 02ea71c71dbf162561f0a5d4151bc436fca38373
commit | author | age
ce048e 1 /*
JM 2  * Copyright 2014 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.wicket.pages;
17
db0401 18 import java.text.MessageFormat;
ce048e 19 import java.util.Arrays;
JM 20 import java.util.Date;
21 import java.util.List;
22
23 import org.apache.wicket.PageParameters;
24 import org.apache.wicket.RestartResponseException;
6209dc 25 import org.apache.wicket.ajax.AjaxRequestTarget;
JM 26 import org.apache.wicket.ajax.markup.html.form.AjaxButton;
ce048e 27 import org.apache.wicket.extensions.markup.html.form.DateTextField;
667163 28 import org.apache.wicket.markup.html.basic.Label;
ce048e 29 import org.apache.wicket.markup.html.form.Button;
667163 30 import org.apache.wicket.markup.html.form.CheckBox;
ce048e 31 import org.apache.wicket.markup.html.form.DropDownChoice;
JM 32 import org.apache.wicket.markup.html.form.Form;
33 import org.apache.wicket.markup.html.form.TextField;
db0401 34 import org.apache.wicket.markup.html.link.Link;
ce048e 35 import org.apache.wicket.model.IModel;
JM 36 import org.apache.wicket.model.Model;
37
38 import com.gitblit.models.RepositoryModel;
39 import com.gitblit.models.TicketModel;
40 import com.gitblit.models.TicketModel.Status;
41 import com.gitblit.models.UserModel;
42 import com.gitblit.tickets.TicketMilestone;
019958 43 import com.gitblit.utils.StringUtils;
ce048e 44 import com.gitblit.wicket.GitBlitWebSession;
JM 45 import com.gitblit.wicket.WicketUtils;
db0401 46 import com.gitblit.wicket.panels.BasePanel.JavascriptEventConfirmation;
ce048e 47
JM 48 /**
49  * Page for creating a new milestone.
50  *
51  * @author James Moger
52  *
53  */
54 public class EditMilestonePage extends RepositoryPage {
55
56     private final String oldName;
019958 57
ce048e 58     private IModel<String> nameModel;
JM 59
60     private IModel<Date> dueModel;
019958 61
ce048e 62     private IModel<Status> statusModel;
019958 63
ce048e 64     private IModel<Boolean> notificationModel;
JM 65
66     public EditMilestonePage(PageParameters params) {
67         super(params);
68
69         RepositoryModel model = getRepositoryModel();
70         if (!app().tickets().isAcceptingTicketUpdates(model)) {
71             // ticket service is read-only
3b23dc 72             throw new RestartResponseException(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
ce048e 73         }
019958 74
ce048e 75         UserModel currentUser = GitBlitWebSession.get().getUser();
JM 76         if (currentUser == null) {
77             currentUser = UserModel.ANONYMOUS;
78         }
79
80         if (!currentUser.isAuthenticated || !currentUser.canAdmin(model)) {
81             // administration prohibited
3b23dc 82             throw new RestartResponseException(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
ce048e 83         }
019958 84
ce048e 85         oldName = WicketUtils.getObject(params);
JM 86         if (StringUtils.isEmpty(oldName)) {
87             // milestone not specified
3b23dc 88             throw new RestartResponseException(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
ce048e 89         }
019958 90
ce048e 91         TicketMilestone tm = app().tickets().getMilestone(getRepositoryModel(), oldName);
JM 92         if (tm == null) {
93             // milestone does not exist
3b23dc 94             throw new RestartResponseException(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
ce048e 95         }
JM 96
97         setStatelessHint(false);
98         setOutputMarkupId(true);
99
6209dc 100         Form<Void> form = new Form<Void>("editForm");
JM 101         add(form);
102
103         nameModel = Model.of(tm.name);
104         dueModel = Model.of(tm.due);
105         statusModel = Model.of(tm.status);
106         notificationModel = Model.of(true);
107
108         form.add(new TextField<String>("name", nameModel));
109         form.add(new DateTextField("due", dueModel, "yyyy-MM-dd"));
667163 110         form.add(new Label("dueFormat", "yyyy-MM-dd"));
JM 111         form.add(new CheckBox("notify", notificationModel));
6209dc 112
JM 113         List<Status> statusChoices = Arrays.asList(Status.Open, Status.Closed);
114         form.add(new DropDownChoice<TicketModel.Status>("status", statusModel, statusChoices));
115
116         form.add(new AjaxButton("save") {
ce048e 117
JM 118             private static final long serialVersionUID = 1L;
119
120             @Override
6209dc 121             protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
ce048e 122                 String name = nameModel.getObject();
JM 123                 if (StringUtils.isEmpty(name)) {
124                     return;
125                 }
019958 126
ce048e 127                 Date due = dueModel.getObject();
JM 128                 Status status = statusModel.getObject();
129                 boolean rename = !name.equals(oldName);
130                 boolean notify = notificationModel.getObject();
019958 131
ce048e 132                 UserModel currentUser = GitBlitWebSession.get().getUser();
JM 133                 String createdBy = currentUser.username;
019958 134
ce048e 135                 TicketMilestone tm = app().tickets().getMilestone(getRepositoryModel(), oldName);
JM 136                 tm.setName(name);
137                 tm.setDue(due);
138                 tm.status = status;
019958 139
ce048e 140                 boolean success = true;
JM 141                 if (rename) {
142                     success = app().tickets().renameMilestone(getRepositoryModel(), oldName, name, createdBy, notify);
143                 }
019958 144
ce048e 145                 if (success && app().tickets().updateMilestone(getRepositoryModel(), tm, createdBy)) {
60bbdf 146                     redirectTo(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
ce048e 147                 } else {
JM 148                     // TODO error
149                 }
150             }
6209dc 151         });
ce048e 152         Button cancel = new Button("cancel") {
JM 153             private static final long serialVersionUID = 1L;
154
155             @Override
156             public void onSubmit() {
3b23dc 157                 setResponsePage(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
ce048e 158             }
JM 159         };
160         cancel.setDefaultFormProcessing(false);
161         form.add(cancel);
162
db0401 163         Link<Void> delete = new Link<Void>("delete") {
JM 164
019958 165             private static final long serialVersionUID = 1L;
JM 166
167             @Override
db0401 168             public void onClick() {
019958 169                 UserModel currentUser = GitBlitWebSession.get().getUser();
JM 170                 String createdBy = currentUser.username;
667163 171                 boolean notify = notificationModel.getObject();
019958 172
667163 173                 if (app().tickets().deleteMilestone(getRepositoryModel(), oldName, createdBy, notify)) {
3b23dc 174                     setResponsePage(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
019958 175                 } else {
db0401 176                     error(MessageFormat.format(getString("gb.milestoneDeleteFailed"), oldName));
019958 177                 }
JM 178             }
179         };
db0401 180
JM 181         delete.add(new JavascriptEventConfirmation("onclick", MessageFormat.format(
182             getString("gb.deleteMilestone"), oldName)));
183
019958 184         form.add(delete);
ce048e 185     }
JM 186
187     @Override
188     protected String getPageName() {
189         return getString("gb.editMilestone");
190     }
191
192     @Override
193     protected Class<? extends BasePage> getRepoNavPageClass() {
194         return TicketsPage.class;
195     }
196 }