From ce5b41ea6b626d2a41b78b832e44135847ef3c06 Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Wed, 2 Dec 2009 11:44:59 -0400 Subject: Map Consumer hierarchy. --- proxy/code/src/META-INF/persistence.xml | 4 +--- .../fedoraproject/candlepin/model/Consumer.java | 27 ++++++++++++---------- .../candlepin/model/test/ConsumerTest.java | 27 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 15 deletions(-) (limited to 'proxy/code/src') diff --git a/proxy/code/src/META-INF/persistence.xml b/proxy/code/src/META-INF/persistence.xml index a28f6ca..47559cb 100644 --- a/proxy/code/src/META-INF/persistence.xml +++ b/proxy/code/src/META-INF/persistence.xml @@ -12,9 +12,7 @@ - + diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index 5e11a6a..3e3cc66 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -66,8 +66,11 @@ public class Consumer { private Owner owner; // TODO: Is this worth mapping? Do we need a hierarchy amidst consumers? - @Transient - private Consumer parent; + @OneToMany(targetEntity=Consumer.class, cascade=CascadeType.ALL) + @JoinTable(name="cp_consumer_hierarchy", + joinColumns=@JoinColumn(name="PARENT_CONSUMER_ID"), + inverseJoinColumns=@JoinColumn(name="CHILD_CONSUMER_ID")) + private Set childConsumers; // TODO: Are we sure we want to track this explicitly? Wouldn't we examine the // entitlements we're consuming and the products associated to them for this info? @@ -91,6 +94,7 @@ public class Consumer { this.info = new ConsumerInfo(); this.info.setConsumer(this); // TODO: ??? + this.childConsumers = new HashSet(); this.consumedProducts = new HashSet(); this.entitlements = new HashSet(); } @@ -98,6 +102,7 @@ public class Consumer { public Consumer() { this.info = new ConsumerInfo(); this.info.setConsumer(this); // TODO: ??? + this.childConsumers = new HashSet(); this.consumedProducts = new HashSet(); this.entitlements = new HashSet(); } @@ -144,18 +149,16 @@ public class Consumer { type = typeIn; } - /** - * @return the parent - */ - public Consumer getParent() { - return parent; + public Set getChildConsumers() { + return childConsumers; } - /** - * @param parent the parent to set - */ - public void setParent(Consumer parent) { - this.parent = parent; + public void setChildConsumers(Set childConsumers) { + this.childConsumers = childConsumers; + } + + public void addChildConsumer(Consumer child) { + this.childConsumers.add(child); } /** diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java index aedb49d..7fccd33 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ConsumerTest.java @@ -152,4 +152,31 @@ public class ConsumerTest extends DatabaseTestFixture { assertNull(lookedUp); } + @Test + public void testConsumerHierarchy() { + beginTransaction(); + + Consumer child1 = new Consumer("child1asd", owner, consumerType); + child1.setMetadataField("foo6", "bar"); +// child1.addConsumedProduct(rhel); + em.persist(child1); + commitTransaction(); + + beginTransaction(); + Consumer child2 = new Consumer("child2", owner, consumerType); + child2.setMetadataField("foo87", "bar"); +// child2.addConsumedProduct(rhel); + em.persist(child2); + commitTransaction(); + + beginTransaction(); + consumer.addChildConsumer(child1); + consumer.addChildConsumer(child2); + em.persist(consumer); + commitTransaction(); + + Consumer lookedUp = (Consumer)em.find(Consumer.class, consumer.getId()); + assertEquals(2, lookedUp.getChildConsumers().size()); + } + } -- cgit