]> git.mxchange.org Git - jshop-core.git/commitdiff
Continued with JPA:
authorRoland Haeder <roland@mxchange.org>
Wed, 23 Sep 2015 10:39:39 +0000 (12:39 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 23 Sep 2015 10:39:57 +0000 (12:39 +0200)
- moved BaseItem and BasketItem to other package
- added more JPA stuff (sorry)
- updated jcore.jar

Signed-off-by:Roland Häder <roland@mxchange.org>
Signed-off-by:Roland Häder <roland@mxchange.org>

17 files changed:
lib/jcore.jar
nbproject/project.properties
src/org/mxchange/jcore/model/contact/CustomerContact.java [new file with mode: 0644]
src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java
src/org/mxchange/jshopcore/model/basket/AddableBasketItem.java
src/org/mxchange/jshopcore/model/basket/items/BaseItem.java [new file with mode: 0644]
src/org/mxchange/jshopcore/model/basket/items/BasketItem.java [new file with mode: 0644]
src/org/mxchange/jshopcore/model/category/BaseCategory.java
src/org/mxchange/jshopcore/model/category/Category.java
src/org/mxchange/jshopcore/model/customer/Customer.java
src/org/mxchange/jshopcore/model/customer/CustomerUtils.java
src/org/mxchange/jshopcore/model/customer/ShopCustomer.java
src/org/mxchange/jshopcore/model/item/BaseItem.java [deleted file]
src/org/mxchange/jshopcore/model/item/BasketItem.java [deleted file]
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 [new file with mode: 0644]

index ead5916a21197864b7915b3c83d96c971ff478c8..e17de0c88e23d89b1f0953bcc4c3f8114d699b84 100644 (file)
Binary files a/lib/jcore.jar and b/lib/jcore.jar differ
index 4776cf1014251389ab2a3b7e2e8a72c762f8303d..5655ed762391d8fd4e01294a614e258fc2700603 100644 (file)
@@ -40,8 +40,8 @@ jar.index=${jnlp.enabled}
 javac.classpath=\
     ${file.reference.jcore.jar}:\
     ${file.reference.jcoreee.jar}:\
-    ${libs.javaee-api-7.0.classpath}:\
-    ${file.reference.commons-codec-1.10.jar}
+    ${file.reference.commons-codec-1.10.jar}:\
+    ${libs.javaee-api-7.0.classpath}
 # Space-separated list of extra javac options
 javac.compilerargs=-Xlint:unchecked -Xlint:deprecation
 javac.deprecation=true
diff --git a/src/org/mxchange/jcore/model/contact/CustomerContact.java b/src/org/mxchange/jcore/model/contact/CustomerContact.java
new file mode 100644 (file)
index 0000000..8f02c12
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcore.model.contact;
+
+/**
+ * Customer contact class
+ *
+ * @author Roland Haeder
+ */
+public class CustomerContact extends BaseContact {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 47_895_786_328_781L;
+}
index afd51fabb3ba9fdb88e8decab211090c8b48ca52..a92211ed979267f4962138adccabfef20355a317 100644 (file)
@@ -23,7 +23,9 @@ import java.text.MessageFormat;
  * This exception is thrown when a query could not be executed
  *
  * @author Roland Haeder
+ * @deprecated No longer used
  */
+@Deprecated
 public class QueryNotExecutedException extends Exception {
        /**
         * Serial number
index 7ed033580ea60c57a9584cb5f44e461698d5d4b0..1c299784f131d9ab1f36034da176d041555e0ca0 100644 (file)
@@ -55,18 +55,18 @@ public interface AddableBasketItem extends Serializable {
        public void setId (final Long id);
 
        /**
-        * Getter for item id (e.g. from product)
+        * Getter for product id
         *
-        * @return the id
+        * @return Product id
         */
-       public Long getItemId ();
+       public Long getProductId ();
 
        /**
-        * Setter for item id (e.g. from product)
+        * Setter for product id
         *
-        * @param id the id to set
+        * @param productId Product id
         */
-       public void setItemId (final Long id);
+       public void setProductId (final Long productId);
 
        /**
         * Getter for item type
diff --git a/src/org/mxchange/jshopcore/model/basket/items/BaseItem.java b/src/org/mxchange/jshopcore/model/basket/items/BaseItem.java
new file mode 100644 (file)
index 0000000..de2f2c0
--- /dev/null
@@ -0,0 +1,177 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jshopcore.model.basket.items;
+
+import java.util.Objects;
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Transient;
+import org.mxchange.jshopcore.model.basket.AddableBasketItem;
+import org.mxchange.jshopcore.model.product.Product;
+
+/**
+ * An item (addedable to a basket) could respresent a product or a discount
+ * coupon. This depends on the type of the item.
+ *
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+@Entity(name = "item")
+public abstract class BaseItem implements AddableBasketItem, Comparable<AddableBasketItem> {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 24_348_671_457_829_156L;
+
+       /**
+        * Entry id (from database backend)
+        */
+       @Id
+       @GeneratedValue
+       private Long id;
+
+       /**
+        * Item amount
+        */
+       @Basic(optional = false)
+       private Long amount;
+
+       /**
+        * Product id
+        */
+       @Column(name = "product_id", length = 20)
+       private Long productId;
+
+       /**
+        * Item type
+        */
+       @Basic(optional = false)
+       @Column(name = "item_type", length = 20)
+       private String itemType;
+
+       /**
+        * Product instance
+        */
+       @Transient
+       private Product product;
+
+       @Override
+       public int compareTo (final AddableBasketItem item) {
+               // item should not be null
+               if (null == item) {
+                       throw new NullPointerException("item is null"); //NOI18N
+               }
+
+               // Is the id the same?
+               if (Objects.equals(this.getProductId(), item.getProductId())) {
+                       // Same id, means same item
+                       return 0;
+               } else if (this.getProductId() > item.getProductId()) {
+                       // This id is larger than compared to
+                       return -1;
+               }
+
+               // The other id is larger
+               return 1;
+       }
+
+       @Override
+       public Long getAmount () {
+               return this.amount;
+       }
+
+       @Override
+       public void setAmount (final Long amount) {
+               this.amount = amount;
+       }
+
+       @Override
+       public Long getId () {
+               return this.id;
+       }
+
+       @Override
+       public void setId (final Long id) {
+               this.id = id;
+       }
+
+       @Override
+       public Long getProductId () {
+               return this.productId;
+       }
+
+       @Override
+       public void setProductId (final Long productId) {
+               this.productId = productId;
+       }
+
+       @Override
+       public String getItemType () {
+               return this.itemType;
+       }
+
+       @Override
+       public void setItemType (final String itemType) {
+               this.itemType = itemType;
+       }
+
+       @Override
+       public Product getProduct () {
+               return this.product;
+       }
+
+       @Override
+       public void setProduct (final Product product) {
+               this.product = product;
+       }
+
+       @Override
+       public boolean equals (final Object object) {
+               // Is it same type?
+               if (!(object instanceof BaseItem)) {
+                       // Not equal types
+                       return false;
+               } else if (!(object instanceof AddableBasketItem)) {
+                       // Not correct interface
+                       return false;
+               }
+
+               // Securely cast to wanted interface
+               AddableBasketItem item = (AddableBasketItem) object;
+
+               // Item id and type must be the same
+               return ((Objects.equals(item.getProductId(), this.getProductId()))
+                               && (Objects.equals(item.getItemType(), this.getItemType())));
+       }
+
+       @Override
+       public int hashCode () {
+               int hash = 5;
+               hash = 29 * hash + Objects.hashCode(this.getProductId());
+               hash = 29 * hash + Objects.hashCode(this.getItemType());
+               return hash;
+       }
+
+       @Override
+       public boolean isProductType () {
+               // Is the instance set?
+               return (this.getProduct() instanceof Product);
+       }
+}
diff --git a/src/org/mxchange/jshopcore/model/basket/items/BasketItem.java b/src/org/mxchange/jshopcore/model/basket/items/BasketItem.java
new file mode 100644 (file)
index 0000000..1ff3aea
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jshopcore.model.basket.items;
+
+import org.mxchange.jshopcore.model.basket.AddableBasketItem;
+import org.mxchange.jshopcore.model.product.Product;
+
+/**
+ * A general basket item
+ *
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+public class BasketItem extends BaseItem implements AddableBasketItem {
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 52_749_158_492_581_578L;
+
+       /**
+        * Default constructor
+        */
+       public BasketItem () {
+       }
+
+       /**
+        * Constructor for an item from given Product instance
+        * 
+        * @param product Product instance
+        */
+       public BasketItem (final Product product) {
+               // Call default constructor
+               this();
+
+               // product must not be null
+               if (null == product) {
+                       // Abort here
+                       throw new NullPointerException("product is null"); //NOI18N
+               }
+
+               // Copy all neccessary values
+               this.setProductId(product.getId());
+               this.setItemType("product"); //NOI18N
+
+               // Copy instance
+               this.setProduct(product);
+       }
+
+       /**
+        * Constructor for an item from given Product instance and amount.
+        * 
+        * @param product Product instance
+        * @param amount Ordered amount
+        */
+       public BasketItem (final Product product, final Long amount) {
+               // Other constructor
+               this(product);
+
+               // amount must not be null
+               if (null == amount) {
+                       // Abort here
+                       throw new NullPointerException("amount is null"); //NOI18N
+               }
+
+               // Set amount
+               this.setAmount(amount);
+       }
+}
index bb371cdf7bc64d39cb0dd9c24eac32379ac62d1c..aa2757b7c357619f829ed934e9456fa7c85a1808 100644 (file)
 package org.mxchange.jshopcore.model.category;
 
 import java.util.Objects;
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
 
 /**
  * A general product category class
  *
  * @author Roland Haeder<roland@mxchange.org>
  */
+@Entity(name = "Category")
+@Table(name = "category")
 public abstract class BaseCategory implements Category, Comparable<Category> {
+
        /**
         * Serial number
         */
@@ -32,16 +41,22 @@ public abstract class BaseCategory implements Category, Comparable<Category> {
        /**
         * Id number of category
         */
+       @Id
+       @GeneratedValue
+       @Column(length = 20)
        private Long id;
 
        /**
         * Parent category id
         */
+       @Column(name = "parent_id", length = 20)
        private Long parentId;
 
        /**
         * Title of category
         */
+       @Basic(optional = false)
+       @Column(length = 100, nullable = false, unique = true)
        private String title;
 
        /**
@@ -90,6 +105,13 @@ public abstract class BaseCategory implements Category, Comparable<Category> {
                return 1;
        }
 
+       @Override
+       public void copyAll (final Category category) {
+               // Copy all data
+               this.setParentId(category.getParentId());
+               this.setTitle(category.getTitle());
+       }
+
        /**
         * Id number of category
         *
index f91422bd1f7eef97b3770bbb6fc62fcbc6b0a13f..5f871c657284d95973c00b33c81e290fe68b2282 100644 (file)
@@ -25,6 +25,13 @@ import java.io.Serializable;
  */
 public interface Category extends Serializable {
 
+       /**
+        * Copies all properties from other category to this
+        *
+        * @param category Source category instance
+        */
+       public void copyAll (final Category category);
+
        /**
         * Id number of category
         * @return the id
index d3a16206aabd496ea5f76e903f7763cfadf02028..559c112f57ebf7c3738bbf3055dcd9f09e9629c4 100644 (file)
@@ -16,6 +16,7 @@
  */
 package org.mxchange.jshopcore.model.customer;
 
+import java.io.Serializable;
 import java.sql.Timestamp;
 import org.mxchange.jcore.model.contact.Contact;
 
@@ -24,21 +25,28 @@ import org.mxchange.jcore.model.contact.Contact;
  *
  * @author Roland Haeder<roland@mxchange.org>
  */
-public interface Customer extends Contact {
+public interface Customer extends Serializable {
 
        /**
-        * Setter for id number from "contact" table
+        * Copies all attributes from other customer object to this
         *
-        * @param contactId Contact id number
+        * @param customer Source instance
         */
-       public void setContactId (final long contactId);
+       public void copyAll (final Customer customer);
 
        /**
-        * Getter for id number from "contact" table
+        * Setter for contact instance
+        *
+        * @param contact Contact instance
+        */
+       public void setContact (final Contact contact);
+
+       /**
+        * Getter for contact instance
         *
         * @return Contact id number
         */
-       public long getContactId();
+       public Contact getContact();
 
        /**
         * Getter for confirmation key
index 0a58bd431a64136504a0585fbbb53630fb66d288..ce15d8f8df538c2327730b25eeb8ff917d9d94a6 100644 (file)
  */
 package org.mxchange.jshopcore.model.customer;
 
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityNotFoundException;
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.digest.Sha2Crypt;
 import org.mxchange.jcore.BaseFrameworkSystem;
+import org.mxchange.jshopcore.model.order.Orderable;
+import org.mxchange.jshopcore.model.order.ShopOrder;
 
 /**
  * An utilities class for customers
@@ -40,21 +41,25 @@ public class CustomerUtils extends BaseFrameworkSystem {
        /**
         * Generates an unique customer number by checking is existence
         *
-        * @param connection Connection instance
+        * @param em Entity manager instance
         * @return Generated customer number (not used before)
         * @throws java.sql.SQLException If any SQL error occured
         */
-       public static String generateCustomerNumber (final Connection connection) throws SQLException {
+       public static String generateCustomerNumber (final EntityManager em) throws SQLException {
                // Trace message
                // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection));
-               // connection cannot be null
-               if (null == connection) {
+
+               // em cannot be null
+               if (null == em) {
                        // Abort here
-                       throw new NullPointerException("connection is null"); //NOI18N
+                       throw new NullPointerException("em is null"); //NOI18N
+               } else if (!em.isOpen()) {
+                       // Not open
+                       throw new IllegalStateException("Entity manager is closed.");
                }
 
-               // Prepare statement
-               PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `customer` WHERE `customer_number` = ? LIMIT 1"); //NOI18N
+               // Fake customer instance
+               Customer customer = null;
 
                // Generate number
                String customerNumber = null;
@@ -73,23 +78,19 @@ public class CustomerUtils extends BaseFrameworkSystem {
                        // Generate new number
                        customerNumber = String.format("%s-%s", part1, part2); //NOI18N
 
-                       // Debug message
-                       // TODO: utils.getLogger().logDebug(MessageFormat.format("generateCustomerNumber: customerNumber={0}", customerNumber));
-                       // Insert customer number
-                       statement.setString(1, customerNumber);
-
-                       // Find it
-                       statement.execute();
-
-                       // Get result
-                       ResultSet result = statement.getResultSet();
-
-                       // Try to get next result
-                       isFound = result.next();
+                       // Try it
+                       try {
+                               // Get instance
+                               customer = em.getReference(ShopCustomer.class, customerNumber);
+                       } catch (final EntityNotFoundException ex) {
+                               // Not found
+                               isFound = false;
+                       }
                }
 
                // Trace message
                // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
+
                // Found one
                return customerNumber;
        }
@@ -97,22 +98,25 @@ public class CustomerUtils extends BaseFrameworkSystem {
        /**
         * Generates an unique access key.
         *
-        * @param connection Connection instance
+        * @param em Entity manager instance
         * @param customer Customer instance
         * @return An unique access key
-        * @throws java.sql.SQLException If any SQL error occured
         */
-       public static String generateAccessKey (final Connection connection, final Customer customer) throws SQLException {
+       public static String generateAccessKey (final EntityManager em, final Customer customer) {
                // Trace message
                // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
-               // connection cannot be null
-               if (null == connection) {
+
+               // em cannot be null
+               if (null == em) {
                        // Abort here
-                       throw new NullPointerException("connection is null"); //NOI18N
+                       throw new NullPointerException("em is null"); //NOI18N
+               } else if (!em.isOpen()) {
+                       // Not open
+                       throw new IllegalStateException("Entity manager is closed.");
                }
 
-               // Prepare statement
-               PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `orders` WHERE `access_key` = ? LIMIT 1"); //NOI18N
+               // Generate fake order instance
+               Orderable orderable = null;
 
                // Generate access keyy
                String accessKey = null;
@@ -123,28 +127,24 @@ public class CustomerUtils extends BaseFrameworkSystem {
                // Is the number used?
                while (isFound) {
                        // Both number parts
-                       String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1000000)), connection, customer.getCustomerNumber());
+                       String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1000000)), em, customer.getCustomerNumber());
 
-                       // Generate new number
+                       // Generate access key, use SHA512 hashing and BASE64-encoding for strong key generation
                        accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
 
-                       // Debug message
-                       // TODO: utils.getLogger().logDebug(MessageFormat.format("generateAccessKey: accessKey={0}", accessKey));
-                       // Insert customer number
-                       statement.setString(1, accessKey);
-
-                       // Find it
-                       statement.execute();
-
-                       // Get result
-                       ResultSet result = statement.getResultSet();
-
-                       // Try to get next result
-                       isFound = result.next();
+                       // Try this
+                       try {
+                               // Get reference
+                               orderable = em.getReference(ShopOrder.class, accessKey);
+                       } catch (final EntityNotFoundException ex) {
+                               // Not found, so abort loop here
+                               isFound = false;
+                       }
                }
 
                // Trace message
                // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));
+
                // Found one
                return accessKey;
        }
index 9b8331bfd563bfc623b2cc9c9057d9c1424612e4..66953563fc7f36d1eb453a4573b9b07e4c22c966 100644 (file)
@@ -29,6 +29,7 @@ import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
 import org.mxchange.jcore.model.contact.BaseContact;
+import org.mxchange.jcore.model.contact.Contact;
 
 /**
  * A shop customer class.
@@ -37,7 +38,7 @@ import org.mxchange.jcore.model.contact.BaseContact;
  */
 @Entity (name = "Customer")
 @Table (name = "customer")
-public class ShopCustomer extends BaseContact implements Customer {
+public class ShopCustomer implements Customer {
 
        /**
         * Serial number
@@ -58,7 +59,7 @@ public class ShopCustomer extends BaseContact implements Customer {
        @JoinColumn (table = "contacts", unique = true)
        @OneToOne (optional = false, targetEntity = BaseContact.class, orphanRemoval = true)
        @Column (name = "customer_contact_id", nullable = false, length = 20)
-       private long contactId;
+       private Contact contact;
 
        /**
         * Confirmation key
@@ -100,13 +101,24 @@ public class ShopCustomer extends BaseContact implements Customer {
        private String customerStatus;
 
        @Override
-       public long getContactId () {
-               return this.contactId;
+       public void copyAll (final Customer customer) {
+               // Copy other data
+               this.setCustomerConfirmKey(customer.getCustomerConfirmKey());
+               this.setCustomerNumber(customer.getCustomerNumber());
+               this.setCustomerPasswordHash(customer.getCustomerPasswordHash());
+               this.setCustomerStatus(customer.getCustomerStatus());
+               this.setCustomerCreated(customer.getCustomerCreated());
+               this.setCustomerLocked(customer.getCustomerLocked());
        }
 
        @Override
-       public void setContactId (final long contactId) {
-               this.contactId = contactId;
+       public Contact getContact () {
+               return this.contact;
+       }
+
+       @Override
+       public void setContact (final Contact contact) {
+               this.contact = contact;
        }
 
        @Override
diff --git a/src/org/mxchange/jshopcore/model/item/BaseItem.java b/src/org/mxchange/jshopcore/model/item/BaseItem.java
deleted file mode 100644 (file)
index 8e4591e..0000000
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
- * Copyright (C) 2015 Roland Haeder
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jshopcore.model.item;
-
-import java.util.Objects;
-import org.mxchange.jshopcore.model.basket.AddableBasketItem;
-import org.mxchange.jshopcore.model.product.Product;
-
-/**
- * An item (addedable to a basket) could respresent a product or a discount
- * coupon. This depends on the type of the item.
- *
- * @author Roland Haeder<roland@mxchange.org>
- */
-public abstract class BaseItem implements AddableBasketItem, Comparable<AddableBasketItem> {
-
-       /**
-        * Serial number
-        */
-       private static final long serialVersionUID = 24_348_671_457_829_156L;
-
-       /**
-        * Entry id (from database backend)
-        */
-       private Long id;
-
-       /**
-        * Item amount
-        */
-       private Long amount;
-
-       /**
-        * Item id number
-        */
-       private Long itemId;
-
-       /**
-        * Item type
-        */
-       private String itemType;
-
-       /**
-        * Item instance
-        */
-       private Product product;
-
-       @Override
-       public int compareTo (final AddableBasketItem item) {
-               // item should not be null
-               if (null == item) {
-                       throw new NullPointerException("item is null"); //NOI18N
-               }
-
-               // Is the id the same?
-               if (Objects.equals(this.getItemId(), item.getItemId())) {
-                       // Same id, means same item
-                       return 0;
-               } else if (this.getItemId() > item.getItemId()) {
-                       // This id is larger than compared to
-                       return -1;
-               }
-
-               // The other id is larger
-               return 1;
-       }
-
-       @Override
-       public Long getAmount () {
-               return this.amount;
-       }
-
-       @Override
-       public void setAmount (final Long amount) {
-               this.amount = amount;
-       }
-
-       @Override
-       public Long getId () {
-               return this.id;
-       }
-
-       @Override
-       public void setId (final Long id) {
-               this.id = id;
-       }
-
-       @Override
-       public Long getItemId () {
-               return this.itemId;
-       }
-
-       @Override
-       public void setItemId (final Long itemId) {
-               this.itemId = itemId;
-       }
-
-       @Override
-       public String getItemType () {
-               return this.itemType;
-       }
-
-       @Override
-       public void setItemType (final String itemType) {
-               this.itemType = itemType;
-       }
-
-       @Override
-       public Product getProduct () {
-               return this.product;
-       }
-
-       @Override
-       public void setProduct (final Product product) {
-               this.product = product;
-       }
-
-       @Override
-       public boolean equals (final Object object) {
-               // Is it same type?
-               if (!(object instanceof BaseItem)) {
-                       // Not equal types
-                       return false;
-               } else if (!(object instanceof AddableBasketItem)) {
-                       // Not correct interface
-                       return false;
-               }
-
-               // Securely cast to wanted interface
-               AddableBasketItem item = (AddableBasketItem) object;
-
-               // Item id and type must be the same
-               return ((Objects.equals(item.getItemId(), this.getItemId()))
-                               && (Objects.equals(item.getItemType(), this.getItemType())));
-       }
-
-       @Override
-       public int hashCode () {
-               int hash = 5;
-               hash = 29 * hash + Objects.hashCode(this.getItemId());
-               hash = 29 * hash + Objects.hashCode(this.getItemType());
-               return hash;
-       }
-
-       @Override
-       public boolean isProductType () {
-               // Is the instance set?
-               return (this.getProduct() instanceof Product);
-       }
-}
diff --git a/src/org/mxchange/jshopcore/model/item/BasketItem.java b/src/org/mxchange/jshopcore/model/item/BasketItem.java
deleted file mode 100644 (file)
index b09b273..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Copyright (C) 2015 Roland Haeder
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jshopcore.model.item;
-
-import org.mxchange.jshopcore.model.basket.AddableBasketItem;
-import org.mxchange.jshopcore.model.product.Product;
-
-/**
- * A general basket item
- *
- * @author Roland Haeder<roland@mxchange.org>
- */
-public class BasketItem extends BaseItem implements AddableBasketItem {
-       /**
-        * Serial number
-        */
-       private static final long serialVersionUID = 52_749_158_492_581_578L;
-
-       /**
-        * Default constructor
-        */
-       public BasketItem () {
-       }
-
-       /**
-        * Constructor for an item from given Product instance
-        * 
-        * @param product Product instance
-        */
-       public BasketItem (final Product product) {
-               // Call default constructor
-               this();
-
-               // product must not be null
-               if (null == product) {
-                       // Abort here
-                       throw new NullPointerException("product is null"); //NOI18N
-               }
-
-               // Copy all neccessary values
-               this.setItemId(product.getId());
-               this.setItemType("product"); //NOI18N
-
-               // Copy instance
-               this.setProduct(product);
-       }
-
-       /**
-        * Constructor for an item from given Product instance and amount.
-        * 
-        * @param product Product instance
-        * @param amount Ordered amount
-        */
-       public BasketItem (final Product product, final Long amount) {
-               // Other constructor
-               this(product);
-
-               // amount must not be null
-               if (null == amount) {
-                       // Abort here
-                       throw new NullPointerException("amount is null"); //NOI18N
-               }
-
-               // Set amount
-               this.setAmount(amount);
-       }
-}
index 93f75ffba48a61789dadaf24261ff7166d6b875e..381c43e2ded87e755c9afcf303ffeb86d5e41777 100644 (file)
@@ -18,6 +18,9 @@ package org.mxchange.jshopcore.model.order;
 
 import java.io.Serializable;
 import java.sql.Timestamp;
+import java.util.List;
+import org.mxchange.jshopcore.model.basket.AddableBasketItem;
+import org.mxchange.jshopcore.model.customer.Customer;
 
 /**
  * An interface for customer orders
@@ -26,6 +29,20 @@ import java.sql.Timestamp;
  */
 public interface Orderable extends Serializable {
 
+       /**
+        * Getter for access key
+        *
+        * @return Access key
+        */
+       public String getAccessKey ();
+
+       /**
+        * Setter for access key
+        *
+        * @param accessKey Access key
+        */
+       public void setAccessKey (final String accessKey);
+
        /**
         * Getter for order id
         *
@@ -41,18 +58,18 @@ public interface Orderable extends Serializable {
        public void setId (final Long id);
 
        /**
-        * Getter for customer id
+        * Getter for customer instance
         *
-        * @return Customer id
+        * @return Customer instance
         */
-       public Long getCustomerId ();
+       public Customer getCustomer ();
 
        /**
-        * Setter for customer id
+        * Setter for customer instance
         *
-        * @param customerId Customer id
+        * @param customer Customer instance
         */
-       public void setCustomerId (final Long customerId);
+       public void setCustomer (final Customer customer);
 
        /**
         * Getter for created timestamp
@@ -67,4 +84,18 @@ public interface Orderable extends Serializable {
         * @param created Created timestamp
         */
        public void setCreated (final Timestamp created);
+
+       /**
+        * Getter for list of ordered basket items
+        *
+        * @return List of items
+        */
+       public List<AddableBasketItem> getOrderedItems ();
+
+       /**
+        * Setter for list of ordered basket items
+        *
+        * @param itemList List of items
+        */
+       public void setOrderedItems (final List<AddableBasketItem> itemList);
 }
index 65361ee8434d58993babb6c5fd43ee22387fcb26..1af471aad4ed2cf582195f73b63844617f5870f9 100644 (file)
 package org.mxchange.jshopcore.model.order;
 
 import java.sql.Timestamp;
+import java.util.List;
 import javax.persistence.Basic;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.JoinColumn;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
+import javax.persistence.Transient;
+import org.mxchange.jshopcore.model.basket.AddableBasketItem;
+import org.mxchange.jshopcore.model.customer.Customer;
 
 /**
  * An entity class for shop orders
@@ -49,11 +54,11 @@ public class ShopOrder implements Orderable {
        private Long id;
 
        /**
-        * Customer id
+        * Customer instance
         */
        @Basic (optional = false)
-       @Column (name = "customer_id", length = 20, nullable = false)
-       private Long customerId;
+       @JoinColumn (name = "customer_id", nullable = false, updatable = false)
+       private Customer customer;
 
        /**
         * Access key
@@ -70,6 +75,22 @@ public class ShopOrder implements Orderable {
        @Column (nullable = false)
        private Timestamp created;
 
+       /**
+        * Item list, don't save this
+        */
+       @Transient
+       private List<AddableBasketItem> itemList;
+
+       @Override
+       public String getAccessKey () {
+               return this.accessKey;
+       }
+
+       @Override
+       public void setAccessKey (final String accessKey) {
+               this.accessKey = accessKey;
+       }
+
        @Override
        public Timestamp getCreated () {
                return this.created;
@@ -81,13 +102,13 @@ public class ShopOrder implements Orderable {
        }
 
        @Override
-       public Long getCustomerId () {
-               return this.customerId;
+       public Customer getCustomer () {
+               return this.customer;
        }
 
        @Override
-       public void setCustomerId (final Long customerId) {
-               this.customerId = customerId;
+       public void setCustomer (final Customer customer) {
+               this.customer = customer;
        }
 
        @Override
@@ -99,4 +120,14 @@ public class ShopOrder implements Orderable {
        public void setId (final Long id) {
                this.id = id;
        }
+
+       @Override
+       public List<AddableBasketItem> getOrderedItems () {
+               return itemList;
+       }
+
+       @Override
+       public void setOrderedItems (final List<AddableBasketItem> itemList) {
+               this.itemList = itemList;
+       }
 }
diff --git a/src/org/mxchange/jshopcore/model/order/items/OrderItem.java b/src/org/mxchange/jshopcore/model/order/items/OrderItem.java
new file mode 100644 (file)
index 0000000..c12cf50
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jshopcore.model.order.items;
+
+import javax.persistence.Table;
+import org.mxchange.jshopcore.model.basket.AddableBasketItem;
+import org.mxchange.jshopcore.model.basket.items.BaseItem;
+
+/**
+ * A general basket item
+ *
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+@Table(name = "ordered_items")
+public class OrderItem extends BaseItem implements AddableBasketItem {
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 44_189_562_738_723_581L;
+
+       /**
+        * Default constructor
+        */
+       public OrderItem () {
+       }
+}