--- /dev/null
+/*
+ * 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_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
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@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.
+ * <p>
+ * @param receiptItem Receipt item
+ * <p>
+ * @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<BillableReceiptItem> 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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_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
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@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<BillableReceiptItem> 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<BillableReceiptItem> 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<BillableReceiptItem> 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<BillableReceiptItem> 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<BillableReceiptItem> 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<BillableReceiptItem> 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.
+ * <p>
+ * @param receiptItem Receipt item
+ * <p>
+ * @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<BillableReceiptItem> 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;
+ }
+
+}