Allow to filter resources by criteria in the resource load window
FEA: ItEr76S23ImproveFilteringArea
This commit is contained in:
parent
6c66ac0f6c
commit
7570bee03c
3 changed files with 206 additions and 37 deletions
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.libreplan.web.common.components.finders;
|
||||
|
||||
import org.libreplan.business.resources.entities.Resource;
|
||||
|
||||
/**
|
||||
* Diferent filters for {@link Resource}.
|
||||
*
|
||||
* @author Manuel Rego Casasnovas <mrego@igalia.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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.<br />
|
||||
* It provides the following criteria to filter: {@link Resource} and
|
||||
* {@link Criterion}.
|
||||
*
|
||||
* @author Manuel Rego Casasnovas <rego@igalia.com>
|
||||
*/
|
||||
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<FilterPair> getFirstTenFilters() {
|
||||
getListMatching().clear();
|
||||
Iterator<Worker> 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<Worker> getListWorkers() {
|
||||
return databaseSnapshots.snapshotListWorkers();
|
||||
private List<FilterPair> fillWithFirstTenFiltersResources() {
|
||||
Map<Class<?>, List<Resource>> mapResources = getMapResources();
|
||||
Iterator<Class<?>> 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<Class<?>, List<Resource>> 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<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 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<FilterPair> 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<Class<?>, List<Resource>> 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<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) {
|
||||
List<Criterion> list = getMapCriterions().get(type);
|
||||
if (list == null) {
|
||||
return;
|
||||
}
|
||||
for (Criterion criterion : list) {
|
||||
addCriterion(type, criterion);
|
||||
if ((limited) && (getListMatching().size() > 9)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Object> 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<Resource> calculateResourcesToShow() {
|
||||
List<Resource> resources = new ArrayList<Resource>();
|
||||
List<Criterion> criteria = new ArrayList<Criterion>();
|
||||
|
||||
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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue