]> git.mxchange.org Git - jfinancials-ejb.git/blob
a42383982982a115dcbb764a62c1a801a01fdc75
[jfinancials-ejb.git] /
1 /*
2  * Copyright (C) 2017 - 2022 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jfinancials.enterprise.financial.receipt;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import org.mxchange.jfinancials.enterprise.product.BaseFinancialsProductEnterpriseBean;
22 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
23 import org.mxchange.jfinancials.model.utils.ReceiptUtils;
24
25 /**
26  * A general bean for receipt-related methods that can be generalized.
27  * <p>
28  * @author Roland Haeder<roland@mxchange.org>
29  */
30 public abstract class BaseFinancialsReceiptEnterpriseBean extends BaseFinancialsProductEnterpriseBean {
31
32         /**
33          * Serial number
34          */
35         private static final long serialVersionUID = 523_676_481_092_175_622L;
36
37         /**
38          * Protected constructor, no instance from this class.
39          */
40         protected BaseFinancialsReceiptEnterpriseBean () {
41                 super();
42         }
43
44         /**
45          * Merges given receipt's data
46          * <p>
47          * @param detachedReceipt Receipt instance to merge
48          * <p>
49          * @return Detached receipt instance
50          */
51         protected BillableReceipt mergeReceipt (final BillableReceipt detachedReceipt) {
52                 // Trace message
53                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeReceipt: detachedReceipt={0} - CALLED!", detachedReceipt)); //NOI18N
54
55                 // The receipt instance must be valid
56                 if (null == detachedReceipt) {
57                         // Throw NPE again
58                         throw new NullPointerException("detachedReceipt is null"); //NOI18N
59                 } else if (detachedReceipt.getReceiptId() == null) {
60                         // Throw NPE again
61                         throw new NullPointerException("detachedReceipt.receiptId is null"); //NOI18N
62                 } else if (detachedReceipt.getReceiptId() < 1) {
63                         // Not valid
64                         throw new IllegalStateException(MessageFormat.format("detachedReceipt.receiptId={0} is not valid.", detachedReceipt.getReceiptId())); //NOI18N
65                 }
66
67                 // Get receipt from it and find it
68                 final BillableReceipt foundReceipt = this.getEntityManager().find(detachedReceipt.getClass(), detachedReceipt.getReceiptId());
69
70                 // Should be found
71                 assert (foundReceipt instanceof BillableReceipt) : MessageFormat.format("Receipt with id {0} not found, but should be.", detachedReceipt.getReceiptId()); //NOI18N
72
73                 // Debug message
74                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeReceipt: foundReceipt.receiptId={0}", foundReceipt.getReceiptId())); //NOI18N
75
76                 // Copy all
77                 ReceiptUtils.copyReceiptData(detachedReceipt, foundReceipt);
78
79                 // Merge receipt instance
80                 final BillableReceipt managedReceipt = this.getEntityManager().merge(foundReceipt);
81
82                 // Should be there
83                 assert (managedReceipt instanceof BillableReceipt) : "managedReceipt is null"; //NOI18N
84
85                 // Set updated timestamp
86                 managedReceipt.setReceiptEntryUpdated(new Date());
87
88                 // Trace message
89                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeReceipt: managedReceipt={0} - EXIT!", managedReceipt)); //NOI18N
90
91                 // Return detached receipt
92                 return managedReceipt;
93         }
94
95 }