use of org.springframework.roo.classpath.operations.jsr303.DateField 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);
}
}
use of org.springframework.roo.classpath.operations.jsr303.DateField in project spring-roo by spring-projects.
the class DtoFieldCreatorProvider method createDateField.
@Override
public void createDateField(ClassOrInterfaceTypeDetails javaTypeDetails, JavaType fieldType, JavaSymbolName fieldName, boolean notNull, boolean nullRequired, boolean future, boolean past, DateFieldPersistenceType persistenceType, String column, String comment, DateTime dateFormat, DateTime timeFormat, String pattern, String value, boolean permitReservedWords, boolean transientModifier, List<AnnotationMetadataBuilder> extraAnnotations) {
final String physicalTypeIdentifier = javaTypeDetails.getDeclaredByMetadataId();
final DateField fieldDetails = new DateField(physicalTypeIdentifier, fieldType, fieldName);
fieldDetails.setNotNull(notNull);
fieldDetails.setNullRequired(nullRequired);
fieldDetails.setFuture(future);
fieldDetails.setPast(past);
if (JdkJavaType.isDateField(fieldType)) {
fieldDetails.setPersistenceType(persistenceType);
}
if (column != null) {
fieldDetails.setColumn(column);
}
if (comment != null) {
fieldDetails.setComment(comment);
}
if (dateFormat != null) {
fieldDetails.setDateFormat(dateFormat);
}
if (timeFormat != null) {
fieldDetails.setTimeFormat(timeFormat);
}
if (pattern != null) {
fieldDetails.setPattern(pattern);
}
if (value != null) {
fieldDetails.setValue(value);
}
if (extraAnnotations != null && !extraAnnotations.isEmpty()) {
fieldDetails.addAnnotations(extraAnnotations);
}
insertField(fieldDetails, permitReservedWords, false);
}
Aggregations