From: Roland Häder Date: Wed, 8 Apr 2020 15:42:17 +0000 (+0200) Subject: Don't cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=195944bd25d16ab1571c45ff871998250c53dbb2;p=jfinancials-ejb.git Don't cherry-pick: - introduced BaseFinancialsProjectEnterpriseBean which holds some project-specific protected methods - implemented business method updateReceipt() Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jfinancials/enterprise/financial/BaseFinancialsProjectEnterpriseBean.java b/src/java/org/mxchange/jfinancials/enterprise/financial/BaseFinancialsProjectEnterpriseBean.java new file mode 100644 index 0000000..4865a6e --- /dev/null +++ b/src/java/org/mxchange/jfinancials/enterprise/financial/BaseFinancialsProjectEnterpriseBean.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2017 - 2020 Free Software Foundation + * + * 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.enterprise.financial; + +import java.text.MessageFormat; +import org.mxchange.jfinancials.enterprise.BaseFinancialsEnterpriseBean; +import org.mxchange.jfinancials.model.receipt.BillableReceipt; +import org.mxchange.jfinancials.model.receipt.Receipts; + +/** + * A general bean for receipt-related methods that can be generalized. + *

+ * @author Roland Haeder + */ +public abstract class BaseFinancialsProjectEnterpriseBean extends BaseFinancialsEnterpriseBean { + + /** + * Serial number + */ + private static final long serialVersionUID = 523_676_481_092_175_622L; + + /** + * Protected constructor, no instance from this class. + */ + protected BaseFinancialsProjectEnterpriseBean () { + super(); + } + + /** + * Merges given receipt's data + *

+ * @param detachedReceipt Receipt instance to merge + *

+ * @return Detached receipt instance + */ + protected BillableReceipt mergeReceipt (final BillableReceipt detachedReceipt) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeReceipt: detachedReceipt={0} - CALLED!", detachedReceipt)); //NOI18N + + // The receipt instance must be valid + if (null == detachedReceipt) { + // Throw NPE again + throw new NullPointerException("detachedReceipt is null"); //NOI18N + } else if (detachedReceipt.getReceiptId() == null) { + // Throw NPE again + throw new NullPointerException("detachedReceipt.receiptId is null"); //NOI18N //NOI18N + } else if (detachedReceipt.getReceiptId() < 1) { + // Not valid + throw new IllegalStateException(MessageFormat.format("detachedReceipt.receiptId={0} is not valid.", detachedReceipt.getReceiptId())); //NOI18N + } + + // Set updated timestamp + // @TODO detachedReceipt.setReceiptUpdated(new Date()); + // Get receipt from it and find it + final BillableReceipt foundReceipt = this.getEntityManager().find(detachedReceipt.getClass(), detachedReceipt.getReceiptId()); + + // Should be found + assert (foundReceipt instanceof BillableReceipt) : MessageFormat.format("Receipt with id {0} not found, but should be.", detachedReceipt.getReceiptId()); //NOI18N + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeReceipt: foundReceipt.receiptId={0}", foundReceipt.getReceiptId())); //NOI18N + + // Copy all + Receipts.copyAll(detachedReceipt, foundReceipt); + + // Merge receipt instance + final BillableReceipt managedReceipt = this.getEntityManager().merge(foundReceipt); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeReceipt: managedReceipt={0} - EXIT!", managedReceipt)); //NOI18N + + // Return detached receipt + return managedReceipt; + } + +} diff --git a/src/java/org/mxchange/jfinancials/model/receipt/FinancialAdminReceiptSessionBean.java b/src/java/org/mxchange/jfinancials/model/receipt/FinancialAdminReceiptSessionBean.java index cf7c412..c68362e 100644 --- a/src/java/org/mxchange/jfinancials/model/receipt/FinancialAdminReceiptSessionBean.java +++ b/src/java/org/mxchange/jfinancials/model/receipt/FinancialAdminReceiptSessionBean.java @@ -21,8 +21,9 @@ import java.util.Date; import javax.ejb.EJB; import javax.ejb.Stateless; import org.mxchange.jcontactsbusiness.model.employee.Employable; -import org.mxchange.jfinancials.enterprise.BaseFinancialsEnterpriseBean; +import org.mxchange.jfinancials.enterprise.financial.BaseFinancialsProjectEnterpriseBean; import org.mxchange.jfinancials.exceptions.receipt.ReceiptAlreadyAddedException; +import org.mxchange.jfinancials.exceptions.receipt.ReceiptNotFoundException; import org.mxchange.jusercore.model.user.User; /** @@ -31,7 +32,7 @@ import org.mxchange.jusercore.model.user.User; * @author Roland Häder */ @Stateless (name = "adminFinancialReceipt", description = "A stateless session bean for handling receipts.") -public class FinancialAdminReceiptSessionBean extends BaseFinancialsEnterpriseBean implements FinancialAdminReceiptSessionBeanRemote { +public class FinancialAdminReceiptSessionBean extends BaseFinancialsProjectEnterpriseBean implements FinancialAdminReceiptSessionBeanRemote { /** * Serial number @@ -113,4 +114,76 @@ public class FinancialAdminReceiptSessionBean extends BaseFinancialsEnterpriseBe return receipt; } + @Override + public BillableReceipt updateReceipt (final BillableReceipt receipt) throws ReceiptNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateReceipt(): receipt={1} - CALLED!", this.getClass().getSimpleName(), receipt)); //NOI18N + + // Validate parameter + if (null == receipt) { + // Throw NPE + throw new NullPointerException("receipt is null"); //NOI18N + } else if (receipt.getReceiptId() == null) { + // Throw IAE + throw new NullPointerException("receipt.receiptId is null"); //NOI18N + } else if (receipt.getReceiptIssued() == null) { + // Throw NPE again + throw new NullPointerException("receipt.receiptIssued is null"); //NOI18N + } else if (receipt.getReceiptBranchOffice() == null) { + // Throw it again + throw new NullPointerException("receipt.receiptBranchOffice is null"); //NOI18N + } else if (receipt.getReceiptBranchOffice().getBranchId() == null) { + // Throw it again + throw new NullPointerException("receipt.receiptBranchOffice.branchId is null"); //NOI18N + } else if (receipt.getReceiptBranchOffice().getBranchId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receipt.receiptBranchOffice.branchId={0} is not valid.", receipt.getReceiptBranchOffice().getBranchId())); //NOI18N + } else if (receipt.getReceiptPaymentType() == null) { + // Throw NPE + throw new NullPointerException("receipt.receiptPaymentType is null"); //NOI18N + } else if ((receipt.getReceiptUser() instanceof User) && receipt.getReceiptUser().getUserId() == null) { + // Throw NPE again + throw new NullPointerException("receipt.receiptUser.userId is null"); //NOI18N + } 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())); //NOI18N + } else if (!this.receiptBean.isReceiptRegistered(receipt)) { + // Has already been registered + throw new ReceiptNotFoundException(receipt); + } + + // Add created instance + receipt.setReceiptCreated(new Date()); + + // Is user instance set? + if (receipt.getReceiptUser() instanceof User) { + // Then get managed instanced + final User managedUser = this.createManaged(receipt.getReceiptUser()); + + // Set it back + receipt.setReceiptUser(managedUser); + } + + // Is employee instance set? + if (receipt.getReceiptSellerEmployee() instanceof Employable) { + // Then get managed instanced + final Employable managedEmployee = this.createManaged(receipt.getReceiptSellerEmployee()); + + // Set it back + receipt.setReceiptSellerEmployee(managedEmployee); + } + + // Set updated timestamp + receipt.setReceiptUpdated(new Date()); + + // Merge data + final BillableReceipt detachedReceipt = this.mergeReceipt(receipt); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateReceipt(): detachedReceipt.receiptId={1} - EXIT!", this.getClass().getSimpleName(), detachedReceipt.getReceiptId())); //NOI18N + + // Return it + return detachedReceipt; + } + }