]> git.mxchange.org Git - jfinancials-ejb.git/commitdiff
Don't cherry-pick:
authorRoland Häder <roland@mxchange.org>
Tue, 19 Sep 2017 20:36:12 +0000 (22:36 +0200)
committerRoland Häder <roland@mxchange.org>
Tue, 19 Sep 2017 20:36:12 +0000 (22:36 +0200)
- added EJB for receipts with all current business methods implemented

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jfinancials/model/receipt/FinancialReceiptSessionBean.java [new file with mode: 0644]

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 (file)
index 0000000..293a321
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+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
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@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<BillableReceipt> 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<BillableReceipt> 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<BillableReceipt> 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<BillableReceipt> 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.
+        * <p>
+        * @param receipt Receipt instance
+        * <p>
+        * @return Whether the receipt has already been registered
+        */
+       private boolean isReceiptRegistered (final BillableReceipt receipt) {
+               // Get all receipts
+               final List<BillableReceipt> 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;
+       }
+
+}