ItEr29S06CUAsignacionGrupoRecursosAPlanificacionItEr28S06: [Refactoring] Filter workers by name/NIF and selected criterions

This commit is contained in:
Diego Pino Garcia 2009-10-06 20:14:59 +02:00 committed by Javier Moran Rua
parent a874c84594
commit 39639c75af
5 changed files with 77 additions and 46 deletions

View file

@ -37,6 +37,28 @@ import org.navalplanner.business.resources.entities.Worker;
*/
public interface IWorkerDAO extends IGenericDAO<Worker, Long> {
/**
* Returns workers which name/NIF partially matches with name, and complies
* all of the given criterions
*
* @param name
* search worker by name/NIF
* @param criterions
* search worker that matches with criterions
* @return
*/
@SuppressWarnings("unchecked")
List<Worker> findByNameAndCriterions(String name, List<Criterion> criterions);
/**
* Returns workers which name/NIF partially matches with name
*
* @param name
* search worker by name(firstname or surname)/NIF
*
*/
List<Worker> findByNameOrNif(String name);
/**
* Finds a {@link Worker} with the NIF param that should be unique.
*
@ -55,17 +77,4 @@ public interface IWorkerDAO extends IGenericDAO<Worker, Long> {
* @return
*/
List<Worker> getWorkers();
/**
* Returns workers which name/NIF partially matches with name, and complies
* all of the given criterions
*
* @param name
* search worker by name/NIF
* @param criterions
* search worker that matches with criterions
* @return
*/
@SuppressWarnings("unchecked")
List<Worker> findByNameAndCriterions(String name, List<Criterion> criterions);
}

View file

@ -20,10 +20,11 @@
package org.navalplanner.business.resources.daos;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.navalplanner.business.common.daos.GenericDAOHibernate;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
@ -68,29 +69,39 @@ public class WorkerDAO extends GenericDAOHibernate<Worker, Long>
@SuppressWarnings("unchecked")
@Override
public List<Worker> findByNameAndCriterions(String name,
List<Criterion> criterionList) {
List<Criterion> criterions) {
// Prepare query
String strQuery = "SELECT worker FROM Worker worker ";
if (criterionList != null && criterionList.size() > 0) {
strQuery += "JOIN worker.criterionSatisfactions AS satisfaction "
+ "JOIN satisfaction.criterion AS criterion ";
// Find workers by name
List<Worker> workers;
if (name == null || name.isEmpty()) {
workers = getWorkers();
} else {
workers = findByNameOrNif(name);
}
strQuery += "WHERE (UPPER(worker.firstName) LIKE :name OR worker.nif LIKE :name) ";
if (criterionList != null && criterionList.size() > 0) {
strQuery += " AND criterion IN (:criterionList)";
// If no criterions selected, returned found workers
if (criterions.isEmpty()) {
return workers;
}
// Execute query
Query query = getSession().createQuery(strQuery);
query.setParameter("name", "%" + name.toUpperCase() + "%");
if (criterionList != null && criterionList.size() > 0) {
query.setParameterList("criterionList", criterionList);
// Filter by criterion
final List<Worker> result = new ArrayList<Worker>();
for (Worker worker : workers) {
if (worker.satisfiesCriterions(new HashSet(criterions))) {
result.add(worker);
}
}
// Get result
return query.list();
return result;
}
@SuppressWarnings("unchecked")
@Override
public List<Worker> findByNameOrNif(String name) {
name = "%" + name + "%";
return getSession().createCriteria(Worker.class).add(
Restrictions.or(Restrictions.or(Restrictions.ilike("firstName",
name), Restrictions.ilike("surname", name)),
Restrictions.like("nif", name))).list();
}
}

View file

@ -36,11 +36,12 @@ import org.navalplanner.business.resources.entities.Worker;
public interface IWorkerSearchModel {
/**
* Gets all {@link Criterion} and groups then by {@link CriterionType}
* Returns all {@link Worker} matching by name (firstname or surname)
*
* @return HashMap<CriterionType, Set<Criterion>>
* @param name
* @return
*/
HashMap<CriterionType, Set<Criterion>> getCriterions();
List<Worker> findWorkers(String name);
/**
* Queries database for retrieving all workers that match to the parameters
@ -51,12 +52,19 @@ public interface IWorkerSearchModel {
* {@link Worker} that complies all criterions
* @return
*/
@SuppressWarnings("unchecked")
List<Worker> findWorkers(String name, List<Criterion> criterions);
/**
* Returns all workers
*
* @return
*/
List<Worker> getAllWorkers();
/**
* Gets all {@link Criterion} and groups then by {@link CriterionType}
*
* @return HashMap<CriterionType, Set<Criterion>>
*/
HashMap<CriterionType, Set<Criterion>> getCriterions();
}

View file

@ -125,16 +125,9 @@ public class WorkerSearchController extends GenericForwardComposer {
* @param criterions
*/
private void searchWorkers(String name, List<Criterion> criterions) {
// No text, and no criterions selected
if (criterions.isEmpty()
&& (name == null || name.isEmpty())) {
refreshListBoxWorkers(workerSearchModel.getAllWorkers());
return;
}
final List<Worker> listWorkers = workerSearchModel.findWorkers(name,
final List<Worker> workers = workerSearchModel.findWorkers(name,
criterions);
refreshListBoxWorkers(listWorkers);
refreshListBoxWorkers(workers);
}
/**
@ -154,7 +147,6 @@ public class WorkerSearchController extends GenericForwardComposer {
result.add((Criterion) node.getData());
}
}
return result;
}

View file

@ -71,6 +71,17 @@ public class WorkerSearchModel implements IWorkerSearchModel {
return workerDAO.findByNameAndCriterions(name, criterions);
}
@Override
@Transactional(readOnly = true)
public List<Worker> findWorkers(String name) {
return workerDAO.findByNameOrNif(name);
}
public List<Worker> findByNameAndCriterions(String name,
List<Criterion> criterions) {
return workerDAO.findByNameAndCriterions(name, criterions);
}
@Override
@Transactional(readOnly = true)
public List<Worker> getAllWorkers() {