Take into account the scenario

It's extracted into a QueryPart

FEA: ItEr75S11PreventLooseChanges
This commit is contained in:
Óscar González Fernández 2011-07-27 17:40:33 +02:00
parent 2b47fed73d
commit 00db73a60f
6 changed files with 128 additions and 32 deletions

View file

@ -32,6 +32,7 @@ import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.SpecificResourceAllocation;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.scenarios.entities.Scenario;
/**
* DAO interface for {@link ResourceAllocation}
@ -42,19 +43,24 @@ public interface IResourceAllocationDAO extends
IGenericDAO<ResourceAllocation, Long> {
List<ResourceAllocation<?>> findAllocationsRelatedToAnyOf(
Scenario onScenario,
List<Resource> resources);
List<ResourceAllocation<?>> findAllocationsRelatedToAnyOf(
Scenario onScenario,
List<Resource> resources, LocalDate intervalFilterStartDate,
LocalDate intervalFilterEndDate);
List<ResourceAllocation<?>> findAllocationsRelatedTo(Resource resource,
List<ResourceAllocation<?>> findAllocationsRelatedTo(Scenario onScenario,
Resource resource,
LocalDate intervalFilterStartDate, LocalDate intervalFilterEndDate);
Map<Criterion, List<GenericResourceAllocation>> findGenericAllocationsByCriterion(
Scenario onScenario,
Date intervalFilterStartDate, Date intervalFilterEndDate);
Map<Criterion, List<GenericResourceAllocation>> findGenericAllocationsBySomeCriterion(
Scenario onScenario,
List<Criterion> criterions, Date intervalFilterStartDate,
Date intervalFilterEndDate);
@ -74,6 +80,8 @@ public interface IResourceAllocationDAO extends
* allocations satisfying the first requirement are returned.
* </p>
*
* @param onScenario
* the scenario the allocations returned will belong to
* @param criterion
* must be not <code>null</code>
* @param intervalFilterStartDate
@ -84,6 +92,7 @@ public interface IResourceAllocationDAO extends
* allocations} found
*/
List<SpecificResourceAllocation> findSpecificAllocationsRelatedTo(
Scenario onScenario,
Criterion criterion,
Date intervalFilterStartDate,
Date intervalFilterEndDate);

View file

@ -31,6 +31,7 @@ import java.util.Map.Entry;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.joda.time.LocalDate;
@ -40,6 +41,7 @@ import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.SpecificResourceAllocation;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.scenarios.entities.Scenario;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
@ -56,28 +58,35 @@ public class ResourceAllocationDAO extends
@Override
public List<ResourceAllocation<?>> findAllocationsRelatedToAnyOf(
List<Resource> resources) {
Scenario onScenario, List<Resource> resources) {
List<ResourceAllocation<?>> result = new ArrayList<ResourceAllocation<?>>();
result.addAll(findSpecificAllocationsRelatedTo(resources, null, null));
result.addAll(findGenericAllocationsFor(resources, null, null));
result.addAll(findSpecificAllocationsRelatedTo(onScenario, resources,
null, null));
result.addAll(findGenericAllocationsFor(onScenario, resources, null,
null));
return result;
}
@Override
public List<ResourceAllocation<?>> findAllocationsRelatedToAnyOf(
Scenario onScenario,
List<Resource> resources, LocalDate intervalFilterStartDate,
LocalDate intervalFilterEndDate) {
List<ResourceAllocation<?>> result = new ArrayList<ResourceAllocation<?>>();
result.addAll(findSpecificAllocationsRelatedTo(resources, intervalFilterStartDate, intervalFilterEndDate));
result.addAll(findGenericAllocationsFor(resources, intervalFilterStartDate, intervalFilterEndDate));
result.addAll(findSpecificAllocationsRelatedTo(onScenario, resources,
intervalFilterStartDate, intervalFilterEndDate));
result.addAll(findGenericAllocationsFor(onScenario, resources,
intervalFilterStartDate, intervalFilterEndDate));
return result;
}
@SuppressWarnings("unchecked")
private List<GenericResourceAllocation> findGenericAllocationsFor(
final Scenario onScenario,
final List<Resource> resources,
final LocalDate intervalFilterStartDate,
final LocalDate intervalFilterEndDate) {
if (resources.isEmpty()) {
return new ArrayList<GenericResourceAllocation>();
}
@ -103,8 +112,10 @@ public class ResourceAllocationDAO extends
@Override
protected IQueryPart[] getExtraParts() {
return new IQueryPart[] { new DatesInterval("task",
intervalFilterStartDate, intervalFilterEndDate) };
return new IQueryPart[] {
new DatesInterval("task", intervalFilterStartDate,
intervalFilterEndDate),
new OnScenario("task", onScenario) };
}
};
@ -114,6 +125,7 @@ public class ResourceAllocationDAO extends
@SuppressWarnings("unchecked")
private List<SpecificResourceAllocation> findSpecificAllocationsRelatedTo(
final Scenario onScenario,
final List<Resource> resources,
final LocalDate intervalFilterStartDate,
final LocalDate intervalFilterEndDate) {
@ -142,8 +154,10 @@ public class ResourceAllocationDAO extends
@Override
protected IQueryPart[] getExtraParts() {
return new IQueryPart[] { new DatesInterval("task",
intervalFilterStartDate, intervalFilterEndDate) };
return new IQueryPart[] {
new DatesInterval("task", intervalFilterStartDate,
intervalFilterEndDate),
new OnScenario("task", onScenario) };
}
};
return (List<SpecificResourceAllocation>) queryBuilder.build(
@ -152,10 +166,12 @@ public class ResourceAllocationDAO extends
@Override
public List<ResourceAllocation<?>> findAllocationsRelatedTo(
Scenario onScenario,
Resource resource, LocalDate intervalFilterStartDate,
LocalDate intervalFilterEndDate) {
return stripAllocationsWithoutAssignations(findAllocationsRelatedToAnyOf(Arrays
.asList(resource), intervalFilterStartDate, intervalFilterEndDate));
return stripAllocationsWithoutAssignations(findAllocationsRelatedToAnyOf(
onScenario, Arrays.asList(resource), intervalFilterStartDate,
intervalFilterEndDate));
}
private <R extends ResourceAllocation<?>> List<R> stripAllocationsWithoutAssignations(
@ -184,7 +200,9 @@ public class ResourceAllocationDAO extends
@Override
public Map<Criterion, List<GenericResourceAllocation>> findGenericAllocationsByCriterion(
final Scenario onScenario,
final Date intervalFilterStartDate, final Date intervalFilterEndDate) {
QueryBuilder queryBuilder = new QueryBuilder() {
@Override
@ -206,8 +224,10 @@ public class ResourceAllocationDAO extends
@Override
protected IQueryPart[] getExtraParts() {
return new IQueryPart[] { new DatesInterval("task",
intervalFilterStartDate, intervalFilterEndDate) };
return new IQueryPart[] {
new DatesInterval("task", intervalFilterStartDate,
intervalFilterEndDate),
new OnScenario("task", onScenario) };
}
};
@ -218,6 +238,7 @@ public class ResourceAllocationDAO extends
@Override
public Map<Criterion, List<GenericResourceAllocation>> findGenericAllocationsBySomeCriterion(
final Scenario onScenario,
final List<Criterion> criterions,
final Date intervalFilterStartDate, final Date intervalFilterEndDate) {
@ -247,8 +268,10 @@ public class ResourceAllocationDAO extends
@Override
protected IQueryPart[] getExtraParts() {
return new IQueryPart[] { new DatesInterval("task",
intervalFilterStartDate, intervalFilterEndDate) };
return new IQueryPart[] {
new DatesInterval("task", intervalFilterStartDate,
intervalFilterEndDate),
new OnScenario("task", onScenario) };
}
};
Query q = queryBuilder.build(getSession());
@ -335,6 +358,7 @@ public class ResourceAllocationDAO extends
@Override
public List<SpecificResourceAllocation> findSpecificAllocationsRelatedTo(
final Scenario onScenario,
final Criterion criterion,
final Date intervalFilterStartDate, final Date intervalFilterEndDate) {
@ -360,8 +384,10 @@ public class ResourceAllocationDAO extends
@Override
protected IQueryPart[] getExtraParts() {
return new IQueryPart[] { new DatesInterval("t",
intervalFilterStartDate, intervalFilterEndDate) };
return new IQueryPart[] {
new DatesInterval("t", intervalFilterStartDate,
intervalFilterEndDate),
new OnScenario("t", onScenario) };
}
};
@ -537,4 +563,36 @@ public class ResourceAllocationDAO extends
}
public class OnScenario implements IQueryPart {
private final Scenario onScenario;
private final String taskAlias;
private OnScenario(String taskAlias, Scenario onScenario) {
Validate.notNull(taskAlias);
Validate.notNull(onScenario);
this.taskAlias = taskAlias;
this.onScenario = onScenario;
}
@Override
public String queryPart() {
return "join " + taskAlias
+ ".taskSource.schedulingData as schedulingData "
+ "join schedulingData.orderElement as orderElement "
+ ", OrderVersion as version ";
}
@Override
public String wherePart() {
return "orderElement.schedulingDatasForVersion[version] = schedulingData "
+ "and version.ownerScenario = :scenario";
}
@Override
public void injectParameters(Query query) {
query.setParameter("scenario", onScenario);
}
}
}

View file

@ -265,6 +265,7 @@ public class ResourceAllocationDAOTest {
@Test
public void testFindAllocationsRelatedToResourcesWithDateFilter() {
Scenario current = scenarioManager.getCurrent();
ResourceAllocation<?> resourceAllocation1 = createValidSpecificResourceAllocation();
resourceAllocationDAO.save(resourceAllocation1);
@ -274,19 +275,23 @@ public class ResourceAllocationDAOTest {
.getEndAsLocalDate();
List<Resource> resources = resourceAllocation1.getAssociatedResources();
assertTrue(resourceAllocationDAO.findAllocationsRelatedToAnyOf(resources,
assertTrue(resourceAllocationDAO.findAllocationsRelatedToAnyOf(
current, resources,
intervalInitDate, intervalEndDate).contains(resourceAllocation1));
intervalEndDate = intervalInitDate;
intervalInitDate = intervalInitDate.minusMonths(1);
assertTrue(resourceAllocationDAO.findAllocationsRelatedToAnyOf(resources,
assertTrue(resourceAllocationDAO.findAllocationsRelatedToAnyOf(current,
resources,
intervalInitDate, intervalEndDate).contains(resourceAllocation1));
intervalEndDate = intervalEndDate.minusMonths(1);
assertFalse(resourceAllocationDAO.findAllocationsRelatedToAnyOf(resources,
assertFalse(resourceAllocationDAO.findAllocationsRelatedToAnyOf(
current, resources,
intervalInitDate, intervalEndDate).contains(resourceAllocation1));
assertTrue(resourceAllocationDAO.findAllocationsRelatedToAnyOf(resources,
assertTrue(resourceAllocationDAO.findAllocationsRelatedToAnyOf(current,
resources,
intervalInitDate, null).contains(resourceAllocation1));
}
}

View file

@ -63,6 +63,7 @@ import org.navalplanner.business.resources.daos.IResourcesSearcher;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.scenarios.IScenarioManager;
import org.navalplanner.business.scenarios.entities.Scenario;
import org.navalplanner.business.users.daos.IOrderAuthorizationDAO;
import org.navalplanner.business.users.daos.IUserDAO;
import org.navalplanner.business.users.entities.OrderAuthorization;
@ -160,6 +161,13 @@ public class ResourceLoadModel implements IResourceLoadModel {
*/
List<Criterion> allCriteriaList;
private Scenario getCurrentScenario() {
if (filterBy != null) {
return filterBy.getCurrentScenario();
}
return scenarioManager.getCurrent();
}
@Override
@Transactional(readOnly = true)
public void initGlobalView(boolean filterByResources) {
@ -246,7 +254,8 @@ public class ResourceLoadModel implements IResourceLoadModel {
// reattaching criterions so the query returns the same criteria as
// keys
allCriteriaList = new ArrayList<Criterion>(criteriaToShowList);
return withAssociatedSpecific(findAllocationsGroupedByCriteria(criteriaToShowList));
return withAssociatedSpecific(findAllocationsGroupedByCriteria(
scenarioManager.getCurrent(), criteriaToShowList));
}
Map<Criterion, List<GenericResourceAllocation>> result = findAllocationsByCriterion();
allCriteriaList = Criterion.sortByInclusionTypeAndName(result.keySet());
@ -263,7 +272,8 @@ public class ResourceLoadModel implements IResourceLoadModel {
if (filter()) {
List<Task> tasks = justTasks(filterBy.getOrder()
.getAllChildrenAssociatedTaskElements());
return findAllocationsGroupedByCriteria(getCriterionsOn(tasks));
return findAllocationsGroupedByCriteria(getCurrentScenario(),
getCriterionsOn(tasks));
} else {
return findAllocationsGroupedByCriteria();
}
@ -274,9 +284,10 @@ public class ResourceLoadModel implements IResourceLoadModel {
}
private Map<Criterion, List<GenericResourceAllocation>> findAllocationsGroupedByCriteria(
Scenario onScenario,
List<Criterion> relatedWith) {
Map<Criterion, List<GenericResourceAllocation>> result = resourceAllocationDAO
.findGenericAllocationsBySomeCriterion(relatedWith,
.findGenericAllocationsBySomeCriterion(onScenario, relatedWith,
asDate(initDateFilter), asDate(endDateFilter));
return doReplacementsIfNeeded(result,
and(onInterval(), new RelatedWithAnyOf(relatedWith)));
@ -285,6 +296,7 @@ public class ResourceLoadModel implements IResourceLoadModel {
private Map<Criterion, List<GenericResourceAllocation>> findAllocationsGroupedByCriteria() {
return doReplacementsIfNeeded(
resourceAllocationDAO.findGenericAllocationsByCriterion(
getCurrentScenario(),
asDate(initDateFilter), asDate(endDateFilter)),
onInterval());
}
@ -316,7 +328,8 @@ public class ResourceLoadModel implements IResourceLoadModel {
List<ResourceAllocation<?>> both = new ArrayList<ResourceAllocation<?>>();
both.addAll(each.getValue());
both.addAll(doReplacementsIfNeeded(resourceAllocationDAO
.findSpecificAllocationsRelatedTo(each.getKey(),
.findSpecificAllocationsRelatedTo(getCurrentScenario(),
each.getKey(),
asDate(initDateFilter), asDate(endDateFilter)),
and(onInterval(), specificRelatedTo(each.getKey()))));
result.put(each.getKey(), both);
@ -680,6 +693,7 @@ public class ResourceLoadModel implements IResourceLoadModel {
map.put(resource, ResourceAllocation
.sortedByStartDate(doReplacementsIfNeeded(
resourceAllocationDAO.findAllocationsRelatedTo(
getCurrentScenario(),
resource, initDateFilter, endDateFilter),
and(onInterval(), relatedToResource(resource)))));
}

View file

@ -56,6 +56,7 @@ import org.navalplanner.business.resources.entities.MachineWorkersConfigurationU
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.ResourceEnum;
import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.business.scenarios.IScenarioManager;
import org.navalplanner.business.workreports.daos.IWorkReportLineDAO;
import org.navalplanner.web.common.IntegrationEntityModel;
import org.navalplanner.web.common.concurrentdetection.OnConcurrentModification;
@ -112,6 +113,9 @@ public class MachineModel extends IntegrationEntityModel implements
@Autowired
private IResourceAllocationDAO resourceAllocationDAO;
@Autowired
private IScenarioManager scenarioManager;
private ClassValidator<Machine> validator = new ClassValidator<Machine>(
Machine.class);
@ -371,9 +375,10 @@ public class MachineModel extends IntegrationEntityModel implements
public boolean canRemove(Machine machine) {
List<Resource> resourcesList = new ArrayList<Resource>();
resourcesList.add(machine);
return dayAssignmentDAO.findByResources(resourcesList).isEmpty() &&
workReportLineDAO.findByResources(resourcesList).isEmpty() &&
resourceAllocationDAO.findAllocationsRelatedToAnyOf(resourcesList).isEmpty();
return dayAssignmentDAO.findByResources(resourcesList).isEmpty()
&& workReportLineDAO.findByResources(resourcesList).isEmpty()
&& resourceAllocationDAO.findAllocationsRelatedToAnyOf(
scenarioManager.getCurrent(), resourcesList).isEmpty();
}
@Override

View file

@ -58,6 +58,7 @@ import org.navalplanner.business.resources.entities.PredefinedCriterionTypes;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.VirtualWorker;
import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.business.scenarios.IScenarioManager;
import org.navalplanner.business.workreports.daos.IWorkReportLineDAO;
import org.navalplanner.web.calendars.IBaseCalendarModel;
import org.navalplanner.web.common.IntegrationEntityModel;
@ -123,6 +124,9 @@ public class WorkerModel extends IntegrationEntityModel implements IWorkerModel
@Autowired
private IResourceAllocationDAO resourceAllocationDAO;
@Autowired
private IScenarioManager scenarioManager;
@Autowired
public WorkerModel(IResourceDAO resourceDAO, ICriterionDAO criterionDAO) {
Validate.notNull(resourceDAO);
@ -597,9 +601,10 @@ public class WorkerModel extends IntegrationEntityModel implements IWorkerModel
public boolean canRemove(Worker worker) {
List<Resource> resourcesList = new ArrayList<Resource>();
resourcesList.add(worker);
return dayAssignmentDAO.findByResources(resourcesList).isEmpty() &&
workReportLineDAO.findByResources(resourcesList).isEmpty() &&
resourceAllocationDAO.findAllocationsRelatedToAnyOf(resourcesList).isEmpty();
return dayAssignmentDAO.findByResources(resourcesList).isEmpty()
&& workReportLineDAO.findByResources(resourcesList).isEmpty()
&& resourceAllocationDAO.findAllocationsRelatedToAnyOf(
scenarioManager.getCurrent(), resourcesList).isEmpty();
}
@Override