/
BackingBean.jv
257 lines (197 loc) · 6.54 KB
/
BackingBean.jv
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
package com.test.view;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import @{entity.getQualifiedName()}@{qbeMetawidgetImports};
/**
* Backing bean for @{entity.getName()} entities.
* <p>
* This class provides CRUD functionality for all @{entity.getName()} entities. It focuses
* purely on Java EE 6 standards (e.g. <tt>@ConversationScoped</tt> for
* state management, <tt>PersistenceContext</tt> for persistence,
* <tt>CriteriaBuilder</tt> for searches) rather than introducing a CRUD framework or
* custom base class.
*/
@Named
@Stateful
@ConversationScoped
public class @{entity.getName()}Bean implements Serializable {
private static final long serialVersionUID = 1L;
/*
* Support creating and retrieving @{entity.getName()} entities
*/
private Long id;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
private @{entity.getName()} @{ccEntity};
public @{entity.getName()} get@{entity.getName()}() {
return this.@{ccEntity};
}
@Inject
private Conversation conversation;
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;
public String create() {
this.conversation.begin();
return "create?faces-redirect=true";
}
public void retrieve() {
if (FacesContext.getCurrentInstance().isPostback()) {
return;
}
if (this.conversation.isTransient()) {
this.conversation.begin();
}
if (this.id == null) {
this.@{ccEntity} = this.search;
} else {
this.@{ccEntity} = this.entityManager.find(@{entity.getName()}.class, getId());
}
}
/*
* Support updating and deleting @{entity.getName()} entities
*/
public String update() {
this.conversation.end();
try {
if (this.id == null) {
this.entityManager.persist(this.@{ccEntity});
return "search?faces-redirect=true";
} else {
this.entityManager.merge(this.@{ccEntity});
return "view?faces-redirect=true&id=" + this.@{ccEntity}.getId();
}
} catch( Exception e ) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( e.getMessage() ));
return null;
}
}
public String delete() {
this.conversation.end();
try {
this.entityManager.remove(this.entityManager.find(@{entity.getName()}.class,
getId()));
this.entityManager.flush();
return "search?faces-redirect=true";
} catch( Exception e ) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( e.getMessage() ));
return null;
}
}
/*
* Support searching @{entity.getName()} entities with pagination
*/
private int page;
private long count;
private List<@{entity.getName()}> pageItems;
private @{entity.getName()} search = new @{entity.getName()}();
public int getPage() {
return this.page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageSize() {
return 10;
}
public @{entity.getName()} getSearch() {
return this.search;
}
public void setSearch(@{entity.getName()} search) {
this.search = search;
}
public void search() {
this.page = 0;
}
public void paginate() {
CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
// Populate this.count
CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);
Root<@{entity.getName()}> root = countCriteria.from(@{entity.getName()}.class);
countCriteria = countCriteria.select(builder.count(root)).where(
getSearchPredicates(root));
this.count = this.entityManager.createQuery(countCriteria)
.getSingleResult();
// Populate this.pageItems
CriteriaQuery<@{entity.getName()}> criteria = builder.createQuery(@{entity.getName()}.class);
root = criteria.from(@{entity.getName()}.class);
TypedQuery<@{entity.getName()}> query = this.entityManager.createQuery(criteria
.select(root).where(getSearchPredicates(root)));
query.setFirstResult(this.page * getPageSize()).setMaxResults(
getPageSize());
this.pageItems = query.getResultList();
}
private Predicate[] getSearchPredicates(Root<@{entity.getName()}> root) {
CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
List<Predicate> predicatesList = new ArrayList<Predicate>();
@{qbeMetawidget}
return predicatesList.toArray(new Predicate[predicatesList.size()]);
}
public List<@{entity.getName()}> getPageItems() {
return this.pageItems;
}
public long getCount() {
return this.count;
}
/*
* Support listing and POSTing back @{entity.getName()} entities (e.g. from inside an
* HtmlSelectOneMenu)
*/
public List<@{entity.getName()}> getAll() {
CriteriaQuery<@{entity.getName()}> criteria = this.entityManager
.getCriteriaBuilder().createQuery(@{entity.getName()}.class);
return this.entityManager.createQuery(
criteria.select(criteria.from(@{entity.getName()}.class))).getResultList();
}
public Converter getConverter() {
return new Converter() {
@Override
public Object getAsObject(FacesContext context,
UIComponent component, String value) {
return @{entity.getName()}Bean.this.entityManager.find(@{entity.getName()}.class,
Long.valueOf(value));
}
@Override
public String getAsString(FacesContext context,
UIComponent component, Object value) {
if (value == null) {
return "";
}
return String.valueOf(((@{entity.getName()}) value).getId());
}
};
}
/*
* Support adding children to bidirectional, one-to-many tables
*/
private @{entity.getName()} add = new @{entity.getName()}();
public @{entity.getName()} getAdd() {
return this.add;
}
public @{entity.getName()} getAdded() {
@{entity.getName()} added = this.add;
this.add = new @{entity.getName()}();
return added;
}
}