use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class JpaAuditOperationsImpl method getModifiedByField.
/**
* Builds modifiedBy field for storing user who last modifies entity registers
*
* @return FieldMetadataBuilder
*/
private FieldMetadataBuilder getModifiedByField(ClassOrInterfaceTypeDetails entityDetails, String columnName) {
// Create field annotations
List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Only add @Column if required by annotation @RooJpaAudit
if (StringUtils.isNotBlank(columnName)) {
AnnotationMetadataBuilder columnAnnotation = new AnnotationMetadataBuilder(JpaJavaType.COLUMN);
columnAnnotation.addStringAttribute("name", columnName);
annotations.add(columnAnnotation);
}
AnnotationMetadataBuilder createdDateAnnotation = new AnnotationMetadataBuilder(SpringJavaType.LAST_MODIFIED_BY);
annotations.add(createdDateAnnotation);
// Create field
FieldDetails fieldDetails = new FieldDetails(PhysicalTypeIdentifier.createIdentifier(entityDetails), JavaType.STRING, new JavaSymbolName("modifiedBy"));
fieldDetails.setModifiers(Modifier.PRIVATE);
fieldDetails.setAnnotations(annotations);
FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails);
return fieldBuilder;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class JpaDataOnDemandCreator method createDataOnDemandConfiguration.
@Override
public JavaType createDataOnDemandConfiguration(String moduleName) {
// Check if alreafy exists
JavaType dodConfig = getDataOnDemandConfiguration();
if (dodConfig != null) {
return dodConfig;
}
// Add spring-boot-test dependency with test scope
projectOperations.addDependency(moduleName, SPRING_BOOT_TEST_DEPENDENCY);
// Get Pom
final Pom module = projectOperations.getPomFromModuleName(moduleName);
// Get test Path for module
final LogicalPath path = LogicalPath.getInstance(Path.SRC_TEST_JAVA, moduleName);
// Create the JavaType for the configuration class
JavaType dodConfigurationClass = new JavaType(String.format("%s.dod.DataOnDemandConfiguration", typeLocationService.getTopLevelPackageForModule(module), moduleName));
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(dodConfigurationClass, path);
if (metadataService.get(declaredByMetadataId) != null) {
// The file already exists
return new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId).getName();
}
// Create the CID builder
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, dodConfigurationClass, PhysicalTypeCategory.CLASS);
cidBuilder.addAnnotation(new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_DATA_ON_DEMAND_CONFIGURATION));
// Write changes to disk
final ClassOrInterfaceTypeDetails configDodCid = cidBuilder.build();
typeManagementService.createOrUpdateTypeOnDisk(configDodCid);
return configDodCid.getName();
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class JpaDataOnDemandCreator method newDataOnDemandClass.
/**
* Creates a new data-on-demand provider for an entity. Silently returns
* if the DoD class already exists.
*
* @param entity to produce a DoD provider for
* @param name the name of the new DoD class
*/
private JavaType newDataOnDemandClass(JavaType entity, JavaType name) {
Validate.notNull(entity, "Entity to produce a data on demand provider for is required");
Validate.notNull(name, "Name of the new data on demand provider is required");
final LogicalPath path = LogicalPath.getInstance(Path.SRC_TEST_JAVA, name.getModule());
Validate.notNull(path, "Location of the new data on demand provider is required");
// Add javax validation dependency
projectOperations.addDependency(name.getModule(), VALIDATION_API_DEPENDENCY);
// Verify the requested entity actually exists as a class and is not
// abstract
final ClassOrInterfaceTypeDetails cid = getEntityDetails(entity);
Validate.isTrue(cid.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS, "Type %s is not a class", entity.getFullyQualifiedTypeName());
Validate.isTrue(!Modifier.isAbstract(cid.getModifier()), "Type %s is abstract", entity.getFullyQualifiedTypeName());
// Check if the requested entity is a JPA @Entity
final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(JpaDataOnDemandCreator.class.getName(), cid);
final AnnotationMetadata entityAnnotation = memberDetails.getAnnotation(ENTITY);
Validate.isTrue(entityAnnotation != null, "Type %s must be a JPA entity type", entity.getFullyQualifiedTypeName());
// Everything is OK to proceed
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, path);
if (metadataService.get(declaredByMetadataId) != null) {
// The file already exists
return new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId).getName();
}
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
final List<AnnotationAttributeValue<?>> dodConfig = new ArrayList<AnnotationAttributeValue<?>>();
dodConfig.add(new ClassAttributeValue(new JavaSymbolName("entity"), entity));
annotations.add(new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_DATA_ON_DEMAND, dodConfig));
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
cidBuilder.setAnnotations(annotations);
// Write changes on disk
final ClassOrInterfaceTypeDetails dodClassCid = cidBuilder.build();
typeManagementService.createOrUpdateTypeOnDisk(dodClassCid);
return cid.getName();
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getDataField.
/**
* @return the "data" field to use, which is either provided by the user or
* produced on demand (never returns null)
*/
private FieldMetadataBuilder getDataField() {
final List<JavaType> parameterTypes = Arrays.asList(entity);
final JavaType listType = new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, parameterTypes);
int index = -1;
while (true) {
// Compute the required field name
index++;
// The type parameters to be used by the field type
final JavaSymbolName fieldName = new JavaSymbolName("data" + StringUtils.repeat("_", index));
dataFieldName = fieldName;
final FieldMetadata candidate = governorTypeDetails.getField(fieldName);
if (candidate != null) {
// Verify if candidate is suitable
if (!Modifier.isPrivate(candidate.getModifier())) {
// next possible name)
continue;
}
if (!candidate.getFieldType().equals(listType)) {
// The equals method also verifies type params are present
continue;
}
// we assume the user knows what they're doing and have made one
return new FieldMetadataBuilder(candidate);
}
// Candidate not found, so let's create one
FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldName, listType);
CommentStructure comment = new CommentStructure();
comment.addComment(new JavadocComment("List of created entities."), CommentLocation.BEGINNING);
fieldBuilder.setCommentStructure(comment);
return fieldBuilder;
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getSizeField.
/**
* Creates size field.
*
* @return {@link FieldMetadataBuilder} for building field into ITD.
*/
private FieldMetadata getSizeField() {
// Create field
FieldMetadataBuilder sizeField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), new JavaSymbolName(SIZE_VAR), JavaType.INT_PRIMITIVE);
CommentStructure comment = new CommentStructure();
comment.addComment(new JavadocComment("Number of elements to create and persist."), CommentLocation.BEGINNING);
sizeField.setCommentStructure(comment);
return sizeField.build();
}
Aggregations