use of org.springframework.roo.classpath.operations.jsr303.SetField 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.operations.jsr303.SetField in project spring-roo by spring-projects.
the class JpaFieldCreatorProvider method createChildFieldOfManyToManyRelation.
/**
* Create child field of a ManyToMany relation
*
* @param childCid
* @param parentType
* @param permitReservedWords
* @param mappedBy
* @param fetch
* @param joinTable
* @param joinColumns
* @param referencedColumns
* @param inverseJoinColumns
* @param inverseReferencedColumns
* @param isList
*/
private void createChildFieldOfManyToManyRelation(ClassOrInterfaceTypeDetails childCid, JavaType parentType, boolean permitReservedWords, JavaSymbolName mappedBy, Fetch fetch, String joinTable, String joinColumns, String referencedColumns, String inverseJoinColumns, String inverseReferencedColumns, boolean isList) {
SetField childFieldDetails;
if (isList) {
childFieldDetails = new ListField(childCid.getDeclaredByMetadataId(), new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(parentType)), mappedBy, parentType, Cardinality.MANY_TO_MANY, null, false);
} else {
childFieldDetails = new SetField(childCid.getDeclaredByMetadataId(), new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(parentType)), mappedBy, parentType, Cardinality.MANY_TO_MANY, null, false);
}
childFieldDetails.setFetch(fetch);
if (StringUtils.isNotBlank(joinTable) || StringUtils.isNotBlank(inverseJoinColumns) || StringUtils.isNotBlank(joinColumns)) {
if (StringUtils.isNotBlank(inverseJoinColumns)) {
}
// Create strings arrays and set @JoinTable annotation
String[] joinColumnsArray = null;
String[] referencedColumnsArray = null;
String[] inverseJoinColumnsArray = null;
String[] inverseReferencedColumnsArray = null;
if (joinColumns != null) {
joinColumnsArray = joinColumns.replace(" ", "").split(",");
}
if (referencedColumns != null) {
referencedColumnsArray = referencedColumns.replace(" ", "").split(",");
}
if (inverseJoinColumns != null) {
inverseJoinColumnsArray = inverseJoinColumns.replace(" ", "").split(",");
}
if (inverseReferencedColumns != null) {
inverseReferencedColumnsArray = inverseReferencedColumns.replace(" ", "").split(",");
}
// Validate same number of elements
if (joinColumnsArray != null && referencedColumnsArray != null) {
Validate.isTrue(joinColumnsArray.length == referencedColumnsArray.length, "--joinColumns and --referencedColumns must have same number of column values");
}
if (inverseJoinColumnsArray != null && inverseReferencedColumnsArray != null) {
Validate.isTrue(inverseJoinColumnsArray.length == inverseReferencedColumnsArray.length, "--inverseJoinColumns and --inverseReferencedColumns must have same number of column values");
}
// JoinTable set
childFieldDetails.setJoinAnnotations(joinTable, joinColumnsArray, referencedColumnsArray, inverseJoinColumnsArray, inverseReferencedColumnsArray);
}
insertField(childFieldDetails, permitReservedWords, false, true);
}
use of org.springframework.roo.classpath.operations.jsr303.SetField in project spring-roo by spring-projects.
the class JpaFieldCreatorProvider method createCollectionEnumeration.
/**
* Create a field for a List or Set of a enumeration
*
* @param parentCid
* @param fieldType
* @param fieldName
* @param permitReservedWords
* @param sizeMin
* @param sizeMax
* @param comment
* @param notNull
* @param isList
*/
private void createCollectionEnumeration(final ClassOrInterfaceTypeDetails parentCid, JavaType fieldType, JavaSymbolName fieldName, boolean permitReservedWords, Integer sizeMin, Integer sizeMax, String comment, boolean notNull, boolean isList) {
SetField parentFieldDetails;
if (isList) {
parentFieldDetails = new SetField(parentCid.getDeclaredByMetadataId(), new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, null, null, false);
} else {
parentFieldDetails = new ListField(parentCid.getDeclaredByMetadataId(), new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, null, null, false);
}
parentFieldDetails.setNotNull(notNull);
if (comment != null) {
parentFieldDetails.setComment(comment);
}
if (sizeMin != null) {
parentFieldDetails.setSizeMin(sizeMin);
}
if (sizeMax != null) {
parentFieldDetails.setSizeMax(sizeMax);
}
// Handle enumeration Set
insertField(parentFieldDetails, permitReservedWords, false, true);
}
use of org.springframework.roo.classpath.operations.jsr303.SetField in project spring-roo by spring-projects.
the class UpdateCompilationUnitTest method testSimpleClass3AddField.
@Test
public void testSimpleClass3AddField() throws Exception {
// Set up
final File file = getResource(SIMPLE_CLASS3_FILE_PATH);
final String fileContents = getResourceContents(file);
final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
final SetField fieldDetails = new SetField(SIMPLE_CLASS3_DECLARED_BY_MID, new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(SIMPLE_CLASS3_TYPE)), new JavaSymbolName("children"), SIMPLE_CLASS3_TYPE, Cardinality.ONE_TO_MANY, new Cascade[] { Cascade.REMOVE }, false);
final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails.getFieldName(), fieldDetails.getFieldType());
fieldBuilder.setFieldInitializer("new HashSet<SimpleClass3>()");
final ClassOrInterfaceTypeDetails newClassDetails = addField(simpleInterfaceDetails, fieldBuilder.build());
// Invoke
final String result = typeParsingService.getCompilationUnitContents(newClassDetails);
saveResult(file, result, "-addField");
checkSimple3Class(result);
assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
// Add another
final ClassOrInterfaceTypeDetails simpleInterfaceDetails2 = typeParsingService.getTypeFromString(result, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
final ReferenceField fieldDetails2 = new ReferenceField(SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS2_TYPE, new JavaSymbolName("referenceField"), Cardinality.MANY_TO_ONE, new Cascade[] { Cascade.REFRESH });
final FieldMetadataBuilder fieldBuilder2 = new FieldMetadataBuilder(fieldDetails2.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails2.getFieldName(), fieldDetails2.getFieldType());
final ClassOrInterfaceTypeDetails newClassDetails2 = addField(simpleInterfaceDetails2, fieldBuilder2.build());
// Invoke
final String result2 = typeParsingService.getCompilationUnitContents(newClassDetails2);
// Save to file for debug
saveResult(file, result2, "-addField2");
checkSimple3Class(result2);
assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
assertTrue(result2.contains("private SimpleClass2 referenceField;"));
}
use of org.springframework.roo.classpath.operations.jsr303.SetField in project spring-roo by spring-projects.
the class DtoOperationsImpl method addFieldsToProjection.
/**
* Removes persistence annotations of provided fields and adds them to a
* ClassOrInterfaceTypeDetailsBuilder representing a Projection in construction.
* Also adds final modifier to fields if required.
*
* @param projectionBuilder the ClassOrInterfaceTypeDetailsBuilder for building the
* Projection class.
* @param fieldsToAdd the List<FieldMetadata> to add.
*/
private void addFieldsToProjection(ClassOrInterfaceTypeDetailsBuilder projectionBuilder, Map<String, FieldMetadata> fieldsToAdd) {
Iterator<Entry<String, FieldMetadata>> iterator = fieldsToAdd.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, FieldMetadata> entry = iterator.next();
FieldMetadata field = entry.getValue();
// List and Set types require special management
FieldMetadataBuilder fieldBuilder = null;
FieldDetails fieldDetails = null;
if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.Set")) {
JavaType fieldType = field.getFieldType().getParameters().get(0);
fieldDetails = new SetField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
fieldBuilder = new FieldMetadataBuilder(fieldDetails);
fieldBuilder.setModifier(field.getModifier());
fieldBuilder.setAnnotations(field.getAnnotations());
} else if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.List")) {
JavaType fieldType = field.getFieldType().getParameters().get(0);
fieldDetails = new ListField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
fieldBuilder = new FieldMetadataBuilder(fieldDetails);
fieldBuilder.setModifier(field.getModifier());
fieldBuilder.setAnnotations(field.getAnnotations());
} else {
// Can't just copy FieldMetadata because field.declaredByMetadataId will be the same
fieldBuilder = new FieldMetadataBuilder(projectionBuilder.getDeclaredByMetadataId(), field);
}
// Add dependency between modules
typeLocationService.addModuleDependency(projectionBuilder.getName().getModule(), field.getFieldType());
// Set new fieldName
fieldBuilder.setFieldName(new JavaSymbolName(entry.getKey()));
// If it is a CollectionField it needs an initializer
String initializer = null;
if (fieldDetails instanceof CollectionField) {
final CollectionField collectionField = (CollectionField) fieldDetails;
initializer = "new " + collectionField.getInitializer() + "()";
} else if (fieldDetails instanceof DateField && fieldDetails.getFieldName().getSymbolName().equals("created")) {
initializer = "new Date()";
}
fieldBuilder.setFieldInitializer(initializer);
// Remove persistence annotations
List<AnnotationMetadata> annotations = field.getAnnotations();
for (AnnotationMetadata annotation : annotations) {
if (annotation.getAnnotationType().getFullyQualifiedTypeName().contains("javax.persistence")) {
fieldBuilder.removeAnnotation(annotation.getAnnotationType());
} else if (annotation.getAnnotationType().getFullyQualifiedTypeName().startsWith("javax.validation")) {
// Add validation dependency
projectOperations.addDependency(projectionBuilder.getName().getModule(), new Dependency("javax.validation", "validation-api", null));
} else if (annotation.getAnnotationType().equals(RooJavaType.ROO_JPA_RELATION)) {
fieldBuilder.removeAnnotation(annotation.getAnnotationType());
}
}
fieldBuilder.setModifier(Modifier.PRIVATE);
// Build field
FieldMetadata projectionField = fieldBuilder.build();
// Add field to DTO
projectionBuilder.addField(projectionField);
}
}
Aggregations