]> git.mxchange.org Git - jfinancials-war.git/commitdiff
Continued a bit:
authorRoland Häder <roland@mxchange.org>
Thu, 8 Jun 2017 21:09:00 +0000 (23:09 +0200)
committerRoland Häder <roland@mxchange.org>
Thu, 8 Jun 2017 21:09:00 +0000 (23:09 +0200)
- renamed/changed receipt controller to session-scoped so the user's receipts can be cached locally

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebRequestBean.java [deleted file]
src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebRequestController.java [deleted file]
src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebSessionBean.java [new file with mode: 0644]
src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebSessionController.java [new file with mode: 0644]

diff --git a/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebRequestBean.java
deleted file mode 100644 (file)
index 62dcccf..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright (C) 2016, 2017 Roland Häder
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jfinancials.beans.financial.receipt;
-
-import javax.annotation.PostConstruct;
-import javax.enterprise.context.RequestScoped;
-import javax.faces.view.facelets.FaceletException;
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import org.mxchange.jfinancials.beans.BaseFinancialsController;
-import org.mxchange.jfinancials.beans.login.user.FinancialsUserLoginWebSessionController;
-import org.mxchange.jfinancials.financial.receipt.FinancialReceiptSessionBeanRemote;
-
-/**
- * An administrative financial receipt bean (controller)
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-@Named ("financialReceiptController")
-@RequestScoped
-public class FinancialsReceiptWebRequestBean extends BaseFinancialsController implements FinancialsReceiptWebRequestController {
-
-       /**
-        * Serial number
-        */
-       private static final long serialVersionUID = 56_189_028_928_371L;
-
-       /**
-        * Remote contact bean
-        */
-       private FinancialReceiptSessionBeanRemote financialBean;
-
-       /**
-        * User instance
-        */
-       @Inject
-       private FinancialsUserLoginWebSessionController userLoginController;
-
-       /**
-        * Constructor
-        */
-       public FinancialsReceiptWebRequestBean () {
-               // Call super constructor
-               super();
-       }
-
-       /**
-        * Post-initialization of this class
-        */
-       @PostConstruct
-       public void init () {
-               // Try it
-               try {
-                       // Get initial context
-                       Context context = new InitialContext();
-
-                       // Try to lookup
-                       this.financialBean = (FinancialReceiptSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/financial!org.mxchange.jfinancials.financial.receipt.FinancialReceiptSessionBeanRemote"); //NOI18N
-               } catch (final NamingException e) {
-                       // Throw again
-                       throw new FaceletException(e);
-               }
-       }
-
-}
diff --git a/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebRequestController.java
deleted file mode 100644 (file)
index 67416d8..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (C) 2016, 2017 Roland Häder
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jfinancials.beans.financial.receipt;
-
-import java.io.Serializable;
-
-/**
- * An administrative interface for financial receipt beans
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-public interface FinancialsReceiptWebRequestController extends Serializable {
-
-}
diff --git a/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebSessionBean.java
new file mode 100644 (file)
index 0000000..05c4268
--- /dev/null
@@ -0,0 +1,305 @@
+/*
+ * Copyright (C) 2016, 2017 Roland Häder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jfinancials.beans.financial.receipt;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.List;
+import javax.annotation.PostConstruct;
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Observes;
+import javax.faces.view.facelets.FaceletException;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcontactsbusiness.BusinessContact;
+import org.mxchange.jfinancials.beans.BaseFinancialsController;
+import org.mxchange.jfinancials.beans.login.user.FinancialsUserLoginWebSessionController;
+import org.mxchange.jfinancials.exceptions.ReceiptAlreadyAddedException;
+import org.mxchange.jfinancials.financial.receipt.FinancialReceiptSessionBeanRemote;
+import org.mxchange.jfinancials.model.receipt.BillableReceipt;
+import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
+import org.mxchange.jproduct.model.payment.PaymentType;
+import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent;
+
+/**
+ * An administrative financial receipt bean (controller)
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@Named ("financialReceiptController")
+@SessionScoped
+public class FinancialsReceiptWebSessionBean extends BaseFinancialsController implements FinancialsReceiptWebSessionController {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 56_189_028_928_371L;
+
+       /**
+        * Remote contact bean
+        */
+       private FinancialReceiptSessionBeanRemote financialBean;
+
+       /**
+        * Payment type being used for this receipt
+        */
+       private PaymentType paymentType;
+
+       /**
+        * Recipient issuing company (for example where the user went shopping to)
+        */
+       private BusinessContact receiptCompany;
+
+       /**
+        * Date/time the receipt has been issued
+        */
+       private Calendar receiptIssued;
+
+       /**
+        * Receipt number (only numbers)
+        */
+       private Long receiptNumber;
+
+       /**
+        * Cached receipts
+        */
+       private final List<BillableReceipt> receipts;
+
+       /**
+        * User instance
+        */
+       @Inject
+       private FinancialsUserLoginWebSessionController userLoginController;
+
+       /**
+        * Constructor
+        */
+       @SuppressWarnings ("CollectionWithoutInitialCapacity")
+       public FinancialsReceiptWebSessionBean () {
+               // Call super constructor
+               super();
+
+               // Init cache
+               this.receipts = new ArrayList<>();
+       }
+
+       /**
+        * Adds the completed receipt to database by calling an EJB business method.
+        * If not all required fields are set, a proper exception is being thrown.
+        * <p>
+        * @return Link outcome
+        */
+       public String addReceipt () {
+               // Pre-check if receipt number is set
+               if (this.getReceiptNumber() == null || this.getReceiptNumber() == 0) {
+                       // Generate one randomly
+                       Long randomNumber = Math.round(Math.random() * 1_000_000);
+
+                       // And set it here
+                       this.setReceiptNumber(randomNumber);
+               }
+
+               // Are all required fields set?
+               if (!this.userLoginController.isUserLoggedIn()) {
+                       // Not logged-in
+                       throw new IllegalStateException("User is not logged-in"); //NOI18N
+               } else if (this.getReceiptCompany() == null) {
+                       // Is not set
+                       throw new NullPointerException("this.receiptCompany is not set."); //NOI18N
+               } else if (this.getReceiptCompany().getBusinessContactId() == null) {
+                       // It must be an already peristed instance
+                       throw new NullPointerException("this.receiptCompany.businessContactId is not set."); //NOI18N
+               } else if (this.getReceiptCompany().getBusinessContactId() < 1) {
+                       // It must be an already peristed instance
+                       throw new IllegalArgumentException(MessageFormat.format("this.receiptCompany.businessContactId={0} is not valid.", this.getReceiptCompany().getBusinessContactId())); //NOI18N
+               } else if (this.getPaymentType() == null) {
+                       // Is not set
+                       throw new NullPointerException("this.paymentType is not set."); //NOI18N
+               } else if (this.getReceiptIssued() == null) {
+                       // Is not set
+                       throw new NullPointerException("this.receiptIssued is not set."); //NOI18N
+               }
+
+               // Prepare receipt instance
+               BillableReceipt receipt = new FinancialReceipt(this.getPaymentType(), this.getReceiptCompany(), this.userLoginController.getLoggedInUser(), this.getReceiptIssued());
+
+               if (this.isReceiptAdded(receipt)) {
+                       // Receipt has already been added
+                       throw new FaceletException(MessageFormat.format("Receipt for receiptCompany={0},receiptIssued={1},receiptNumber={2} has already been added.", this.getReceiptCompany().getBusinessContactId(), this.getReceiptIssued().toString(), this.getReceiptNumber())); //NOI18N
+               }
+
+               // Init variable
+               BillableReceipt updatedReceipt;
+
+               // All is set, then try to call EJB
+               try {
+                       // Add it
+                       updatedReceipt = this.financialBean.addReceipt(receipt);
+               } catch (final ReceiptAlreadyAddedException ex) {
+                       // Throw it again
+                       throw new FaceletException(ex);
+               }
+
+               // @TODO Maybe fire event here?
+               // Return redirect outcome
+               return "add_receipt_item?faces-redirect=true"; //NOI18N
+       }
+
+       /**
+        * Event observer for logged-in user
+        * <p>
+        * @param event Event instance
+        */
+       public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
+               // event should not be null
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getLoggedInUser() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.loggedInUser is null"); //NOI18N
+               } else if (event.getLoggedInUser().getUserId() == null) {
+                       // userId is null
+                       throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
+               } else if (event.getLoggedInUser().getUserId() < 1) {
+                       // Not avalid id
+                       throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
+               }
+
+               // Fill cache with all found user's receipts
+               this.receipts.addAll(this.financialBean.allUsersReceipts(this.userLoginController.getLoggedInUser()));
+       }
+
+       /**
+        * Returns a list of all payment types
+        * <p>
+        * @return A list of all payment types
+        */
+       public List<PaymentType> allPaymentTypes () {
+               return Arrays.asList(PaymentType.values());
+       }
+
+       /**
+        * Getter for payment type
+        * <p>
+        * @return Payment type
+        */
+       public PaymentType getPaymentType () {
+               return this.paymentType;
+       }
+
+       /**
+        * Setter for payment type
+        * <p>
+        * @param paymentType Payment type
+        */
+       public void setPaymentType (final PaymentType paymentType) {
+               this.paymentType = paymentType;
+       }
+
+       /**
+        * Getter for receipt issuing company
+        * <p>
+        * @return Receipt issuing company
+        */
+       public BusinessContact getReceiptCompany () {
+               return this.receiptCompany;
+       }
+
+       /**
+        * Setter for receipt issuing company
+        * <p>
+        * @param receiptCompany Receipt issuing company
+        */
+       public void setReceiptCompany (final BusinessContact receiptCompany) {
+               this.receiptCompany = receiptCompany;
+       }
+
+       /**
+        * Getter for receipt issue date and time
+        * <p>
+        * @return Receipt issue date and time
+        */
+       @SuppressWarnings ("ReturnOfDateField")
+       public Calendar getReceiptIssued () {
+               return this.receiptIssued;
+       }
+
+       /**
+        * Setter for receipt issue date and time
+        * <p>
+        * @param receiptIssued Receipt issue date and time
+        */
+       @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+       public void setReceiptIssued (final Calendar receiptIssued) {
+               this.receiptIssued = receiptIssued;
+       }
+
+       /**
+        * Getter for receipt number
+        * <p>
+        * @return Receipt number
+        */
+       public Long getReceiptNumber () {
+               return this.receiptNumber;
+       }
+
+       /**
+        * Setter for receipt number
+        * <p>
+        * @param receiptNumber Receipt number
+        */
+       public void setReceiptNumber (final Long receiptNumber) {
+               this.receiptNumber = receiptNumber;
+       }
+
+       /**
+        * Post-initialization of this class
+        */
+       @PostConstruct
+       public void init () {
+               // Try it
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // Try to lookup
+                       this.financialBean = (FinancialReceiptSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/financial!org.mxchange.jfinancials.financial.receipt.FinancialReceiptSessionBeanRemote"); //NOI18N
+               } catch (final NamingException e) {
+                       // Throw again
+                       throw new FaceletException(e);
+               }
+       }
+
+       /**
+        * Checks if receipt has already been added to database
+        * <p>
+        * @param receipt Prepared receipt instance
+        * <p>
+        * @return Whether the receipt has already been added
+        */
+       private boolean isReceiptAdded (final BillableReceipt receipt) {
+               // Always trust the cache
+               return this.receipts.contains(receipt);
+       }
+
+}
diff --git a/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebSessionController.java b/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebSessionController.java
new file mode 100644 (file)
index 0000000..5d25e5a
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2016, 2017 Roland Häder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jfinancials.beans.financial.receipt;
+
+import java.io.Serializable;
+import javax.ejb.Local;
+
+/**
+ * An administrative interface for financial receipt beans
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@Local
+public interface FinancialsReceiptWebSessionController extends Serializable {
+
+}