use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaIntegrationTestMetadata method getExpectedExceptionField.
/**
* Builds and returns a <code>public</code> ExpectedException field.
*
* @return {@link FieldMetadataBuilder}
*/
private FieldMetadataBuilder getExpectedExceptionField() {
FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(this.getId(), Modifier.PUBLIC, EXPECTED_EXCEPTION_FIELD_NAME, EXPECTED_EXCEPTION, String.format("%s.none()", getNameOfJavaType(EXPECTED_EXCEPTION)));
// Add @Rule
fieldBuilder.addAnnotation(new AnnotationMetadataBuilder(RULE));
return fieldBuilder;
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class DbreMetadata method addOneToManyFields.
private void addOneToManyFields(final Table table) {
Validate.notNull(table, "Table required");
if (table.isJoinTable()) {
return;
}
for (final ForeignKey exportedKey : table.getExportedKeys()) {
final Table exportedKeyForeignTable = exportedKey.getForeignTable();
Validate.notNull(exportedKeyForeignTable, "Foreign key table for foreign key '%s' in table '%s' does not exist. One-to-many relationship not created", exportedKey.getName(), table.getFullyQualifiedTableName());
if (exportedKeyForeignTable.isJoinTable()) {
continue;
}
final String foreignTableName = exportedKeyForeignTable.getName();
final String foreignSchemaName = exportedKeyForeignTable.getSchema().getName();
final Table foreignTable = database.getTable(foreignTableName, foreignSchemaName);
Validate.notNull(foreignTable, "Related table '%s' could not be found but was referenced by table '%s'", exportedKeyForeignTable.getFullyQualifiedTableName(), table.getFullyQualifiedTableName());
if (isOneToOne(foreignTable, foreignTable.getImportedKey(exportedKey.getName()))) {
continue;
}
final Short keySequence = exportedKey.getKeySequence();
final String fieldSuffix = keySequence != null && keySequence > 0 ? String.valueOf(keySequence) : "";
JavaSymbolName fieldName = new JavaSymbolName(getInflectorPlural(DbreTypeUtils.suggestFieldName(foreignTableName)) + fieldSuffix);
JavaSymbolName mappedByFieldName = null;
if (exportedKey.getReferenceCount() == 1) {
final Reference reference = exportedKey.getReferences().iterator().next();
mappedByFieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(reference.getForeignColumnName()));
} else {
mappedByFieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(table) + fieldSuffix);
}
// Check for existence of same field - ROO-1691
while (true) {
if (!hasFieldInItd(fieldName)) {
break;
}
fieldName = new JavaSymbolName(fieldName.getSymbolName() + "_");
}
final FieldMetadataBuilder fieldBuilder = getOneToManyMappedByField(fieldName, mappedByFieldName, foreignTableName, foreignSchemaName, exportedKey.getOnUpdate(), exportedKey.getOnDelete());
addToBuilder(fieldBuilder);
// Exclude these fields in @RooToString to avoid circular references
// - ROO-1399
excludeFieldsInToStringAnnotation(fieldBuilder.getFieldName().getSymbolName());
}
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class DbreMetadata method getManyToManyOwningSideField.
private FieldMetadataBuilder getManyToManyOwningSideField(final JavaSymbolName fieldName, final Table joinTable, final Table inverseSideTable, final CascadeAction onUpdate, final CascadeAction onDelete) {
final JavaType element = DbreTypeUtils.findTypeForTable(managedEntities, inverseSideTable);
Validate.notNull(element, "Attempted to create many-to-many owning-side field '%s' in '%s' %s", fieldName, destination.getFullyQualifiedTypeName(), getErrorMsg(inverseSideTable.getFullyQualifiedTableName()));
final List<JavaType> params = Arrays.asList(element);
final JavaType fieldType = new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, params);
// Add annotations to field
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Add @ManyToMany annotation
final AnnotationMetadataBuilder manyToManyBuilder = new AnnotationMetadataBuilder(MANY_TO_MANY);
annotations.add(manyToManyBuilder);
// Add @JoinTable annotation
final AnnotationMetadataBuilder joinTableBuilder = new AnnotationMetadataBuilder(JOIN_TABLE);
final List<AnnotationAttributeValue<?>> joinTableAnnotationAttributes = new ArrayList<AnnotationAttributeValue<?>>();
joinTableAnnotationAttributes.add(new StringAttributeValue(new JavaSymbolName(NAME), joinTable.getName()));
final Iterator<ForeignKey> iter = joinTable.getImportedKeys().iterator();
// Add "joinColumns" attribute containing nested @JoinColumn annotations
final List<NestedAnnotationAttributeValue> joinColumnArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
final Set<Reference> firstKeyReferences = iter.next().getReferences();
for (final Reference reference : firstKeyReferences) {
final AnnotationMetadataBuilder joinColumnBuilder = getJoinColumnAnnotation(reference, firstKeyReferences.size() > 1);
joinColumnArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), joinColumnBuilder.build()));
}
joinTableAnnotationAttributes.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName("joinColumns"), joinColumnArrayValues));
// Add "inverseJoinColumns" attribute containing nested @JoinColumn
// annotations
final List<NestedAnnotationAttributeValue> inverseJoinColumnArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
final Set<Reference> lastKeyReferences = iter.next().getReferences();
for (final Reference reference : lastKeyReferences) {
final AnnotationMetadataBuilder joinColumnBuilder = getJoinColumnAnnotation(reference, lastKeyReferences.size() > 1);
inverseJoinColumnArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), joinColumnBuilder.build()));
}
joinTableAnnotationAttributes.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName("inverseJoinColumns"), inverseJoinColumnArrayValues));
// Add attributes to a @JoinTable annotation builder
joinTableBuilder.setAttributes(joinTableAnnotationAttributes);
annotations.add(joinTableBuilder);
return new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, fieldName, fieldType);
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class DbreMetadata method getOneToOneMappedByField.
private FieldMetadataBuilder getOneToOneMappedByField(final JavaSymbolName fieldName, final JavaType fieldType, final JavaSymbolName mappedByFieldName, final CascadeAction onUpdate, final CascadeAction onDelete) {
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
final AnnotationMetadataBuilder oneToOneBuilder = new AnnotationMetadataBuilder(ONE_TO_ONE);
oneToOneBuilder.addStringAttribute(MAPPED_BY, mappedByFieldName.getSymbolName());
addCascadeType(oneToOneBuilder, onUpdate, onDelete);
annotations.add(oneToOneBuilder);
return new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, fieldName, fieldType);
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class DbreMetadata method getOneToManyMappedByField.
private FieldMetadataBuilder getOneToManyMappedByField(final JavaSymbolName fieldName, final JavaSymbolName mappedByFieldName, final String foreignTableName, final String foreignSchemaName, final CascadeAction onUpdate, final CascadeAction onDelete) {
final JavaType element = DbreTypeUtils.findTypeForTableName(managedEntities, foreignTableName, foreignSchemaName);
Validate.notNull(element, "Attempted to create one-to-many mapped-by field '%s' in '%s' %s", fieldName, destination.getFullyQualifiedTypeName(), getErrorMsg(foreignTableName + "." + foreignSchemaName));
final JavaType fieldType = new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(element));
// Add @OneToMany annotation
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
final AnnotationMetadataBuilder oneToManyBuilder = new AnnotationMetadataBuilder(ONE_TO_MANY);
oneToManyBuilder.addStringAttribute(MAPPED_BY, mappedByFieldName.getSymbolName());
addCascadeType(oneToManyBuilder, onUpdate, onDelete);
annotations.add(oneToManyBuilder);
return new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, fieldName, fieldType);
}
Aggregations