Search in sources :

Example 16 with JavaSymbolName

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;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 17 with JavaSymbolName

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;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 18 with JavaSymbolName

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();
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) LogicalPath(org.springframework.roo.project.LogicalPath) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 19 with JavaSymbolName

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;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 20 with JavaSymbolName

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;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Aggregations

JavaSymbolName (org.springframework.roo.model.JavaSymbolName)317 ArrayList (java.util.ArrayList)186 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)155 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)142 JavaType (org.springframework.roo.model.JavaType)133 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)121 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)119 SpringJavaType (org.springframework.roo.model.SpringJavaType)61 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)59 JdkJavaType (org.springframework.roo.model.JdkJavaType)59 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)48 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)47 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)45 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)36 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)29 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)29 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)27 EnumDetails (org.springframework.roo.model.EnumDetails)25