diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/common/components/finders/WorkerFilterEnum.java b/libreplan-webapp/src/main/java/org/libreplan/web/common/components/finders/WorkerFilterEnum.java new file mode 100644 index 000000000..a22a8d2b2 --- /dev/null +++ b/libreplan-webapp/src/main/java/org/libreplan/web/common/components/finders/WorkerFilterEnum.java @@ -0,0 +1,50 @@ +/* + * This file is part of LibrePlan + * + * Copyright (C) 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 . + */ + +package org.libreplan.web.common.components.finders; + +import org.libreplan.business.resources.entities.Resource; + +/** + * Diferent filters for {@link Resource}. + * + * @author Manuel Rego Casasnovas + */ +public enum WorkerFilterEnum implements IFilterEnum { + + RESOURCE(_("Resource")), CRITERION(_("Criterion")); + + /** + * Forces to mark the string as needing translation + */ + private static String _(String string) { + return string; + } + + private String description; + + private WorkerFilterEnum(String description) { + this.description = description; + } + + public String toString() { + return this.description; + } + +} diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/common/components/finders/WorkerMultipleFiltersFinder.java b/libreplan-webapp/src/main/java/org/libreplan/web/common/components/finders/WorkerMultipleFiltersFinder.java index a10f92f6b..179f01865 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/common/components/finders/WorkerMultipleFiltersFinder.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/common/components/finders/WorkerMultipleFiltersFinder.java @@ -3,7 +3,7 @@ * * 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. + * 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 @@ -23,67 +23,160 @@ package org.libreplan.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.libreplan.business.hibernate.notification.PredefinedDatabaseSnapshots; -import org.libreplan.business.resources.entities.Worker; +import org.libreplan.business.resources.entities.Criterion; +import org.libreplan.business.resources.entities.CriterionType; +import org.libreplan.business.resources.entities.Resource; import org.springframework.beans.factory.annotation.Autowired; +/** + * Implements all the methods needed to search the different criteria to filter + * the {@link Resource}s.
+ * It provides the following criteria to filter: {@link Resource} and + * {@link Criterion}. + * + * @author Manuel Rego Casasnovas + */ public class WorkerMultipleFiltersFinder extends MultipleFiltersFinder { @Autowired private PredefinedDatabaseSnapshots databaseSnapshots; - private IFilterEnum workerFilterEnum = new IFilterEnum() { - @Override - public String toString() { - return "Resource ( Worker )"; - } - }; - - /** - * Forces to mark the string as needing translation - */ - private static String _(String string) { - return string; + protected WorkerMultipleFiltersFinder() { } @Override public List getFirstTenFilters() { getListMatching().clear(); - Iterator iteratorWorker = getListWorkers().iterator(); - while(iteratorWorker.hasNext() && getListMatching().size() < 10) { - Worker worker = iteratorWorker.next(); - getListMatching().add( - new FilterPair(workerFilterEnum, worker.getDescription(), - worker)); - } + fillWithFirstTenFiltersResources(); + fillWithFirstTenFiltersCriterions(); addNoneFilter(); return getListMatching(); } - private List getListWorkers() { - return databaseSnapshots.snapshotListWorkers(); + private List fillWithFirstTenFiltersResources() { + Map, List> mapResources = getMapResources(); + Iterator> iteratorClass = mapResources.keySet().iterator(); + while (iteratorClass.hasNext() && getListMatching().size() < 10) { + Class className = iteratorClass.next(); + for (int i = 0; getListMatching().size() < 10 + && i < mapResources.get(className).size(); i++) { + Resource resource = mapResources.get(className).get(i); + addResource(className, resource); + } + } + return getListMatching(); + } + + private Map, List> getMapResources() { + return databaseSnapshots.snapshotMapResources(); + } + + private void addResource(Class className, Resource resource) { + String pattern = resource.getName(); + getListMatching().add( + new FilterPair(WorkerFilterEnum.RESOURCE, className + .getSimpleName(), pattern, resource)); + } + + private List fillWithFirstTenFiltersCriterions() { + SortedMap> mapCriterions = getMapCriterions(); + Iterator 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> getMapCriterions() { + return databaseSnapshots.snapshotCriterionsMap(); + } + + private void addCriterion(CriterionType type, Criterion criterion) { + String pattern = criterion.getName() + " ( " + type.getName() + " )"; + getListMatching().add( + new FilterPair(WorkerFilterEnum.CRITERION, type + .getResource().toLowerCase(), pattern, criterion)); } - @Override public List getMatching(String filter) { getListMatching().clear(); if ((filter != null) && (!filter.isEmpty())) { filter = StringUtils.deleteWhitespace(filter.toLowerCase()); - searchInWorkers(filter); + searchInResources(filter); + searchInCriterionTypes(filter); } + addNoneFilter(); return getListMatching(); - } - private void searchInWorkers(String filter) { - for (Worker worker : getListWorkers()) { - String name = StringUtils.deleteWhitespace(worker.getDescription() + + private void searchInResources(String filter) { + Map, List> mapResources = databaseSnapshots + .snapshotMapResources(); + for (Class className : mapResources.keySet()) { + for (Resource resource : mapResources.get(className)) { + String name = StringUtils.deleteWhitespace(resource.getName() + .toLowerCase()); + if (name.contains(filter)) { + addResource(className, resource); + if ((filter.length() < 3) && (getListMatching().size() > 9)) { + return; + } + } + } + } + } + + 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)) { - getListMatching().add(new FilterPair( - workerFilterEnum, worker.getShortDescription(), worker)); + if (name.contains(filter)) { + setFilterPairCriterionType(type, limited); + } else { + searchInCriterions(type, filter); + } + } + } + + private void searchInCriterions(CriterionType type, String filter) { + List 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) { + List list = getMapCriterions().get(type); + if (list == null) { + return; + } + for (Criterion criterion : list) { + addCriterion(type, criterion); + if ((limited) && (getListMatching().size() > 9)) { + return; } } } diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/resourceload/ResourceLoadController.java b/libreplan-webapp/src/main/java/org/libreplan/web/resourceload/ResourceLoadController.java index 3af09f504..e764bb1c5 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/resourceload/ResourceLoadController.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/resourceload/ResourceLoadController.java @@ -43,6 +43,7 @@ import org.libreplan.business.planner.chart.ILoadChartData; import org.libreplan.business.planner.chart.ResourceLoadChartData; import org.libreplan.business.planner.entities.DayAssignment; import org.libreplan.business.planner.entities.TaskElement; +import org.libreplan.business.resources.daos.IResourcesSearcher; import org.libreplan.business.resources.entities.Criterion; import org.libreplan.business.resources.entities.Resource; import org.libreplan.web.common.components.bandboxsearch.BandboxMultipleSearch; @@ -123,6 +124,9 @@ public class ResourceLoadController implements Composer { private IOrderPlanningGate planningControllerEntryPoints; + @Autowired + private IResourcesSearcher resourcesSearcher; + public ResourceLoadController() { } @@ -265,7 +269,7 @@ public class ResourceLoadController implements Composer { result.add(filterTypeChanger); result.add(new ByDatesFilter(onChange, filterBy)); WorkersOrCriteriaBandbox bandbox = new WorkersOrCriteriaBandbox( - onChange, filterBy, filterTypeChanger); + onChange, filterBy, filterTypeChanger, resourcesSearcher); result.add(bandbox); result.add(new ByNamePaginator(onChange, filterBy, filterTypeChanger, bandbox)); @@ -501,10 +505,13 @@ public class ResourceLoadController implements Composer { private List entitiesSelected = null; + private final IResourcesSearcher resourcesSearcher; + private WorkersOrCriteriaBandbox(Runnable onChange, - PlanningState filterBy, - FilterTypeChanger filterType) { + PlanningState filterBy, FilterTypeChanger filterType, + IResourcesSearcher resourcesSearcher) { super(onChange, filterBy, filterType); + this.resourcesSearcher = resourcesSearcher; } @Override @@ -563,14 +570,33 @@ public class ResourceLoadController implements Composer { parameters.clearResourcesToShow(); parameters.clearCriteriaToShow(); } else if (isFilteringByResource()) { - parameters.setResourcesToShow(as(Resource.class, - entitiesSelected)); + parameters.setResourcesToShow(calculateResourcesToShow()); } else { parameters.setCriteriaToShow(as(Criterion.class, entitiesSelected)); } } + private List calculateResourcesToShow() { + List resources = new ArrayList(); + List criteria = new ArrayList(); + + for (Object each : entitiesSelected) { + if (each instanceof Resource) { + resources.add((Resource) each); + } else { + criteria.add((Criterion) each); + } + } + + if (!criteria.isEmpty()) { + resources.addAll(resourcesSearcher.searchBoth() + .byCriteria(criteria).execute()); + } + + return resources; + } + public boolean hasEntitiesSelected() { return entitiesSelected != null && !entitiesSelected.isEmpty(); }