-
Notifications
You must be signed in to change notification settings - Fork 5
/
AddonBean.java
319 lines (254 loc) · 7.26 KB
/
AddonBean.java
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package org.jboss.forge.view;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
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 org.jboss.forge.website.model.Addon;
/**
* Backing bean for Addon entities.
* <p>
* This class provides CRUD functionality for all Addon 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 AddonBean implements Serializable
{
private static final long serialVersionUID = 1L;
/*
* Support creating and retrieving Addon entities
*/
private Long id;
public Long getId()
{
return this.id;
}
public void setId(Long id)
{
this.id = id;
}
private Addon addon;
public Addon getAddon()
{
return this.addon;
}
@Inject
private Conversation conversation;
@PersistenceContext(unitName = "website-persistence-unit", 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.addon = this.example;
}
else
{
this.addon = findById(getId());
}
}
public Addon findById(Long id)
{
return this.entityManager.find(Addon.class, id);
}
/*
* Support updating and deleting Addon entities
*/
public String update()
{
this.conversation.end();
try
{
if (this.id == null)
{
this.entityManager.persist(this.addon);
return "search?faces-redirect=true";
}
else
{
this.entityManager.merge(this.addon);
return "view?faces-redirect=true&id=" + this.addon.getId();
}
}
catch (Exception e)
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
return null;
}
}
public String delete()
{
this.conversation.end();
try
{
Addon deletableEntity = findById(getId());
this.entityManager.remove(deletableEntity);
this.entityManager.flush();
return "search?faces-redirect=true";
}
catch (Exception e)
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
return null;
}
}
/*
* Support searching Addon entities with pagination
*/
private int page;
private long count;
private List<Addon> pageItems;
private Addon example = new Addon();
public int getPage()
{
return this.page;
}
public void setPage(int page)
{
this.page = page;
}
public int getPageSize()
{
return 10;
}
public Addon getExample()
{
return this.example;
}
public void setExample(Addon example)
{
this.example = example;
}
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<Addon> root = countCriteria.from(Addon.class);
countCriteria = countCriteria.select(builder.count(root)).where(
getSearchPredicates(root));
this.count = this.entityManager.createQuery(countCriteria)
.getSingleResult();
// Populate this.pageItems
CriteriaQuery<Addon> criteria = builder.createQuery(Addon.class);
root = criteria.from(Addon.class);
TypedQuery<Addon> 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<Addon> root)
{
CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
List<Predicate> predicatesList = new ArrayList<Predicate>();
String groupId = this.example.getGroupId();
if (groupId != null && !"".equals(groupId))
{
predicatesList.add(builder.like(builder.lower(root.<String> get("groupId")), '%' + groupId.toLowerCase() + '%'));
}
String artifactId = this.example.getArtifactId();
if (artifactId != null && !"".equals(artifactId))
{
predicatesList.add(builder.like(builder.lower(root.<String> get("artifactId")), '%' + artifactId.toLowerCase() + '%'));
}
return predicatesList.toArray(new Predicate[predicatesList.size()]);
}
public List<Addon> getPageItems()
{
return this.pageItems;
}
public long getCount()
{
return this.count;
}
/*
* Support listing and POSTing back Addon entities (e.g. from inside an
* HtmlSelectOneMenu)
*/
public List<Addon> getAll()
{
CriteriaQuery<Addon> criteria = this.entityManager
.getCriteriaBuilder().createQuery(Addon.class);
return this.entityManager.createQuery(
criteria.select(criteria.from(Addon.class))).getResultList();
}
@Resource
private SessionContext sessionContext;
public Converter getConverter()
{
final AddonBean ejbProxy = this.sessionContext.getBusinessObject(AddonBean.class);
return new Converter()
{
@Override
public Object getAsObject(FacesContext context,
UIComponent component, String value)
{
return ejbProxy.findById(Long.valueOf(value));
}
@Override
public String getAsString(FacesContext context,
UIComponent component, Object value)
{
if (value == null)
{
return "";
}
return String.valueOf(((Addon) value).getId());
}
};
}
/*
* Support adding children to bidirectional, one-to-many tables
*/
private Addon add = new Addon();
public Addon getAdd()
{
return this.add;
}
public Addon getAdded()
{
Addon added = this.add;
this.add = new Addon();
return added;
}
}