From 8988f562d1bd45f107dd522f31bf8394fc329184 Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Tue, 1 Dec 2009 12:24:32 -0400 Subject: Map Entitlement, add some non-nullable columns, etc. --- .../fedoraproject/candlepin/model/Entitlement.java | 55 +++++++++++++-- .../candlepin/model/EntitlementPool.java | 13 +++- .../org/fedoraproject/candlepin/model/Owner.java | 3 +- .../candlepin/model/test/EntitlementPoolTest.java | 79 ++++++++++++++++++++++ .../candlepin/test/DatabaseTestFixture.java | 19 ++++-- 5 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 proxy/code/src/org/fedoraproject/candlepin/model/test/EntitlementPoolTest.java diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java b/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java index 446e78d..e651c39 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java @@ -17,6 +17,18 @@ package org.fedoraproject.candlepin.model; import java.util.Date; import java.util.List; +import javax.persistence.CascadeType; +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.JoinTable; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import javax.persistence.Transient; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @@ -40,23 +52,56 @@ import javax.xml.bind.annotation.XmlTransient; */ @XmlRootElement @XmlAccessorType(XmlAccessType.PROPERTY) -public class Entitlement extends BaseModel { +@Entity +@Table(name="cp_entitlement") +public class Entitlement { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Long id; + + @ManyToOne + @JoinColumn(nullable=false) private Owner owner; + + @ManyToOne + @JoinColumn(nullable=false) private EntitlementPool pool; + + @Transient +// @OneToMany(targetEntity=Product.class, cascade=CascadeType.ALL) +// @JoinTable(name="cp_product_hierarchy", +// joinColumns=@JoinColumn(name="PARENT_PRODUCT_ID"), +// inverseJoinColumns=@JoinColumn(name="CHILD_PRODUCT_ID")) private List childEntitlements; private Date startDate; + public Entitlement() { + } + /** - * default ctor + * @return the id */ - public Entitlement() { - super(null); + public Long getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(Long id) { + this.id = id; + } + + public Entitlement(EntitlementPool poolIn, Owner ownerIn, Date startDateIn) { + pool = poolIn; + owner = ownerIn; + startDate = startDateIn; } /** - * @return the org + * @return the owner */ @XmlTransient public Owner getOwner() { diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java index 7ffa155..7829048 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java @@ -16,10 +16,12 @@ package org.fedoraproject.candlepin.model; import java.util.Date; +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.ManyToOne; import javax.persistence.Table; import javax.xml.bind.annotation.XmlAccessType; @@ -42,17 +44,23 @@ public class EntitlementPool { private Long id; @ManyToOne + @JoinColumn(nullable=false) private Owner owner; @ManyToOne + @JoinColumn(nullable=false) private Product product; + @Column(nullable=false) private Long maxMembers; - + + @Column(nullable=false) private Long currentMembers; + @Column(nullable=false) private Date startDate; + @Column(nullable=false) private Date endDate; public EntitlementPool() { @@ -65,6 +73,9 @@ public class EntitlementPool { this.maxMembers = maxMembersIn; this.startDate = startDateIn; this.endDate = endDateIn; + + // Always assume no current members if creating a new pool. + this.currentMembers = new Long(0); } /** diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java b/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java index eace64c..fe41fb7 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java @@ -22,6 +22,7 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; import javax.xml.bind.annotation.XmlAccessType; @@ -52,7 +53,7 @@ public class Owner { @Transient private List consumers; - @Transient + @OneToMany(mappedBy="owner", targetEntity=EntitlementPool.class) private List entitlementPools; @Transient diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/EntitlementPoolTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/EntitlementPoolTest.java new file mode 100644 index 0000000..2517d3b --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/EntitlementPoolTest.java @@ -0,0 +1,79 @@ +/** + * Copyright (c) 2009 Red Hat, Inc. + * + * This software is licensed to you under the GNU General Public License, + * version 2 (GPLv2). There is NO WARRANTY for this software, express or + * implied, including the implied warranties of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 + * along with this software; if not, see + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. + * + * Red Hat trademarks are not licensed under GPLv2. No permission is + * granted to use or replicate Red Hat trademarks that are incorporated + * in this software or its documentation. + */ +package org.fedoraproject.candlepin.model.test; + +import java.sql.Date; + +import java.util.Calendar; + +import org.fedoraproject.candlepin.model.EntitlementPool; +import org.fedoraproject.candlepin.model.Owner; +import org.fedoraproject.candlepin.model.Product; + +import org.fedoraproject.candlepin.test.DatabaseTestFixture; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + + +public class EntitlementPoolTest extends DatabaseTestFixture { + + private EntitlementPool pool; + private Product prod; + private Owner owner; + + @Before + public void createObjects() { + beginTransaction(); + String ownerName = "Example Corporation"; + owner = new Owner(ownerName); + em.persist(owner); + prod = new Product("cptest-label", "My Product"); + em.persist(prod); + commitTransaction(); + beginTransaction(); + pool = new EntitlementPool(owner, prod, new Long(1000), + createDate(2009, 11, 30), createDate(2015, 11, 30)); + em.persist(pool); + commitTransaction(); + } + + private Date createDate(int year, int month, int day) { + Calendar cal = Calendar.getInstance(); + + cal.set(Calendar.YEAR, year); + cal.set(Calendar.MONTH, month); + cal.set(Calendar.DATE, day); + + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + + Date jsqlD = new Date(cal.getTime().getTime()); + return jsqlD; + } + + @Test + public void testCreate() { + EntitlementPool lookedUp = (EntitlementPool)em.find(EntitlementPool.class, pool.getId()); + assertNotNull(lookedUp); + assertEquals(owner.getId(), lookedUp.getOwner().getId()); + assertEquals(prod.getId(), lookedUp.getProduct().getId()); + + } + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/test/DatabaseTestFixture.java b/proxy/code/src/org/fedoraproject/candlepin/test/DatabaseTestFixture.java index 485b982..286e07b 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/test/DatabaseTestFixture.java +++ b/proxy/code/src/org/fedoraproject/candlepin/test/DatabaseTestFixture.java @@ -8,6 +8,7 @@ import javax.persistence.EntityTransaction; import org.fedoraproject.candlepin.model.Consumer; import org.fedoraproject.candlepin.model.ConsumerInfo; import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.model.Entitlement; import org.fedoraproject.candlepin.model.EntitlementPool; import org.fedoraproject.candlepin.model.Owner; import org.fedoraproject.candlepin.model.Product; @@ -46,6 +47,18 @@ public class DatabaseTestFixture { beginTransaction(); } + List ents = em.createQuery("from Entitlement e"). + getResultList(); + for (Entitlement e : ents) { + em.remove(e); + } + + List pools = em.createQuery("from EntitlementPool p"). + getResultList(); + for (EntitlementPool p : pools) { + em.remove(p); + } + // TODO: Would rather be doing this, but such a bulk delete does not seem to respect // the cascade to child products and thus fails. // em.createQuery("delete from Product").executeUpdate(); @@ -77,12 +90,6 @@ public class DatabaseTestFixture { em.remove(c); } - List pools = em.createQuery("from EntitlementPool p"). - getResultList(); - for (EntitlementPool p : pools) { - em.remove(p); - } - commitTransaction(); em.close(); } -- cgit From 449f14c6a0eae1253c5987edd16429def8e3d1f2 Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Tue, 1 Dec 2009 12:43:12 -0400 Subject: Remove Entitlement.childEntitlements. Don't think we'll need an entitlement hierarchy. Entitlements already map to pools which map to products, these are in a hierarchy already. --- .../fedoraproject/candlepin/model/Entitlement.java | 27 ---------------------- .../org/fedoraproject/candlepin/model/Owner.java | 1 + 2 files changed, 1 insertion(+), 27 deletions(-) diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java b/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java index e651c39..f6c50db 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java @@ -68,13 +68,6 @@ public class Entitlement { @JoinColumn(nullable=false) private EntitlementPool pool; - @Transient -// @OneToMany(targetEntity=Product.class, cascade=CascadeType.ALL) -// @JoinTable(name="cp_product_hierarchy", -// joinColumns=@JoinColumn(name="PARENT_PRODUCT_ID"), -// inverseJoinColumns=@JoinColumn(name="CHILD_PRODUCT_ID")) - private List childEntitlements; - private Date startDate; public Entitlement() { @@ -115,21 +108,6 @@ public class Entitlement { this.owner = ownerIn; } - /** - * @return the childEntitlements - */ - public List getChildEntitlements() { - return childEntitlements; - } - - /** - * @param childEntitlements the childEntitlements to set - */ - public void setChildEntitlements(List childEntitlements) { - this.childEntitlements = childEntitlements; - } - - /** * @return Returns the product. */ @@ -137,7 +115,6 @@ public class Entitlement { return this.pool.getProduct(); } - /** * @return Returns the pool. */ @@ -145,7 +122,6 @@ public class Entitlement { return pool; } - /** * @param poolIn The pool to set. */ @@ -153,7 +129,6 @@ public class Entitlement { pool = poolIn; } - /** * @return Returns the startDate. */ @@ -161,13 +136,11 @@ public class Entitlement { return startDate; } - /** * @param startDateIn The startDate to set. */ public void setStartDate(Date startDateIn) { startDate = startDateIn; } - } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java b/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java index fe41fb7..46f8999 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Owner.java @@ -53,6 +53,7 @@ public class Owner { @Transient private List consumers; + // EntitlementPool is the owning side of this relationship. @OneToMany(mappedBy="owner", targetEntity=EntitlementPool.class) private List entitlementPools; -- cgit