proxyClass = (Class<T>)Proxy.getProxyClass(
cls.getClassLoader(), new Class[]{cls});
ctor = getConstructorForInvocationHandler (proxyClass);
persistenceCapable = cls.getAnnotation(PersistenceCapable.class);
if (persistenceCapable == null) {
throw new ClusterJUserException(local.message(
"ERR_No_Persistence_Capable_Annotation", name));
}
this.tableName = persistenceCapable.table();
}
this.table = getTable(dictionary);
if (table == null) {
throw new ClusterJUserException(local.message("ERR_Get_NdbTable", name, tableName));
}
if (logger.isDebugEnabled()) logger.debug("Found Table for " + tableName);
// the id field handlers will be initialized via registerPrimaryKeyColumn
this.primaryKeyColumnNames = table.getPrimaryKeyColumnNames();
this.numberOfIdFields = primaryKeyColumnNames.length;
this.idFieldHandlers = new DomainFieldHandlerImpl[numberOfIdFields];
this.idFieldNumbers = new int[numberOfIdFields];
// the partition key field handlers will be initialized via registerPrimaryKeyColumn
this.partitionKeyColumnNames = table.getPartitionKeyColumnNames();
this.numberOfPartitionKeyColumns = partitionKeyColumnNames.length;
this.partitionKeyFieldHandlers = new DomainFieldHandlerImpl[numberOfPartitionKeyColumns];
// Process indexes for the table. There might not be a field associated with the index.
// The first entry in indexHandlerImpls is for the mandatory hash primary key,
// which is not really an index but is treated as an index by query.
Index primaryIndex = dictionary.getIndex("PRIMARY$KEY", tableName, "PRIMARY");
IndexHandlerImpl primaryIndexHandler =
new IndexHandlerImpl(this, dictionary, primaryIndex, primaryKeyColumnNames);
indexHandlerImpls.add(primaryIndexHandler);
String[] indexNames = table.getIndexNames();
for (String indexName: indexNames) {
// the index alias is the name as known by the user (without the $unique suffix)
String indexAlias = removeUniqueSuffix(indexName);
Index index = dictionary.getIndex(indexName, tableName, indexAlias);
String[] columnNames = index.getColumnNames();
IndexHandlerImpl imd = new IndexHandlerImpl(this, dictionary, index, columnNames);
indexHandlerImpls.add(imd);
}
if (dynamic) {
// for each column in the database, create a field
List<String> fieldNameList = new ArrayList<String>();
for (String columnName: table.getColumnNames()) {
Column storeColumn = table.getColumn(columnName);
DomainFieldHandlerImpl domainFieldHandler = null;
domainFieldHandler =
new DomainFieldHandlerImpl(this, table, numberOfFields++, storeColumn);
String fieldName = domainFieldHandler.getName();
fieldNameList.add(fieldName);
fieldNameToNumber.put(domainFieldHandler.getName(), domainFieldHandler.getFieldNumber());
persistentFieldHandlers.add(domainFieldHandler);
if (!storeColumn.isPrimaryKey()) {
nonPKFieldHandlers.add(domainFieldHandler);
}
}
fieldNames = fieldNameList.toArray(new String[fieldNameList.size()]);
} else {
// Iterate the fields (names and types based on get/set methods) in the class
List<String> fieldNameList = new ArrayList<String>();
Method[] methods = cls.getMethods();
for (Method method: methods) {
// remember get methods
String methodName = method.getName();
String name = convertMethodName(methodName);
Class type = getType(method);
DomainFieldHandlerImpl domainFieldHandler = null;
if (methodName.startsWith("get")) {
Method unmatched = unmatchedSetMethods.get(name);
if (unmatched == null) {
// get is first of the pair; put it into the unmatched map
unmatchedGetMethods.put(name, method);
} else {
// found the potential match
if (getType(unmatched).equals(type)) {
// method names and types match
unmatchedSetMethods.remove(name);
domainFieldHandler = new DomainFieldHandlerImpl(this, table,
numberOfFields++, name, type, method, unmatched);
} else {
// both unmatched because of type mismatch
unmatchedGetMethods.put(name, method);
}
}
} else if (methodName.startsWith("set")) {
Method unmatched = unmatchedGetMethods.get(name);
if (unmatched == null) {
// set is first of the pair; put it into the unmatched map
unmatchedSetMethods.put(name, method);
} else {
// found the potential match
if (getType(unmatched).equals(type)) {
// method names and types match
unmatchedGetMethods.remove(name);
domainFieldHandler = new DomainFieldHandlerImpl(this, table,
numberOfFields++, name, type, unmatched, method);
} else {
// both unmatched because of type mismatch
unmatchedSetMethods.put(name, method);
}
}
}
if (domainFieldHandler != null) {
// found matching methods
// set up field name to number map
String fieldName = domainFieldHandler.getName();
fieldNameList.add(fieldName);
fieldNameToNumber.put(domainFieldHandler.getName(), domainFieldHandler.getFieldNumber());
// put field into either persistent or not persistent list
if (domainFieldHandler.isPersistent()) {
persistentFieldHandlers.add(domainFieldHandler);
if (!domainFieldHandler.isPrimaryKey()) {
nonPKFieldHandlers.add(domainFieldHandler);
}
}
if (domainFieldHandler.isPrimitive()) {
primitiveFieldHandlers.add(domainFieldHandler);
}
}
}
fieldNames = fieldNameList.toArray(new String[fieldNameList.size()]);
// done with methods; if anything in unmatched we have a problem
if ((!unmatchedGetMethods.isEmpty()) || (!unmatchedSetMethods.isEmpty())) {
throw new ClusterJUserException(
local.message("ERR_Unmatched_Methods",
unmatchedGetMethods, unmatchedSetMethods));
}
}