James Moger
2015-12-10 63e4d9660de3f4c615a8e9444aaf3c3de128e77c
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;
667163 27 import org.apache.wicket.markup.html.basic.Label;
ce048e 28 import org.apache.wicket.markup.html.form.Button;
667163 29 import org.apache.wicket.markup.html.form.CheckBox;
ce048e 30 import org.apache.wicket.markup.html.form.DropDownChoice;
JM 31 import org.apache.wicket.markup.html.form.Form;
32 import org.apache.wicket.markup.html.form.TextField;
db0401 33 import org.apache.wicket.markup.html.link.Link;
ce048e 34 import org.apache.wicket.model.IModel;
JM 35 import org.apache.wicket.model.Model;
36
37 import com.gitblit.models.RepositoryModel;
38 import com.gitblit.models.TicketModel;
39 import com.gitblit.models.TicketModel.Status;
40 import com.gitblit.models.UserModel;
41 import com.gitblit.tickets.TicketMilestone;
019958 42 import com.gitblit.utils.StringUtils;
ce048e 43 import com.gitblit.wicket.GitBlitWebSession;
71a347 44 import com.gitblit.wicket.Html5DateField;
ce048e 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));
71a347 109         form.add(new Html5DateField("due", dueModel, "yyyy-MM-dd"));
667163 110         form.add(new Label("dueFormat", "yyyy-MM-dd"));
JM 111         form.add(new CheckBox("notify", notificationModel));
71a347 112         addBottomScriptInline("{var e=document.createElement('input');e.type='date';if(e.type=='date'){$('[name=\"due\"]~.help-inline').hide()}}");
PM 113         addBottomScript("scripts/wicketHtml5Patch.js");
114         
6209dc 115         List<Status> statusChoices = Arrays.asList(Status.Open, Status.Closed);
JM 116         form.add(new DropDownChoice<TicketModel.Status>("status", statusModel, statusChoices));
117
118         form.add(new AjaxButton("save") {
ce048e 119
JM 120             private static final long serialVersionUID = 1L;
121
122             @Override
6209dc 123             protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
ce048e 124                 String name = nameModel.getObject();
JM 125                 if (StringUtils.isEmpty(name)) {
126                     return;
127                 }
019958 128
ce048e 129                 Date due = dueModel.getObject();
JM 130                 Status status = statusModel.getObject();
131                 boolean rename = !name.equals(oldName);
132                 boolean notify = notificationModel.getObject();
019958 133
ce048e 134                 UserModel currentUser = GitBlitWebSession.get().getUser();
JM 135                 String createdBy = currentUser.username;
019958 136
ce048e 137                 TicketMilestone tm = app().tickets().getMilestone(getRepositoryModel(), oldName);
JM 138                 tm.setName(name);
139                 tm.setDue(due);
140                 tm.status = status;
019958 141
ce048e 142                 boolean success = true;
JM 143                 if (rename) {
144                     success = app().tickets().renameMilestone(getRepositoryModel(), oldName, name, createdBy, notify);
145                 }
019958 146
ce048e 147                 if (success && app().tickets().updateMilestone(getRepositoryModel(), tm, createdBy)) {
60bbdf 148                     redirectTo(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
ce048e 149                 } else {
JM 150                     // TODO error
151                 }
152             }
6209dc 153         });
ce048e 154         Button cancel = new Button("cancel") {
JM 155             private static final long serialVersionUID = 1L;
156
157             @Override
158             public void onSubmit() {
3b23dc 159                 setResponsePage(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
ce048e 160             }
JM 161         };
162         cancel.setDefaultFormProcessing(false);
163         form.add(cancel);
164
db0401 165         Link<Void> delete = new Link<Void>("delete") {
JM 166
019958 167             private static final long serialVersionUID = 1L;
JM 168
169             @Override
db0401 170             public void onClick() {
019958 171                 UserModel currentUser = GitBlitWebSession.get().getUser();
JM 172                 String createdBy = currentUser.username;
667163 173                 boolean notify = notificationModel.getObject();
019958 174
667163 175                 if (app().tickets().deleteMilestone(getRepositoryModel(), oldName, createdBy, notify)) {
3b23dc 176                     setResponsePage(TicketsPage.class, WicketUtils.newOpenTicketsParameter(repositoryName));
019958 177                 } else {
db0401 178                     error(MessageFormat.format(getString("gb.milestoneDeleteFailed"), oldName));
019958 179                 }
JM 180             }
181         };
db0401 182
JM 183         delete.add(new JavascriptEventConfirmation("onclick", MessageFormat.format(
184             getString("gb.deleteMilestone"), oldName)));
185
019958 186         form.add(delete);
ce048e 187     }
JM 188
189     @Override
190     protected String getPageName() {
191         return getString("gb.editMilestone");
192     }
193
194     @Override
195     protected Class<? extends BasePage> getRepoNavPageClass() {
196         return TicketsPage.class;
197     }
198 }