From: Roland Häder Date: Thu, 8 Jun 2017 21:09:00 +0000 (+0200) Subject: Continued a bit: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=37b3c360add2b29163effb56b1dff8836946e544;p=jfinancials-war.git Continued a bit: - renamed/changed receipt controller to session-scoped so the user's receipts can be cached locally Signed-off-by: Roland Häder --- 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 index 62dcccf9..00000000 --- a/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebRequestBean.java +++ /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 . - */ -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) - *

- * @author Roland Häder - */ -@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 index 67416d87..00000000 --- a/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebRequestController.java +++ /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 . - */ -package org.mxchange.jfinancials.beans.financial.receipt; - -import java.io.Serializable; - -/** - * An administrative interface for financial receipt beans - *

- * @author Roland Häder - */ -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 index 00000000..05c4268b --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebSessionBean.java @@ -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 . + */ +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) + *

+ * @author Roland Häder + */ +@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 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. + *

+ * @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 + *

+ * @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 + *

+ * @return A list of all payment types + */ + public List allPaymentTypes () { + return Arrays.asList(PaymentType.values()); + } + + /** + * Getter for payment type + *

+ * @return Payment type + */ + public PaymentType getPaymentType () { + return this.paymentType; + } + + /** + * Setter for payment type + *

+ * @param paymentType Payment type + */ + public void setPaymentType (final PaymentType paymentType) { + this.paymentType = paymentType; + } + + /** + * Getter for receipt issuing company + *

+ * @return Receipt issuing company + */ + public BusinessContact getReceiptCompany () { + return this.receiptCompany; + } + + /** + * Setter for receipt issuing company + *

+ * @param receiptCompany Receipt issuing company + */ + public void setReceiptCompany (final BusinessContact receiptCompany) { + this.receiptCompany = receiptCompany; + } + + /** + * Getter for receipt issue date and time + *

+ * @return Receipt issue date and time + */ + @SuppressWarnings ("ReturnOfDateField") + public Calendar getReceiptIssued () { + return this.receiptIssued; + } + + /** + * Setter for receipt issue date and time + *

+ * @param receiptIssued Receipt issue date and time + */ + @SuppressWarnings ("AssignmentToDateFieldFromParameter") + public void setReceiptIssued (final Calendar receiptIssued) { + this.receiptIssued = receiptIssued; + } + + /** + * Getter for receipt number + *

+ * @return Receipt number + */ + public Long getReceiptNumber () { + return this.receiptNumber; + } + + /** + * Setter for receipt number + *

+ * @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 + *

+ * @param receipt Prepared receipt instance + *

+ * @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 index 00000000..5d25e5a2 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/financial/receipt/FinancialsReceiptWebSessionController.java @@ -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 . + */ +package org.mxchange.jfinancials.beans.financial.receipt; + +import java.io.Serializable; +import javax.ejb.Local; + +/** + * An administrative interface for financial receipt beans + *

+ * @author Roland Häder + */ +@Local +public interface FinancialsReceiptWebSessionController extends Serializable { + +}