ItEr08S10CreacionEntidadesServidorCriterios: Erase ResourceGroup and associated classes.

This commit is contained in:
Óscar González Fernández 2009-05-14 19:19:59 +02:00 committed by Javier Moran Rua
parent d6647b3b90
commit 549852211a
10 changed files with 91 additions and 389 deletions

View file

@ -1,12 +0,0 @@
package org.navalplanner.business.resources.daos;
import org.navalplanner.business.common.daos.IGenericDao;
import org.navalplanner.business.resources.entities.ResourceGroup;
/**
* DAO interface for the <code>ResourceGroup</code> entity.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*
*/
public interface IResourceGroupDao extends IGenericDao<ResourceGroup, Long> {}

View file

@ -7,33 +7,29 @@ import org.springframework.beans.factory.annotation.Autowired;
// (I think it is not necessary).
/**
* A registry of resource DAOs. Classes in which dependency injection (DI) is
* not directly supported by Spring (e.g. entities) must use this class to
* A registry of resource DAOs. Classes in which dependency injection (DI) is
* not directly supported by Spring (e.g. entities) must use this class to
* access resource DAOs. For the rest of classes (e.g. services, tests, etc.),
* Spring DI is a more convenient option.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*
*/
public final class ResourcesDaoRegistry {
private static ResourcesDaoRegistry instance = new ResourcesDaoRegistry();
@Autowired
private IResourceDao resourceDao;
@Autowired
private IWorkerDao workerDao;
@Autowired
private IResourceGroupDao resourceGroupDao;
private ResourcesDaoRegistry() {}
private ResourcesDaoRegistry() {
}
public static ResourcesDaoRegistry getInstance() {
return instance;
}
public static IResourceDao getResourceDao() {
return getInstance().resourceDao;
}
@ -42,8 +38,4 @@ public final class ResourcesDaoRegistry {
return getInstance().workerDao;
}
public static IResourceGroupDao getResourceGroupDao() {
return getInstance().resourceGroupDao;
}
}

View file

@ -1,15 +0,0 @@
package org.navalplanner.business.resources.daos.impl;
import org.navalplanner.business.common.daos.impl.GenericDaoHibernate;
import org.navalplanner.business.resources.daos.IResourceGroupDao;
import org.navalplanner.business.resources.entities.ResourceGroup;
/**
* Hibernate DAO for the <code>ResourceGroup</code> entity.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*
*/
public class ResourceGroupDaoHibernate
extends GenericDaoHibernate<ResourceGroup, Long>
implements IResourceGroupDao {}

View file

@ -13,54 +13,34 @@ import org.navalplanner.business.resources.daos.ResourcesDaoRegistry;
/**
* This class acts as the base class for all resources.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*
*/
public abstract class Resource {
private Long id;
private ResourceGroup resourceGroup;
@SuppressWarnings("unused")
private long version;
public Long getId() {
return id;
}
public ResourceGroup getResourceGroup() {
return resourceGroup;
}
public void setResourceGroup(ResourceGroup resourceGroup) {
this.resourceGroup = resourceGroup;
}
public abstract int getDailyCapacity();
/**
* It removes the resource from the database and updates references.
* The default implementation removes the resource from the resource group
* it belongs to (if it belongs to someone) and from the database. This
* implementation should be valid for simple resources.
* It removes the resource from the database and updates references. The
* default implementation removes the resource from the resource group it
* belongs to (if it belongs to someone) and from the database. This
* implementation should be valid for simple resources.
*/
public void remove() {
/* Remove from the resource group it belongs to. */
ResourceGroup resourceGroup = getResourceGroup();
if (resourceGroup != null) {
resourceGroup.removeResource(this);
}
/* Remove from the database. */
try {
ResourcesDaoRegistry.getResourceDao().remove(getId());
} catch (InstanceNotFoundException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -1,87 +0,0 @@
package org.navalplanner.business.resources.entities;
import java.util.HashSet;
import java.util.Set;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.resources.daos.ResourcesDaoRegistry;
/**
* This class models a resource group. A resource group represents a resource
* containing other resources.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*
*/
public class ResourceGroup extends Resource {
private Set<Resource> resources = new HashSet<Resource>();
public Set<Resource> getResources() {
return resources;
}
public void setResources(Set<Resource> resources) {
this.resources = resources;
}
public void addResource(Resource resource) {
/*
* Remove resource from its current resource group (if it belongs to
* one).
*/
if (resource.getResourceGroup() != null) {
resource.getResourceGroup().removeResource(resource);
}
/* Add resource to this resource group. */
resource.setResourceGroup(this);
resources.add(resource);
}
public void addResource(Long resourceId) throws InstanceNotFoundException {
Resource resource =
ResourcesDaoRegistry.getResourceDao().find(resourceId);
addResource(resource);
}
public void removeResource(Resource resource) {
if (resources.contains(resource)) {
resources.remove(resource);
resource.setResourceGroup(null);
}
}
@Override
public int getDailyCapacity() {
int dailyCapacity = 0;
for (Resource r : resources) {
dailyCapacity += r.getDailyCapacity();
}
return dailyCapacity;
}
@Override
public void remove() {
for (Resource r : resources) {
r.setResourceGroup(null);
}
resources.clear();
super.remove();
}
}

View file

@ -8,9 +8,7 @@ import org.navalplanner.business.resources.entities.Worker;
/**
* Interface for the resource management service.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*
*/
public interface ResourceService {
@ -24,16 +22,9 @@ public interface ResourceService {
public Resource findResource(Long resourceId)
throws InstanceNotFoundException;
/**
* It adds a resource to a resource group. It the resource already belongs
* to a resource group, the resource is moved to the new group.
*/
public void addResourceToResourceGroup(Long resourceId,
Long resourceGroupId) throws InstanceNotFoundException;
public int getResourceDailyCapacity(Long resourceId)
throws InstanceNotFoundException;
public int getResourceDailyCapacity(Long resourceId)
throws InstanceNotFoundException;
/**
* It removes a resource. If the resource is a composite resource, the
* resources contained in it are not removed.

View file

@ -4,9 +4,7 @@ import java.util.List;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.resources.daos.IResourceDao;
import org.navalplanner.business.resources.daos.IResourceGroupDao;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.ResourceGroup;
import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.business.resources.services.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
@ -23,9 +21,6 @@ public class ResourceServiceImpl implements ResourceService {
@Autowired
private IResourceDao resourceDao;
@Autowired
private IResourceGroupDao resourceGroupDao;
public void saveResource(Resource resource) {
resourceDao.save(resource);
}
@ -38,15 +33,6 @@ public class ResourceServiceImpl implements ResourceService {
}
public void addResourceToResourceGroup(Long resourceId, Long resourceGroupId)
throws InstanceNotFoundException {
ResourceGroup resourceGroup = resourceGroupDao.find(resourceGroupId);
resourceGroup.addResource(resourceId);
}
@Transactional(readOnly = true)
public int getResourceDailyCapacity(Long resourceId)
throws InstanceNotFoundException {

View file

@ -1,66 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- Data source. -->
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"
p:jndiName="${dataSource.jndiName}"
p:resourceRef="true" />
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
p:jndiName="${dataSource.jndiName}" p:resourceRef="true" />
<!-- Hibernate Session Factory. -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:/navalplanner-business-hibernate.cfg.xml">
<property name="mappingResources">
<list>
<value>org/navalplanner/business/resources/entities/Resources.hbm.xml</value>
<value>
org/navalplanner/business/resources/entities/Resources.hbm.xml
</value>
</list>
</property>
</bean>
<!-- Spring Transaction manager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<!-- Enable configuration of transactional behavior based on
annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- For enabling annotation-based configuration (in particular, required
for "@Autowired") -->
<context:annotation-config/>
<!-- ======================== Business Objects ======================== -->
p:sessionFactory-ref="sessionFactory" />
<!--
Enable configuration of transactional behavior based on
annotations
-->
<tx:annotation-driven transaction-manager="transactionManager" />
<!--
For enabling annotation-based configuration (in particular,
required for "@Autowired")
-->
<context:annotation-config />
<!--
======================== Business Objects
========================
-->
<!-- Resource DAOs and registry -->
<bean id="resourceDao"
class="org.navalplanner.business.resources.daos.impl.ResourceDaoHibernate"/>
<bean id="workerDao"
class="org.navalplanner.business.resources.daos.impl.WorkerDaoHibernate"/>
<bean id="resourceGroupDao"
class="org.navalplanner.business.resources.daos.impl.ResourceGroupDaoHibernate"/>
<bean id="resourceDao"
class="org.navalplanner.business.resources.daos.impl.ResourceDaoHibernate" />
<bean id="resourcesDaoRegistry"
class="org.navalplanner.business.resources.daos.ResourcesDaoRegistry"
factory-method="getInstance"/>
<bean id="workerDao"
class="org.navalplanner.business.resources.daos.impl.WorkerDaoHibernate" />
<bean id="resourcesDaoRegistry"
class="org.navalplanner.business.resources.daos.ResourcesDaoRegistry"
factory-method="getInstance" />
<!-- Service layer -->
<bean id="resourceService"
class="org.navalplanner.business.resources.services.impl.ResourceServiceImpl"/>
<bean id="resourceService"
class="org.navalplanner.business.resources.services.impl.ResourceServiceImpl" />
</beans>

View file

@ -4,46 +4,31 @@
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="org.navalplanner.business.resources.entities.Resource">
<id name="id" access="field">
<generator class="native"/>
<generator class="native" />
</id>
<!-- IMPORTANT: type="long" must be specified (otherwise, Hibernate
infers type="integer". -->
<version name="version" type="long" access="field"/>
<many-to-one name="resourceGroup"
column="RESOURCE_GROUP_ID"
class="org.navalplanner.business.resources.entities.ResourceGroup"/>
<joined-subclass
<!--
IMPORTANT: type="long" must be specified (otherwise,
Hibernate infers type="integer".
-->
<version name="version" type="long" access="field" />
<joined-subclass
name="org.navalplanner.business.resources.entities.Worker">
<key column="WORKER_ID"/>
<property name="firstName"/>
<property name="surname"/>
<property name="nif"/>
<property name="dailyHours"/>
<key column="WORKER_ID" />
<property name="firstName" />
<property name="surname" />
<property name="nif" />
<property name="dailyHours" />
</joined-subclass>
<joined-subclass
name="org.navalplanner.business.resources.entities.ResourceGroup">
<key column="RESOURCE_GROUP_ID"/>
<set name="resources" inverse="true" cascade="save-update">
<key column="RESOURCE_GROUP_ID"/>
<one-to-many
class="org.navalplanner.business.resources.entities.Resource"/>
</set>
</joined-subclass>
</class>
</hibernate-mapping>

View file

@ -8,7 +8,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.resources.daos.IResourceDao;
import org.navalplanner.business.resources.entities.ResourceGroup;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.business.resources.services.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,7 +19,6 @@ import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
@ -45,159 +44,39 @@ public class ResourceServiceTest {
@Autowired
private SessionFactory sessionFactory;
@Test
public void testAddResourceToResourceGroup()
throws InstanceNotFoundException {
/* Two workers. One of them belongs to a resource group. */
Worker worker1 = new Worker("worker-1", "worker-1-surname",
"11111111A", 8);
Worker worker2 = new Worker("worker-2", "worker-2-surname",
"22222222B", 7);
ResourceGroup resourceGroup1 = new ResourceGroup();
resourceGroup1.addResource(worker1);
resourceService.saveResource(resourceGroup1); // worker1 is also saved.
resourceService.saveResource(worker2);
/* A resource group. */
ResourceGroup resourceGroup2 = new ResourceGroup();
resourceService.saveResource(resourceGroup2);
/* Add workers to resource group. */
resourceService.addResourceToResourceGroup(worker1.getId(),
resourceGroup2.getId());
resourceService.addResourceToResourceGroup(worker2.getId(),
resourceGroup2.getId());
/* Check resource group. */
ResourceGroup resourceGroup = (ResourceGroup) resourceService
.findResource(resourceGroup2.getId());
assertEquals(2, resourceGroup.getResources().size());
assertTrue(resourceGroup.getResources().contains(worker1));
assertTrue(resourceGroup.getResources().contains(worker2));
/* Check worker1 is no longer in group 1. */
assertFalse(resourceGroup1.getResources().contains(worker1));
}
@Test
public void testGetResourceDailyCapacity() throws InstanceNotFoundException {
/* Three workers. */
Worker worker1 = new Worker("worker-1", "worker-1-surname",
"11111111A", 8);
Worker worker2 = new Worker("worker-2", "worker-2-surname",
"22222222B", 7);
Worker worker3 = new Worker("worker-3", "worker-3-surname",
"33333333C", 6);
/* A group of two workers. */
ResourceGroup resourceGroup1 = new ResourceGroup();
Worker worker4 = new Worker("worker-4", "worker-4-surname",
"44444444D", 5);
Worker worker5 = new Worker("worker-5", "worker-5-surname",
"55555555E", 4);
resourceGroup1.addResource(worker4);
resourceGroup1.addResource(worker5);
/*
* A complex group containing the first three workers and a group with
* the last two workers.
*/
ResourceGroup resourceGroup2 = new ResourceGroup();
resourceGroup2.addResource(worker1);
resourceGroup2.addResource(worker2);
resourceGroup2.addResource(worker3);
resourceGroup2.addResource(resourceGroup1);
/* Calculate total daily capacity. */
int totalDailyCapacity = worker1.getDailyCapacity()
+ worker2.getDailyCapacity() + worker3.getDailyCapacity()
+ worker4.getDailyCapacity() + worker5.getDailyCapacity();
/* Save the second group (and in consequence all resources). */
resourceService.saveResource(resourceGroup2);
/* Test ResourceService's getResourceDailyCapacity. */
int resourceGroupDailyCapacity = resourceService
.getResourceDailyCapacity(resourceGroup2.getId());
assertEquals(totalDailyCapacity, resourceGroupDailyCapacity);
}
@Test
public void testRemoveResource() throws InstanceNotFoundException {
/* A group of three workers. */
ResourceGroup resourceGroup = new ResourceGroup();
Worker worker1 = new Worker("worker-1", "worker-2-surname",
"11111111A", 8);
Worker worker2 = new Worker("worker-2", "worker-3-surname",
"22222222B", 6);
Worker worker3 = new Worker("worker-3", "worker-3-surname",
"33333333C", 4);
resourceGroup.addResource(worker1);
resourceGroup.addResource(worker2);
resourceGroup.addResource(worker3);
resourceService.saveResource(resourceGroup);
resourceService.saveResource(worker1);
resourceService.saveResource(worker2);
resourceService.saveResource(worker3);
/* Remove worker 3. */
resourceService.removeResource(worker3.getId());
/* Check worker 3 does not exist. */
assertFalse(resourceDao.exists(worker3.getId()));
/*
* Check worker 3 is not in resource group and the other workers are
* still in the group.
*/
assertFalse(resourceGroup.getResources().contains(worker3));
assertTrue(resourceGroup.getResources().contains(worker1));
assertTrue(resourceGroup.getResources().contains(worker2));
/* Remove the group. */
resourceService.removeResource(resourceGroup.getId());
/* Check the resource group does not exist. */
assertFalse(resourceDao.exists(resourceGroup.getId()));
/* Check workers still exist. */
assertTrue(resourceDao.exists(worker1.getId()));
assertTrue(resourceDao.exists(worker2.getId()));
/* Check workers do not belong to any resource group. */
assertNull(worker1.getResourceGroup());
assertNull(worker2.getResourceGroup());
/* Remove workers. */
resourceService.removeResource(worker1.getId());
resourceService.removeResource(worker2.getId());
/* Check workers do not exist. */
assertFalse(resourceDao.exists(worker1.getId()));
assertFalse(resourceDao.exists(worker2.getId()));
}
@Test
public void testListWorkers() throws Exception {
final int previousWorkers = resourceService.getWorkers().size();
ResourceGroup resourceGroup = new ResourceGroup();
Worker worker1 = new Worker("worker-1", "worker-2-surname",
"11111111A", 8);
Worker worker2 = new Worker("worker-2", "worker-3-surname",
"22222222B", 6);
Worker worker3 = new Worker("worker-3", "worker-3-surname",
Resource worker3 = new Worker("worker-3", "worker-3-surname",
"33333333C", 4);
resourceGroup.addResource(worker1);
resourceGroup.addResource(worker2);
resourceService.saveResource(resourceGroup);
assertEquals(
"Two workers has been created when saving the resource group",
previousWorkers + 2, resourceService.getWorkers().size());
resourceService.saveResource(worker1);
resourceService.saveResource(worker2);
assertEquals("Two workers have been created", previousWorkers + 2,
resourceService.getWorkers().size());
resourceService.saveResource(worker3);
assertEquals("Three workers has been created", previousWorkers + 3,
resourceService.getWorkers().size());