1 package info.ejava.examples.ejb.cdisales.web;
2
3 import java.io.Serializable;
4 import java.math.BigDecimal;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.List;
8 import java.util.Random;
9
10 import info.ejava.examples.ejb.cdisales.bl.ProductCatalog;
11 import info.ejava.examples.ejb.cdisales.bl.Tx;
12 import info.ejava.examples.ejb.cdisales.bo.CurrentUser;
13 import info.ejava.examples.ejb.cdisales.bo.Member;
14 import info.ejava.examples.ejb.cdisales.bo.Product;
15 import info.ejava.examples.ejb.cdisales.bo.ProductCategory;
16 import info.ejava.examples.ejb.cdisales.ejb.InvalidProduct;
17
18 import javax.annotation.PostConstruct;
19 import javax.annotation.PreDestroy;
20 import javax.enterprise.context.Conversation;
21 import javax.enterprise.context.ConversationScoped;
22 import javax.faces.component.UICommand;
23 import javax.faces.component.UIForm;
24 import javax.faces.model.SelectItem;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27
28 import org.slf4j.LoggerFactory;
29 import org.slf4j.Logger;
30
31 @SuppressWarnings("serial")
32
33 @Named("sellerController")
34 @ConversationScoped
35
36
37
38
39 public class SellerController implements Serializable {
40 private static final Logger logger = LoggerFactory.getLogger(SellerController.class);
41
42
43
44
45 @Inject
46 private ErrorController error;
47
48
49 @Inject
50 private Conversation conversation;
51
52
53
54
55
56 @Inject @Tx
57 private ProductCatalog catalog;
58
59
60
61
62
63
64
65
66
67 @Inject
68 private CurrentUser user;
69
70
71
72
73
74
75 private List<Product> products;
76
77
78
79
80
81 private Product product;
82
83
84
85
86
87
88
89
90
91
92
93 private UIForm form;
94
95
96
97
98
99
100 private UIForm tableForm;
101
102
103
104
105
106
107
108 private UICommand addCommand;
109
110 @PostConstruct
111 public void init() {
112 logger.debug("*** SellerController({}):init ***", super.hashCode());
113 conversation.begin();
114 }
115
116 @PreDestroy
117 public void destroy() {
118 logger.debug("*** SellerController({}):destroy ***", super.hashCode());
119 }
120
121
122 public Member getSeller() {
123 return user.getMember();
124 }
125 public List<Product> getProducts() {
126 logger.debug("getProducts()={}", products);
127 if (products==null) {
128 products = catalog.getSellerProducts(user.getMember(), 0, 0);
129 }
130 return products;
131 }
132 public void setProducts(List<Product> products) {
133 logger.debug("setProducts({})", products);
134 this.products=products;
135 }
136
137
138
139
140
141
142
143
144 public Product getProduct() {
145 logger.debug("getProduct()={}", product);
146 return product;
147 }
148 public void setProduct(Product product) {
149 logger.debug("setProduct({})", product);
150 this.product = product;
151 }
152 public List<SelectItem> getCategories() {
153 logger.debug("getCategories()");
154 List<SelectItem> list = new ArrayList<SelectItem>(ProductCategory.values().length);
155 for (ProductCategory pc: ProductCategory.values()) {
156 list.add(new SelectItem(pc, pc.getPrettyName()));
157 }
158 return list;
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 public UICommand getAddCommand() {
177 logger.debug("getAddCommand()={}", addCommand);
178 return addCommand;
179 }
180 public void setAddCommand(UICommand addCommand) {
181 logger.debug("setAddCommand(addCommand={})", addCommand);
182 this.addCommand = addCommand;
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public String addNew() {
198 logger.debug("addNew()");
199 this.product = new Product();
200
201
202
203 Random r = new Random();
204 product.setCategory(ProductCategory.values()[r.nextInt(ProductCategory.SPORT.ordinal())]);
205 product.setName("name" + r.nextInt(100));
206 product.setYear(1980 + r.nextInt(30));
207 product.setPrice(new BigDecimal(r.nextInt(10000)));
208 form.setRendered(true);
209 addCommand.setRendered(false);
210 return null;
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 public UIForm getForm() {
236 logger.debug("getForm()={}, rendered={}", form, form==null?null : form.isRendered());
237 return form;
238 }
239 public void setForm(UIForm form) {
240 logger.debug("setForm(form={}, rendered={})", form, form==null? null : form.isRendered());
241 this.form = form;
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 public String add() {
259 logger.debug("add(): product={}", product);
260 products.add(product);
261 Collections.sort(products, new Product.ProductASC());
262 form.setRendered(false);
263 addCommand.setRendered(true);
264 return "/seller/seller-products";
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 public UIForm getTableForm() {
299 logger.debug("getTableForm()={}, rendered={}", tableForm, tableForm==null?null:tableForm.isRendered());
300 return tableForm;
301 }
302 public void setTableForm(UIForm tableForm) {
303 logger.debug("setTableForm(tableForm={}, rendered={})", tableForm, tableForm==null ? null : tableForm.isRendered());
304 this.tableForm = tableForm;
305 }
306
307
308
309
310
311
312
313
314 public String delete() {
315 logger.debug("delete(): product={}", product);
316 products.remove(product);
317 catalog.remove(product);
318 return null;
319 }
320
321 public String save() {
322 logger.debug("save", product);
323
324 product.setSeller(user.getMember());
325
326 products.remove(product);
327 try {
328 product = catalog.addProduct(product);
329
330 products.add(product);
331 Collections.sort(products, new Product.ProductASC());
332 return null;
333 } catch (InvalidProduct ex) {
334 String errorMsg = "error saving product:" + product;
335 error.setError(errorMsg);
336 error.setException(ex);
337 return "error";
338 }
339 }
340 }