From 91566583222e7d9f4dc3117f29e86c709b08b6b0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 17 Oct 2017 23:41:17 +0200 Subject: [PATCH] Don't cherry-pick: - added EJBs for administrative and general purposes for receipt items - implemented all business methods MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../FinancialAdminReceiptItemSessionBean.java | 127 ++++++++++++ .../FinancialReceiptItemSessionBean.java | 186 ++++++++++++++++++ 2 files changed, 313 insertions(+) create mode 100644 src/java/org/mxchange/jfinancials/model/receipt_item/FinancialAdminReceiptItemSessionBean.java create mode 100644 src/java/org/mxchange/jfinancials/model/receipt_item/FinancialReceiptItemSessionBean.java diff --git a/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialAdminReceiptItemSessionBean.java b/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialAdminReceiptItemSessionBean.java new file mode 100644 index 0000000..c01ea24 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialAdminReceiptItemSessionBean.java @@ -0,0 +1,127 @@ +/* + * 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_item; + +import java.text.MessageFormat; +import java.util.Date; +import java.util.List; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException; +import org.mxchange.jfinancials.model.receipt.item.BillableReceiptItem; +import org.mxchange.jfinancials.model.receipt.item.ReceiptItems; + +/** + * A stateless bean for general purposes for receipt items + *

+ * @author Roland Häder + */ +@Stateless (name = "adminFinancialReceiptItem", description = "A stateless session bean for administrative purposes for receipt itemss.") +public class FinancialAdminReceiptItemSessionBean extends BaseFinancialsDatabaseBean implements FinancialAdminReceiptItemSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 659_868_076_723_743L; + + /** + * General EJB + */ + @EJB (lookup = "java:global/jfinancials-ejb/financialReceiptItem!org.mxchange.jfinancials.model.receipt.item.FinancialReceiptItemSessionBeanRemote") + private FinancialReceiptItemSessionBeanRemote receiptItemBean; + + @Override + public BillableReceiptItem addReceiptItem (final BillableReceiptItem receiptItem) throws ReceiptItemAlreadyAddedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addReceiptItem(): receiptItem={1} - CALLED!", this.getClass().getSimpleName(), receiptItem)); //NOI18N + + // Validate parameter + if (null == receiptItem) { + // Throw NPE + throw new NullPointerException("receiptItem is null"); //NOI18N + } else if (receiptItem.getItemProduct() == null) { + // Throw it again + throw new NullPointerException("receiptItem.itemProduct is null"); //NOI18N + } else if (receiptItem.getItemProduct().getProductId() == null) { + // Throw it again + throw new NullPointerException("receiptItem.itemProduct.productId is null"); //NOI18N + } else if (receiptItem.getItemProduct().getProductId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receiptItem.itemProduct.productId={0} is invalid.", receiptItem.getItemProduct().getProductId())); //NOI18N + } else if (receiptItem.getItemProductQuantity() < 1) { + // Throw IAE again + throw new IllegalArgumentException(MessageFormat.format("receiptItem.itemProductQuantity={0} is invalid.", receiptItem.getItemProductQuantity())); //NOI18N + } else if (receiptItem.getItemReceipt() == null) { + // Throw NPE + throw new NullPointerException("receiptItem.itemReceipt is null"); //NOI18N + } else if (receiptItem.getItemReceipt().getReceiptId() == null) { + // Throw NPE + throw new NullPointerException("receiptItem.itemReceipt.receiptId is null"); //NOI18N + } else if (receiptItem.getItemReceipt().getReceiptId() < 1) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("receiptItem.itemReceipt.receiptId={0} is invalid.", receiptItem.getItemReceipt().getReceiptId())); //NOI18N + } else if (this.isReceiptItemRegistered(receiptItem)) { + // Is already registered + throw new ReceiptItemAlreadyAddedException(receiptItem); + } + + // Add created timestamp + receiptItem.setItemCreated(new Date()); + + // Persist it + this.getEntityManager().persist(receiptItem); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addReceiptItem(): receiptItem.itemId={1} - EXIT!", this.getClass().getSimpleName(), receiptItem.getItemId())); //NOI18N + + // Return updated instance + return receiptItem; + } + + /** + * Checks if given receipt item has already been added to receipt. + *

+ * @param receiptItem Receipt item + *

+ * @return Whether item has already been added to receipt item's receipt + */ + private boolean isReceiptItemRegistered (final BillableReceiptItem receiptItem) { + // Default is not found + boolean isFound = false; + + /* + * Get all receipt's receipt items. No need to look global as people may + * buy the same item again and again ... ;-) + */ + final List receiptItems = this.receiptItemBean.allReceiptItems(receiptItem.getItemReceipt()); + + // Loop through all entries + for (final BillableReceiptItem item : receiptItems) { + // Is it the same item? + if (ReceiptItems.isSameReceiptItem(item, receiptItem)) { + // Found it + isFound = true; + break; + } + } + + // Return flag + return isFound; + } + +} diff --git a/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialReceiptItemSessionBean.java b/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialReceiptItemSessionBean.java new file mode 100644 index 0000000..4918c86 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialReceiptItemSessionBean.java @@ -0,0 +1,186 @@ +/* + * 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_item; + +import java.text.MessageFormat; +import java.util.Date; +import java.util.List; +import javax.ejb.Stateless; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemAlreadyAddedException; +import org.mxchange.jfinancials.model.receipt.BillableReceipt; +import org.mxchange.jfinancials.model.receipt.item.BillableReceiptItem; +import org.mxchange.jfinancials.model.receipt.item.ReceiptItems; +import org.mxchange.jusercore.model.user.User; + +/** + * A stateless bean for general purposes for receipt items + *

+ * @author Roland Häder + */ +@Stateless (name = "financialReceiptItem", description = "A stateless session bean for handling receipt itemss.") +public class FinancialReceiptItemSessionBean extends BaseFinancialsDatabaseBean implements FinancialReceiptItemSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 659_868_076_723_742L; + + @Override + public BillableReceiptItem addReceiptItem (final BillableReceiptItem receiptItem) throws ReceiptItemAlreadyAddedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addReceiptItem(): receiptItem={1} - CALLED!", this.getClass().getSimpleName(), receiptItem)); //NOI18N + + // Validate parameter + if (null == receiptItem) { + // Throw NPE + throw new NullPointerException("receiptItem is null"); //NOI18N + } else if (receiptItem.getItemProduct() == null) { + // Throw it again + throw new NullPointerException("receiptItem.itemProduct is null"); //NOI18N + } else if (receiptItem.getItemProduct().getProductId() == null) { + // Throw it again + throw new NullPointerException("receiptItem.itemProduct.productId is null"); //NOI18N + } else if (receiptItem.getItemProduct().getProductId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receiptItem.itemProduct.productId={0} is invalid.", receiptItem.getItemProduct().getProductId())); //NOI18N + } else if (receiptItem.getItemProductQuantity() < 1) { + // Throw IAE again + throw new IllegalArgumentException(MessageFormat.format("receiptItem.itemProductQuantity={0} is invalid.", receiptItem.getItemProductQuantity())); //NOI18N + } else if (receiptItem.getItemReceipt() == null) { + // Throw NPE + throw new NullPointerException("receiptItem.itemReceipt is null"); //NOI18N + } else if (receiptItem.getItemReceipt().getReceiptId() == null) { + // Throw NPE + throw new NullPointerException("receiptItem.itemReceipt.receiptId is null"); //NOI18N + } else if (receiptItem.getItemReceipt().getReceiptId() < 1) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("receiptItem.itemReceipt.receiptId={0} is invalid.", receiptItem.getItemReceipt().getReceiptId())); //NOI18N + } else if (this.isReceiptItemRegistered(receiptItem)) { + // Is already registered + throw new ReceiptItemAlreadyAddedException(receiptItem); + } + + // Add created timestamp + receiptItem.setItemCreated(new Date()); + + // Persist it + this.getEntityManager().persist(receiptItem); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addReceiptItem(): receiptItem.itemId={1} - EXIT!", this.getClass().getSimpleName(), receiptItem.getItemId())); //NOI18N + + // Return updated instance + return receiptItem; + } + + @Override + @SuppressWarnings ("unchecked") + public List allReceiptItems () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allReceiptItems(): CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get named query + final Query query = this.getEntityManager().createNamedQuery("AllReceiptItems"); //NOI18N + + // Get list + final List receiptItems = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allReceiptItems(): receiptItems()={1} EXIT!", this.getClass().getSimpleName(), receiptItems.size())); //NOI18N + + // Return it + return receiptItems; + } + + @Override + @SuppressWarnings ("unchecked") + public List allReceiptItems (final BillableReceipt receipt) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allReceiptItems(): receipt={1} - CALLED!", this.getClass().getSimpleName(), receipt)); //NOI18N + + // Get named query + final Query query = this.getEntityManager().createNamedQuery("SearchAssignedReceiptItems"); //NOI18N + + // Set parameter + query.setParameter("itemReceipt", receipt); //NOI18N + + // Get list + final List receiptItems = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allReceiptItems(): receiptItems()={1} EXIT!", this.getClass().getSimpleName(), receiptItems.size())); //NOI18N + + // Return it + return receiptItems; + } + + @Override + @SuppressWarnings ("unchecked") + public List allUsersReceiptItems (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsersReceiptItems(): user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // Get named query + final Query query = this.getEntityManager().createNamedQuery("SearchAllUserReceiptItems"); //NOI18N + + // Set parameter + query.setParameter("receiptUser", user); //NOI18N + + // Get list + final List receiptItems = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allReceiptItems(): receiptItems()={1} EXIT!", this.getClass().getSimpleName(), receiptItems.size())); //NOI18N + + // Return it + return receiptItems; + } + + /** + * Checks if given receipt item has already been added to receipt. + *

+ * @param receiptItem Receipt item + *

+ * @return Whether item has already been added to receipt item's receipt + */ + private boolean isReceiptItemRegistered (final BillableReceiptItem receiptItem) { + // Default is not found + boolean isFound = false; + + /* + * Get all receipt's receipt items. No need to look global as people may + * buy the same item again and again ... ;-) + */ + final List receiptItems = this.allReceiptItems(receiptItem.getItemReceipt()); + + // Loop through all entries + for (final BillableReceiptItem item : receiptItems) { + // Is it the same item? + if (ReceiptItems.isSameReceiptItem(item, receiptItem)) { + // Found it + isFound = true; + break; + } + } + + // Return flag + return isFound; + } + +} -- 2.39.5