From: Roland Häder Date: Mon, 23 Oct 2017 20:47:31 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=09890d4c6b70bf949436ed014c6c3009c54e850c;p=jproduct-core.git Continued: - added created timestamps for category and product - removed deprecated copyAll() methods, as "complex" methods should be implemented outside the entity's class Signed-off-by: Roland Häder --- diff --git a/src/org/mxchange/jproduct/model/category/Category.java b/src/org/mxchange/jproduct/model/category/Category.java index 51553f7..f77dc97 100644 --- a/src/org/mxchange/jproduct/model/category/Category.java +++ b/src/org/mxchange/jproduct/model/category/Category.java @@ -17,6 +17,7 @@ package org.mxchange.jproduct.model.category; import java.io.Serializable; +import java.util.Date; /** * An interface for categories @@ -26,11 +27,18 @@ import java.io.Serializable; public interface Category extends Serializable { /** - * Copies all properties from other category to this + * Getter for created timestamp *

- * @param category Source category instance + * @return Created timestamp */ - void copyAll (final Category category); + Date getCategoryCreated (); + + /** + * Setter for created timestamp + *

+ * @param categoryCreated Created timestamp + */ + void setCategoryCreated (final Date categoryCreated); /** * Id number of category diff --git a/src/org/mxchange/jproduct/model/category/ProductCategory.java b/src/org/mxchange/jproduct/model/category/ProductCategory.java index 43c3ff5..029d6f8 100644 --- a/src/org/mxchange/jproduct/model/category/ProductCategory.java +++ b/src/org/mxchange/jproduct/model/category/ProductCategory.java @@ -16,6 +16,7 @@ */ package org.mxchange.jproduct.model.category; +import java.util.Date; import java.util.Objects; import javax.persistence.Basic; import javax.persistence.CascadeType; @@ -25,8 +26,12 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.persistence.OneToOne; import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; import javax.persistence.Transient; /** @@ -36,6 +41,11 @@ import javax.persistence.Transient; */ @Entity (name = "product_category") @Table (name = "product_category") +@NamedQueries ( + { + @NamedQuery (name = "AllProductCategories", query = "SELECT pc FROM product_category AS pc ORDER BY pc.categoryId ASC") + } +) @SuppressWarnings ("PersistenceUnitPresent") public class ProductCategory implements Category { @@ -45,6 +55,14 @@ public class ProductCategory implements Category { @Transient private static final long serialVersionUID = 21_458_945_712_659L; + /** + * When this entry has been created + */ + @Basic (optional = false) + @Column (name = "category_created", updatable = false, nullable = false) + @Temporal (TemporalType.TIMESTAMP) + private Date categoryCreated; + /** * Id number of category */ @@ -83,6 +101,9 @@ public class ProductCategory implements Category { * statistics */ public ProductCategory (final String categoryTitle, final Category parentCategory, final Boolean categoryShownInStatistics) { + // Call other constructor + this(); + // Set all here this.categoryTitle = categoryTitle; this.parentCategory = parentCategory; @@ -95,15 +116,6 @@ public class ProductCategory implements Category { public ProductCategory () { } - @Override - @Deprecated - public void copyAll (final Category category) { - // Copy all data - this.setParentCategory(category.getParentCategory()); - this.setCategoryTitle(category.getCategoryTitle()); - this.setCategoryShownInStatistics(category.getCategoryShownInStatistics()); - } - @Override public boolean equals (final Object object) { if (this == object) { @@ -114,19 +126,29 @@ public class ProductCategory implements Category { return false; } - final Category other = (Category) object; + final Category category = (Category) object; - if (!Objects.equals(this.getCategoryTitle(), other.getCategoryTitle())) { + if (!Objects.equals(this.getCategoryTitle(), category.getCategoryTitle())) { return false; - } else if (!Objects.equals(this.getCategoryId(), other.getCategoryId())) { - return false; - } else if (!Objects.equals(this.getCategoryShownInStatistics(), other.getCategoryShownInStatistics())) { + } else if (!Objects.equals(this.getCategoryId(), category.getCategoryId())) { return false; } return true; } + @Override + @SuppressWarnings ("ReturnOfDateField") + public Date getCategoryCreated () { + return this.categoryCreated; + } + + @Override + @SuppressWarnings ("AssignmentToDateFieldFromParameter") + public void setCategoryCreated (final Date categoryCreated) { + this.categoryCreated = categoryCreated; + } + @Override public Long getCategoryId () { return this.categoryId; @@ -170,8 +192,10 @@ public class ProductCategory implements Category { @Override public int hashCode () { int hash = 7; + hash = 13 * hash + Objects.hashCode(this.getCategoryId()); hash = 13 * hash + Objects.hashCode(this.getCategoryTitle()); + return hash; } diff --git a/src/org/mxchange/jproduct/model/product/GenericProduct.java b/src/org/mxchange/jproduct/model/product/GenericProduct.java index 5b91d12..2cd7d5e 100644 --- a/src/org/mxchange/jproduct/model/product/GenericProduct.java +++ b/src/org/mxchange/jproduct/model/product/GenericProduct.java @@ -16,6 +16,7 @@ */ package org.mxchange.jproduct.model.product; +import java.util.Date; import java.util.Objects; import javax.persistence.Basic; import javax.persistence.CascadeType; @@ -25,8 +26,12 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.persistence.OneToOne; import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; import javax.persistence.Transient; import org.mxchange.jproduct.model.category.Category; import org.mxchange.jproduct.model.category.ProductCategory; @@ -39,6 +44,12 @@ import org.mxchange.jproduct.model.category.ProductCategory; */ @Entity (name = "generic_products") @Table (name = "generic_products") +@NamedQueries ( + { + @NamedQuery (name = "AllProducts", query = "SELECT p FROM generic_products AS p ORDER BY p.productId ASC"), + @NamedQuery (name = "AllAvailableProducts", query = "SELECT p FROM generic_products AS p WHERE p.productAvailability=TRUE ORDER BY p.productId ASC") + } +) @SuppressWarnings ("PersistenceUnitPresent") public class GenericProduct implements Product { @@ -62,6 +73,14 @@ public class GenericProduct implements Product { @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH, optional = false) private Category productCategory; + /** + * When this product has been created + */ + @Basic(optional = false) + @Column(name = "product_created", nullable = false, updatable = false) + @Temporal(TemporalType.TIMESTAMP) + private Date productCreated; + /** * Id number of product */ @@ -81,7 +100,7 @@ public class GenericProduct implements Product { * Title of product */ @Basic (optional = false) - @Column (name = "product_title", length = 100, nullable = false) + @Column (name = "product_title", length = 100, nullable = false, unique = true) private String productTitle; /** @@ -106,16 +125,6 @@ public class GenericProduct implements Product { this.productAvailability = productAvailability; } - @Override - @Deprecated - public void copyAll (final Product product) { - // Copy all - this.setProductAvailability(product.getProductAvailability()); - this.setProductCategory(product.getProductCategory()); - this.setProductPrice(product.getProductPrice()); - this.setProductTitle(product.getProductTitle()); - } - @Override public boolean equals (final Object object) { if (this == object) { @@ -130,8 +139,6 @@ public class GenericProduct implements Product { if (!Objects.equals(this.getProductId(), product.getProductId())) { return false; - } else if (!Objects.equals(this.getProductPrice(), product.getProductPrice())) { - return false; } else if (!Objects.equals(this.getProductTitle(), product.getProductTitle())) { return false; } @@ -159,6 +166,18 @@ public class GenericProduct implements Product { this.productCategory = productCategory; } + @Override + @SuppressWarnings ("ReturnOfDateField") + public Date getProductCreated () { + return this.productCreated; + } + + @Override + @SuppressWarnings ("AssignmentToDateFieldFromParameter") + public void setProductCreated (final Date productCreated) { + this.productCreated = productCreated; + } + @Override public Long getProductId () { return this.productId; @@ -194,7 +213,6 @@ public class GenericProduct implements Product { int hash = 7; hash = 23 * hash + Objects.hashCode(this.getProductId()); - hash = 23 * hash + Objects.hashCode(this.getProductPrice()); hash = 23 * hash + Objects.hashCode(this.getProductTitle()); return hash; diff --git a/src/org/mxchange/jproduct/model/product/Product.java b/src/org/mxchange/jproduct/model/product/Product.java index feb532b..b07b2ad 100644 --- a/src/org/mxchange/jproduct/model/product/Product.java +++ b/src/org/mxchange/jproduct/model/product/Product.java @@ -17,6 +17,7 @@ package org.mxchange.jproduct.model.product; import java.io.Serializable; +import java.util.Date; import org.mxchange.jproduct.model.category.Category; /** @@ -27,11 +28,18 @@ import org.mxchange.jproduct.model.category.Category; public interface Product extends Serializable { /** - * Copies all properties from source product to this. + * Getter for created timestamp *

- * @param product Source product + * @return Created timestamp */ - void copyAll (final Product product); + Date getProductCreated (); + + /** + * Setter for created timestamp + *

+ * @param productCreated Created timestamp + */ + void setProductCreated (final Date productCreated); /** * Getter for product availability