From: Roland Haeder Date: Wed, 23 Sep 2015 10:39:39 +0000 (+0200) Subject: Continued with JPA: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6126a768e5387d79605a680f24421a2662b02c24;p=jshop-core.git Continued with JPA: - moved BaseItem and BasketItem to other package - added more JPA stuff (sorry) - updated jcore.jar Signed-off-by:Roland Häder Signed-off-by:Roland Häder --- diff --git a/lib/jcore.jar b/lib/jcore.jar index ead5916..e17de0c 100644 Binary files a/lib/jcore.jar and b/lib/jcore.jar differ diff --git a/nbproject/project.properties b/nbproject/project.properties index 4776cf1..5655ed7 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -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 index 0000000..8f02c12 --- /dev/null +++ b/src/org/mxchange/jcore/model/contact/CustomerContact.java @@ -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 . + */ +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; +} diff --git a/src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java b/src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java index afd51fa..a92211e 100644 --- a/src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java +++ b/src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java @@ -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 diff --git a/src/org/mxchange/jshopcore/model/basket/AddableBasketItem.java b/src/org/mxchange/jshopcore/model/basket/AddableBasketItem.java index 7ed0335..1c29978 100644 --- a/src/org/mxchange/jshopcore/model/basket/AddableBasketItem.java +++ b/src/org/mxchange/jshopcore/model/basket/AddableBasketItem.java @@ -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 index 0000000..de2f2c0 --- /dev/null +++ b/src/org/mxchange/jshopcore/model/basket/items/BaseItem.java @@ -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 . + */ +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 + */ +@Entity(name = "item") +public abstract class BaseItem implements AddableBasketItem, Comparable { + + /** + * 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 index 0000000..1ff3aea --- /dev/null +++ b/src/org/mxchange/jshopcore/model/basket/items/BasketItem.java @@ -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 . + */ +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 + */ +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); + } +} diff --git a/src/org/mxchange/jshopcore/model/category/BaseCategory.java b/src/org/mxchange/jshopcore/model/category/BaseCategory.java index bb371cd..aa2757b 100644 --- a/src/org/mxchange/jshopcore/model/category/BaseCategory.java +++ b/src/org/mxchange/jshopcore/model/category/BaseCategory.java @@ -17,13 +17,22 @@ 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 */ +@Entity(name = "Category") +@Table(name = "category") public abstract class BaseCategory implements Category, Comparable { + /** * Serial number */ @@ -32,16 +41,22 @@ public abstract class BaseCategory implements Category, Comparable { /** * 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 { return 1; } + @Override + public void copyAll (final Category category) { + // Copy all data + this.setParentId(category.getParentId()); + this.setTitle(category.getTitle()); + } + /** * Id number of category * diff --git a/src/org/mxchange/jshopcore/model/category/Category.java b/src/org/mxchange/jshopcore/model/category/Category.java index f91422b..5f871c6 100644 --- a/src/org/mxchange/jshopcore/model/category/Category.java +++ b/src/org/mxchange/jshopcore/model/category/Category.java @@ -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 diff --git a/src/org/mxchange/jshopcore/model/customer/Customer.java b/src/org/mxchange/jshopcore/model/customer/Customer.java index d3a1620..559c112 100644 --- a/src/org/mxchange/jshopcore/model/customer/Customer.java +++ b/src/org/mxchange/jshopcore/model/customer/Customer.java @@ -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 */ -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 diff --git a/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java b/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java index 0a58bd4..ce15d8f 100644 --- a/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java +++ b/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java @@ -16,13 +16,14 @@ */ 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; } diff --git a/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java b/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java index 9b8331b..6695356 100644 --- a/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java +++ b/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java @@ -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 index 8e4591e..0000000 --- a/src/org/mxchange/jshopcore/model/item/BaseItem.java +++ /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 . - */ -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 - */ -public abstract class BaseItem implements AddableBasketItem, Comparable { - - /** - * 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 index b09b273..0000000 --- a/src/org/mxchange/jshopcore/model/item/BasketItem.java +++ /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 . - */ -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 - */ -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); - } -} diff --git a/src/org/mxchange/jshopcore/model/order/Orderable.java b/src/org/mxchange/jshopcore/model/order/Orderable.java index 93f75ff..381c43e 100644 --- a/src/org/mxchange/jshopcore/model/order/Orderable.java +++ b/src/org/mxchange/jshopcore/model/order/Orderable.java @@ -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 getOrderedItems (); + + /** + * Setter for list of ordered basket items + * + * @param itemList List of items + */ + public void setOrderedItems (final List itemList); } diff --git a/src/org/mxchange/jshopcore/model/order/ShopOrder.java b/src/org/mxchange/jshopcore/model/order/ShopOrder.java index 65361ee..1af471a 100644 --- a/src/org/mxchange/jshopcore/model/order/ShopOrder.java +++ b/src/org/mxchange/jshopcore/model/order/ShopOrder.java @@ -17,15 +17,20 @@ 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 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 getOrderedItems () { + return itemList; + } + + @Override + public void setOrderedItems (final List 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 index 0000000..c12cf50 --- /dev/null +++ b/src/org/mxchange/jshopcore/model/order/items/OrderItem.java @@ -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 . + */ +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 + */ +@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 () { + } +}