From d9813ba4b96f94ce4941575c4c6dd2cd5d92db5c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 19 Sep 2017 21:06:24 +0200 Subject: [PATCH] Continued: - added Receipts utilities class which currently does only compare two receipt instances with each other: entity-equality or branch office, receipt number and "owning" user instance must all be the same - added receiptNumber field - added 2 named queries for receipts, finding all (any) and all user's receipts - sorted members a bit MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../ReceiptAlreadyAddedException.java | 20 +++ .../model/receipt/BillableReceipt.java | 28 +++-- .../model/receipt/FinancialReceipt.java | 75 +++++++---- .../jfinancials/model/receipt/Receipts.java | 116 ++++++++++++++++++ 4 files changed, 208 insertions(+), 31 deletions(-) create mode 100644 src/org/mxchange/jfinancials/model/receipt/Receipts.java diff --git a/src/org/mxchange/jfinancials/exceptions/ReceiptAlreadyAddedException.java b/src/org/mxchange/jfinancials/exceptions/ReceiptAlreadyAddedException.java index 3a67b83..a2ccf07 100644 --- a/src/org/mxchange/jfinancials/exceptions/ReceiptAlreadyAddedException.java +++ b/src/org/mxchange/jfinancials/exceptions/ReceiptAlreadyAddedException.java @@ -16,6 +16,9 @@ */ package org.mxchange.jfinancials.exceptions; +import java.text.MessageFormat; +import org.mxchange.jfinancials.model.receipt.BillableReceipt; + /** * This exception is thrown when a receipt was already added *

@@ -28,4 +31,21 @@ public class ReceiptAlreadyAddedException extends Exception { */ private static final long serialVersionUID = 19_065_867_127_647_624L; + /** + * Default constructor + */ + public ReceiptAlreadyAddedException () { + super(); + } + + /** + * Constructor with receipt instance + *

+ * @param receipt Receipt instance + */ + public ReceiptAlreadyAddedException (final BillableReceipt receipt) { + // Call super constructor + super(MessageFormat.format("Receipt with id {0} and number {1} has already been added.", receipt.getReceiptId(), receipt.getReceiptNumber())); + } + } diff --git a/src/org/mxchange/jfinancials/model/receipt/BillableReceipt.java b/src/org/mxchange/jfinancials/model/receipt/BillableReceipt.java index 04baca4..06b33ff 100644 --- a/src/org/mxchange/jfinancials/model/receipt/BillableReceipt.java +++ b/src/org/mxchange/jfinancials/model/receipt/BillableReceipt.java @@ -18,7 +18,7 @@ package org.mxchange.jfinancials.model.receipt; import java.io.Serializable; import java.util.Calendar; -import org.mxchange.jcontactsbusiness.basicdata.BusinessBasicData; +import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice; import org.mxchange.jproduct.model.payment.PaymentType; import org.mxchange.jusercore.model.user.User; @@ -72,6 +72,20 @@ public interface BillableReceipt extends Serializable { void setReceiptId (final Long receiptId); /** + * Getter for receipt number + *

+ * @return Receipt number + */ + Long getReceiptNumber (); + + /** + * Setter for receipt number + *

+ * @param receiptNumber Receipt number + */ + void setReceiptNumber (final Long receiptNumber); + + /** * Getter for receipt's user *

* @return Receipt's user @@ -86,18 +100,18 @@ public interface BillableReceipt extends Serializable { void setReceiptUser (final User receiptUser); /** - * Getter for seller instance + * Getter for branch office instance *

- * @return Seller instance + * @return Branch office instance */ - BusinessBasicData getReceiptSeller (); + BranchOffice getReceiptBranchOffice (); /** - * Setter for seller instance + * Setter for branch office instance *

- * @param receiptSeller Seller instance + * @param receiptBranch Branch office instance */ - void setReceiptSeller (final BusinessBasicData receiptSeller); + void setReceiptBranchOffice (final BranchOffice receiptBranch); /** * Getter for payment type diff --git a/src/org/mxchange/jfinancials/model/receipt/FinancialReceipt.java b/src/org/mxchange/jfinancials/model/receipt/FinancialReceipt.java index 83aebc7..9a9685f 100644 --- a/src/org/mxchange/jfinancials/model/receipt/FinancialReceipt.java +++ b/src/org/mxchange/jfinancials/model/receipt/FinancialReceipt.java @@ -28,13 +28,15 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.persistence.OneToOne; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; -import org.mxchange.jcontactsbusiness.basicdata.BusinessBasicData; -import org.mxchange.jcontactsbusiness.basicdata.CompanyBasicData; +import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice; +import org.mxchange.jcontactsbusiness.model.branchoffice.CompanyBranchOffice; import org.mxchange.jproduct.model.payment.PaymentType; import org.mxchange.jusercore.model.user.LoginUser; import org.mxchange.jusercore.model.user.User; @@ -47,6 +49,12 @@ import org.mxchange.jusercore.model.user.User; @Table ( name = "receipts" ) +@NamedQueries ( + { + @NamedQuery (name = "AllReceipts", query = "SELECT r FROM receipts AS r ORDER BY r.receiptId ASC"), + @NamedQuery (name = "SearchAllUserReceipts", query = "SELECT r FROM receipts AS r WHERE r.receiptUser = :receiptUser ORDER BY r.receiptId ASC") + } +) @SuppressWarnings ("PersistenceUnitPresent") public class FinancialReceipt implements BillableReceipt { @@ -56,6 +64,13 @@ public class FinancialReceipt implements BillableReceipt { @Transient private static final long serialVersionUID = 185_867_217_461L; + /** + * Seller instance + */ + @JoinColumn (name = "receipt_branch_id", referencedColumnName = "branch_id", nullable = false, updatable = false) + @OneToOne (targetEntity = CompanyBranchOffice.class, cascade = CascadeType.REFRESH, optional = false) + private BranchOffice receiptBranchOffice; + /** * When this receipt entry has been created */ @@ -80,6 +95,12 @@ public class FinancialReceipt implements BillableReceipt { @Column (name = "receipt_issued", nullable = false) private Calendar receiptIssued; + /** + * Receipt number + */ + @Column (name = "receipt_number") + private Long receiptNumber; + /** * Payment type (cash, credit card, EC card ...) */ @@ -88,18 +109,11 @@ public class FinancialReceipt implements BillableReceipt { @Enumerated (EnumType.STRING) private PaymentType receiptPaymentType; - /** - * Seller instance - */ - @JoinColumn (name = "receipt_seller_id", referencedColumnName = "business_data_id", nullable = false, updatable = false) - @OneToOne (targetEntity = CompanyBasicData.class, cascade = CascadeType.REFRESH, optional = false) - private BusinessBasicData receiptSeller; - /** * Which user this receipt belongs to */ - @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id", nullable = false, updatable = false) - @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false) + @JoinColumn (name = "receipt_user_id", referencedColumnName = "user_id") + @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH) private User receiptUser; /** @@ -112,17 +126,17 @@ public class FinancialReceipt implements BillableReceipt { * Constructor with payment type, seller and user *

* @param receiptPaymentType Payment type - * @param receiptSeller Seller instance + * @param branchOffice Branch office instance * @param receiptUser User instance * @param receiptIssued When this receipt has been issued */ - public FinancialReceipt (final PaymentType receiptPaymentType, final BusinessBasicData receiptSeller, final User receiptUser, final Calendar receiptIssued) { + public FinancialReceipt (final PaymentType receiptPaymentType, final BranchOffice branchOffice, final User receiptUser, final Calendar receiptIssued) { // Call other constructor first this(); // Set all values this.receiptPaymentType = receiptPaymentType; - this.receiptSeller = receiptSeller; + this.receiptBranchOffice = branchOffice; this.receiptUser = receiptUser; this.receiptIssued = receiptIssued; } @@ -142,9 +156,11 @@ public class FinancialReceipt implements BillableReceipt { if (!Objects.equals(this.getReceiptId(), receipt.getReceiptId())) { return false; + } else if (!Objects.equals(this.getReceiptNumber(), receipt.getReceiptNumber())) { + return false; } else if (this.getReceiptPaymentType() != receipt.getReceiptPaymentType()) { return false; - } else if (!Objects.equals(this.getReceiptSeller(), receipt.getReceiptSeller())) { + } else if (!Objects.equals(this.getReceiptBranchOffice(), receipt.getReceiptBranchOffice())) { return false; } else if (!Objects.equals(this.getReceiptUser(), receipt.getReceiptUser())) { return false; @@ -155,6 +171,16 @@ public class FinancialReceipt implements BillableReceipt { return true; } + @Override + public BranchOffice getReceiptBranchOffice () { + return this.receiptBranchOffice; + } + + @Override + public void setReceiptBranchOffice (final BranchOffice receiptBranchOffice) { + this.receiptBranchOffice = receiptBranchOffice; + } + @Override @SuppressWarnings ("ReturnOfDateField") public Calendar getReceiptCreated () { @@ -190,23 +216,23 @@ public class FinancialReceipt implements BillableReceipt { } @Override - public PaymentType getReceiptPaymentType () { - return this.receiptPaymentType; + public Long getReceiptNumber () { + return this.receiptNumber; } @Override - public void setReceiptPaymentType (final PaymentType receiptPaymentType) { - this.receiptPaymentType = receiptPaymentType; + public void setReceiptNumber (final Long receiptNumber) { + this.receiptNumber = receiptNumber; } @Override - public BusinessBasicData getReceiptSeller () { - return this.receiptSeller; + public PaymentType getReceiptPaymentType () { + return this.receiptPaymentType; } @Override - public void setReceiptSeller (final BusinessBasicData receiptSeller) { - this.receiptSeller = receiptSeller; + public void setReceiptPaymentType (final PaymentType receiptPaymentType) { + this.receiptPaymentType = receiptPaymentType; } @Override @@ -224,8 +250,9 @@ public class FinancialReceipt implements BillableReceipt { int hash = 5; hash = 89 * hash + Objects.hashCode(this.getReceiptId()); + hash = 89 * hash + Objects.hashCode(this.getReceiptNumber()); hash = 89 * hash + Objects.hashCode(this.getReceiptPaymentType()); - hash = 89 * hash + Objects.hashCode(this.getReceiptSeller()); + hash = 89 * hash + Objects.hashCode(this.getReceiptBranchOffice()); hash = 89 * hash + Objects.hashCode(this.getReceiptUser()); return hash; diff --git a/src/org/mxchange/jfinancials/model/receipt/Receipts.java b/src/org/mxchange/jfinancials/model/receipt/Receipts.java new file mode 100644 index 0000000..7c00650 --- /dev/null +++ b/src/org/mxchange/jfinancials/model/receipt/Receipts.java @@ -0,0 +1,116 @@ +/* + * 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; + +import java.io.Serializable; +import java.text.MessageFormat; +import java.util.Objects; + +/** + * A utilities class for receipts + * + * @author Roland Häder + */ +public class Receipts implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 2_867_938_676_165_401L; + + public static boolean isSameReceipt (final BillableReceipt receipt1, final BillableReceipt receipt2) { + // Pre-compare both as entities (same id) + if (Objects.equals(receipt1, receipt2)) { + // Same entity (with id number) + return true; + } + + // Validate parameter + if (null == receipt1) { + // Throw NPE + throw new NullPointerException("receipt1 is null"); + } else if ((receipt1.getReceiptId() instanceof Long) && (receipt1.getReceiptId() < 1)) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receipt1.receiptId={0} is not valid.", receipt1.getReceiptId())); + } else if (receipt1.getReceiptNumber() == null) { + // Throw NPE + throw new NullPointerException("receipt1.receiptNumber is null"); + } else if (receipt1.getReceiptNumber() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receipt1.receiptNumber={0} is not valid", receipt1.getReceiptNumber())); + } else if (receipt1.getReceiptBranchOffice() == null) { + // Throw NPE + throw new NullPointerException("receipt1.receiptBranchOffice is null"); + } else if (receipt1.getReceiptBranchOffice().getBranchId() == null) { + // Throw NPE + throw new NullPointerException("receipt1.receiptBranchOffice.branchId is null"); + } else if (receipt1.getReceiptBranchOffice().getBranchId() < 1) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("receipt1.receiptBranchOffice.branchId={0} is not valid", receipt1.getReceiptBranchOffice().getBranchId())); + } else if (receipt1.getReceiptPaymentType()== null) { + // Throw NPE + throw new NullPointerException("receipt1.receiptPaymentType is null"); + } else if (null == receipt2) { + // Throw NPE + throw new NullPointerException("receipt2 is null"); + } else if ((receipt2.getReceiptId() instanceof Long) && (receipt2.getReceiptId() < 1)) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receipt2.receiptId={0} is not valid.", receipt2.getReceiptId())); + } else if (receipt2.getReceiptNumber() == null) { + // Throw NPE + throw new NullPointerException("receipt2.receiptNumber is null"); + } else if (receipt2.getReceiptNumber() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("receipt2.receiptNumber={0} is not valid", receipt2.getReceiptNumber())); + } else if (receipt2.getReceiptBranchOffice() == null) { + // Throw NPE + throw new NullPointerException("receipt2.receiptBranchOffice is null"); + } else if (receipt2.getReceiptBranchOffice().getBranchId() == null) { + // Throw NPE + throw new NullPointerException("receipt2.receiptBranchOffice.branchId is null"); + } else if (receipt2.getReceiptBranchOffice().getBranchId() < 1) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("receipt2.receiptBranchOffice.branchId={0} is not valid", receipt2.getReceiptBranchOffice().getBranchId())); + } else if (receipt2.getReceiptPaymentType()== null) { + // Throw NPE + throw new NullPointerException("receipt2.receiptPaymentType is null"); + } + + // Now check all individually + if (!Objects.equals(receipt1.getReceiptBranchOffice(), receipt2.getReceiptBranchOffice())) { + // Other branch offices + return false; + } else if (!Objects.equals(receipt1.getReceiptNumber(), receipt2.getReceiptNumber())) { + // Other receipt number + return false; + } else if (!Objects.equals(receipt1.getReceiptUser(), receipt2.getReceiptUser())) { + // Other user (unlikely to happen + return false; + } + + // Maybe same receipt! + return true; + } + + /** + * Private default constructor + */ + private Receipts () { + // Utilities don't have instances + } + +} -- 2.39.5