]> git.mxchange.org Git - jfinancials-war.git/blob
1ee5fbda4714049770ba1d3b7850a79340f413bb
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jfinancials.beans.financial.model.receipt;
18
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Comparator;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Objects;
27 import javax.annotation.PostConstruct;
28 import javax.cache.Cache;
29 import javax.ejb.EJB;
30 import javax.enterprise.context.RequestScoped;
31 import javax.enterprise.event.Event;
32 import javax.enterprise.event.Observes;
33 import javax.enterprise.inject.Any;
34 import javax.faces.view.facelets.FaceletException;
35 import javax.inject.Inject;
36 import javax.inject.Named;
37 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
38 import org.mxchange.jcontactsbusiness.model.employee.Employable;
39 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
40 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
41 import org.mxchange.jfinancials.events.receipt.added.ObservableReceiptAddedEvent;
42 import org.mxchange.jfinancials.events.receipt.added.ReceiptAddedEvent;
43 import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException;
44 import org.mxchange.jfinancials.exceptions.receipt.ReceiptNotFoundException;
45 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
46 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
47 import org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote;
48 import org.mxchange.jproduct.model.payment.PaymentType;
49 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
50
51 /**
52  * A general financial receipt bean (controller)
53  * <p>
54  * @author Roland Häder<roland@mxchange.org>
55  */
56 @Named ("receiptController")
57 @RequestScoped
58 public class FinancialsReceiptWebRequestBean extends BaseFinancialsBean implements FinancialsReceiptWebRequestController {
59
60         /**
61          * Serial number
62          */
63         private static final long serialVersionUID = 56_189_028_928_371L;
64
65         /**
66          * Event being fired when user has added a new receipt
67          */
68         @Inject
69         @Any
70         private Event<ObservableReceiptAddedEvent> addedReceiptEvent;
71
72         /**
73          * All receipts list
74          */
75         private final List<BillableReceipt> allReceipts;
76
77         /**
78          * Filtered receipts list
79          */
80         private List<BillableReceipt> filteredReceipts;
81
82         /**
83          * Bar-code number
84          */
85         private Long receiptBarCodeNumber;
86
87         /**
88          * EJB for general financial receipt purposes
89          */
90         @EJB (lookup = "java:global/jfinancials-ejb/financialReceipt!org.mxchange.jfinancials.model.receipt.FinancialReceiptSessionBeanRemote")
91         private FinancialReceiptSessionBeanRemote receiptBean;
92
93         /**
94          * Recipient issuing company (for example where the user went shopping to)
95          */
96         private BranchOffice receiptBranchOffice;
97
98         /**
99          * A list of all branch offices (globally)
100          */
101         @Inject
102         @NamedCache (cacheName = "receiptCache")
103         private Cache<Long, BillableReceipt> receiptCache;
104
105         /**
106          * Date/time the receipt has been issued
107          */
108         private Date receiptIssued;
109
110         /**
111          * Receipt number (only numbers)
112          */
113         private Long receiptNumber;
114
115         /**
116          * Payment type being used for this receipt
117          */
118         private PaymentType receiptPaymentType;
119
120         /**
121          * Register number
122          */
123         private Long receiptRegisterNumber;
124
125         /**
126          * Selling employee
127          */
128         private Employable receiptSellerEmployee;
129
130         /**
131          * User instance
132          */
133         @Inject
134         private FinancialsUserLoginWebSessionController userLoginController;
135
136         /**
137          * User's receipts
138          */
139         private final List<BillableReceipt> userReceipts;
140
141         /**
142          * Constructor
143          */
144         @SuppressWarnings ("CollectionWithoutInitialCapacity")
145         public FinancialsReceiptWebRequestBean () {
146                 // Call super constructor
147                 super();
148
149                 // Init lists
150                 this.allReceipts = new LinkedList<>();
151                 this.userReceipts = new LinkedList<>();
152         }
153
154         /**
155          * Adds the completed receipt to database by calling an EJB business method.
156          * If not all required fields are set, a proper exception is being thrown.
157          * <p>
158          * @return Link outcome
159          */
160         public String addReceipt () {
161                 // Are all required fields set?
162                 if (!this.userLoginController.isUserLoggedIn()) {
163                         // Not logged-in
164                         throw new IllegalStateException("User is not logged-in"); //NOI18N
165                 } else if (this.getReceiptBranchOffice() == null) {
166                         // Is not set
167                         throw new NullPointerException("this.receiptBranchOffice is not set."); //NOI18N
168                 } else if (this.getReceiptBranchOffice().getBranchId() == null) {
169                         // It must be an already peristed instance
170                         throw new NullPointerException("this.receiptBranchOffice.businessContactId is not set."); //NOI18N
171                 } else if (this.getReceiptBranchOffice().getBranchId() < 1) {
172                         // It must be an already peristed instance
173                         throw new IllegalArgumentException(MessageFormat.format("this.receiptBranchOffice.businessContactId={0} is not valid.", this.getReceiptBranchOffice().getBranchId())); //NOI18N
174                 } else if (this.getReceiptPaymentType() == null) {
175                         // Is not set
176                         throw new NullPointerException("this.receiptPaymentType is not set."); //NOI18N
177                 } else if (this.getReceiptIssued() == null) {
178                         // Is not set
179                         throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
180                 }
181
182                 // Prepare receipt instance
183                 final BillableReceipt receipt = this.createReceiptInstance();
184
185                 // Is the receipt already there?
186                 if (this.isReceiptAdded(receipt)) {
187                         // Receipt has already been added
188                         throw new FaceletException(MessageFormat.format("Receipt for receiptBranchOffice={0},receiptIssued={1},receiptNumber={2} has already been added.", this.getReceiptBranchOffice().getBranchId(), this.getReceiptIssued().toString(), this.getReceiptNumber())); //NOI18N
189                 }
190
191                 // Init variable
192                 final BillableReceipt updatedReceipt;
193
194                 // All is set, then try to call EJB
195                 try {
196                         // Add it
197                         updatedReceipt = this.receiptBean.addReceipt(receipt);
198                 } catch (final ReceiptAlreadyAddedException ex) {
199                         // Throw it again
200                         throw new FaceletException(ex);
201                 }
202
203                 // Fire event with updated instance
204                 this.addedReceiptEvent.fire(new ReceiptAddedEvent(updatedReceipt));
205
206                 // Clear bean
207                 this.clear();
208
209                 // Return redirect outcome
210                 return "add_receipt_item?faces-redirect=true"; //NOI18N
211         }
212
213         /**
214          * Observes events being fired when a new receipt has been added
215          * <p>
216          * @param event Event being fired
217          */
218         public void afterAddedReceiptEvent (@Observes final ObservableReceiptAddedEvent event) {
219                 // Validate parameter
220                 if (null == event) {
221                         // Throw NPE
222                         throw new NullPointerException("event is null"); //NOI18N
223                 } else if (event.getReceipt() == null) {
224                         // Throw NPE again
225                         throw new NullPointerException("event.receipt is null"); //NOI18N
226                 } else if (event.getReceipt().getReceiptId() == null) {
227                         // Throw it again
228                         throw new NullPointerException("event.receipt.receiptId is null"); //NOI18N
229                 } else if (event.getReceipt().getReceiptId() < 1) {
230                         // Throw IAE
231                         throw new IllegalArgumentException(MessageFormat.format("event.receipt.receiptId={0} is invalid", event.getReceipt().getReceiptId())); //NOI18N
232                 }
233
234                 // Add to cache and general list
235                 this.receiptCache.put(event.getReceipt().getReceiptId(), event.getReceipt());
236                 this.allReceipts.add(event.getReceipt());
237
238                 // Is same user?
239                 if (this.userLoginController.isUserLoggedIn() && Objects.equals(event.getReceipt().getReceiptUser(), this.userLoginController.getLoggedInUser())) {
240                         // Same user, then add it
241                         this.userReceipts.add(event.getReceipt());
242                 }
243         }
244
245         /**
246          * Event observer for logged-in user
247          * <p>
248          * @param event Event instance
249          */
250         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
251                 // event should not be null
252                 if (null == event) {
253                         // Throw NPE
254                         throw new NullPointerException("event is null"); //NOI18N
255                 } else if (event.getLoggedInUser() == null) {
256                         // Throw NPE again
257                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
258                 } else if (event.getLoggedInUser().getUserId() == null) {
259                         // userId is null
260                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
261                 } else if (event.getLoggedInUser().getUserId() < 1) {
262                         // Not avalid id
263                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
264                 }
265
266                 // Fill cache with all found user's receipts
267                 for (final BillableReceipt receipt : this.allReceipts) {
268                         // Is same user?
269                         if (Objects.equals(receipt.getReceiptUser(), event.getLoggedInUser())) {
270                                 // Yes, then add it
271                                 this.userReceipts.add(receipt);
272                         }
273                 }
274         }
275
276         /**
277          * Returns all receipts
278          * <p>
279          * @return All receipts
280          */
281         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
282         public List<BillableReceipt> allReceipts () {
283                 return this.allReceipts;
284         }
285
286         @Override
287         public BillableReceipt findReceiptById (final Long receiptId) throws ReceiptNotFoundException {
288                 // Validate parameter
289                 if (null == receiptId) {
290                         // Throw NPE
291                         throw new NullPointerException("receiptId is null"); //NOI18N
292                 } else if (receiptId < 1) {
293                         // Throw IAE
294                         throw new IllegalArgumentException("receiptId=" + receiptId + " is invalid."); //NOI18N
295                 } else if (!this.receiptCache.containsKey(receiptId)) {
296                         // Not found
297                         throw new ReceiptNotFoundException(receiptId);
298                 }
299
300                 // Get it from cache
301                 final BillableReceipt receipt = this.receiptCache.get(receiptId);
302
303                 // Return it
304                 return receipt;
305         }
306
307         /**
308          * Getter for filtered receipts
309          * <p>
310          * @return Filtered receipts
311          */
312         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
313         public List<BillableReceipt> getFilteredReceipts () {
314                 return this.filteredReceipts;
315         }
316
317         /**
318          * Setter for filtered receipts
319          * <p>
320          * @param filteredReceipts Filtered receipts
321          */
322         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
323         public void setFilteredReceipts (final List<BillableReceipt> filteredReceipts) {
324                 this.filteredReceipts = filteredReceipts;
325         }
326
327         /**
328          * Getter for receipt's bar-code number
329          * <p>
330          * @return Receipt's bar-code number
331          */
332         public Long getReceiptBarCodeNumber () {
333                 return this.receiptBarCodeNumber;
334         }
335
336         /**
337          * Setter for receipt's bar-code number
338          * <p>
339          * @param receiptBarCodeNumber Receipt's bar-code number
340          */
341         public void setReceiptBarCodeNumber (final Long receiptBarCodeNumber) {
342                 this.receiptBarCodeNumber = receiptBarCodeNumber;
343         }
344
345         /**
346          * Getter for receipt issuing branch office
347          * <p>
348          * @return Receipt issuing branch office
349          */
350         public BranchOffice getReceiptBranchOffice () {
351                 return this.receiptBranchOffice;
352         }
353
354         /**
355          * Setter for receipt issuing branch office
356          * <p>
357          * @param receiptBranchOffice Receipt issuing branch office
358          */
359         public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) {
360                 this.receiptBranchOffice = receiptBranchOffice;
361         }
362
363         /**
364          * Getter for receipt issue date and time
365          * <p>
366          * @return Receipt issue date and time
367          */
368         @SuppressWarnings ("ReturnOfDateField")
369         public Date getReceiptIssued () {
370                 return this.receiptIssued;
371         }
372
373         /**
374          * Setter for receipt issue date and time
375          * <p>
376          * @param receiptIssued Receipt issue date and time
377          */
378         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
379         public void setReceiptIssued (final Date receiptIssued) {
380                 this.receiptIssued = receiptIssued;
381         }
382
383         /**
384          * Getter for receipt number
385          * <p>
386          * @return Receipt number
387          */
388         public Long getReceiptNumber () {
389                 return this.receiptNumber;
390         }
391
392         /**
393          * Setter for receipt number
394          * <p>
395          * @param receiptNumber Receipt number
396          */
397         public void setReceiptNumber (final Long receiptNumber) {
398                 this.receiptNumber = receiptNumber;
399         }
400
401         /**
402          * Getter for payment type
403          * <p>
404          * @return Payment type
405          */
406         public PaymentType getReceiptPaymentType () {
407                 return this.receiptPaymentType;
408         }
409
410         /**
411          * Setter for payment type
412          * <p>
413          * @param receiptPaymentType Payment type
414          */
415         public void setReceiptPaymentType (final PaymentType receiptPaymentType) {
416                 this.receiptPaymentType = receiptPaymentType;
417         }
418
419         /**
420          * Getter for receipt register's number
421          * <p>
422          * @return Receipt register's number
423          */
424         public Long getReceiptRegisterNumber () {
425                 return this.receiptRegisterNumber;
426         }
427
428         /**
429          * Setter for receipt register's number
430          * <p>
431          * @param receiptRegisterNumber Receipt register's number
432          */
433         public void setReceiptRegisterNumber (final Long receiptRegisterNumber) {
434                 this.receiptRegisterNumber = receiptRegisterNumber;
435         }
436
437         /**
438          * Getter for receipt seller employee
439          * <p>
440          * @return Receipt seller employee
441          */
442         public Employable getReceiptSellerEmployee () {
443                 return this.receiptSellerEmployee;
444         }
445
446         /**
447          * Setter for receipt seller employee
448          * <p>
449          * @param receiptSellerEmployee Receipt seller employee
450          */
451         public void setReceiptSellerEmployee (final Employable receiptSellerEmployee) {
452                 this.receiptSellerEmployee = receiptSellerEmployee;
453         }
454
455         @PostConstruct
456         public void initCache () {
457                 // Is cache there?
458                 if (!this.receiptCache.iterator().hasNext()) {
459                         // Get whole list
460                         final List<BillableReceipt> receipts = this.receiptBean.allReceipts();
461
462                         // Add all
463                         for (final BillableReceipt receipt : receipts) {
464                                 // Add it to cache
465                                 this.receiptCache.put(receipt.getReceiptId(), receipt);
466                         }
467                 }
468
469                 // Is the list empty, but filled cache?
470                 if (this.allReceipts.isEmpty() && this.receiptCache.iterator().hasNext()) {
471                         // Get iterator
472                         final Iterator<Cache.Entry<Long, BillableReceipt>> iterator = this.receiptCache.iterator();
473
474                         // Build up list
475                         while (iterator.hasNext()) {
476                                 // GEt next element
477                                 final Cache.Entry<Long, BillableReceipt> next = iterator.next();
478
479                                 // Add to list
480                                 this.allReceipts.add(next.getValue());
481                         }
482
483                         // Sort list
484                         this.allReceipts.sort(new Comparator<BillableReceipt>() {
485                                 @Override
486                                 public int compare (final BillableReceipt o1, final BillableReceipt o2) {
487                                         return o1.getReceiptId() > o2.getReceiptId() ? 1 : o1.getReceiptId() < o2.getReceiptId() ? -1 : 0;
488                                 }
489                         }
490                         );
491                 }
492         }
493
494         @Override
495         public boolean isReceiptAdded (final BillableReceipt receipt) {
496                 // Always trust the cache
497                 return this.allReceipts.contains(receipt);
498         }
499
500         /**
501          * Clears this bean
502          */
503         private void clear () {
504                 // Clear all fields
505                 this.setReceiptBarCodeNumber(null);
506                 this.setReceiptBranchOffice(null);
507                 this.setReceiptIssued(null);
508                 this.setReceiptNumber(null);
509                 this.setReceiptPaymentType(null);
510                 this.setReceiptRegisterNumber(null);
511                 this.setReceiptSellerEmployee(null);
512         }
513
514         /**
515          * Returns a fully created receipt instance (except primary key, of course)
516          * <p>
517          * @return Receipt instance
518          */
519         private BillableReceipt createReceiptInstance () {
520                 // Init receipt instance
521                 final BillableReceipt receipt = new FinancialReceipt(this.getReceiptPaymentType(), this.getReceiptBranchOffice(), this.userLoginController.getLoggedInUser(), this.getReceiptIssued());
522
523                 // Set optional fields
524                 receipt.setReceiptNumber(this.getReceiptNumber());
525                 receipt.setReceiptBarCodeNumber(this.getReceiptBarCodeNumber());
526                 receipt.setReceiptRegisterNumber(this.getReceiptRegisterNumber());
527                 receipt.setReceiptSellerEmployee(this.getReceiptSellerEmployee());
528
529                 // Return it
530                 return receipt;
531         }
532 }