ItEr21S04ArquitecturaServidorItEr20S04: Removing use of Resoruce Service.
Signed-off-by: Óscar González Fernández <ogonzalez@igalia.com> Adding required @Transacional(readOnly = true)
This commit is contained in:
parent
c059e6acb6
commit
a37fcb84ea
12 changed files with 102 additions and 395 deletions
|
|
@ -1,12 +1,15 @@
|
|||
package org.navalplanner.business.resources.daos;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.navalplanner.business.common.daos.IGenericDAO;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
|
||||
/**
|
||||
* DAO interface for the <code>Resource</code> entity.
|
||||
*
|
||||
* @author Fernando Bellas Permuy <fbellas@udc.es>
|
||||
*
|
||||
*/
|
||||
public interface IResourceDAO extends IGenericDAO<Resource, Long> {}
|
||||
public interface IResourceDAO extends IGenericDAO<Resource, Long> {
|
||||
public List<Worker> getWorkers();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,24 @@
|
|||
package org.navalplanner.business.resources.daos;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.navalplanner.business.common.daos.GenericDAOHibernate;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* Hibernate DAO for the <code>Resource</code> entity.
|
||||
*
|
||||
* @author Fernando Bellas Permuy <fbellas@udc.es>
|
||||
*
|
||||
*/
|
||||
@Repository
|
||||
@Scope(BeanDefinition.SCOPE_SINGLETON)
|
||||
public class ResourceDAO extends GenericDAOHibernate<Resource, Long>
|
||||
implements IResourceDAO {}
|
||||
public class ResourceDAO extends GenericDAOHibernate<Resource, Long> implements
|
||||
IResourceDAO {
|
||||
@Override
|
||||
public List<Worker> getWorkers() {
|
||||
return list(Worker.class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
package org.navalplanner.business.resources.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -29,7 +28,7 @@ import org.navalplanner.business.common.BaseEntity;
|
|||
* @author Fernando Bellas Permuy <fbellas@udc.es>
|
||||
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
|
||||
*/
|
||||
public abstract class Resource extends BaseEntity {
|
||||
public abstract class Resource extends BaseEntity{
|
||||
|
||||
private ResourceCalendar calendar;
|
||||
|
||||
|
|
@ -370,7 +369,19 @@ public abstract class Resource extends BaseEntity {
|
|||
return criterionSatisfactions.contains(satisfaction);
|
||||
}
|
||||
|
||||
public void checkNotOverlaps(List<CriterionType> types) {
|
||||
public void checkNotOverlaps() {
|
||||
checkNotOverlaps(getRelatedTypes());
|
||||
}
|
||||
|
||||
private List<CriterionType> getRelatedTypes() {
|
||||
List<CriterionType> types = new ArrayList<CriterionType>();
|
||||
for (CriterionSatisfaction criterionSatisfaction : getAllSatisfactions()) {
|
||||
types.add(criterionSatisfaction.getCriterion().getType());
|
||||
}
|
||||
return types;
|
||||
}
|
||||
|
||||
private void checkNotOverlaps(List<CriterionType> types) {
|
||||
for (CriterionType criterionType : types) {
|
||||
if (!criterionType.allowSimultaneousCriterionsPerResource()) {
|
||||
List<CriterionSatisfaction> satisfactions = query().from(
|
||||
|
|
|
|||
|
|
@ -1,55 +0,0 @@
|
|||
package org.navalplanner.business.resources.services;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.resources.entities.ICriterion;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
|
||||
/**
|
||||
* Interface for the resource management service.
|
||||
* @author Fernando Bellas Permuy <fbellas@udc.es>
|
||||
*/
|
||||
public interface IResourceService {
|
||||
|
||||
/**
|
||||
* It updates or inserts the resource passed as a parameter. If the resource
|
||||
* is a composite resource, updating or inserting is cascaded to the
|
||||
* resources contained in it.
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public void saveResource(Resource resource);
|
||||
|
||||
/**
|
||||
* It checks if the version of the detached object passed as a parameter is
|
||||
* older than the one in the database. If it is older, it throws
|
||||
* <code>org.springframework.dao.OptimisticLockingFailureException</code>.
|
||||
* It can not be called as part of a READ-WRITE transaction.
|
||||
*/
|
||||
public void checkVersion(Resource resource);
|
||||
|
||||
public Resource findResource(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.
|
||||
*/
|
||||
public void removeResource(Long resourceId)
|
||||
throws InstanceNotFoundException;
|
||||
|
||||
public List<Worker> getWorkers();
|
||||
|
||||
public List<Resource> getResources();
|
||||
|
||||
public <T extends Resource> List<T> getResources(Class<T> klass);
|
||||
|
||||
public Set<Resource> getSetOfResourcesSatisfying(ICriterion criterion);
|
||||
|
||||
}
|
||||
|
|
@ -1,100 +0,0 @@
|
|||
package org.navalplanner.business.resources.services;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.resources.daos.ICriterionTypeDAO;
|
||||
import org.navalplanner.business.resources.daos.IResourceDAO;
|
||||
import org.navalplanner.business.resources.entities.CriterionType;
|
||||
import org.navalplanner.business.resources.entities.ICriterion;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Implementation of the resource management service. Resource DAOs are
|
||||
* autowired.
|
||||
* @author Fernando Bellas Permuy <fbellas@udc.es>
|
||||
*/
|
||||
@Service
|
||||
@Scope(BeanDefinition.SCOPE_SINGLETON)
|
||||
@Transactional
|
||||
public class ResourceServiceImpl implements IResourceService {
|
||||
|
||||
@Autowired
|
||||
private IResourceDAO resourceDao;
|
||||
|
||||
@Autowired
|
||||
private ICriterionTypeDAO criterionTypeDAO;
|
||||
|
||||
|
||||
@Transactional
|
||||
public void saveResource(Resource resource) {
|
||||
checkResourceIsOk(resource);
|
||||
resourceDao.save(resource);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public void checkVersion(Resource resource) {
|
||||
resourceDao.checkVersion(resource);
|
||||
}
|
||||
|
||||
private void checkResourceIsOk(Resource resource) {
|
||||
List<CriterionType> types = criterionTypeDAO.getCriterionTypes();
|
||||
resource.checkNotOverlaps(types);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Resource findResource(Long resourceId)
|
||||
throws InstanceNotFoundException {
|
||||
|
||||
return resourceDao.find(resourceId);
|
||||
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public int getResourceDailyCapacity(Long resourceId)
|
||||
throws InstanceNotFoundException {
|
||||
|
||||
return resourceDao.find(resourceId).getDailyCapacity();
|
||||
|
||||
}
|
||||
|
||||
public void removeResource(Long resourceId)
|
||||
throws InstanceNotFoundException {
|
||||
resourceDao.remove(resourceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Worker> getWorkers() {
|
||||
return resourceDao.list(Worker.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Resource> getSetOfResourcesSatisfying(ICriterion criterion) {
|
||||
List<Resource> resources = resourceDao.list(Resource.class);
|
||||
HashSet<Resource> result = new HashSet<Resource>();
|
||||
for (Resource resource : resources) {
|
||||
if (criterion.isSatisfiedBy(resource)) {
|
||||
result.add(resource);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Resource> getResources() {
|
||||
return resourceDao.list(Resource.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Resource> List<T> getResources(Class<T> klass) {
|
||||
return resourceDao.list(klass);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,182 +0,0 @@
|
|||
package org.navalplanner.business.test.resources.services;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
|
||||
import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE;
|
||||
|
||||
import org.hibernate.validator.ClassValidator;
|
||||
import org.hibernate.validator.InvalidStateException;
|
||||
import org.hibernate.validator.InvalidValue;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.navalplanner.business.common.IAdHocTransactionService;
|
||||
import org.navalplanner.business.common.IOnTransaction;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.resources.daos.ICriterionDAO;
|
||||
import org.navalplanner.business.resources.daos.ICriterionTypeDAO;
|
||||
import org.navalplanner.business.resources.daos.IResourceDAO;
|
||||
import org.navalplanner.business.resources.entities.Criterion;
|
||||
import org.navalplanner.business.resources.entities.CriterionWithItsType;
|
||||
import org.navalplanner.business.resources.entities.ICriterion;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
import org.navalplanner.business.resources.services.IResourceService;
|
||||
import org.navalplanner.business.test.resources.daos.CriterionDAOTest;
|
||||
import org.navalplanner.business.test.resources.entities.CriterionTest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* A class for testing <code>ResourceService</code>. The service and the
|
||||
* resource DAOs are autowired.
|
||||
* @author Fernando Bellas Permuy <fbellas@udc.es>
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
|
||||
BUSINESS_SPRING_CONFIG_TEST_FILE })
|
||||
@Transactional
|
||||
public class ResourceServiceTest {
|
||||
|
||||
@Autowired
|
||||
private IResourceService resourceService;
|
||||
|
||||
@Autowired
|
||||
private IResourceDAO resourceDao;
|
||||
|
||||
@Autowired
|
||||
private ICriterionDAO criterionDAO;
|
||||
|
||||
@Autowired
|
||||
private ICriterionTypeDAO criterionTypeDAO;
|
||||
|
||||
@Autowired
|
||||
private IAdHocTransactionService adHocTransactionService;
|
||||
|
||||
private Worker worker;
|
||||
|
||||
private Worker[] invalidWorkers;
|
||||
|
||||
private Criterion criterion;
|
||||
|
||||
@Test
|
||||
public void afterRemovingASavedWorkerNoLongerExists()
|
||||
throws InstanceNotFoundException {
|
||||
givenSavedWorker();
|
||||
resourceService.removeResource(worker.getId());
|
||||
assertFalse(resourceDao.exists(worker.getId()));
|
||||
}
|
||||
|
||||
private void givenSavedWorker() {
|
||||
this.worker = new Worker("worker-1", "worker-2-surname", "11111111A", 8);
|
||||
resourceService.saveResource(this.worker);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWorkersReturnsTheNewlyCreatedResources() {
|
||||
final int previousWorkers = resourceService.getWorkers().size();
|
||||
givenSavedWorker();
|
||||
givenSavedWorker();
|
||||
assertEquals("Two workers has been saved", previousWorkers + 2,
|
||||
resourceService.getWorkers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidValuesAreReportedByClassValidator() {
|
||||
givenInvalidWorkers();
|
||||
for (Worker invalidWorker : invalidWorkers) {
|
||||
thenHasSomeInvalidValue(invalidWorker);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void invalidWorkerCannotBeSaved() {
|
||||
givenInvalidWorkers();
|
||||
for (Worker invalidWorker : invalidWorkers) {
|
||||
thenCannotBeSaved(invalidWorker);
|
||||
}
|
||||
}
|
||||
|
||||
private void thenCannotBeSaved(Worker invalidWorker) {
|
||||
try {
|
||||
resourceService.saveResource(invalidWorker);
|
||||
fail("must send invalid state exception");
|
||||
} catch (InvalidStateException e) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
|
||||
private void thenHasSomeInvalidValue(Worker invalidWorker) {
|
||||
ClassValidator<Worker> workerValidator = new ClassValidator<Worker>(
|
||||
Worker.class);
|
||||
InvalidValue[] invalidValues = workerValidator
|
||||
.getInvalidValues(invalidWorker);
|
||||
assertEquals(1, invalidValues.length);
|
||||
}
|
||||
|
||||
private void givenInvalidWorkers() {
|
||||
invalidWorkers = new Worker[] {
|
||||
new Worker("first name", null, "233233", 3),
|
||||
new Worker("first name", "second name", "233233", -1),
|
||||
new Worker(null, "second name", "233233", 3),
|
||||
new Worker("first name", "second name", null, 3) };
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void versionIsIncreased() {
|
||||
givenSavedWorker();
|
||||
|
||||
long versionValueAfterSave = worker.getVersion();
|
||||
worker.setFirstName("blabla");
|
||||
resourceService.saveResource(worker);
|
||||
|
||||
assertThat(worker.getVersion(), not(equalTo(versionValueAfterSave)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void versionIsIncreasedWhenAddingSatisfactions() throws Exception {
|
||||
givenSavedWorker();
|
||||
givenCriterion();
|
||||
|
||||
long versionValueAfterSave = worker.getVersion();
|
||||
|
||||
worker.addSatisfaction(new CriterionWithItsType(criterion.getType(),
|
||||
criterion));
|
||||
resourceService.saveResource(worker);
|
||||
|
||||
assertThat(worker.getVersion(), not(equalTo(versionValueAfterSave)));
|
||||
}
|
||||
|
||||
private void givenCriterion() {
|
||||
this.criterion = CriterionDAOTest.createValidCriterion();
|
||||
adHocTransactionService.onTransaction(new IOnTransaction<Void>() {
|
||||
|
||||
@Override
|
||||
public Void execute() {
|
||||
criterionTypeDAO.save(criterion.getType());
|
||||
criterionDAO.save(criterion);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setOfResourcesSatisfyingReturnTheResourcesMatchedByCriterion() {
|
||||
givenSavedWorker();
|
||||
|
||||
ICriterion criterion = CriterionTest.justThisResourcesCriterion(worker);
|
||||
|
||||
assertEquals(1, resourceService.getSetOfResourcesSatisfying(
|
||||
criterion).size());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
package org.navalplanner.web.common.converters;
|
||||
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.resources.daos.IResourceDAO;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.services.IResourceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* A {@link IConverter} for {@link Resource} <br />
|
||||
|
|
@ -17,13 +18,14 @@ import org.springframework.stereotype.Component;
|
|||
public class ResourceConverter implements IConverter<Resource> {
|
||||
|
||||
@Autowired
|
||||
private IResourceService resourceService;
|
||||
private IResourceDAO resourceDAO;
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Resource asObject(String stringRepresentation) {
|
||||
long id = Long.parseLong(stringRepresentation);
|
||||
try {
|
||||
return resourceService.findResource(id);
|
||||
return resourceDAO.find(id);
|
||||
} catch (InstanceNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,13 +13,13 @@ import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
|||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.resources.daos.ICriterionDAO;
|
||||
import org.navalplanner.business.resources.daos.ICriterionTypeDAO;
|
||||
import org.navalplanner.business.resources.daos.IResourceDAO;
|
||||
import org.navalplanner.business.resources.entities.Criterion;
|
||||
import org.navalplanner.business.resources.entities.CriterionType;
|
||||
import org.navalplanner.business.resources.entities.CriterionWithItsType;
|
||||
import org.navalplanner.business.resources.entities.ICriterionType;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
import org.navalplanner.business.resources.services.IResourceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
|
@ -46,7 +46,7 @@ public class CriterionsModel implements ICriterionsModel {
|
|||
private ICriterionTypeDAO criterionTypeDAO;
|
||||
|
||||
@Autowired
|
||||
private IResourceService resourceService;
|
||||
private IResourceDAO resourceDAO;
|
||||
|
||||
private ICriterionType<?> criterionType;
|
||||
|
||||
|
|
@ -179,7 +179,7 @@ public class CriterionsModel implements ICriterionsModel {
|
|||
Validate.notNull(resourceType, "resourceType must be not null");
|
||||
Validate.notNull(criterion, "criterion must be not null");
|
||||
List<T> result = new ArrayList<T>();
|
||||
for (T r : resourceService.getResources(resourceType)) {
|
||||
for (T r : resourceDAO.list(resourceType)) {
|
||||
if (criterion.isSatisfiedBy(r)) {
|
||||
result.add(r);
|
||||
}
|
||||
|
|
@ -188,8 +188,9 @@ public class CriterionsModel implements ICriterionsModel {
|
|||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<Worker> getAllWorkers() {
|
||||
return resourceService.getWorkers();
|
||||
return resourceDAO.getWorkers();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -203,9 +204,10 @@ public class CriterionsModel implements ICriterionsModel {
|
|||
public void activateAll(Collection<? extends Resource> resources) {
|
||||
for (Resource resource : resources) {
|
||||
Resource reloaded = find(resource.getId());
|
||||
reloaded.addSatisfaction(new CriterionWithItsType(criterionType,
|
||||
criterion));
|
||||
resourceService.saveResource(reloaded);
|
||||
reloaded
|
||||
.addSatisfaction(new CriterionWithItsType(criterionType, criterion));
|
||||
resourceDAO.save(reloaded);
|
||||
reloaded.checkNotOverlaps();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -214,15 +216,17 @@ public class CriterionsModel implements ICriterionsModel {
|
|||
public void deactivateAll(Collection<? extends Resource> resources) {
|
||||
for (Resource resource : resources) {
|
||||
Resource reloaded = find(resource.getId());
|
||||
reloaded.finish(new CriterionWithItsType(criterionType, criterion));
|
||||
resourceService.saveResource(reloaded);
|
||||
reloaded.finish(new CriterionWithItsType(criterionType,
|
||||
criterion));
|
||||
resourceDAO.save(reloaded);
|
||||
reloaded.checkNotOverlaps();
|
||||
}
|
||||
}
|
||||
|
||||
private Resource find(Long id) {
|
||||
Resource reloaded;
|
||||
try {
|
||||
reloaded = resourceService.findResource(id);
|
||||
reloaded = resourceDAO.find(id);
|
||||
} catch (InstanceNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,11 +3,14 @@ package org.navalplanner.web.resources.worker;
|
|||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.resources.entities.Criterion;
|
||||
import org.navalplanner.business.resources.entities.CriterionSatisfaction;
|
||||
import org.navalplanner.business.resources.entities.ICriterion;
|
||||
import org.navalplanner.business.resources.entities.ICriterionType;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
|
||||
/**
|
||||
|
|
@ -88,4 +91,7 @@ public interface IWorkerModel {
|
|||
void unassignSatisfactions(
|
||||
Collection<? extends CriterionSatisfaction> satisfactions);
|
||||
|
||||
void setWorker(Worker worker);
|
||||
|
||||
Set<Resource> getSetOfResourcesSatisfying(ICriterion criterion);
|
||||
}
|
||||
|
|
@ -16,15 +16,16 @@ import org.hibernate.validator.InvalidValue;
|
|||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.resources.daos.ICriterionDAO;
|
||||
import org.navalplanner.business.resources.daos.IResourceDAO;
|
||||
import org.navalplanner.business.resources.entities.Criterion;
|
||||
import org.navalplanner.business.resources.entities.CriterionSatisfaction;
|
||||
import org.navalplanner.business.resources.entities.CriterionWithItsType;
|
||||
import org.navalplanner.business.resources.entities.ICriterion;
|
||||
import org.navalplanner.business.resources.entities.ICriterionType;
|
||||
import org.navalplanner.business.resources.entities.Interval;
|
||||
import org.navalplanner.business.resources.entities.PredefinedCriterionTypes;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
import org.navalplanner.business.resources.services.IResourceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
|
@ -40,22 +41,25 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
|
||||
public class WorkerModel implements IWorkerModel {
|
||||
|
||||
private final IResourceService resourceService;
|
||||
@Autowired
|
||||
private IResourceDAO resourceDAO;
|
||||
|
||||
private final ICriterionType<?>[] laboralRelatedTypes = {
|
||||
PredefinedCriterionTypes.LEAVE,
|
||||
PredefinedCriterionTypes.WORK_RELATIONSHIP };
|
||||
private Worker worker;
|
||||
private ClassValidator<Worker> workerValidator;
|
||||
|
||||
private final ICriterionDAO criterionDAO;
|
||||
|
||||
private IMultipleCriterionActiveAssigner localizationsAssigner;
|
||||
|
||||
@Autowired
|
||||
public WorkerModel(IResourceService resourceService,
|
||||
public WorkerModel(IResourceDAO resourceDAO,
|
||||
ICriterionDAO criterionDAO) {
|
||||
Validate.notNull(resourceService);
|
||||
Validate.notNull(resourceDAO);
|
||||
Validate.notNull(criterionDAO);
|
||||
this.resourceService = resourceService;
|
||||
this.resourceDAO = resourceDAO;
|
||||
this.workerValidator = new ClassValidator<Worker>(Worker.class);
|
||||
this.criterionDAO = criterionDAO;
|
||||
}
|
||||
|
|
@ -69,7 +73,8 @@ public class WorkerModel implements IWorkerModel {
|
|||
throw new ValidationException(invalidValues);
|
||||
}
|
||||
getLocalizationsAssigner().applyChanges();
|
||||
resourceService.saveResource(worker);
|
||||
resourceDAO.save(worker);
|
||||
worker.checkNotOverlaps();
|
||||
worker = null;
|
||||
localizationsAssigner = null;
|
||||
}
|
||||
|
|
@ -77,7 +82,7 @@ public class WorkerModel implements IWorkerModel {
|
|||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<Worker> getWorkers() {
|
||||
return resourceService.getWorkers();
|
||||
return resourceDAO.getWorkers();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -99,7 +104,7 @@ public class WorkerModel implements IWorkerModel {
|
|||
public void prepareEditFor(Worker worker) {
|
||||
Validate.notNull(worker, "worker must be not null");
|
||||
try {
|
||||
this.worker = (Worker) resourceService.findResource(worker.getId());
|
||||
this.worker = (Worker) resourceDAO.find(worker.getId());
|
||||
forceLoadSatisfactions(this.worker);
|
||||
localizationsAssigner = new MultipleCriterionActiveAssigner(
|
||||
criterionDAO, this.worker,
|
||||
|
|
@ -124,7 +129,7 @@ public class WorkerModel implements IWorkerModel {
|
|||
|
||||
/* Check worker's version. */
|
||||
Worker worker = getWorker();
|
||||
resourceService.checkVersion(worker);
|
||||
resourceDAO.checkVersion(worker);
|
||||
|
||||
/* Add criterion satisfaction. */
|
||||
edited.setResource(worker);
|
||||
|
|
@ -157,7 +162,7 @@ public class WorkerModel implements IWorkerModel {
|
|||
|
||||
/* Check worker's version. */
|
||||
Worker worker = getWorker();
|
||||
resourceService.checkVersion(worker);
|
||||
resourceDAO.checkVersion(worker);
|
||||
|
||||
/* Remove criterion satisfaction. */
|
||||
worker.removeCriterionSatisfaction(satisfaction);
|
||||
|
|
@ -170,7 +175,7 @@ public class WorkerModel implements IWorkerModel {
|
|||
|
||||
/* Check worker's version. */
|
||||
Worker worker = getWorker();
|
||||
resourceService.checkVersion(worker);
|
||||
resourceDAO.checkVersion(worker);
|
||||
|
||||
/* Assign criteria. */
|
||||
getLocalizationsAssigner().assign(criteria);
|
||||
|
|
@ -183,7 +188,7 @@ public class WorkerModel implements IWorkerModel {
|
|||
|
||||
/* Check worker's version. */
|
||||
Worker worker = getWorker();
|
||||
resourceService.checkVersion(worker);
|
||||
resourceDAO.checkVersion(worker);
|
||||
|
||||
/* Unassign criterion satisfactions. */
|
||||
getLocalizationsAssigner().unassign(satisfactions);
|
||||
|
|
@ -384,4 +389,22 @@ public class WorkerModel implements IWorkerModel {
|
|||
public List<CriterionSatisfaction> getLaboralRelatedCriterionSatisfactions() {
|
||||
return worker.query().oneOf(laboralRelatedTypes).result();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWorker(Worker worker) {
|
||||
this.worker = worker;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Set<Resource> getSetOfResourcesSatisfying(ICriterion criterion) {
|
||||
List<Resource> resources = resourceDAO.list(Resource.class);
|
||||
HashSet<Resource> result = new HashSet<Resource>();
|
||||
for (Resource resource : resources) {
|
||||
if (criterion.isSatisfiedBy(resource)) {
|
||||
result.add(resource);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,21 +2,15 @@ package org.navalplanner.web.resources;
|
|||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
|
||||
import static org.navalplanner.web.WebappGlobalNames.WEBAPP_SPRING_CONFIG_FILE;
|
||||
import static org.navalplanner.web.test.WebappGlobalNames.WEBAPP_SPRING_CONFIG_TEST_FILE;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.management.RuntimeErrorException;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.validator.InvalidStateException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.navalplanner.business.common.IAdHocTransactionService;
|
||||
|
|
@ -26,15 +20,9 @@ import org.navalplanner.business.common.exceptions.ValidationException;
|
|||
import org.navalplanner.business.resources.daos.ICriterionDAO;
|
||||
import org.navalplanner.business.resources.daos.ICriterionTypeDAO;
|
||||
import org.navalplanner.business.resources.entities.Criterion;
|
||||
import org.navalplanner.business.resources.entities.CriterionSatisfaction;
|
||||
import org.navalplanner.business.resources.entities.CriterionType;
|
||||
import org.navalplanner.business.resources.entities.CriterionWithItsType;
|
||||
import org.navalplanner.business.resources.entities.ICriterion;
|
||||
import org.navalplanner.business.resources.entities.ICriterionType;
|
||||
import org.navalplanner.business.resources.entities.PredefinedCriterionTypes;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
import org.navalplanner.business.resources.services.IResourceService;
|
||||
import org.navalplanner.web.resources.criterion.CriterionsModel;
|
||||
import org.navalplanner.web.resources.criterion.ICriterionsModel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@ import org.junit.Test;
|
|||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.resources.daos.ICriterionDAO;
|
||||
import org.navalplanner.business.resources.daos.IResourceDAO;
|
||||
import org.navalplanner.business.resources.entities.Criterion;
|
||||
import org.navalplanner.business.resources.entities.PredefinedCriterionTypes;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
import org.navalplanner.business.resources.services.IResourceService;
|
||||
import org.navalplanner.web.resources.worker.WorkerModel;
|
||||
|
||||
/**
|
||||
|
|
@ -26,7 +26,7 @@ public class WorkerModelTest {
|
|||
@Test
|
||||
public void testWorkerValid() throws ValidationException,
|
||||
InstanceNotFoundException {
|
||||
IResourceService resourceServiceMock = createMock(IResourceService.class);
|
||||
IResourceDAO resourceDAOMock = createMock(IResourceDAO.class);
|
||||
ICriterionDAO criterionServiceMock = createMock(ICriterionDAO.class);
|
||||
Worker workerToReturn = new Worker();
|
||||
workerToReturn.setDailyHours(2);
|
||||
|
|
@ -39,12 +39,13 @@ public class WorkerModelTest {
|
|||
criterionServiceMock
|
||||
.findByType(PredefinedCriterionTypes.LOCATION_GROUP))
|
||||
.andReturn(criterions).anyTimes();
|
||||
expect(resourceServiceMock.findResource(workerToReturn.getId()))
|
||||
expect(resourceDAOMock.find(workerToReturn.getId()))
|
||||
.andReturn(workerToReturn);
|
||||
resourceServiceMock.saveResource(workerToReturn);
|
||||
replay(resourceServiceMock, criterionServiceMock);
|
||||
resourceDAOMock.save(workerToReturn);
|
||||
workerToReturn.checkNotOverlaps();
|
||||
replay(resourceDAOMock, criterionServiceMock);
|
||||
// perform actions
|
||||
WorkerModel workerModel = new WorkerModel(resourceServiceMock,
|
||||
WorkerModel workerModel = new WorkerModel(resourceDAOMock,
|
||||
criterionServiceMock);
|
||||
|
||||
workerModel.prepareEditFor(workerToReturn);
|
||||
|
|
@ -54,7 +55,7 @@ public class WorkerModelTest {
|
|||
@Test(expected = ValidationException.class)
|
||||
public void testWorkerInvalid() throws ValidationException,
|
||||
InstanceNotFoundException {
|
||||
IResourceService resourceServiceMock = createMock(IResourceService.class);
|
||||
IResourceDAO resourceDAOMock = createMock(IResourceDAO.class);
|
||||
ICriterionDAO criterionServiceMock = createMock(ICriterionDAO.class);
|
||||
Worker workerToReturn = new Worker();
|
||||
// expectations
|
||||
|
|
@ -63,11 +64,11 @@ public class WorkerModelTest {
|
|||
criterionServiceMock
|
||||
.findByType(PredefinedCriterionTypes.LOCATION_GROUP))
|
||||
.andReturn(criterions).anyTimes();
|
||||
expect(resourceServiceMock.findResource(workerToReturn.getId()))
|
||||
expect(resourceDAOMock.find(workerToReturn.getId()))
|
||||
.andReturn(workerToReturn);
|
||||
replay(resourceServiceMock, criterionServiceMock);
|
||||
replay(resourceDAOMock, criterionServiceMock);
|
||||
// perform actions
|
||||
WorkerModel workerModel = new WorkerModel(resourceServiceMock,
|
||||
WorkerModel workerModel = new WorkerModel(resourceDAOMock,
|
||||
criterionServiceMock);
|
||||
workerModel.prepareEditFor(workerToReturn);
|
||||
workerModel.save();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue