diff options
| author | Mike McCune <mmccune@gibson.pdx.redhat.com> | 2009-06-25 17:35:48 -0700 |
|---|---|---|
| committer | Mike McCune <mmccune@gibson.pdx.redhat.com> | 2009-06-25 17:36:07 -0700 |
| commit | 463c90ebcb3de09da113200d2fd68419f28a9415 (patch) | |
| tree | 8c90d89c367e760fbf2edb3b99b68d1d79b51505 /proxy/code/src | |
| parent | eb9c1dfff7c7611ac29d60528007c82812a545ae (diff) | |
| download | candlepin-463c90ebcb3de09da113200d2fd68419f28a9415.tar.gz candlepin-463c90ebcb3de09da113200d2fd68419f28a9415.tar.xz candlepin-463c90ebcb3de09da113200d2fd68419f28a9415.zip | |
adding java model code
Diffstat (limited to 'proxy/code/src')
9 files changed, 745 insertions, 0 deletions
diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/BaseModel.java b/proxy/code/src/org/fedoraproject/candlepin/model/BaseModel.java new file mode 100644 index 0000000..0091949 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/BaseModel.java @@ -0,0 +1,66 @@ +/** + * 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; + +import java.util.UUID; + +/** + * @author mmccune + * + */ +public class BaseModel { + + private String uuid; + private String name; + + /** + * Construct new with UUID + * @param uuid + */ + public BaseModel(String uuid) { + this.uuid = uuid; + } + + /** + * Default constructor + */ + public BaseModel() { + + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + /** + * Generate a UUID for an object. + * @return String UUID. + */ + public static String generateUUID() { + return UUID.randomUUID().toString(); + } +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java new file mode 100644 index 0000000..b97ee5e --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -0,0 +1,96 @@ +/** + * 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; + +import java.util.LinkedList; +import java.util.List; + + +public class Consumer extends BaseModel { + + private String type; + private Organization organization; + private Consumer parent; + private List<Product> consumedProducts; + + /** + * @param uuid + */ + public Consumer(String uuid) { + super(uuid); + } + /** + * @return the type + */ + public String getType() { + return type; + } + /** + * @param type the type to set + */ + public void setType(String type) { + this.type = type; + } + /** + * @return the parent + */ + public Consumer getParent() { + return parent; + } + /** + * @param parent the parent to set + */ + public void setParent(Consumer parent) { + this.parent = parent; + } + /** + * @return the consumedProducts + */ + public List<Product> getConsumedProducts() { + return consumedProducts; + } + /** + * @param consumedProducts the consumedProducts to set + */ + public void setConsumedProducts(List<Product> consumedProducts) { + this.consumedProducts = consumedProducts; + } + /** + * @return the organization + */ + public Organization getOrganization() { + return organization; + } + /** + * @param organization the organization to set + */ + public void setOrganization(Organization organization) { + this.organization = organization; + } + + /** + * Add a Product to this Consumer. + * + */ + public void addConsumedProduct(Product p) { + if (this.consumedProducts == null) { + this.consumedProducts = new LinkedList<Product>(); + } + this.consumedProducts.add(p); + + } + + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java b/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java new file mode 100644 index 0000000..a573ad9 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Entitlement.java @@ -0,0 +1,53 @@ +/** + * 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; + +import java.util.List; + + +public class Entitlement extends BaseModel { + + private Organization org; + private List<Entitlement> childEntitlements; + + /** + * @return the org + */ + public Organization getOrg() { + return org; + } + + /** + * @param org the org to set + */ + public void setOrg(Organization org) { + this.org = org; + } + + /** + * @return the childEntitlements + */ + public List<Entitlement> getChildEntitlements() { + return childEntitlements; + } + + /** + * @param childEntitlements the childEntitlements to set + */ + public void setChildEntitlements(List<Entitlement> childEntitlements) { + this.childEntitlements = childEntitlements; + } + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java new file mode 100644 index 0000000..aa59c62 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/EntitlementPool.java @@ -0,0 +1,120 @@ +/** + * 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; + +import java.util.Date; + + +public class EntitlementPool extends BaseModel { + + private Organization organization; + private Product product; + private long maxMembers; + private long currentMembers; + + private Date startDate; + private Date endDate; + + /** + * @param uuid + */ + public EntitlementPool(String uuid) { + super(uuid); + } + + /** + * Default const + */ + public EntitlementPool() { + + } + + /** + * @return the product + */ + public Product getProduct() { + return product; + } + /** + * @param product the product to set + */ + public void setProduct(Product product) { + this.product = product; + } + /** + * @return the startDate + */ + public Date getStartDate() { + return startDate; + } + /** + * @param startDate the startDate to set + */ + public void setStartDate(Date startDate) { + this.startDate = startDate; + } + /** + * @return the endDate + */ + public Date getEndDate() { + return endDate; + } + /** + * @param endDate the endDate to set + */ + public void setEndDate(Date endDate) { + this.endDate = endDate; + } + /** + * @return the maxMembers + */ + public long getMaxMembers() { + return maxMembers; + } + /** + * @param maxMembers the maxMembers to set + */ + public void setMaxMembers(long maxMembers) { + this.maxMembers = maxMembers; + } + /** + * @return the currentMembers + */ + public long getCurrentMembers() { + return currentMembers; + } + /** + * @param currentMembers the currentMembers to set + */ + public void setCurrentMembers(long currentMembers) { + this.currentMembers = currentMembers; + } + + /** + * @return the organization + */ + public Organization getOrganization() { + return organization; + } + + /** + * @param organization the organization to set + */ + public void setOrganization(Organization organization) { + this.organization = organization; + } + + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java new file mode 100644 index 0000000..1e31f33 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java @@ -0,0 +1,115 @@ +/** + * 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; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import org.apache.commons.lang.ClassUtils; + + +/** + * @author mmccune + * + */ +public class ObjectFactory { + + private static ObjectFactory instance = new ObjectFactory(); + + private ObjectFactory() { + } + + public static ObjectFactory get() { + return instance; + } + + /** + * Lookup an Organization by UUID + * @param uuid to lookup + * @return Organization + */ + public BaseModel lookupByUUID(Class<?> clazz, String uuid) { + BaseModel retval = (BaseModel) callNewMethod(clazz.getName(), + (Object[]) null); + retval.setUuid(uuid); + return retval; + } + + /** + * Create a new instance of the classname passed in. + * + * @param className + * @return instance of class passed in. + */ + private static Object callNewMethod(String className, Object... args) { + Object retval = null; + + try { + Class<?> clazz = Thread.currentThread(). + getContextClassLoader().loadClass(className); + if (args == null || args.length == 0) { + retval = clazz.newInstance(); + } + else { + try { + Constructor[] ctors = clazz.getConstructors(); + for (Constructor ctor : ctors) { + if (isCompatible(ctor.getParameterTypes(), args)) { + return ctor.newInstance(args); + } + } + } + catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } + catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + } + + } + catch (InstantiationException e) { + throw new RuntimeException(e); + } + catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + + return retval; + } + + /* This is insanity itself, but the reflection APIs ignore inheritance. + * So, if you ask for a method that accepts (Integer, HashMap, HashMap), + * and the class only has (Integer, Map, Map), you won't find the method. + * This method uses Class.isAssignableFrom to solve this problem. + */ + private static boolean isCompatible(Class[] declaredParams, Object[] params) { + if (params.length != declaredParams.length) { + return false; + } + + for (int i = 0; i < params.length; i++) { + if (!declaredParams[i].isInstance(params[i])) { + return false; + } + } + return true; + } + + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Organization.java b/proxy/code/src/org/fedoraproject/candlepin/model/Organization.java new file mode 100644 index 0000000..3fd7be4 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Organization.java @@ -0,0 +1,105 @@ +/** + * 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; + +import java.util.LinkedList; +import java.util.List; + + +public class Organization extends BaseModel { + + private List<Consumer> consumers; + private List<EntitlementPool> entitlementPools; + private List<User> users; + + /** + * @param uuid + */ + public Organization(String uuid) { + super(uuid); + } + + /** + * Default + */ + public Organization() { + } + + /** + * @return the consumers + */ + public List<Consumer> getConsumers() { + return consumers; + } + /** + * @param consumers the consumers to set + */ + public void setConsumers(List<Consumer> consumers) { + this.consumers = consumers; + } + /** + * @return the entitlementPools + */ + public List<EntitlementPool> getEntitlementPools() { + return entitlementPools; + } + /** + * @param entitlementPools the entitlementPools to set + */ + public void setEntitlementPools(List<EntitlementPool> entitlementPools) { + this.entitlementPools = entitlementPools; + } + /** + * @return the users + */ + public List<User> getUsers() { + return users; + } + /** + * @param users the users to set + */ + public void setUsers(List<User> users) { + this.users = users; + } + + /** + * Add a user. + * @param u to add to this org. + */ + public void addUser(User u) { + u.setOrganization(this); + if (this.users == null) { + this.users = new LinkedList<User>(); + } + this.users.add(u); + } + + public void addConsumer(Consumer c) { + c.setOrganization(this); + if (this.consumers == null) { + this.consumers = new LinkedList<Consumer>(); + } + this.consumers.add(c); + + } + + public void addEntitlementPool(EntitlementPool pool) { + pool.setOrganization(this); + if (this.entitlementPools == null) { + this.entitlementPools = new LinkedList<EntitlementPool>(); + } + this.entitlementPools.add(pool); + } +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Product.java b/proxy/code/src/org/fedoraproject/candlepin/model/Product.java new file mode 100644 index 0000000..b20c783 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Product.java @@ -0,0 +1,63 @@ +/** + * 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; + +import java.util.LinkedList; +import java.util.List; + + +public class Product extends BaseModel { + + private List<Product> childProducts; + + /** + * Create product with UUID + * @param uuid + */ + public Product(String uuid) { + super(uuid); + } + + /** + * Default constructor + */ + public Product() { + } + + /** + * @return the childProducts + */ + public List<Product> getChildProducts() { + return childProducts; + } + + /** + * @param childProducts the childProducts to set + */ + public void setChildProducts(List<Product> childProducts) { + this.childProducts = childProducts; + } + + /** + * Add a child of this Product. + * @param p to add + */ + public void addChildProduct(Product p) { + if (this.childProducts == null) { + this.childProducts = new LinkedList<Product>(); + } + this.childProducts.add(p); + } +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/User.java b/proxy/code/src/org/fedoraproject/candlepin/model/User.java new file mode 100644 index 0000000..96f72df --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/User.java @@ -0,0 +1,61 @@ +/** + * 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; + + +public class User extends BaseModel { + + private Organization organization; + private String login; + private String password; + + /** + * @return the login + */ + public String getLogin() { + return login; + } + /** + * @param login the login to set + */ + public void setLogin(String login) { + this.login = login; + } + /** + * @return the password + */ + public String getPassword() { + return password; + } + /** + * @param password the password to set + */ + public void setPassword(String password) { + this.password = password; + } + /** + * @return the organization + */ + public Organization getOrganization() { + return organization; + } + /** + * @param organization the organization to set + */ + public void setOrganization(Organization organization) { + this.organization = organization; + } + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/OrganizationTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/OrganizationTest.java new file mode 100644 index 0000000..d135ab0 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/OrganizationTest.java @@ -0,0 +1,66 @@ +/** + * + */ +package org.fedoraproject.candlepin.model.test; + +import java.util.UUID; + +import junit.framework.TestCase; + +import org.fedoraproject.candlepin.model.BaseModel; +import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.Entitlement; +import org.fedoraproject.candlepin.model.EntitlementPool; +import org.fedoraproject.candlepin.model.ObjectFactory; +import org.fedoraproject.candlepin.model.Organization; +import org.fedoraproject.candlepin.model.Product; +import org.fedoraproject.candlepin.model.User; + +/** + * + * + */ +public class OrganizationTest extends TestCase { + + public void testOrg() throws Exception { + Organization o = new Organization(BaseModel.generateUUID()); + assertNotNull(o); + } + + public void testLookup() throws Exception { + String lookedUp = BaseModel.generateUUID(); + Organization o = (Organization) ObjectFactory.get(). + lookupByUUID(Organization.class, lookedUp); + assertNotNull(o); + } + + public void testObjectRelationships() throws Exception { + Organization org = new Organization(BaseModel.generateUUID()); + org.setName("test-org"); + // Product + Product rhel = new Product(BaseModel.generateUUID()); + rhel.setName("Red Hat Enterprise Linux"); + + // User + User u = new User(); + u.setLogin("test-login"); + u.setPassword("redhat"); + org.addUser(u); + assertEquals(1, org.getUsers().size()); + + // Consumer + Consumer c = new Consumer(BaseModel.generateUUID()); + c.setOrganization(org); + org.addConsumer(c); + c.addConsumedProduct(rhel); + assertEquals(1, org.getConsumers().size()); + assertEquals(1, c.getConsumedProducts().size()); + + // EntitlementPool + EntitlementPool pool = new EntitlementPool(BaseModel.generateUUID()); + org.addEntitlementPool(pool); + pool.setProduct(rhel); + assertEquals(1, org.getEntitlementPools().size()); + + } +} |
