dk.apaq.framework.criteria.rules.BeanFiltrationItem Maven / Gradle / Ivy
/*
* CrudContainer
* Copyright (C) 2011 by Apaq
*
* 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 dk.apaq.framework.criteria.rules;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import dk.apaq.framework.criteria.Rule.FiltrationItem;
/**
*
*/
public class BeanFiltrationItem implements FiltrationItem {
private static final String[] GETTERTYPES = {"get", "is", "has"};
private final T bean;
public BeanFiltrationItem(T bean) {
this.bean = bean;
}
@Override
public Object getFiltrationProperty(String propertyId) {
String preparedId = capitalize(propertyId);
Method method = null;
for (String type : GETTERTYPES) {
method = lookupGetterMethod(type + preparedId);
if (method != null) {
break;
}
}
if (method != null) {
return invoke(method);
}
return null;
}
public T getBean() {
return bean;
}
private String capitalize(String value) {
if (value.length() == 0) {
return value;
}
if (value.length() == 1) {
return value.toUpperCase();
}
return value.substring(0, 1).toUpperCase() + value.substring(1);
}
private Object invoke(Method method) {
try {
return method.invoke(this.bean);
} catch (IllegalAccessException ex) {
return null;
} catch (IllegalArgumentException ex) {
return null;
} catch (InvocationTargetException ex) {
return null;
}
}
private Method lookupGetterMethod(String name) {
try {
return bean.getClass().getMethod(name);
} catch (NoSuchMethodException ex) {
return null;
} catch (SecurityException ex) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy