[Bug #1187] Fix issue filtering TaskGroup by criteria in company Gantt view

FEA: ItEr75S04BugFixing
This commit is contained in:
Manuel Rego Casasnovas 2011-10-11 12:28:30 +02:00
parent a904496e72
commit 94edf7fd42
7 changed files with 646 additions and 54 deletions

View file

@ -0,0 +1,50 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2011 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.navalplanner.web.common.components.finders;
import org.navalplanner.business.planner.entities.TaskGroup;
/**
* Different filters for {@link TaskGroup} in company view Gantt.
*
* @author Manuel Rego Casasnovas <rego@igalia.com>
*/
public enum TaskGroupFilterEnum implements IFilterEnum {
Criterion(_("Criterion")), Label(_("Label")), ExternalCompany(_("Customer")), State(
_("State")), Code(_("Code")), CustomerReference(
_("Customer Reference"));
/**
* Forces to mark the string as needing translation
*/
private static String _(String string) {
return string;
}
private String description;
private TaskGroupFilterEnum(String description) {
this.description = description;
}
public String toString() {
return this.description;
}
}

View file

@ -0,0 +1,337 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2011 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.navalplanner.web.common.components.finders;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import org.apache.commons.lang.StringUtils;
import org.navalplanner.business.externalcompanies.entities.ExternalCompany;
import org.navalplanner.business.hibernate.notification.PredefinedDatabaseSnapshots;
import org.navalplanner.business.labels.entities.Label;
import org.navalplanner.business.labels.entities.LabelType;
import org.navalplanner.business.orders.entities.OrderStatusEnum;
import org.navalplanner.business.planner.entities.TaskGroup;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.CriterionType;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Implements all the methods needed to search the criteria to filter the
* {@link TaskGroup} in company view Gantt. Provides multiples criteria to
* filter like {@link Criterion} and {@link Label}.
*
* @author Manuel Rego Casasnovas <rego@igalia.com>
*/
public class TaskGroupsMultipleFiltersFinder extends MultipleFiltersFinder {
@Autowired
private PredefinedDatabaseSnapshots databaseSnapshots;
protected TaskGroupsMultipleFiltersFinder() {
}
public List<FilterPair> getFirstTenFilters() {
getListMatching().clear();
fillWithFirstTenFiltersLabels();
fillWithFirstTenFiltersCriterions();
fillWithFirstTenFiltersCustomer();
fillWithFirstTenFiltersState();
fillWihtFirstTenFiltersCodes();
fillWihtFirstTenFiltersCustomerReferences();
addNoneFilter();
return getListMatching();
}
private List<FilterPair> fillWithFirstTenFiltersLabels() {
Map<LabelType, List<Label>> mapLabels = databaseSnapshots
.snapshotLabelsMap();
Iterator<LabelType> iteratorLabelType = mapLabels.keySet().iterator();
while (iteratorLabelType.hasNext() && getListMatching().size() < 10) {
LabelType type = iteratorLabelType.next();
for (int i = 0; getListMatching().size() < 10
&& i < mapLabels.get(type).size(); i++) {
Label label = mapLabels.get(type).get(i);
addLabel(type, label);
}
}
return getListMatching();
}
private List<FilterPair> fillWithFirstTenFiltersCriterions() {
SortedMap<CriterionType, List<Criterion>> mapCriterions = getMapCriterions();
Iterator<CriterionType> iteratorCriterionType = mapCriterions.keySet()
.iterator();
while (iteratorCriterionType.hasNext() && getListMatching().size() < 10) {
CriterionType type = iteratorCriterionType.next();
for (int i = 0; getListMatching().size() < 10
&& i < mapCriterions.get(type).size(); i++) {
Criterion criterion = mapCriterions.get(type).get(i);
addCriterion(type, criterion);
}
}
return getListMatching();
}
private SortedMap<CriterionType, List<Criterion>> getMapCriterions() {
return databaseSnapshots.snapshotCriterionsMap();
}
private List<FilterPair> fillWithFirstTenFiltersCustomer() {
List<ExternalCompany> externalCompanies = databaseSnapshots
.snapshotExternalCompanies();
for (int i = 0; getListMatching().size() < 10
&& i < externalCompanies.size(); i++) {
ExternalCompany externalCompany = externalCompanies.get(i);
addExternalCompany(externalCompany);
}
return getListMatching();
}
private List<FilterPair> fillWithFirstTenFiltersState() {
for (int i = 0; getListMatching().size() < 10
&& i < OrderStatusEnum.values().length; i++) {
OrderStatusEnum state = OrderStatusEnum.values()[i];
addState(state);
}
return getListMatching();
}
private List<FilterPair> fillWihtFirstTenFiltersCodes() {
List<String> ordersCodes = databaseSnapshots.snapshotOrdersCodes();
for (int i = 0; getListMatching().size() < 10 && i < ordersCodes.size(); i++) {
String code = ordersCodes.get(i);
addCode(code);
}
return getListMatching();
}
private List<FilterPair> fillWihtFirstTenFiltersCustomerReferences() {
List<String> customerReferences = databaseSnapshots
.snapshotCustomerReferences();
for (int i = 0; getListMatching().size() < 10
&& i < customerReferences.size(); i++) {
String reference = customerReferences.get(i);
addCustomerReference(reference);
}
return getListMatching();
}
public List<FilterPair> getMatching(String filter) {
getListMatching().clear();
if ((filter != null) && (!filter.isEmpty())) {
filter = StringUtils.deleteWhitespace(filter.toLowerCase());
if (filter.indexOf("rc:") == 0) {
searchInCustomerReferences(filter);
} else if (filter.indexOf("cod:") == 0) {
this.searchInOrderCodes(filter);
} else {
searchInCriterionTypes(filter);
searchInLabelTypes(filter);
searchInExternalCompanies(filter);
searchInOrderStatus(filter);
}
}
addNoneFilter();
return getListMatching();
}
private void searchInCriterionTypes(String filter) {
boolean limited = (filter.length() < 3);
for (CriterionType type : getMapCriterions().keySet()) {
String name = StringUtils.deleteWhitespace(type.getName()
.toLowerCase());
if (name.contains(filter)) {
setFilterPairCriterionType(type, limited);
} else {
searchInCriterions(type, filter);
}
}
}
private void searchInCriterions(CriterionType type, String filter) {
List<Criterion> list = getMapCriterions().get(type);
if (list == null) {
return;
}
for (Criterion criterion : list) {
String name = StringUtils.deleteWhitespace(criterion.getName()
.toLowerCase());
if (name.contains(filter)) {
addCriterion(type, criterion);
if ((filter.length() < 3) && (getListMatching().size() > 9)) {
return;
}
}
}
}
private void setFilterPairCriterionType(CriterionType type, boolean limited) {
for (Criterion criterion : getMapCriterions().get(type)) {
addCriterion(type, criterion);
if ((limited) && (getListMatching().size() > 9)) {
return;
}
}
}
private void searchInLabelTypes(String filter) {
Map<LabelType, List<Label>> mapLabels = getLabelsMap();
boolean limited = (filter.length() < 3);
for (LabelType type : mapLabels.keySet()) {
String name = StringUtils.deleteWhitespace(type.getName()
.toLowerCase());
if (name.contains(filter)) {
setFilterPairLabelType(type, limited);
} else {
searchInLabels(type, filter);
}
}
}
private Map<LabelType, List<Label>> getLabelsMap() {
return databaseSnapshots.snapshotLabelsMap();
}
private void searchInLabels(LabelType type, String filter) {
for (Label label : getLabelsMap().get(type)) {
String name = StringUtils.deleteWhitespace(label.getName()
.toLowerCase());
if (name.contains(filter)) {
addLabel(type, label);
if ((filter.length() < 3) && (getListMatching().size() > 9)) {
return;
}
}
}
}
private void setFilterPairLabelType(LabelType type, boolean limited) {
for (Label label : getLabelsMap().get(type)) {
addLabel(type, label);
if ((limited) && (getListMatching().size() > 9)) {
return;
}
}
}
private void searchInExternalCompanies(String filter){
for(ExternalCompany externalCompany : databaseSnapshots
.snapshotExternalCompanies()){
String name = StringUtils.deleteWhitespace(externalCompany
.getName().toLowerCase());
String nif = StringUtils.deleteWhitespace(externalCompany.getNif()
.toLowerCase());
if ((name.contains(filter)) || (nif.contains(filter))) {
addExternalCompany(externalCompany);
if ((filter.length() < 3) && (getListMatching().size() > 9)) {
return;
}
}
}
}
private void searchInOrderStatus(String filter) {
for (OrderStatusEnum state : OrderStatusEnum.values()) {
String name = StringUtils.deleteWhitespace(state.name()
.toLowerCase());
if (name.contains(filter)) {
addState(state);
if ((filter.length() < 3) && (getListMatching().size() > 9)) {
return;
}
}
}
}
private void searchInOrderCodes(String filter) {
if (filter.indexOf("cod:") == 0) {
String codeFilter = filter.replaceFirst("cod:", "");
for (String code : databaseSnapshots.snapshotOrdersCodes()) {
code = StringUtils.deleteWhitespace(code.toLowerCase());
if (code.equals(codeFilter)) {
addCode(code);
return;
}
}
}
}
private void searchInCustomerReferences(String filter) {
if (filter.indexOf("rc:") == 0) {
String referenceFilter = filter.replaceFirst("rc:", "");
for (String reference : databaseSnapshots
.snapshotCustomerReferences()) {
reference = StringUtils.deleteWhitespace(reference
.toLowerCase());
if (reference.equals(referenceFilter)) {
addCustomerReference(reference);
return;
}
}
}
}
private void addCriterion(CriterionType type, Criterion criterion) {
String pattern = criterion.getName() + " ( " + type.getName() + " )";
getListMatching().add(
new FilterPair(TaskGroupFilterEnum.Criterion, type
.getResource()
.toLowerCase(), pattern, criterion));
}
private void addLabel(LabelType type, Label label) {
String pattern = label.getName() + " ( " + type.getName() + " )";
getListMatching().add(
new FilterPair(TaskGroupFilterEnum.Label, pattern, label));
}
private void addExternalCompany(ExternalCompany externalCompany) {
String pattern = externalCompany.getName() + " :: "
+ externalCompany.getNif();
getListMatching().add(
new FilterPair(TaskGroupFilterEnum.ExternalCompany,
pattern, externalCompany));
}
private void addState(OrderStatusEnum state) {
getListMatching().add(
new FilterPair(TaskGroupFilterEnum.State, state.name(),
state));
}
private void addCode(String code) {
getListMatching().add(
new FilterPair(TaskGroupFilterEnum.Code, code, code));
}
private void addCustomerReference(String reference) {
getListMatching().add(
new FilterPair(TaskGroupFilterEnum.CustomerReference,
reference, reference));
}
}

View file

@ -1,50 +0,0 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
* Copyright (C) 2010-2011 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.navalplanner.web.planner;
import java.util.Date;
import java.util.List;
import org.navalplanner.business.orders.entities.Order;
import org.navalplanner.business.planner.entities.TaskGroup;
import org.navalplanner.web.common.components.finders.FilterPair;
import org.navalplanner.web.orders.OrderPredicate;
import org.zkoss.ganttz.IPredicate;
/**
* Checks if {@link Order}, the start date and finish date from associated
* {@link Task} matches attributes
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
public class CompanyPredicate extends OrderPredicate implements IPredicate {
public CompanyPredicate(List<FilterPair> filters, Date startDate,
Date finishDate, Boolean includeOrderElements) {
super(filters, startDate, finishDate, includeOrderElements);
}
@Override
protected boolean acceptFiltersDates(Order order) {
TaskGroup associatedTaskElement = order.getAssociatedTaskElement();
return (this.acceptStartDate(associatedTaskElement.getStartDate()) && (acceptFinishDate(associatedTaskElement
.getEndDate())));
}
}

View file

@ -0,0 +1,252 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2011 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.navalplanner.web.planner;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.navalplanner.business.externalcompanies.entities.ExternalCompany;
import org.navalplanner.business.labels.entities.Label;
import org.navalplanner.business.orders.entities.Order;
import org.navalplanner.business.orders.entities.OrderElement;
import org.navalplanner.business.orders.entities.OrderStatusEnum;
import org.navalplanner.business.planner.entities.GenericResourceAllocation;
import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.TaskElement;
import org.navalplanner.business.planner.entities.TaskGroup;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.web.common.components.finders.FilterPair;
import org.navalplanner.web.common.components.finders.TaskGroupFilterEnum;
import org.zkoss.ganttz.IPredicate;
/**
* Checks if {@link TaskGroup} in company Gantt view matches with the different
* filters.
*
* @author Manuel Rego Casasnovas <rego@igalia.com>
*/
public class TaskGroupPredicate implements IPredicate {
private List<FilterPair> filters;
private Date startDate;
private Date finishDate;
private Boolean includeChildren;
public TaskGroupPredicate(List<FilterPair> filters, Date startDate,
Date finishDate, Boolean includeChildren) {
this.filters = filters;
this.startDate = startDate;
this.finishDate = finishDate;
this.includeChildren = includeChildren;
}
@Override
public boolean accepts(Object object) {
final TaskGroup taskGroup = (TaskGroup) object;
return accepts(taskGroup);
}
private boolean accepts(TaskGroup taskGroup) {
if (taskGroup == null) {
return false;
}
if (acceptFilters(taskGroup) && acceptFiltersDates(taskGroup)) {
return true;
}
return false;
}
private boolean acceptFilters(TaskGroup taskGroup) {
if ((filters == null) || (filters.isEmpty())) {
return true;
}
for (FilterPair filter : filters) {
if (!acceptFilter(filter, taskGroup)) {
return false;
}
}
return true;
}
private boolean acceptFilter(FilterPair filter, TaskGroup taskGroup) {
switch ((TaskGroupFilterEnum) filter.getType()) {
case Criterion:
return acceptCriterion(filter, taskGroup);
case Label:
return acceptLabel(filter, taskGroup);
case ExternalCompany:
return acceptExternalCompany(filter, taskGroup);
case State:
return acceptState(filter, taskGroup);
case Code:
return acceptCode(filter, taskGroup);
case CustomerReference:
return acceptCustomerReference(filter, taskGroup);
}
return false;
}
private boolean acceptCriterion(FilterPair filter, TaskElement taskElement) {
Criterion filterCriterion = (Criterion) filter.getValue();
if (existCriterionInTaskElementResourceAllocations(filterCriterion,
taskElement)) {
return true;
}
if (includeChildren) {
for (TaskElement each : taskElement.getAllChildren()) {
if (existCriterionInTaskElementResourceAllocations(
filterCriterion, each)) {
return true;
}
}
}
return false;
}
private boolean existCriterionInTaskElementResourceAllocations(
Criterion filterCriterion, TaskElement taskElement) {
for (ResourceAllocation<?> each : taskElement
.getAllResourceAllocations()) {
if (acceptsCriterionInResourceAllocation(filterCriterion, each)) {
return true;
}
}
return false;
}
private boolean acceptsCriterionInResourceAllocation(
Criterion filterCriterion, ResourceAllocation<?> resourceAllocation) {
if (resourceAllocation instanceof GenericResourceAllocation) {
Set<Criterion> criteria = ((GenericResourceAllocation) resourceAllocation)
.getCriterions();
for (Criterion criterion : criteria) {
if (criterion.getId().equals(filterCriterion.getId())) {
return true;
}
}
}
return false;
}
private boolean acceptLabel(FilterPair filter, TaskGroup taskGroup) {
Label filterLabel = (Label) filter.getValue();
Order order = (Order) taskGroup.getOrderElement();
if (existLabelInOrderElement(filterLabel, order)) {
return true;
}
if (this.includeChildren) {
for (OrderElement orderElement : order.getAllOrderElements()) {
if (existLabelInOrderElement(filterLabel, orderElement)) {
return true;
}
}
}
return false;
}
private boolean existLabelInOrderElement(Label filterLabel,
OrderElement order) {
for (Label label : order.getLabels()) {
if (label.getId().equals(filterLabel.getId())) {
return true;
}
}
return false;
}
private boolean acceptExternalCompany(FilterPair filter, TaskGroup taskGroup) {
Order order = (Order) taskGroup.getOrderElement();
ExternalCompany filterCustomer = (ExternalCompany) filter.getValue();
if ((order.getCustomer() != null)
&& (order.getCustomer().getId().equals(filterCustomer.getId()))) {
return true;
}
return false;
}
private boolean acceptState(FilterPair filter, TaskGroup taskGroup) {
Order order = (Order) taskGroup.getOrderElement();
OrderStatusEnum filterState = (OrderStatusEnum) filter.getValue();
if ((order.getState() != null)
&& (order.getState().equals(filterState))) {
return true;
}
return false;
}
private boolean acceptCode(FilterPair filter, TaskGroup taskGroup) {
Order order = (Order) taskGroup.getOrderElement();
String filterCode = (String) filter.getValue();
return order.getCode().equals(filterCode);
}
private boolean acceptCustomerReference(FilterPair filter,
TaskGroup taskGroup) {
Order order = (Order) taskGroup.getOrderElement();
String filterCustomerReference = (String) filter.getValue();
return order.getCustomerReference().equals(filterCustomerReference);
}
protected boolean acceptFiltersDates(TaskGroup taskGroup) {
// Check if exist work report items into interval between the start date
// and finish date.
return (acceptStartDate(taskGroup.getStartDate()) && (acceptFinishDate(taskGroup
.getEndDate())));
}
protected boolean acceptStartDate(Date initDate) {
if ((initDate == null) && (startDate == null)) {
return true;
}
return isLowerToFinishDate(initDate, finishDate);
}
protected boolean acceptFinishDate(Date deadLine) {
if ((deadLine == null) && (finishDate == null)) {
return true;
}
return isGreaterToStartDate(deadLine, startDate);
}
private boolean isGreaterToStartDate(Date date, Date startDate) {
if (startDate == null) {
return true;
}
if (date != null && (date.compareTo(startDate) >= 0)) {
return true;
}
return false;
}
private boolean isLowerToFinishDate(Date date, Date finishDate) {
if (finishDate == null) {
return true;
}
if (date != null && (date.compareTo(finishDate) <= 0)) {
return true;
}
return false;
}
}

View file

@ -34,7 +34,7 @@ import org.navalplanner.business.common.entities.ProgressType;
import org.navalplanner.business.planner.entities.TaskElement;
import org.navalplanner.web.common.components.bandboxsearch.BandboxMultipleSearch;
import org.navalplanner.web.common.components.finders.FilterPair;
import org.navalplanner.web.planner.CompanyPredicate;
import org.navalplanner.web.planner.TaskGroupPredicate;
import org.navalplanner.web.planner.tabs.MultipleTabsPlannerController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
@ -58,7 +58,6 @@ import org.zkoss.zul.ComboitemRenderer;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.SimpleListModel;
import org.zkoss.zul.Vbox;
/**
@ -131,6 +130,7 @@ public class CompanyPlanningController implements Composer {
.getFellow("filterFinishDate");
bdFilters = (BandboxMultipleSearch) filterComponent
.getFellow("bdFilters");
bdFilters.setFinder("taskGroupsMultipleFiltersFinder");
checkIncludeOrderElements = (Checkbox) filterComponent
.getFellow("checkIncludeOrderElements");
filterComponent.setVisible(true);
@ -276,7 +276,7 @@ public class CompanyPlanningController implements Composer {
if (listFilters.isEmpty() && startDate == null && finishDate == null) {
return null;
}
return new CompanyPredicate(listFilters, startDate, finishDate,
return new TaskGroupPredicate(listFilters, startDate, finishDate,
includeOrderElements);
}

View file

@ -756,7 +756,8 @@ public class CompanyPlanningModel implements ICompanyPlanningModel {
if (associatedTaskElement != null
&& STATUS_VISUALIZED.contains(order.getState())
&& (predicate == null || predicate.accepts(order))) {
&& (predicate == null || predicate
.accepts(associatedTaskElement))) {
associatedTaskElement.setSimplifiedAssignedStatusCalculationEnabled(true);
result.add(associatedTaskElement);
ordersToShow.add(order);

View file

@ -29,6 +29,8 @@
<bean id="ordersMultipleFiltersFinder" class="org.navalplanner.web.common.components.finders.OrdersMultipleFiltersFinder" scope="singleton" />
<bean id="taskGroupsMultipleFiltersFinder" class="org.navalplanner.web.common.components.finders.TaskGroupsMultipleFiltersFinder" scope="singleton" />
<bean id="orderElementsMultipleFiltersFinder" class="org.navalplanner.web.common.components.finders.OrderElementsMultipleFiltersFinder" scope="singleton"/>
<bean id="taskElementsMultipleFiltersFinder" class="org.navalplanner.web.common.components.finders.TaskElementsMultipleFiltersFinder" scope="singleton" />