/** * Copyright © 2002 Instituto Superior Técnico * * This file is part of FenixEdu Academic. * * FenixEdu Academic is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * FenixEdu Academic 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with FenixEdu Academic. If not, see <http://www.gnu.org/licenses/>. */ package org.fenixedu.academic.service.services.commons; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.beanutils.PropertyUtils; import org.fenixedu.academic.domain.exceptions.DomainException; import org.fenixedu.commons.StringNormalizer; public abstract class AbstractSearchObjects<T> { protected Collection<T> process(Collection<T> objects, String value, int limit, Map<String, String> arguments) { List<T> result; String slotName = arguments.get("slot"); if (value == null) { result = (List<T>) objects; } else { result = new ArrayList<T>(); String[] values = StringNormalizer.normalize(value).split("\\p{Space}+"); outter: for (T object : objects) { try { String objectValue = (String) PropertyUtils.getProperty(object, slotName); if (objectValue == null) { continue; } String normalizedValue = StringNormalizer.normalize(objectValue); for (int i = 0; i < values.length; i++) { String part = values[i]; if (!normalizedValue.contains(part)) { continue outter; } } result.add(object); if (result.size() >= limit) { break; } } catch (IllegalAccessException e) { throw new DomainException("searchObject.type.notFound", e); } catch (InvocationTargetException e) { throw new DomainException("searchObject.failed.read", e); } catch (NoSuchMethodException e) { throw new DomainException("searchObject.failed.read", e); } } } Collections.sort(result, new BeanComparator(slotName)); return result; } }