Refactoring: Class for creating GanttDiagram

Move code in CriticalPathBuilder for creating a Gantt diagram in memory
to its own class

FEA: ItEr76S04BugFixing
This commit is contained in:
Diego Pino 2012-06-12 16:02:28 +02:00
parent 215d8cb395
commit a11c38d34c
3 changed files with 106 additions and 97 deletions

View file

@ -25,7 +25,6 @@ import static org.libreplan.web.I18nHelper._;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
@ -52,6 +51,7 @@ import org.libreplan.business.scenarios.entities.Scenario;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.entities.User;
import org.libreplan.web.UserUtil;
import org.libreplan.web.planner.tabs.GanttDiagramBuilder;
import org.libreplan.web.security.SecurityUtils;
import org.libreplan.web.users.bootstrap.MandatoryUser;
import org.libreplan.web.users.services.CustomUser;
@ -60,13 +60,10 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zkoss.ganttz.adapters.PlannerConfiguration;
import org.zkoss.ganttz.data.ConstraintCalculator;
import org.zkoss.ganttz.data.DependencyType;
import org.zkoss.ganttz.data.DependencyType.Point;
import org.zkoss.ganttz.data.GanttDate;
import org.zkoss.ganttz.data.GanttDiagramGraph;
import org.zkoss.ganttz.data.GanttDiagramGraph.IAdapter;
import org.zkoss.ganttz.data.IDependency;
import org.zkoss.ganttz.data.constraint.Constraint;
import org.zkoss.ganttz.util.LongOperationFeedback;
@ -134,14 +131,17 @@ public class TemplateModel implements ITemplateModel {
return visible;
}
@Override
public TaskElement getSource() {
return source;
}
@Override
public TaskElement getDestination() {
return destination;
}
@Override
public DependencyType getType() {
return type;
}
@ -369,9 +369,10 @@ public class TemplateModel implements ITemplateModel {
private void doReassignationsOn(Order order, Scenario from, Scenario to) {
copyAssignments(order, from, to);
installDependenciesEnforcer(order, TemplateModelAdapter.create(to,
asLocalDate(order.getInitDate()),
asLocalDate(order.getDeadline()), resourcesSearcher));
GanttDiagramBuilder.createForcingDependencies(order,
TemplateModelAdapter.create(to,
asLocalDate(order.getInitDate()),
asLocalDate(order.getDeadline()), resourcesSearcher));
doReassignations(order, to);
doTheSaving(order);
}
@ -386,39 +387,6 @@ public class TemplateModel implements ITemplateModel {
}
}
private void installDependenciesEnforcer(Order order, TemplateModelAdapter adapter) {
GanttDiagramGraph<TaskElement, DependencyWithVisibility> graph = createFor(order, adapter);
TaskSource taskSource = order.getTaskSource();
graph.addTopLevel(taskSource.getTask());
for (Dependency each : getAllDependencies(order)) {
graph.addWithoutEnforcingConstraints(DependencyWithVisibility
.existent(each));
}
}
private GanttDiagramGraph<TaskElement, DependencyWithVisibility> createFor(
Order order, IAdapter<TaskElement, DependencyWithVisibility> adapter) {
List<Constraint<GanttDate>> startConstraints = PlannerConfiguration
.getStartConstraintsGiven((GanttDate) GanttDate.createFrom(order.getInitDate()));
List<Constraint<GanttDate>> endConstraints = PlannerConfiguration
.getEndConstraintsGiven((GanttDate) GanttDate.createFrom(order.getDeadline()));
GanttDiagramGraph<TaskElement, DependencyWithVisibility> result = GanttDiagramGraph
.create(order.isScheduleBackwards(), adapter, startConstraints,
endConstraints,
order.getDependenciesConstraintsHavePriority());
return result;
}
private Set<Dependency> getAllDependencies(Order order) {
Set<Dependency> dependencies = new HashSet<Dependency>();
for (TaskElement each : getTaskElementsFrom(order)) {
Set<Dependency> dependenciesWithThisOrigin = each
.getDependenciesWithThisOrigin();
dependencies.addAll(dependenciesWithThisOrigin);
}
return dependencies;
}
private void doReassignations(Order order, Scenario scenario) {
for (Task each : getTasksFrom(order)) {
each.reassignAllocationsWithNewResources(scenario,

View file

@ -21,19 +21,14 @@
package org.libreplan.web.planner.tabs;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.joda.time.LocalDate;
import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.IOnTransaction;
import org.libreplan.business.common.Registry;
import org.libreplan.business.orders.entities.Order;
import org.libreplan.business.orders.entities.TaskSource;
import org.libreplan.business.planner.entities.Dependency;
import org.libreplan.business.planner.entities.TaskElement;
import org.libreplan.business.resources.daos.IResourcesSearcher;
import org.libreplan.business.scenarios.entities.Scenario;
@ -42,11 +37,8 @@ import org.libreplan.web.common.TemplateModelAdapter;
import org.libreplan.web.planner.order.PlanningStateCreator;
import org.libreplan.web.planner.order.PlanningStateCreator.IActionsOnRetrieval;
import org.libreplan.web.planner.order.PlanningStateCreator.PlanningState;
import org.zkoss.ganttz.adapters.PlannerConfiguration;
import org.zkoss.ganttz.data.GanttDate;
import org.zkoss.ganttz.data.GanttDiagramGraph;
import org.zkoss.ganttz.data.GanttDiagramGraph.IAdapter;
import org.zkoss.ganttz.data.constraint.Constraint;
import org.zkoss.ganttz.data.criticalpath.CriticalPathCalculator;
import org.zkoss.zk.ui.Desktop;
@ -108,16 +100,13 @@ public class CriticalPathBuilder {
final Order order = state.getOrder();
final Scenario currentScenario = state.getCurrentScenario();
CriticalPathCalculator<TaskElement, DependencyWithVisibility> criticalPathCalculator = CriticalPathCalculator
.create(order.getDependenciesConstraintsHavePriority());
IAdapter<TaskElement, DependencyWithVisibility> adapter = TemplateModelAdapter
.create(currentScenario, asLocalDate(order.getInitDate()),
asLocalDate(order.getDeadline()), resourcesSearcher);
GanttDiagramGraph<TaskElement, DependencyWithVisibility> graph = createFor(
order, adapter);
TaskSource taskSource = order.getTaskSource();
graph.addTopLevel(taskSource.getTask());
addDependencies(graph, order);
GanttDiagramGraph<TaskElement, DependencyWithVisibility> graph = GanttDiagramBuilder
.createForcingDependencies(order, adapter);
CriticalPathCalculator<TaskElement, DependencyWithVisibility> criticalPathCalculator = CriticalPathCalculator
.create(order.getDependenciesConstraintsHavePriority());
return criticalPathCalculator.calculateCriticalPath(graph);
}
@ -125,46 +114,4 @@ public class CriticalPathBuilder {
return date != null ? LocalDate.fromDateFields(date) : null;
}
private void addDependencies(
GanttDiagramGraph<TaskElement, DependencyWithVisibility> graph,
Order order) {
for (Dependency each : getAllDependencies(order)) {
graph.addWithoutEnforcingConstraints(DependencyWithVisibility
.existent(each));
}
}
private Set<Dependency> getAllDependencies(Order order) {
Set<Dependency> dependencies = new HashSet<Dependency>();
for (TaskElement each : getTaskElementsFrom(order)) {
Set<Dependency> dependenciesWithThisOrigin = each
.getDependenciesWithThisOrigin();
dependencies.addAll(dependenciesWithThisOrigin);
}
return dependencies;
}
private List<TaskElement> getTaskElementsFrom(Order order) {
List<TaskElement> result = new ArrayList<TaskElement>();
for (TaskSource each : order.getTaskSourcesFromBottomToTop()) {
result.add(each.getTask());
}
return result;
}
private GanttDiagramGraph<TaskElement, DependencyWithVisibility> createFor(
Order order, IAdapter<TaskElement, DependencyWithVisibility> adapter) {
GanttDate orderStart = GanttDate.createFrom(order.getInitDate());
List<Constraint<GanttDate>> startConstraints = PlannerConfiguration
.getStartConstraintsGiven(orderStart);
GanttDate deadline = GanttDate.createFrom(order.getDeadline());
List<Constraint<GanttDate>> endConstraints = PlannerConfiguration
.getEndConstraintsGiven(deadline);
GanttDiagramGraph<TaskElement, DependencyWithVisibility> result = GanttDiagramGraph
.create(order.isScheduleBackwards(), adapter, startConstraints,
endConstraints,
order.getDependenciesConstraintsHavePriority());
return result;
}
}

View file

@ -0,0 +1,94 @@
/*
* This file is part of LibrePlan
*
* Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
* Copyright (C) 2010-2012 Igalia, S.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.libreplan.web.planner.tabs;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.libreplan.business.orders.entities.Order;
import org.libreplan.business.orders.entities.TaskSource;
import org.libreplan.business.planner.entities.Dependency;
import org.libreplan.business.planner.entities.TaskElement;
import org.libreplan.web.common.TemplateModel.DependencyWithVisibility;
import org.zkoss.ganttz.adapters.PlannerConfiguration;
import org.zkoss.ganttz.data.GanttDate;
import org.zkoss.ganttz.data.GanttDiagramGraph;
import org.zkoss.ganttz.data.GanttDiagramGraph.IAdapter;
import org.zkoss.ganttz.data.constraint.Constraint;
/**
*
* @author Diego Pino García <dpino@igalia.com>
*
* Builds a Gantt Diagram in memory, useful for doing operations with a
* Gantt Diagram out of the Gantt View
*
*/
public class GanttDiagramBuilder {
public static GanttDiagramGraph<TaskElement, DependencyWithVisibility> createForcingDependencies(
Order order, IAdapter<TaskElement, DependencyWithVisibility> adapter) {
GanttDiagramGraph<TaskElement, DependencyWithVisibility> graph = createFor(
order, adapter);
TaskSource taskSource = order.getTaskSource();
graph.addTopLevel(taskSource.getTask());
for (Dependency each : getAllDependencies(order)) {
graph.addWithoutEnforcingConstraints(DependencyWithVisibility
.existent(each));
}
return graph;
}
private static GanttDiagramGraph<TaskElement, DependencyWithVisibility> createFor(
Order order, IAdapter<TaskElement, DependencyWithVisibility> adapter) {
List<Constraint<GanttDate>> startConstraints = PlannerConfiguration
.getStartConstraintsGiven(GanttDate.createFrom(order.getInitDate()));
List<Constraint<GanttDate>> endConstraints = PlannerConfiguration
.getEndConstraintsGiven(GanttDate.createFrom(order.getDeadline()));
GanttDiagramGraph<TaskElement, DependencyWithVisibility> result = GanttDiagramGraph
.create(order.isScheduleBackwards(), adapter, startConstraints,
endConstraints,
order.getDependenciesConstraintsHavePriority());
return result;
}
private static Set<Dependency> getAllDependencies(Order order) {
Set<Dependency> dependencies = new HashSet<Dependency>();
for (TaskElement each : getTaskElementsFrom(order)) {
Set<Dependency> dependenciesWithThisOrigin = each
.getDependenciesWithThisOrigin();
dependencies.addAll(dependenciesWithThisOrigin);
}
return dependencies;
}
private static List<TaskElement> getTaskElementsFrom(Order order) {
List<TaskElement> result = new ArrayList<TaskElement>();
for (TaskSource each : order.getTaskSourcesFromBottomToTop()) {
result.add(each.getTask());
}
return result;
}
}