/*
* Copyright 2010 Semafor Informatik & Energie AG, Basel, Switzerland
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.semafor.gendas.service;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;
import ch.semafor.gendas.dao.ElementDao;
import ch.semafor.gendas.dao.ElementTypeDao;
import ch.semafor.gendas.dao.PropertyTypeDao;
import ch.semafor.gendas.model.CoreException;
import ch.semafor.gendas.model.Element;
import ch.semafor.gendas.model.ElementRefList;
import ch.semafor.gendas.model.ElementRefs;
import ch.semafor.gendas.model.ElementType;
import ch.semafor.gendas.model.Modification;
import ch.semafor.gendas.model.Property;
import ch.semafor.gendas.model.PropertyType;
import ch.semafor.gendas.model.PropertyValueList;
/**
* create element tree and load bean from id
* @author tar
*
*/
public class ElementCreator extends Creator {
private final Logger logger = LoggerFactory.getLogger(ElementCreator.class);
final ElementTypeDao elementTypeDao; // NOPMD by wim on 9/20/10 3:01 PM
final ElementDao elementDao; // NOPMD by wim on 9/20/10 3:00 PM
final PropertyTypeDao propertyTypeDao; // NOPMD by wim on 9/20/10 3:00 PM
private Map<Object, Element> beanElementMap; // NOPMD by wim on 9/20/10 3:03 PM
private Map<Long, Object> loadedBean; // NOPMD by wim on 9/20/10 3:03 PM
private PersistenceCache cache=null;
/**
* @param elementTypeDao
* @param propertyTypeDao
*/
public ElementCreator(final ElementTypeDao elementTypeDao,
final ElementDao elementDao,
final PropertyTypeDao propertyTypeDao,
final PersistenceCache cache ){
super();
this.elementTypeDao = elementTypeDao;
this.elementDao = elementDao;
this.propertyTypeDao = propertyTypeDao;
this.cache = cache;
// this.idName = idName;
// this.idVersion = idVersion;
resetMaps();
}
public void resetMaps(){
beanElementMap = new HashMap<Object, Element>();
loadedBean = new HashMap<Long, Object>();
}
/**
* @throws ElementTypeNotFoundException
* */
private ElementType getElementType(final String typeName)
throws CoreException {
final ElementType elType = elementTypeDao.findByName(typeName);
if (elType == null) {
throw new CoreException("ElementType " + typeName + " not found");
}
return elType;
}
/**
* get method for setting property
*
* @param propName
* of property
* @return set method
*/
private Method getGetProperty(final Class beanClass, final String propName) {
if (propName == null || propName.length() == 0) {
return null; // NOPMD by wim on 9/20/10 2:54 PM
}
// ToDo, append, PMD: InefficientStringBuffering
final StringBuffer name = new StringBuffer("get" + propName);
name.setCharAt(3, Character.toUpperCase(propName.charAt(0)));
try {
return beanClass.getMethod(name.toString()); // NOPMD by wim on 9/20/10 2:54 PM
} catch (NoSuchMethodException ex) {
logger.warn("No such Method '{}' in {}", name,
beanClass.getCanonicalName());
return null;
}
}
/**
* set property value in element
*
* @param propName
* @param value
* @param retType
* @param element
*/
private void setProperty(final String propName, final Object value,
final Class retType, final Element element) {
try {
final PropertyType propType = propertyTypeDao.findByName(propName);
Property prop = element.getProperty(propType);
if (prop == null) {
prop = new Property(element, propType);
}
if (value instanceof java.util.List) {
List l = (java.util.List) value;
logger.debug("setting {} properties of \"{}\"", l.size(), prop.getType().getName());
for (int i = 0; i < l.size(); i++) {
setPropertyValue(prop, i, retType, l.get(i));
}
} else {
setPropertyValue(prop, 0, retType, value);
}
if (logger.isDebugEnabled()) {
logger.debug("adding property \"{}\"", prop.getType().getName());
}
} catch (CoreException ex) {
logger.info(ex.getMessage());
}
}
private void setIdAndVersion(final Object bean, final Element element, final Class genArgType ) throws CoreException{
final ElementType elType; // NOPMD PMD: AvoidFinalLocalVariable by wim on 9/20/10 2:59 PM
if (bean instanceof java.util.List) {
logger.debug("bean {} instance of list<{}>", bean.getClass().getCanonicalName(),
genArgType.getCanonicalName() + ">");
elType = getElementType(genArgType.getCanonicalName());
} else {
elType = getElementType(bean.getClass().getCanonicalName());
}
logger.info( "setting bean property {} for type {}", elType.getBeanId(), elType.getName() );
setId(bean, element.getId(), elType.getBeanId());
if( element.getVersion() != null){
Integer v = Integer.valueOf(element.getVersion().intValue());
setVersion(bean, v, elType.getBeanVersionId());
}
}
/** set all ids and versions recursively (must be called after save)
* @param bean to be set id and version
* @param element id and version holder
* @param genArgType type of contained objects if bean is a list
* @throws CoreException
*
*/
public void setMatchingIdsAndVersions(final Object bean, final Element element, final Class genArgType ) throws CoreException{
final Class<?> clazz = bean.getClass();
logger.debug("class {}", clazz.getCanonicalName());
if (beanElementMap.containsKey(bean)) {
return;
}
setIdAndVersion( bean, element, genArgType );
logger.debug("checking Element ({})", element.toString());
beanElementMap.put(bean, element);
ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
public void doWith(final Method method) {
try {
if (((method.getName().startsWith("get") && method.getName().length() > 3) || (method
.getName().startsWith("is") && method.getName().length() > 2))
&& !method.getName().equals("getClass")) {
final Class retType = method.getReturnType();
logger.debug("checking method return type {}",
retType.getCanonicalName());
if (!isPrimitiveType(retType)) {
Class genArgType = null;
String type = retType.getCanonicalName();
if (method.getReturnType().equals(java.util.List.class)) {
genArgType = getGenericArgType(method.getGenericReturnType());
type = genArgType.getCanonicalName();
}
logger.debug("composite {}", type);
final String propName = getPropertyName(method);
if (element.getElementType().hasReference(propName)) {
logger.debug("about to get reference {}", propName);
final Object ref = method.invoke(bean);
if (ref != null && ref instanceof java.util.List) {
logger.debug("LIST SIZE {}",
((java.util.List) ref).size());
if (!isPrimitiveType(genArgType)) {
ElementRefs refs =element.getElementRefs(propName);
if( refs != null ){
logger.debug("setting ids/versions for references {}", propName);
final Iterator<Element> eIter = refs.getLastListOfElements().iterator();
final Iterator<Object> oIter =((java.util.List) ref).iterator();
while( eIter.hasNext() && oIter.hasNext()){
Object o = oIter.next();
if (o != null) {
setMatchingIdsAndVersions(o, eIter.next(), genArgType);
}
}
}
logger.debug("end");
}
}
}
}
}
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/**
* creates a new element tree from object
*
* @param bean
* object to create element tree from
* @param genArgType
* generic type (must be valid if bean is a list)
* @return created element
* @throws ElementTypeNotFoundException
*/
public Element create(final Object bean, final Class genArgType)
throws CoreException, ElementCreationException {
final Class<?> clazz = bean.getClass();
logger.debug("class {}", clazz.getCanonicalName());
final ElementType elType; // NOPMD PMD: AvoidFinalLocalVariableby wim on
// 9/20/10 2:59 PM
if (bean instanceof java.util.List) {
logger.debug("bean {} instance of list<{}>", clazz.getCanonicalName(),
genArgType.getCanonicalName() + ">");
elType = getElementType(genArgType.getCanonicalName());
} else {
elType = getElementType(clazz.getCanonicalName());
}
logger.debug("creating Element ({})", elType.toString());
if (beanElementMap.containsKey(bean)) {
return beanElementMap.get(bean); // NOPMD by wim on 9/20/10 2:54 PM
}
final Element element = new Element(elType);
beanElementMap.put(bean, element);
ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
public void doWith(final Method method) throws IllegalArgumentException,
IllegalAccessException {
try {
logger.debug("Method is {}", method.getName());
if (((method.getName().startsWith("get") &&
method.getName().length() > 3) ||
(method.getName().startsWith("is") &&
method.getName().length() > 2)) &&
!method.getName().equals("getClass")) {
final Class retType = method.getReturnType();
logger.debug("checking method return type {}",
retType.getCanonicalName());
if (isPrimitiveType(retType)) {
String propName = getPropertyName(method);
logger.debug("property name {} type {}", propName,
retType.getCanonicalName());
if( elType.isBeanId( propName )){
if( method.getReturnType().equals( Long.class)){
element.setId( (Long)method.invoke(bean));
}
else if( method.getReturnType().equals( String.class)){
String id = (String)method.invoke(bean);
if( id!= null ){
element.setId( new Long(id));
}
else {
element.setId( null );
}
}
}
else if( elType.isBeanVersionId(propName)){
Long v = 0L;
if (retType.equals(java.lang.Integer.class)
|| retType.getCanonicalName().equals("int")) {
v = new Long((Integer)method.invoke(bean));
// above line as suggested by FB: DM_NUMBER_CTOR v = new Long((Integer)
// (value));
} else if (retType.equals(java.lang.Long.class)
|| retType.getCanonicalName().equals("long")) {
v = (Long) method.invoke(bean);
}
element.setVersion(v); // property must be Int of Long!!
}
else {
setProperty(propName, method.invoke(bean), retType, element);
}
} else {
Class genArgType = null;
String type = retType.getCanonicalName();
if (method.getReturnType().equals(java.util.List.class)) {
genArgType = getGenericArgType(method.getGenericReturnType());
type = genArgType.getCanonicalName();
}
logger.debug("composite {}", type);
String propName = getPropertyName(method);
logger.debug("about to add reference {}", propName);
final Object ref = method.invoke(bean);
if (ref != null) {
try {
if (ref instanceof java.util.List) {
logger.debug("LIST SIZE {}",
((java.util.List) ref).size());
if (isPrimitiveType(genArgType)) {
logger.debug("LIST ELEMENTS TYPE {}",
genArgType.getCanonicalName());
setProperty(propName, (java.util.List) ref, genArgType,
element);
} else { // a list of composite types
logger.debug("adding list reference {}", propName);
final List<Element> elements = new ArrayList<Element>();
for (Object o : (java.util.List) ref) {
if (o != null) {
elements.add(create(o, genArgType));
}
}
logger.debug("end");
element.setListOfElements(propName, elements);
}
} else { // not a list
logger.debug("adding reference {}", propName);
element.addElement(propName, create(ref, genArgType));
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace(); // NOPMD by wim on 9/20/10 2:57 PM
} catch (ElementCreationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} catch (InvocationTargetException e) {
logger.error(e.getMessage());
} catch (CoreException e) {
logger.error(e.getMessage());
}
}
});
logger.debug("returning Element {}", element);
return element;
}
private void setPropertyValue(final Property prop, int i,
final Class retType, final Object value) throws CoreException {
if (retType.equals(java.lang.String.class)) {
prop.setString(i, (String) (value));
return; // NOPMD by wim on 9/20/10 3:04 PM
}
if (retType.equals(java.lang.Integer.class)) {
prop.setInt(i, (Integer) (value));
return; // NOPMD by wim on 9/20/10 3:04 PM
}
if (retType.getCanonicalName().equals("int")) {
prop.setInt(i, (Integer) (value));
return; // NOPMD by wim on 9/20/10 3:04 PM
}
if (retType.equals(java.lang.Long.class)) {
prop.setLong(i, (Long) (value));
return; // NOPMD by wim on 9/20/10 3:04 PM
}
if (retType.getCanonicalName().equals("long")) {
prop.setLong(i, (Long) (value));
return; // NOPMD by wim on 9/20/10 3:04 PM
}
if (retType.equals(java.lang.Double.class)) {
prop.setDouble(i, (Double) (value));
return; // NOPMD by wim on 9/20/10 3:06 PM
}
if (retType.getCanonicalName().equals("double")) {
prop.setDouble(i, (Double) (value));
return; // NOPMD by wim on 9/20/10 3:06 PM
}
if (retType.isEnum()) {
if (value != null) {
prop.setString(i, value.toString());
}
return; // NOPMD by wim on 9/20/10 3:06 PM
// else
// logger.error("invalid enum value for property " + propName);
}
if (retType.equals(java.lang.Boolean.class)) {
prop.setBool(i, (Boolean) (value));
return; // NOPMD by wim on 9/20/10 3:06 PM
}
if (retType.getCanonicalName().equals("boolean")) {
prop.setBool(i, (Boolean) (value));
return;
}
if (retType.isAssignableFrom(javax.xml.datatype.XMLGregorianCalendar.class)) {
if( value!=null){
XMLGregorianCalendar d = (XMLGregorianCalendar) value; // NOPMD by wim on
// 9/20/10 3:00 PM
prop.setDate(i, d.toGregorianCalendar());
}
return;
}
if (retType.isAssignableFrom(java.util.GregorianCalendar.class)) {
if( value!=null){
prop.setDate(i, (GregorianCalendar) value);
}
return;
}
if (retType.equals(java.math.BigDecimal.class)) {
if( value!=null){
logger.debug("save big decimal {}", (BigDecimal)value);
prop.setDecimal(i, (BigDecimal) value);
}
}
}
/**
* find method based on name only
*
* @param clazz
* @param name
* @return method or null if not found
*/
private Method findMethod(final Class clazz, final String name) {
for (Method m : ReflectionUtils.getAllDeclaredMethods(clazz)) {
if (m.getName().equals(name)) {
return m; // NOPMD by wim on 9/20/10 3:06 PM
}
}
return null;
}
private void loadSimpleProperty(Method method, final PropertyValueList vlist,
final Class paramType, final Object bean) throws CoreException {
try {
if (paramType.equals(java.lang.String.class)) {
// ToDo, check, PMD: AvoidInstantiatingObjectsInLoops
method.invoke(bean, new Object[] { vlist.getString(0) });
} else if (paramType.equals(java.lang.Integer.class)
|| paramType.getCanonicalName().equals("int")) {
// ToDo, check, PMD: AvoidInstantiatingObjectsInLoops
method.invoke(bean, new Object[] { vlist.getInt(0) });
} else if (paramType.equals(java.lang.Long.class)
|| paramType.getCanonicalName().equals("long")) {
// ToDo, check, PMD: AvoidInstantiatingObjectsInLoops
method.invoke(bean, new Object[] { vlist.getLong(0) });
} else if (paramType.equals(java.lang.Double.class)
|| paramType.getCanonicalName().equals("double")) {
// ToDo, check, PMD: AvoidInstantiatingObjectsInLoops
method.invoke(bean, new Object[] { vlist.getDouble(0) });
} else if (paramType.equals(java.lang.Boolean.class)
|| paramType.getCanonicalName().equals("boolean")) {
// ToDo, check, PMD: AvoidInstantiatingObjectsInLoops
method.invoke(bean, new Object[] { vlist.getBool(0) });
} else if (paramType.equals(java.math.BigDecimal.class)) {
logger.debug("load big decimal {}", vlist.getDecimal(0));
method.invoke(bean, new Object[] { vlist.getDecimal(0) });
} else if (paramType.isAssignableFrom(java.util.GregorianCalendar.class)) {
method.invoke(bean, new Object[] { vlist.getDate(0) });
} else if (paramType
.isAssignableFrom(javax.xml.datatype.XMLGregorianCalendar.class)) {
XMLGregorianCalendar gcal =
DatatypeFactory.newInstance().newXMLGregorianCalendar(vlist.getDate(0));
method.invoke(bean, new Object[] { gcal });
} else if (paramType.isEnum()) {
Object[] e = paramType.getEnumConstants(); // NOPMD by wim on 9/20/10 3:00 PM
final String eval = vlist.getString(0);
for (int i = 0; i < e.length; i++) {
if (eval != null && eval.equals(e[i].toString())) {
method.invoke(bean, new Object[] { e[i] });
}
}
}
} catch (IllegalAccessException ex) {
throw new CoreException("IllegalAccessException");
} catch (InvocationTargetException ex) {
throw new CoreException("InvovationTargetException");
} catch (DatatypeConfigurationException e) {
throw new CoreException("DataTypeInvocationException");
}
}
private void loadProperties(final Element element, final long revision,
final Object bean, final Class clazz) throws CoreException {
for (Property p : element.getProperties()) {
logger.debug("load properties for {}", p.getType().getName());
if (p.isInRevision(revision)) {
final PropertyValueList vlist = p.getValueList(revision);
if (vlist != null) {
if (vlist.isValid()) { // NOPMD by wim on 9/20/10 3:06 PM
String name = getMethodName("set", p.getType().getName());
Method method = findMethod(clazz, name);
if (method != null) { // ok this is a normal setter
final Class paramTypes[] = method.getParameterTypes();
loadSimpleProperty(method, vlist, paramTypes[0], bean);
} else { // no setter found, assume we have to get a list first
name = getMethodName("get", p.getType().getName());
method = findMethod(clazz, name);
if (method != null) {
try {
Object ref = method.invoke(bean);
if (ref instanceof java.util.List) {
List l = (java.util.List) ref;
logger.debug("load {} list properties for {}",
l.size(), p.getType().getName());
for (int i = 0; i < vlist.getValues().size(); i++) {
l.add((vlist.getValues().get(i)).getObject());
}
}
} catch (IllegalArgumentException e) {
throw new CoreException("IllegalArgumentException");
} catch (IllegalAccessException e) {
throw new CoreException("IllegalAccesstException");
} catch (InvocationTargetException e) {
throw new CoreException("InvocationTargetException");
}
}
}
}
}
else {
logger.debug(" no values");
}
}
else{
logger.debug(" not in revision");
}
}
}
private void loadElementRefs(final Element element, final long revision,
final Object bean, final Class clazz) {
for (ElementRefs c : element.getListOfElementRefs()) {
if (c.isInRevision(revision)) {
try {
String name = getMethodName("set", c.getRefName());
Method method = findMethod(clazz, name);
if (method != null) { // NOPMD by wim on 9/20/10 2:56 PM
final Class paramTypes[] = method.getParameterTypes();
String typeName = paramTypes[0].getCanonicalName();
List l = new ArrayList(); // NOPMD by wim on 9/20/10 3:00 PM
boolean isList = false;
if ("java.util.List".equals(typeName)) {
isList = true;
logger.debug(" LIST {}", typeName);
}
final ElementRefList reflist = c.getElementRefList(revision);
if (reflist != null) {
for (Element e : reflist.getElementList()) {
l.add(loadElement(e, revision));
}
}
if (isList) {
method.invoke(bean, new Object[] { l });
} else if (!l.isEmpty()) {
method.invoke(bean, new Object[] { l.get(0) });
}
} else {
name = getMethodName("get", c.getRefName());
method = findMethod(clazz, name);
if (method == null) {
name = getMethodName("is", c.getRefName());
method = findMethod(clazz, name);
}
if (method != null) {
final Class retType = method.getReturnType();
String typeName = retType.getCanonicalName();
List l = (List) method.invoke(bean); // NOPMD by wim on 9/20/10
// 3:04 PM
final ElementRefList reflist = c.getElementRefList(revision);
if (reflist != null) {
for (Element e : reflist.getElementList()) {
l.add(loadElement(e, revision));
}
}
}
}
}
// ToDo, needs investigation, FB: REC_CATCH_EXCEPTION
catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
/**
* load an object
* @param element
* @return loaded object
* @throws CoreException
*/
public Object load(final Element element)
throws CoreException {
return load(element, null);
}
/**
* load an object by revision
* @param element
* @param revision
* @return loaded object
* @throws CoreException
*/
public Object load(final Element element, final Long revision) throws CoreException {
if (revision == null) {
return loadElement(element, Modification.MaxRevision); // NOPMD by wim on 9/20/10 3:51 PM
}
return loadElement(element, revision);
}
/**
* get revision of element by timestamp
* @param element
* @param timestamp
* @return revision
* @throws CoreException
*/
private long getRevision( final Element element, final Date timestamp ) throws CoreException{
if (timestamp == null) {
return Modification.MaxRevision;
}
final Modification hist = element.getModification(timestamp);
if (hist == null) {
throw new CoreException("No modification with Timestamp "
+ timestamp.toString() + " found");
}
return hist.getRevision();
}
/**
* get bean of element by rev. check if already loaded
* @param element
* @param rev
* @return bean
* @throws CoreException
*/
private Object loadElement(final Element element, final Long rev) throws CoreException {
if (logger.isDebugEnabled()) {
logger.debug("creating Element (" + element.getElementType().getName()
+ ")" //+ " id " + getId(bean)
+ " element id " + element.getId() + " assigned "
+ loadedBean.containsKey(element.getId()));
}
if (loadedBean.containsKey(element.getId())) {
// we have this bean already loaded
return loadedBean.get(element.getId());
}
// this is the first load
if( cache != null ){
Object o = cache.getCachedObject(element.getId(), rev);
if( o!= null ){
loadedBean.put(element.getId(), o);
return o;
}
}
Class clazz=null;
Object bean=null;
try{
clazz = Class.forName(element.getElementType().getName());
Constructor constr = clazz.getConstructor();
bean = constr.newInstance();
}
catch( Exception e ){
throw new CoreException( e.getMessage() );
}
loadedBean.put(element.getId(), bean);
// load valid Properties
loadProperties(element, rev, bean, clazz);
// Load valid Elements of ElementRefs
loadElementRefs(element, rev, bean, clazz);
// attempt to set version and id
setIdAndVersion( bean, element, null );
if( cache != null ){
cache.putCachedObject(element, rev, bean);
}
return bean;
}
/**
* set id of bean if possible
* @param bean
* @param id
* @param idName
*/
void setId(Object bean, final Long id, final String idName) { // NOPMD by wim on 9/20/10 3:04 PM
final String name = getMethodName("set", idName);
final Method setId = findMethod(bean.getClass(), name);
//final Method setId = getSetProperty(bean.getClass(), idName);
if (setId != null) {
if( setId.getParameterTypes().length!=1 ){
logger.warn( "set id error for class {}", bean.getClass().getCanonicalName() );
return;
}
try {
if( setId.getParameterTypes()[0].equals(Long.class) ){
setId.invoke(bean, new Object[] { id });
}
else if( setId.getParameterTypes()[0].equals(String.class) ){
setId.invoke(bean, new Object[] { id.toString() });
}
else {
logger.warn( "unsupported type for setting id {}", setId.getParameterTypes()[0]);
}
// logger.info("set id {}", id );
} catch (IllegalAccessException iac) {
logger.warn( "set id error {}", iac.getMessage() );
// iac.printStackTrace(); // NOPMD by wim on 9/20/10 2:57 PM
} catch (IllegalArgumentException iarg) {
logger.warn( "set id error {}", iarg.getMessage() );
// iarg.printStackTrace();
} catch (InvocationTargetException ite) {
logger.warn( "set id error {}", ite.getMessage() );
// ite.printStackTrace();
}
}
else {
logger.warn( "property {} for type {} not found", idName, bean.getClass().getCanonicalName() );
}
}
private void setVersion(final Object bean, final Integer ver, final String idVersion) {
final String name = getMethodName("set", idVersion);
final Method setVersion = findMethod(bean.getClass(), name);
if (setVersion != null) {
try {
setVersion.invoke(bean, new Object[] { ver });
logger.debug("set version {}", ver );
} catch (IllegalAccessException iac) {
logger.warn( "set version error {}", iac.getMessage() );
// iac.printStackTrace();
} catch (IllegalArgumentException iarg) {
logger.warn( "set version error {}", iarg.getMessage() );
// iarg.printStackTrace();
} catch (InvocationTargetException ite) {
logger.warn( "set version error {}", ite.getMessage() );
// ite.printStackTrace();
}
}
}
private Long getId(final Object bean, final String idName) {
final Method getId = getGetProperty(bean.getClass(), idName);
if (getId != null) {
try {
return (Long) getId.invoke(bean, new Object[] {}); // NOPMD by wim on
// 9/20/10 3:51 PM
} catch (Exception e) {
logger.warn("cannot set id {}", e.getMessage() );
}
}
return null;
}
}