SprintManager.java
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package jsf2.demo.scrum.web.controller;
import jsf2.demo.scrum.model.entities.Project;
import jsf2.demo.scrum.model.entities.Sprint;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.validator.ValidatorException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Dr. Spock (spock at dev.java.net)
*/
@ManagedBean(name = "sprintManager")
@SessionScoped
public class SprintManager extends AbstractManager implements Serializable {
private static final long serialVersionUID = 1L;
private Sprint currentSprint;
private DataModel<Sprint> sprints;
private List<Sprint> sprintList;
@ManagedProperty("#{projectManager}")
private ProjectManager projectManager;
private Project currentProject;
@PostConstruct
public void construct() {
init();
}
@PreDestroy
public void destroy() {
sprints = null;
if (null != sprintList) {
sprintList.clear();
sprintList = null;
}
projectManager = null;
currentProject = null;
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("sprintManager");
}
public void init() {
Sprint sprint = new Sprint();
Project pmCurrentProject = getProjectManager().getCurrentProject();
sprint.setProject(pmCurrentProject);
setCurrentSprint(sprint);
if (pmCurrentProject != null) {
sprintList = new LinkedList<Sprint>(pmCurrentProject.getSprints());
} else {
sprintList = Collections.emptyList();
}
sprints = new ListDataModel<Sprint>(sprintList);
}
public String create() {
Sprint sprint = new Sprint();
sprint.setProject(getProjectManager().getCurrentProject());
setCurrentSprint(sprint);
return "create";
}
public String save() {
if (currentSprint != null) {
try {
Sprint merged = doInTransaction(new PersistenceAction<Sprint>() {
public Sprint execute(EntityManager em) {
if (currentSprint.isNew()) {
em.persist(currentSprint);
} else if (!em.contains(currentSprint)) {
return em.merge(currentSprint);
}
return currentSprint;
}
});
if (!currentSprint.equals(merged)) {
setCurrentSprint(merged);
int idx = sprintList.indexOf(currentSprint);
if (idx != -1) {
sprintList.set(idx, merged);
}
}
getProjectManager().getCurrentProject().addSprint(merged);
if (!sprintList.contains(merged)) {
sprintList.add(merged);
}
} catch (Exception e) {
getLogger(getClass()).log(Level.SEVERE, "Error on try to save Sprint: " + currentSprint, e);
addMessage("Error on try to save Sprint", FacesMessage.SEVERITY_ERROR);
return null;
}
}
return "show";
}
public String edit() {
setCurrentSprint(sprints.getRowData());
return "edit";
}
public String remove() {
final Sprint sprint = sprints.getRowData();
if (sprint != null) {
try {
doInTransaction(new PersistenceActionWithoutResult() {
public void execute(EntityManager em) {
if (em.contains(sprint)) {
em.remove(sprint);
} else {
em.remove(em.merge(sprint));
}
}
});
getProjectManager().getCurrentProject().removeSpring(sprint);
sprintList.remove(sprint);
} catch (Exception e) {
getLogger(getClass()).log(Level.SEVERE, "Error on try to remove Sprint: " + currentSprint, e);
addMessage("Error on try to remove Sprint", FacesMessage.SEVERITY_ERROR);
return null;
}
}
return "show";
}
/*
* This method can be pointed to by a validator methodExpression, such as:
*
* <h:inputText id="itName" value="#{sprintManager.currentSprint.name}" required="true"
* requiredMessage="#{i18n['sprint.form.label.name.required']}" maxLength="30" size="30"
* validator="#{sprintManager.checkUniqueSprintName}" />
*/
public void checkUniqueSprintNameFacesValidatorMethod(FacesContext context, UIComponent component, Object newValue) {
final String newName = (String) newValue;
String message = checkUniqueSprintNameApplicationValidatorMethod(newName);
if (null != message) {
throw new ValidatorException(getFacesMessageForKey("sprint.form.label.name.unique"));
}
}
/*
* This method is called by the JSR-303 SprintNameUniquenessConstraintValidator.
* If it returns non-null, the result must be interpreted as the localized
* validation message.
*
*/
public String checkUniqueSprintNameApplicationValidatorMethod(String newValue) {
String message = null;
final String newName = newValue;
try {
Long count = doInTransaction(new PersistenceAction<Long>() {
public Long execute(EntityManager em) {
Query query = em.createNamedQuery((currentSprint.isNew()) ? "sprint.new.countByNameAndProject" : "sprint.countByNameAndProject");
query.setParameter("name", newName);
query.setParameter("project", getProjectManager().getCurrentProject());
if (!currentSprint.isNew()) {
query.setParameter("currentSprint", currentSprint);
}
return (Long) query.getSingleResult();
}
});
if (count != null && count > 0) {
message = getFacesMessageForKey("sprint.form.label.name.unique").getSummary();
}
} catch (ManagerException ex) {
Logger.getLogger(SprintManager.class.getName()).log(Level.SEVERE, null, ex);
}
return message;
}
public String cancelEdit() {
return "show";
}
public String showStories() {
setCurrentSprint(sprints.getRowData());
return "showStories";
}
public String showDashboard() {
setCurrentSprint(sprints.getRowData());
return "showDashboard";
}
public Sprint getCurrentSprint() {
return currentSprint;
}
public void setCurrentSprint(Sprint currentSprint) {
this.currentSprint = currentSprint;
}
public DataModel<Sprint> getSprints() {
this.sprints = new ListDataModel<Sprint>(projectManager.getCurrentProject().getSprints());
return this.sprints;
}
public void setSprints(DataModel<Sprint> sprints) {
this.sprints = sprints;
}
public ProjectManager getProjectManager() {
return projectManager;
}
public void setProjectManager(ProjectManager projectManager) {
this.projectManager = projectManager;
}
public Project getProject() {
Project pmCurrentProject = projectManager.getCurrentProject();
// Verify if the currentProject is out of date
// If there is a new CurrentProject we need to update sprintList and set currentSprint to null and tell user he/she needs to select a Sprint
if (pmCurrentProject != currentProject) {
this.setCurrentSprint(null);
this.sprintList = pmCurrentProject.getSprints();
this.sprints = new ListDataModel<Sprint>(sprintList);
this.currentProject = pmCurrentProject;
}
return currentProject;
}
public void setProject(Project project) {
projectManager.setCurrentProject(project);
}
}