super();
this.localDAO = localDAO;
this.field = field;
SimplifiedRelation simplifiedRelation = field.getAnnotation(SimplifiedRelation.class);
remoteKeyColumnName = simplifiedRelation.remoteKeyColumnName();
remoteValueColumnName = simplifiedRelation.remoteValueColumnName();
order = simplifiedRelation.order();
if(simplifiedRelation.addTablePrefix()){
if(simplifiedRelation.deplurifyTablePrefix() && localDAO.getTableName().endsWith("s")){
remoteTableName = localDAO.getTableName().substring(0, localDAO.getTableName().length()-1) + simplifiedRelation.table();
}else{
remoteTableName = localDAO.getTableName() + simplifiedRelation.table();
}
}else{
remoteTableName = simplifiedRelation.table();
}
if (!StringUtils.isEmpty(simplifiedRelation.keyField())) {
try {
keyField = ReflectionUtils.getField(beanClass, simplifiedRelation.keyField());
if (keyField == null) {
throw new RuntimeException("Unable to find field " + simplifiedRelation.keyField() + " in " + beanClass.getClass());
}
} catch (SecurityException e) {
throw new RuntimeException(e);
}
} else {
List<Field> fields = ReflectionUtils.getFields(beanClass);
for (Field localBeanField : fields) {
if (localBeanField.isAnnotationPresent(DAOManaged.class) && localBeanField.isAnnotationPresent(Key.class)) {
if (this.keyField == null) {
keyField = localBeanField;
} else {
throw new RuntimeException("Multiple fields marked with @Key annotation found in class " + beanClass + " therefore keyField has to set on the @SimplifiedRelation annotation of field " + field.getName());
}
}
}
}
if (queryParameterPopulators != null) {
for (QueryParameterPopulator<?> queryParameterPopulator : queryParameterPopulators) {
if (queryParameterPopulator.getType().equals(remoteClass)) {
this.queryParameterPopulator = (QueryParameterPopulator<RemoteType>) queryParameterPopulator;
}
}
}
if (this.queryParameterPopulator == null) {
preparedStatementMethod = PreparedStatementQueryMethods.getQueryMethod(remoteClass);
if (preparedStatementMethod == null) {
throw new RuntimeException("Unable to to find a query parameter populator or prepared statement method matching " + remoteClass + " of @SimplfiedRelation and @OneToMany annotated field " + field.getName() + " in " + beanClass);
}
}
if (typePopulators != null) {
for (BeanStringPopulator<?> typePopulator : typePopulators) {
if (typePopulator.getType().equals(remoteClass)) {
beanResultSetPopulator = new TypeBasedResultSetPopulator<RemoteType>((BeanStringPopulator<RemoteType>) typePopulator, remoteValueColumnName);
}
}
}
if (beanResultSetPopulator == null) {
Method resultSetMethod = ResultSetMethods.getColumnNameMethod(remoteClass);
if (resultSetMethod != null) {
beanResultSetPopulator = new MethodBasedResultSetPopulator<RemoteType>(resultSetMethod, remoteValueColumnName);
} else {
throw new RuntimeException("Unable to to find a type populator or resultset method matching " + remoteClass + " of @SimplfiedRelation and @OneToMany annotated field " + field.getName() + " in " + beanClass);
}
}
if(simplifiedRelation.preserveListOrder()){
if(StringUtils.isEmpty(simplifiedRelation.indexColumn())){
throw new RuntimeException("Preserve list order enabled but no index column specified for @SimplifiedRelation annotated field " + field.getName() + " in " + beanClass);
}
preserveListOrder = true;
indexColumnName = simplifiedRelation.indexColumn();
}
}