use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class JpaFieldCreatorProvider method createParentFieldOfToManyRelation.
/**
* Create parent field of a *ToMany relation
*
* @param parentCid
* @param childCid
* @param fieldName
* @param fieldType
* @param cardinality
* @param permitReservedWords
* @param sizeMin
* @param sizeMax
* @param comment
* @param notNull
* @param mappedBy
* @param fetch
* @param aggregation
* @param orphanRemoval
* @param cascadeType
* @param isList
*/
private void createParentFieldOfToManyRelation(ClassOrInterfaceTypeDetails parentCid, ClassOrInterfaceTypeDetails childCid, JavaSymbolName fieldName, JavaType fieldType, Cardinality cardinality, boolean permitReservedWords, Integer sizeMin, Integer sizeMax, String comment, boolean notNull, JavaSymbolName mappedBy, Fetch fetch, Boolean aggregation, Boolean orphanRemoval, Cascade[] cascadeType, boolean isList) {
if (cascadeType == null) {
// prepare cascadType
if (aggregation) {
cascadeType = Cascade.MERGE_PERSIST;
} else {
// Composition
cascadeType = Cascade.ALL_ARRAY;
}
}
// Check if child field exist
checkFieldExists(mappedBy, false, childCid, "mappedBy");
SetField parentFieldDetails;
if (isList) {
parentFieldDetails = new ListField(parentCid.getDeclaredByMetadataId(), new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, cardinality, cascadeType, false);
} else {
parentFieldDetails = new SetField(parentCid.getDeclaredByMetadataId(), new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, cardinality, cascadeType, false);
}
parentFieldDetails.setNotNull(notNull);
if (comment != null) {
parentFieldDetails.setComment(comment);
}
if (sizeMin != null) {
parentFieldDetails.setSizeMin(sizeMin);
}
if (sizeMax != null) {
parentFieldDetails.setSizeMax(sizeMax);
}
parentFieldDetails.setMappedBy(mappedBy);
parentFieldDetails.setFetch(fetch);
if (orphanRemoval == null) {
if (aggregation) {
orphanRemoval = false;
} else {
orphanRemoval = true;
}
}
AnnotationMetadataBuilder rooJpaRelationAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_RELATION);
if (aggregation) {
rooJpaRelationAnnotation.addEnumAttribute("type", RooEnumDetails.RELATION_TYPE_AGGREGATION);
} else {
rooJpaRelationAnnotation.addEnumAttribute("type", RooEnumDetails.RELATION_TYPE_COMPOSITION);
}
parentFieldDetails.addAdditionaAnnotation(rooJpaRelationAnnotation);
// insert parent field
insertField(parentFieldDetails, permitReservedWords, false, true);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class IdentifierMetadata method setDateAnnotations.
private void setDateAnnotations(final String columnDefinition, final List<AnnotationMetadataBuilder> annotations) {
// Add JSR 220 @Temporal annotation to date fields
String temporalType = StringUtils.defaultIfEmpty(StringUtils.upperCase(columnDefinition), "DATE");
if ("DATETIME".equals(temporalType)) {
// ROO-2606
temporalType = "TIMESTAMP";
}
final AnnotationMetadataBuilder temporalBuilder = new AnnotationMetadataBuilder(TEMPORAL);
temporalBuilder.addEnumAttribute("value", new EnumDetails(TEMPORAL_TYPE, new JavaSymbolName(temporalType)));
annotations.add(temporalBuilder);
final AnnotationMetadataBuilder dateTimeFormatBuilder = new AnnotationMetadataBuilder(DATE_TIME_FORMAT);
dateTimeFormatBuilder.addStringAttribute("style", "M-");
annotations.add(dateTimeFormatBuilder);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class IdentifierMetadata method getColumnBuilder.
private AnnotationMetadataBuilder getColumnBuilder(final Identifier identifier) {
final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
columnBuilder.addStringAttribute("name", identifier.getColumnName());
if (StringUtils.isNotBlank(identifier.getColumnDefinition())) {
columnBuilder.addStringAttribute("columnDefinition", identifier.getColumnDefinition());
}
columnBuilder.addBooleanAttribute("nullable", false);
// Add length attribute for Strings
if (identifier.getColumnSize() < 4000 && identifier.getFieldType().equals(JavaType.STRING)) {
columnBuilder.addIntegerAttribute("length", identifier.getColumnSize());
}
// Add precision and scale attributes for numeric fields
if (identifier.getScale() > 0 && (identifier.getFieldType().equals(JavaType.DOUBLE_OBJECT) || identifier.getFieldType().equals(JavaType.DOUBLE_PRIMITIVE) || identifier.getFieldType().equals(BIG_DECIMAL))) {
columnBuilder.addIntegerAttribute("precision", identifier.getColumnSize());
columnBuilder.addIntegerAttribute("scale", identifier.getScale());
}
return columnBuilder;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class IdentifierMetadata method getFieldBuilders.
/**
* Locates declared fields.
* <p>
* If no parent is defined, one will be located or created. All declared
* fields will be returned.
*
* @return fields (never returns null)
*/
private List<FieldMetadataBuilder> getFieldBuilders() {
// Locate all declared fields
final List<? extends FieldMetadata> declaredFields = governorTypeDetails.getDeclaredFields();
// Add fields to ITD from annotation
final List<FieldMetadata> fields = new ArrayList<FieldMetadata>();
if (identifierServiceResult != null) {
for (final Identifier identifier : identifierServiceResult) {
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
annotations.add(getColumnBuilder(identifier));
if (identifier.getFieldType().equals(DATE)) {
setDateAnnotations(identifier.getColumnDefinition(), annotations);
}
final FieldMetadata idField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, identifier.getFieldName(), identifier.getFieldType()).build();
// Only add field to ITD if not declared on governor
if (!hasField(declaredFields, idField)) {
fields.add(idField);
}
}
}
fields.addAll(declaredFields);
// Remove fields with static and transient modifiers
for (final Iterator<FieldMetadata> iter = fields.iterator(); iter.hasNext(); ) {
final FieldMetadata field = iter.next();
if (Modifier.isStatic(field.getModifier()) || Modifier.isTransient(field.getModifier())) {
iter.remove();
}
}
// Remove fields with the @Transient annotation
final List<FieldMetadata> transientAnnotatedFields = governorTypeDetails.getFieldsWithAnnotation(TRANSIENT);
if (fields.containsAll(transientAnnotatedFields)) {
fields.removeAll(transientAnnotatedFields);
}
final List<FieldMetadataBuilder> fieldBuilders = new ArrayList<FieldMetadataBuilder>();
if (!fields.isEmpty()) {
for (final FieldMetadata field : fields) {
fieldBuilders.add(new FieldMetadataBuilder(field));
}
return fieldBuilders;
}
// We need to create a default identifier field
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Compute the column name, as required
final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
columnBuilder.addStringAttribute("name", "id");
columnBuilder.addBooleanAttribute("nullable", false);
annotations.add(columnBuilder);
fieldBuilders.add(new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, new JavaSymbolName("id"), LONG_OBJECT));
return fieldBuilders;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class JpaTestCreator method createUnitTest.
@Override
public void createUnitTest(final JavaType entity) {
Validate.notNull(entity, "Class to produce an unit test class for is required");
// Check if provided JavaType is a Repository
ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(entity);
Validate.notNull(cid.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Type must be a Roo JPA Entity type.");
// Create JPA DataOnDemand artifacts
List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(entity);
for (DataOnDemandCreatorProvider dodCreator : dodCreators) {
dodCreator.createDataOnDemand(entity);
}
final JavaType name = new JavaType(entity + "Test", entity.getModule());
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(entity.getModule()));
if (metadataService.get(declaredByMetadataId) != null) {
// The file already exists
return;
}
// Add @RooUnitTest to source file
AnnotationMetadataBuilder rooUnitTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_UNIT_TEST);
rooUnitTestAnnotation.addClassAttribute("targetClass", entity);
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
cidBuilder.addAnnotation(rooUnitTestAnnotation);
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Aggregations