use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.
the class JpaAuditOperationsImpl method getCreatedByField.
/**
* Builds createdBy field for storing user who creates entity registers
*
* @return FieldMetadataBuilder
*/
private FieldMetadataBuilder getCreatedByField(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.CREATED_BY);
annotations.add(createdDateAnnotation);
// Create field
FieldDetails fieldDetails = new FieldDetails(PhysicalTypeIdentifier.createIdentifier(entityDetails), JavaType.STRING, new JavaSymbolName("createdBy"));
fieldDetails.setModifiers(Modifier.PRIVATE);
fieldDetails.setAnnotations(annotations);
FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails);
return fieldBuilder;
}
use of org.springframework.roo.model.JavaSymbolName 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.model.JavaSymbolName 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.model.JavaSymbolName in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getRandomPersistentEntityMethod.
/**
* @return the "getRandomEntity():Entity" method (never returns null)
*/
private MethodMetadata getRandomPersistentEntityMethod() {
// Method definition to find or build
final JavaSymbolName methodName = new JavaSymbolName("getRandom" + this.entity.getSimpleTypeName());
// Locate user-defined method
final MethodMetadata userMethod = getGovernorMethod(methodName);
if (userMethod != null) {
Validate.isTrue(userMethod.getReturnType().equals(this.entity), "Method '%s' on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
this.randomPersistentEntityMethod = userMethod;
return userMethod;
}
// Create method
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// init();
bodyBuilder.appendFormalLine("init();");
// return data.get(rnd.nextInt(data.size()));
bodyBuilder.appendFormalLine("return %1$s().get(%2$s().nextInt(%1$s().size()));", getAccessorMethod(getDataField().build()).getMethodName(), getAccessorMethod(getRndField().build()).getMethodName());
final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, bodyBuilder);
CommentStructure comment = new CommentStructure();
JavadocComment javadocComment = new JavadocComment(String.format("Returns a generated and persisted {@link %s} in a random index.", this.entity.getSimpleTypeName()), null, String.format("%1$s a random {@link %1$s}", this.entity.getSimpleTypeName()), null);
comment.addComment(javadocComment, CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(comment);
this.randomPersistentEntityMethod = methodBuilder.build();
return this.randomPersistentEntityMethod;
}
use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getNewRandomTransientEntityMethod.
private MethodMetadata getNewRandomTransientEntityMethod() {
// Method definition to find or build
final JavaSymbolName methodName = new JavaSymbolName("getNewRandomTransient" + this.entity.getSimpleTypeName());
// Locate user-defined method
final MethodMetadata userMethod = getGovernorMethod(methodName);
if (userMethod != null) {
Validate.isTrue(userMethod.getReturnType().equals(entity), "Method '%s' on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
this.newTransientEntityMethod = userMethod;
return userMethod;
}
// Create method
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// int randomIndex = getSize() + rnd.nextInt(Integer.MAX_VALUE - getSize());
bodyBuilder.appendFormalLine("int randomIndex = %1$s() + %2$s().nextInt(Integer.MAX_VALUE - %1$s());", this.sizeAccesorName, getAccessorMethod(getRndField().build()).getMethodName());
// return factory.create(randomIndex);
bodyBuilder.appendFormalLine("return %s().%s(randomIndex);", getAccessorMethod(getEntityFactoryField().build()).getMethodName(), this.entityFactoryMetadata.getCreateFactoryMethodName());
final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, bodyBuilder);
CommentStructure comment = new CommentStructure();
JavadocComment javadocComment = new JavadocComment(String.format("Creates a new transient %s in a random index out of " + "the initial list of the created entities,".concat(IOUtils.LINE_SEPARATOR).concat("with an index greater than {@link %s#getSize()} - 1."), this.entity.getSimpleTypeName(), this.governorPhysicalTypeMetadata.getType().getSimpleTypeName()), null, String.format("%1$s the generated transient {@link %1$s}", this.entity.getSimpleTypeName()), null);
comment.addComment(javadocComment, CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(comment);
this.newTransientEntityMethod = methodBuilder.build();
return this.newTransientEntityMethod;
}
Aggregations