]> git.mxchange.org Git - jshop-core.git/commitdiff
Dusting a bit off old code:
authorRoland Häder <roland@mxchange.org>
Tue, 31 Jan 2023 13:54:35 +0000 (14:54 +0100)
committerRoland Häder <roland@mxchange.org>
Tue, 31 Jan 2023 13:54:35 +0000 (14:54 +0100)
- added constructor for the JPA and custom for initializing this entity
  correctly
- implemented Comparable<T> interface

src/org/mxchange/jshopcore/model/basket/items/BaseItem.java
src/org/mxchange/jshopcore/model/order/Orderable.java
src/org/mxchange/jshopcore/model/order/ShopOrder.java
src/org/mxchange/jshopcore/model/order/items/OrderItem.java

index 269dbc4cfcac2d8212e626420bfde1f7dcebfd2d..d58bb86a64c3f225b71019029160ef3aa922b465 100644 (file)
@@ -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;
        }
 
index 5cdfc3bfe111d599b2d4977f9266c4f075a7276a..09e424440ec4ba69235c4f92185cabbd6872a104 100644 (file)
@@ -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
index c63c3bc5f3b93f6d6ed27fbb3c8a3d473eed14fb..052b35b0f9c252f64649e00a23851f342a8ac3bf 100644 (file)
@@ -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
index a28017efd7ed026db710be60b9e5086313cba7c2..e753e42103a59ffdc3b2b7a8d327d962b3b85ce9 100644 (file)
@@ -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;
        }