From df37ae229c741e44eb42092c91a2a180c1db5598 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 19 Sep 2017 22:36:12 +0200 Subject: [PATCH] Don't cherry-pick: - added EJB for receipts with all current business methods implemented MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../receipt/FinancialReceiptSessionBean.java | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 src/java/org/mxchange/jfinancials/model/receipt/FinancialReceiptSessionBean.java diff --git a/src/java/org/mxchange/jfinancials/model/receipt/FinancialReceiptSessionBean.java b/src/java/org/mxchange/jfinancials/model/receipt/FinancialReceiptSessionBean.java new file mode 100644 index 0000000..293a321 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/model/receipt/FinancialReceiptSessionBean.java @@ -0,0 +1,185 @@ +/* + * Copyright (C) 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.model.receipt; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Objects; +import javax.ejb.Stateless; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jfinancials.exceptions.ReceiptAlreadyAddedException; +import org.mxchange.jusercore.model.user.User; + +/** + * A stateless bean for general purposes for receipts + *

+ * @author Roland Häder + */ +@Stateless (name = "financialReceipt", description = "A stateless session bean for handling receipts.") +public class FinancialReceiptSessionBean extends BaseFinancialsDatabaseBean implements FinancialReceiptSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 659_868_076_723_741L; + + @Override + public BillableReceipt addReceipt (final BillableReceipt receipt) throws ReceiptAlreadyAddedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addReceipt(): receipt={1} - CALLED!", this.getClass().getSimpleName(), receipt)); + + // Validate parameter + if (null == receipt) { + // Throw NPE + throw new NullPointerException("receipt is null"); + } else if (receipt.getReceiptId() != null) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receipt.receiptId={0} is not expected.", receipt.getReceiptId())); + } else if (receipt.getReceiptIssued() == null) { + // Throw NPE again + throw new NullPointerException("receipt.receiptIssued is null"); + } else if (receipt.getReceiptBranchOffice() == null) { + // Throw it again + throw new NullPointerException("receipt.receiptBranchOffice is null"); + } else if (receipt.getReceiptBranchOffice().getBranchId() == null) { + // Throw it again + throw new NullPointerException("receipt.receiptBranchOffice.branchId is null"); + } else if (receipt.getReceiptBranchOffice().getBranchId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receipt.receiptBranchOffice.branchId={0} is not valid.", receipt.getReceiptBranchOffice().getBranchId())); + } else if (receipt.getReceiptPaymentType() == null) { + // Throw NPE + throw new NullPointerException("receipt.receiptPaymentType is null"); + } else if ((receipt.getReceiptUser() instanceof User) && receipt.getReceiptUser().getUserId() == null) { + // Throw NPE again + throw new NullPointerException("receipt.receiptUser.userId is null"); + } else if ((receipt.getReceiptUser() instanceof User) && receipt.getReceiptUser().getUserId() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("receipt.receiptUser.userId={0} is not valid", receipt.getReceiptUser().getUserId())); + } else if (this.isReceiptRegistered(receipt)) { + // Has already been registered + throw new ReceiptAlreadyAddedException(receipt); + } + + // Add created instance + receipt.setReceiptCreated(new GregorianCalendar()); + + // Persist it + this.getEntityManager().persist(receipt); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addReceipt(): receipt.receiptId={1} - EXIT!", this.getClass().getSimpleName(), receipt.getReceiptId())); + + // Return it + return receipt; + } + + @Override + @SuppressWarnings ("unchecked") + public List allReceipts () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allReceipts(): CALLED!", this.getClass().getSimpleName())); + + // Query all + final Query query = this.getEntityManager().createNamedQuery("AllReceipts"); + + // Get all + final List receipts = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allReceipts(): receipts.size()={1} EXIT!", this.getClass().getSimpleName())); + + // Return it + return receipts; + } + + @Override + @SuppressWarnings ("unchecked") + public List allUsersReceipts (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsersReceipts(): user={1} - CALLED!", this.getClass().getSimpleName(), user)); + + // Validate parameter + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); + } else if (user.getUserId() == null) { + // Throw it again + throw new NullPointerException("user.userId is null"); + } else if (user.getUserId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); + } + + // Query all + final Query query = this.getEntityManager().createNamedQuery("SearchAllUserReceipts"); + + // Add parameter + query.setParameter("receiptUser", user); + + // Get all + final List receipts = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsersReceipts(): receipts.size()={1} EXIT!", this.getClass().getSimpleName())); + + // Return it + return receipts; + } + + /** + * Checks if given receipt is already persisted by checking receipt number + * and branch office combination. + *

+ * @param receipt Receipt instance + *

+ * @return Whether the receipt has already been registered + */ + private boolean isReceiptRegistered (final BillableReceipt receipt) { + // Get all receipts + final List receipts = this.allReceipts(); + + // Is the list empty? + if (receipts.isEmpty()) { + // Abort here + return false; + } + + // Default is not found + boolean isFound = false; + + // Now, check each entry + for (final BillableReceipt foundReceipt : receipts) { + // Is same entity or same receipt number and branch office found? + if (Objects.equals(foundReceipt, receipt)) { + // Yes, then stop searching + isFound = true; + break; + } else if (Receipts.isSameReceipt(foundReceipt, receipt)){ + // Yes, then stop searching + isFound = true; + break; + } + } + + // Return flag + return isFound; + } + +} -- 2.39.5