Search in sources :

Example 16 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getConstructorWithEntityManager.

/**
 * Builds constructor with EntityManager argument.
 *
 * @return ConstructorMetadataBuilder for adding constructor to ITD
 */
private ConstructorMetadataBuilder getConstructorWithEntityManager() {
    ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    constructorBuilder.addParameter(ENTITY_MANAGER_VAR, JpaJavaType.ENTITY_MANAGER);
    // this(entityManager, 10);
    bodyBuilder.appendFormalLine(String.format("this(%1$s, 10);", ENTITY_MANAGER_VAR));
    constructorBuilder.setModifier(Modifier.PUBLIC);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    CommentStructure comment = new CommentStructure();
    List<String> paramsInfo = new ArrayList<String>();
    paramsInfo.add(ENTITY_MANAGER_VAR + " to persist entities");
    JavadocComment javadocComment = new JavadocComment(String.format("Creates a new {@link %s}.", this.governorPhysicalTypeMetadata.getType().getSimpleTypeName()), paramsInfo, null, null);
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    constructorBuilder.setCommentStructure(comment);
    return constructorBuilder;
}
Also used : JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 17 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getRndField.

private FieldMetadataBuilder getRndField() {
    int index = -1;
    while (true) {
        // Compute the required field name
        index++;
        final JavaSymbolName fieldName = new JavaSymbolName("rnd" + StringUtils.repeat("_", index));
        this.rndFieldName = 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(RANDOM)) {
                // Candidate isn't a java.util.Random, so it isn't suitable
                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
        builder.getImportRegistrationResolver().addImports(RANDOM, SECURE_RANDOM);
        final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId());
        fieldBuilder.setModifier(Modifier.PRIVATE);
        fieldBuilder.setFieldName(fieldName);
        fieldBuilder.setFieldType(RANDOM);
        fieldBuilder.setFieldInitializer("new SecureRandom()");
        CommentStructure comment = new CommentStructure();
        comment.addComment(new JavadocComment("Random generator for the entities index."), CommentLocation.BEGINNING);
        fieldBuilder.setCommentStructure(comment);
        return fieldBuilder;
    }
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 18 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getInitMethod.

/**
 * Returns the DoD type's "void init()" method (existing or generated)
 *
 * @param persistMethod
 *            (required)
 * @return never `null`
 */
private MethodMetadataBuilder getInitMethod(final int quantity) {
    // Method definition to find or build
    final JavaSymbolName methodName = new JavaSymbolName("init");
    final JavaType[] parameterTypes = {};
    final List<JavaSymbolName> parameterNames = Collections.<JavaSymbolName>emptyList();
    final JavaType returnType = JavaType.VOID_PRIMITIVE;
    // Locate user-defined method
    final MethodMetadata userMethod = getGovernorMethod(methodName, parameterTypes);
    if (userMethod != null) {
        Validate.isTrue(userMethod.getReturnType().equals(returnType), "Method '%s' on '%s' must return '%s'", methodName, destination, returnType.getNameIncludingTypeParameters());
        return new MethodMetadataBuilder(userMethod);
    }
    // Create the method body
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("int from = 0;");
    bodyBuilder.appendFormalLine("int to = " + quantity + ";");
    // CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("%s cb = %s().getCriteriaBuilder();", getNameOfJavaType(JpaJavaType.CRITERIA_BUILDER), getAccessorMethod(getEntityManagerField().build()).getMethodName());
    // CriteriaQuery<Entity> cq = cb.createQuery(Entity.class);
    bodyBuilder.appendFormalLine("%1$s<%2$s> cq = cb.createQuery(%2$s.class);", getNameOfJavaType(JpaJavaType.CRITERIA_QUERY), getNameOfJavaType(this.entity));
    // Root<Entity> rootEntry = cq.from(Entity.class);
    bodyBuilder.appendFormalLine("%1$s<%2$s> rootEntry = cq.from(%2$s.class);", getNameOfJavaType(JpaJavaType.ROOT), getNameOfJavaType(this.entity));
    // CriteriaQuery<Owner> all = cq.select(rootEntry);
    bodyBuilder.appendFormalLine("%s<%s> all = cq.select(rootEntry);", getNameOfJavaType(JpaJavaType.CRITERIA_QUERY), getNameOfJavaType(this.entity));
    // TypedQuery<Owner> allQuery =
    bodyBuilder.appendFormalLine("%s<%s> allQuery = ", getNameOfJavaType(JpaJavaType.TYPED_QUERY), getNameOfJavaType(this.entity));
    // entityManager.createQuery(all).setFirstResult(from).setMaxResults(to);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("%s().createQuery(all).setFirstResult(from).setMaxResults(to);", getAccessorMethod(getEntityManagerField().build()).getMethodName());
    // setData(allQuery.getResultList());
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("%s(allQuery.getResultList());", getMutatorMethod(getDataField().build()).getMethodName());
    // if (getData() == null) {
    bodyBuilder.appendFormalLine("if (%s() == null) {", getAccessorMethod(getDataField().build()).getMethodName());
    // throw new IllegalStateException(
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("throw new IllegalStateException(");
    // "Find entries implementation for 'Owner' illegally returned null");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("\"Find entries implementation for '%s' illegally returned null\");", getNameOfJavaType(this.entity));
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // if (!data.isEmpty()) {
    bodyBuilder.appendFormalLine("if (!%s().isEmpty()) {", getAccessorMethod(getDataField().build()).getMethodName());
    // return;
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("return;");
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // setData(new ArrayList<Entity>());
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("%s(new %s<%s>());", getMutatorMethod(getDataField().build()).getMethodName(), getNameOfJavaType(JdkJavaType.ARRAY_LIST), getNameOfJavaType(this.entity));
    // for (int i = from; i < to; i++) {
    bodyBuilder.appendFormalLine("for (int i = from; i < to; i++) {");
    bodyBuilder.indent();
    // Entity obj = factory.create(i);
    bodyBuilder.appendFormalLine("%s %s = %s().%s(i);", getNameOfJavaType(this.entity), OBJ_VAR, getAccessorMethod(getEntityFactoryField().build()).getMethodName(), this.entityFactoryMetadata.getCreateFactoryMethodName());
    // try {
    bodyBuilder.appendFormalLine("try {");
    bodyBuilder.indent();
    // entityManager.persist(obj);
    bodyBuilder.appendFormalLine("%s().persist(%s);", getAccessorMethod(getEntityManagerField().build()).getMethodName(), OBJ_VAR);
    // } catch (final ConstraintViolationException e) {
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("} catch (final %s e) {", getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION_EXCEPTION));
    bodyBuilder.indent();
    // final StringBuilder msg = new StringBuilder();
    bodyBuilder.appendFormalLine("final StringBuilder msg = new StringBuilder();");
    // for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter
    bodyBuilder.appendFormalLine("for (%s<%s<?>> iter = e.getConstraintViolations().iterator(); iter", getNameOfJavaType(JdkJavaType.ITERATOR), getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION));
    // .hasNext();) {
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("  .hasNext();) {");
    // final ConstraintViolation<?> cv = iter.next();
    bodyBuilder.appendFormalLine("final %s<?> cv = iter.next();", getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION));
    // msg.append("[").append(cv.getRootBean().getClass().getName()).append(".")
    bodyBuilder.appendFormalLine("msg.append(\"[\").append(cv.getRootBean().getClass().getName()).append(\".\")");
    // .append(cv.getPropertyPath()).append(": ").append(cv.getMessage())
    bodyBuilder.appendFormalLine(".append(cv.getPropertyPath()).append(\": \").append(cv.getMessage())");
    // .append(" (invalid value = ").append(cv.getInvalidValue()).append(")").append("]");
    bodyBuilder.appendFormalLine(".append(\" (invalid value = \").append(cv.getInvalidValue()).append(\")\").append(\"]\");");
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // throw new IllegalStateException(msg.toString(), e);
    bodyBuilder.appendFormalLine("throw new IllegalStateException(msg.toString(), e);");
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // entityManager.flush();
    bodyBuilder.appendFormalLine("%s().%s();", getAccessorMethod(getEntityManagerField().build()).getMethodName(), FLUSH_METHOD_NAME);
    // data.add(obj);
    bodyBuilder.appendFormalLine("%s().add(%s);", getAccessorMethod(getDataField().build()).getMethodName(), OBJ_VAR);
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // Create the method
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
    CommentStructure comment = new CommentStructure();
    JavadocComment javadocComment = new JavadocComment("Creates the initial list of generated entities.");
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(comment);
    return methodBuilder;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) 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 19 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getEntityFactoryField.

/**
 * Creates an EntityFactory field related to this entity.
 *
 * @return {@link FieldMetadataBuilder} for building field into ITD.
 */
private FieldMetadataBuilder getEntityFactoryField() {
    // Create field
    FieldMetadataBuilder entityFactoryField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), new JavaSymbolName(FACTORY_VAR), this.entityFactoryMetadata.getGovernorType());
    entityFactoryField.setFieldInitializer(String.format("new %s()", getNameOfJavaType(this.entityFactoryMetadata.getGovernorType())));
    CommentStructure comment = new CommentStructure();
    comment.addComment(new JavadocComment("Factory to create entity instances."), CommentLocation.BEGINNING);
    entityFactoryField.setCommentStructure(comment);
    return entityFactoryField;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 20 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getEntityManagerField.

/**
 * Creates EntityManager field.
 *
 * @return {@link FieldMetadataBuilder} for building field into ITD.
 */
private FieldMetadataBuilder getEntityManagerField() {
    // Create field
    FieldMetadataBuilder entityManagerField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), new JavaSymbolName(ENTITY_MANAGER_VAR), JpaJavaType.ENTITY_MANAGER);
    CommentStructure comment = new CommentStructure();
    comment.addComment(new JavadocComment("EntityManager to persist the entities."), CommentLocation.BEGINNING);
    entityManagerField.setCommentStructure(comment);
    return entityManagerField;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Aggregations

CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)28 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)28 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)23 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)16 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)14 ArrayList (java.util.ArrayList)13 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)12 JavaType (org.springframework.roo.model.JavaType)12 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)11 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)10 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)7 JdkJavaType (org.springframework.roo.model.JdkJavaType)7 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)6 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)6 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)5 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)4 TypeDeclaration (com.github.antlrjavaparser.api.body.TypeDeclaration)4 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)4 ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)4 ParseException (com.github.antlrjavaparser.ParseException)3