From dd11e75dd147c9fa7107efd7356a06963389dd25 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Tue, 31 Jan 2023 14:54:35 +0100
Subject: [PATCH] Dusting a bit off old code: - added constructor for the JPA
 and custom for initializing this entity   correctly - implemented
 Comparable<T> interface

---
 .../model/basket/items/BaseItem.java          |   2 +
 .../jshopcore/model/order/Orderable.java      |  22 +++-
 .../jshopcore/model/order/ShopOrder.java      | 102 ++++++++++++++++--
 .../model/order/items/OrderItem.java          |  48 ++++++++-
 4 files changed, 162 insertions(+), 12 deletions(-)

diff --git a/src/org/mxchange/jshopcore/model/basket/items/BaseItem.java b/src/org/mxchange/jshopcore/model/basket/items/BaseItem.java
index 269dbc4..d58bb86 100644
--- a/src/org/mxchange/jshopcore/model/basket/items/BaseItem.java
+++ b/src/org/mxchange/jshopcore/model/basket/items/BaseItem.java
@@ -55,8 +55,10 @@ public abstract class BaseItem implements AddableBasketItem {
 	@Override
 	public int hashCode () {
 		int hash = 5;
+
 		hash = 29 * hash + Objects.hashCode(this.getItemProduct().getProductId());
 		hash = 29 * hash + Objects.hashCode(this.getItemType());
+
 		return hash;
 	}
 
diff --git a/src/org/mxchange/jshopcore/model/order/Orderable.java b/src/org/mxchange/jshopcore/model/order/Orderable.java
index 5cdfc3b..09e4244 100644
--- a/src/org/mxchange/jshopcore/model/order/Orderable.java
+++ b/src/org/mxchange/jshopcore/model/order/Orderable.java
@@ -27,7 +27,7 @@ import org.mxchange.jshopcore.model.basket.AddableBasketItem;
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-public interface Orderable extends Serializable {
+public interface Orderable extends Comparable<Orderable>, Serializable {
 
 	/**
 	 * Getter for access key
@@ -48,14 +48,28 @@ public interface Orderable extends Serializable {
 	 * <p>
 	 * @return Created timestamp
 	 */
-	Date getOrderCreated ();
+	Date getOrderEntryCreated ();
 
 	/**
 	 * Setter for created timestamp
 	 * <p>
-	 * @param created Created timestamp
+	 * @param orderEntryCreated Created timestamp
 	 */
-	void setOrderCreated (final Date created);
+	void setOrderEntryCreated (final Date orderEntryCreated);
+
+	/**
+	 * Getter for updated timestamp
+	 * <p>
+	 * @return Updated timestamp
+	 */
+	Date getOrderEntryUpdated ();
+
+	/**
+	 * Setter for updated timestamp
+	 * <p>
+	 * @param orderEntryUpdated Updated timestamp
+	 */
+	void setOrderEntryUpdated (final Date orderEntryUpdated);
 
 	/**
 	 * Getter for customer instance
diff --git a/src/org/mxchange/jshopcore/model/order/ShopOrder.java b/src/org/mxchange/jshopcore/model/order/ShopOrder.java
index c63c3bc..052b35b 100644
--- a/src/org/mxchange/jshopcore/model/order/ShopOrder.java
+++ b/src/org/mxchange/jshopcore/model/order/ShopOrder.java
@@ -16,6 +16,7 @@
  */
 package org.mxchange.jshopcore.model.order;
 
+import java.text.MessageFormat;
 import java.util.Date;
 import java.util.List;
 import java.util.Objects;
@@ -32,7 +33,10 @@ import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
 import javax.persistence.Transient;
+import org.mxchange.jcoreutils.comparable.ComparableUtils;
+import org.mxchange.jcoreutils.number.SafeNumberUtils;
 import org.mxchange.jcustomercore.model.customer.Customer;
+import org.mxchange.jcustomercore.model.utils.CustomerUtils;
 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
 
 /**
@@ -70,8 +74,15 @@ public class ShopOrder implements Orderable {
 	 */
 	@Basic (optional = false)
 	@Temporal (TemporalType.TIMESTAMP)
-	@Column (name = "order_created", nullable = false)
-	private Date orderCreated;
+	@Column (name = "order_entry_created", nullable = false, updatable = false)
+	private Date orderEntryCreated;
+
+	/**
+	 * Updated timestamp
+	 */
+	@Temporal (TemporalType.TIMESTAMP)
+	@Column (name = "order_entry_created", insertable = false)
+	private Date orderEntryUpdated;
 
 	/**
 	 * Order orderId
@@ -87,6 +98,73 @@ public class ShopOrder implements Orderable {
 	@Transient
 	private List<AddableBasketItem> orderedItems;
 
+	/**
+	 * The default constructor is for the JPA to invoke
+	 */
+	public ShopOrder () {
+	}
+
+	/**
+	 * Constructor with all required fields
+	 * <p>
+	 * @param accessKey Access key
+	 * @param customer  An instance of a Customer class
+	 */
+	public ShopOrder (final String accessKey, final Customer customer) {
+		// Invoke simplier constructor first
+		this();
+
+		// Check all parameter
+		if (null == accessKey) {
+			// Throw NPE
+			throw new NullPointerException("Parameter 'accessKey' is null"); //NOI18N
+		} else if (accessKey.isEmpty()) {
+			// Throw IAE
+			throw new IllegalArgumentException("Parameter 'accessKey' is empty"); //NOI18N
+		} else if (null == customer) {
+			// Throw NPE again
+			throw new NullPointerException("Parameter 'customer' is null"); //NOI18N
+		} else if (customer.getCustomerId() == null) {
+			// Throw it again
+			throw new NullPointerException("customer.customerId is null"); //NOI18N
+		} else if (customer.getCustomerId() < 1) {
+			// Throw IAE again
+			throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is invalid", customer.getCustomerId())); //NOI18N
+		}
+
+		// Set all here
+		this.accessKey = accessKey;
+		this.customer = customer;
+	}
+
+	@Override
+	public int compareTo (final Orderable orderable) {
+		// Check parameter on null-reference and equality to this
+		if (null == orderable) {
+			// Should not happen
+			throw new NullPointerException("orderable is null"); //NOI18N
+		} else if (Objects.equals(this, orderable)) {
+			// Same object
+			return 0;
+		}
+
+		// Init comparators
+		final int comparators[] = {
+			// First compare access key
+			this.getAccessKey().compareTo(orderable.getAccessKey()),
+			// ... next customer
+			CustomerUtils.compare(this.getCustomer(), orderable.getCustomer()),
+			// ... next primary key
+			SafeNumberUtils.compare(this.getOrderId(), orderable.getOrderId())
+		};
+
+		// Check all values
+		final int comparison = ComparableUtils.checkAll(comparators);
+
+		// Return value
+		return comparison;
+	}
+
 	@Override
 	public boolean equals (final Object object) {
 		if (this == object) {
@@ -134,14 +212,26 @@ public class ShopOrder implements Orderable {
 
 	@Override
 	@SuppressWarnings ("ReturnOfDateField")
-	public Date getOrderCreated () {
-		return this.orderCreated;
+	public Date getOrderEntryCreated () {
+		return this.orderEntryCreated;
+	}
+
+	@Override
+	@SuppressWarnings ("AssignmentToDateFieldFromParameter")
+	public void setOrderEntryCreated (final Date orderEntryCreated) {
+		this.orderEntryCreated = orderEntryCreated;
+	}
+
+	@Override
+	@SuppressWarnings ("ReturnOfDateField")
+	public Date getOrderEntryUpdated () {
+		return this.orderEntryUpdated;
 	}
 
 	@Override
 	@SuppressWarnings ("AssignmentToDateFieldFromParameter")
-	public void setOrderCreated (final Date orderCreated) {
-		this.orderCreated = orderCreated;
+	public void setOrderEntryUpdated (final Date orderEntryUpdated) {
+		this.orderEntryUpdated = orderEntryUpdated;
 	}
 
 	@Override
diff --git a/src/org/mxchange/jshopcore/model/order/items/OrderItem.java b/src/org/mxchange/jshopcore/model/order/items/OrderItem.java
index a28017e..e753e42 100644
--- a/src/org/mxchange/jshopcore/model/order/items/OrderItem.java
+++ b/src/org/mxchange/jshopcore/model/order/items/OrderItem.java
@@ -16,6 +16,7 @@
  */
 package org.mxchange.jshopcore.model.order.items;
 
+import java.text.MessageFormat;
 import java.util.Objects;
 import javax.persistence.Basic;
 import javax.persistence.CascadeType;
@@ -80,6 +81,7 @@ public class OrderItem extends BaseItem implements AddableBasketItem {
 	/**
 	 * Product instance
 	 */
+	@Basic (optional = false)
 	@JoinColumn (name = "order_product_id", updatable = false)
 	@OneToOne (targetEntity = GenericProduct.class, cascade = CascadeType.REFRESH)
 	private Product product;
@@ -90,6 +92,44 @@ public class OrderItem extends BaseItem implements AddableBasketItem {
 	public OrderItem () {
 	}
 
+	/**
+	 * Constructor with all required fields
+	 * <p>
+	 * @param itemType      Item type
+	 * @param orderedAmount Ordered amount
+	 * @param product       Ordered product
+	 */
+	public OrderItem (final String itemType, final Long orderedAmount, final Product product) {
+		// Invoke simplier constrcutor
+		this();
+
+		// Check all parameter
+		if (null == itemType) {
+			// Throw NPE
+			throw new NullPointerException("Parameter 'itemType' is null"); //NOI18N
+		} else if (null == orderedAmount) {
+			// Throw it again
+			throw new NullPointerException("Parameter 'orderedAmount' is null"); //NOI18N
+		} else if (orderedAmount < 1) {
+			// Throw IAE
+			throw new IllegalArgumentException(MessageFormat.format("orderedAmount={0} is invalid", orderedAmount)); //NOI18N
+		} else if (null == product) {
+			// Throw NPE
+			throw new NullPointerException("Parameter 'product' is null"); //NOI18N
+		} else if (product.getProductId() == null) {
+			// Throw NPE
+			throw new NullPointerException("product.productId is null"); //NOI18N
+		} else if (product.getProductId() < 1) {
+			// Throw IAE
+			throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is invalid", product.getProductId())); //NOI18N
+		}
+
+		// Set all here
+		this.itemType = itemType;
+		this.orderedAmount = orderedAmount;
+		this.product = product;
+	}
+
 	@Override
 	public boolean equals (final Object object) {
 		if (this == object) {
@@ -102,9 +142,11 @@ public class OrderItem extends BaseItem implements AddableBasketItem {
 
 		final AddableBasketItem item = (AddableBasketItem) object;
 
-		if (!Objects.equals(this.itemType, item.getItemType())) {
+		if (!Objects.equals(this.getItemType(), item.getItemType())) {
 			return false;
-		} else if (!Objects.equals(this.orderedAmount, item.getOrderedAmount())) {
+		} else if (!Objects.equals(this.getOrderedAmount(), item.getOrderedAmount())) {
+			return false;
+		} else if (!Objects.equals(this.getItemProduct(), item.getItemProduct())) {
 			return false;
 		}
 
@@ -154,9 +196,11 @@ public class OrderItem extends BaseItem implements AddableBasketItem {
 	@Override
 	public int hashCode () {
 		int hash = 3;
+
 		hash = 53 * hash + Objects.hashCode(this.getItemType());
 		hash = 53 * hash + Objects.hashCode(this.getOrderedAmount());
 		hash = 53 * hash + Objects.hashCode(this.getItemProduct());
+
 		return hash;
 	}
 
-- 
2.39.5