org.apache.metamodel.pojo.ObjectTableDataProvider Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.metamodel.pojo;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import org.apache.metamodel.schema.ColumnType;
import org.apache.metamodel.schema.ColumnTypeImpl;
import org.apache.metamodel.util.SimpleTableDef;
/**
* {@link TableDataProvider} for regular Java objects with getter and setter
* methods. Each of these method pairs will be treated as fields in a table.
*
* @param
*/
public final class ObjectTableDataProvider implements TableDataProvider {
private static final long serialVersionUID = 1L;
private final String _tableName;
private final Collection _collection;
private final Class _class;
private final SimpleTableDef _tableDef;
private final Map> _fieldTypes;
public ObjectTableDataProvider(Class cls) {
this(cls.getSimpleName(), cls);
}
public ObjectTableDataProvider(String tableName, Class cls) {
this(tableName, cls, new ArrayList());
}
public ObjectTableDataProvider(String tableName, Class cls, Collection collection) {
_tableName = tableName;
_collection = collection;
_class = cls;
_fieldTypes = new HashMap>();
_tableDef = createTableDef();
}
@Override
public String getName() {
return _tableName;
}
@Override
public Iterator iterator() {
return _collection.iterator();
}
@Override
public SimpleTableDef getTableDef() {
return _tableDef;
}
private SimpleTableDef createTableDef() {
final Map columns = new TreeMap();
final Method[] methods = _class.getMethods();
for (final Method method : methods) {
final String methodName = method.getName();
if (methodName.startsWith("get") && !"getClass".equals(methodName)) {
if (method.getParameterTypes().length == 0) {
final Class> returnType = method.getReturnType();
if (returnType != null) {
final String columnName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
_fieldTypes.put(columnName, returnType);
final ColumnType columnType = ColumnTypeImpl.convertColumnType(returnType);
columns.put(columnName, columnType);
}
}
}
}
final int size = columns.size();
final String[] columnNames= new String[size];
final ColumnType[] columnTypes = new ColumnType[size];
final Set> entrySet = columns.entrySet();
int i= 0;
for (Entry entry : entrySet) {
columnNames[i] = entry.getKey();
columnTypes[i] = entry.getValue();
i++;
}
return new SimpleTableDef(_tableName, columnNames,
columnTypes);
}
@Override
public Object getValue(final String column, E record) {
final Method getterMethod = getMethod(column, "get");
try {
final Object result = getterMethod.invoke(record);
return result;
} catch (Exception e) {
throw new IllegalStateException("Failed to invoke getter method: " + getterMethod, e);
}
}
@Override
public void insert(Map recordData) {
final E object;
try {
object = _class.newInstance();
} catch (Exception e) {
throw new IllegalStateException("Failed to instantiate a new instance of " + _class, e);
}
final Set> entrySet = recordData.entrySet();
for (Entry entry : entrySet) {
final String column = entry.getKey();
final Class> fieldType = _fieldTypes.get(column);
final Method setterMethod = getMethod(column, "set", fieldType);
final Object value = entry.getValue();
try {
setterMethod.invoke(object, value);
} catch (Exception e) {
throw new IllegalStateException("Failed to invoke setter method: " + setterMethod, e);
}
}
_collection.add(object);
}
private Method getMethod(final String column, final String prefix, final Class>... parameterTypes) {
Method getterMethod;
try {
getterMethod = _class.getMethod(prefix + Character.toUpperCase(column.charAt(0)) + column.substring(1),
parameterTypes);
} catch (Exception e) {
try {
getterMethod = _class.getMethod(prefix + column, parameterTypes);
} catch (Exception e2) {
throw new IllegalArgumentException("Could not find '" + prefix + "' method for column: " + column);
}
}
return getterMethod;
}
}