Search in sources :

Example 81 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class RepositoryJpaCustomImplMetadata method getFindByReferencedFieldsImpl.

/**
 * Method that generates the findBy referenced fields implementation method
 *
 * @param the FieldMetadata of the referenced field
 * @param method to implement
 * @param referencedPathFieldName the String with the the referenced field name
 *            in "path" format.
 * @param ids the entity id fields
 * @param fields the entity fields to search for
 *
 * @return
 */
private MethodMetadata getFindByReferencedFieldsImpl(FieldMetadata referencedField, MethodMetadata method, String referencedPathFieldName, List<FieldMetadata> fields) {
    // Define method name
    JavaSymbolName methodName = method.getMethodName();
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
    // Define method parameter names
    List<JavaSymbolName> parameterNames = method.getParameterNames();
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Use provided findAllByReference method to generate its implementation
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, method.getReturnType(), parameterTypes, parameterNames, null);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Getting variable name to use in the code
    JavaSymbolName referencedFieldParamName = parameterNames.get(0);
    JavaSymbolName globalSearch = parameterNames.get(1);
    JavaSymbolName pageable = parameterNames.get(2);
    String entity = this.entity.getSimpleTypeName();
    String entityVariable = StringUtils.uncapitalize(entity);
    // Types to import
    JavaType projection = QUERYDSL_PROJECTIONS;
    bodyBuilder.newLine();
    // QEntity qEntity = QEntity.entity;
    bodyBuilder.appendFormalLine(String.format("%1$s %2$s = %1$s.%2$s;", entityQtype.getNameIncludingTypeParameters(false, importResolver), entityVariable));
    bodyBuilder.newLine();
    // Construct query
    buildQuery(bodyBuilder, entityVariable, globalSearch, referencedFieldParamName, referencedField, referencedPathFieldName, null, null, this.defaultReturnType, null, null);
    bodyBuilder.newLine();
    // AttributeMappingBuilder mapping = buildMapper()
    StringBuffer mappingBuilderLine = new StringBuffer();
    mappingBuilderLine.append(String.format("%s mapping = buildMapper()", SpringletsJavaType.SPRINGLETS_QUERYDSL_REPOSITORY_SUPPORT_ATTRIBUTE_BUILDER.getNameIncludingTypeParameters(false, this.importResolver)));
    // .map(entiyVarName, varName) ...
    if (!this.typesAreProjections.get(this.defaultReturnType)) {
        // Return type is the same entity
        Iterator<FieldMetadata> iterator = fields.iterator();
        while (iterator.hasNext()) {
            FieldMetadata field = iterator.next();
            String entityFieldName = field.getFieldName().getSymbolName();
            mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s.%s)", getConstantForField(entityFieldName).getFieldName(), entityVariable, entityFieldName));
        }
    } else {
        // Return type is a projection
        List<Pair<String, String>> projectionFields = this.typesFieldMaps.get(this.defaultReturnType);
        Iterator<Pair<String, String>> iterator = projectionFields.iterator();
        while (iterator.hasNext()) {
            Pair<String, String> entry = iterator.next();
            mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s)", getConstantForField(entry.getKey()).getFieldName(), entry.getValue()));
        }
    }
    mappingBuilderLine.append(";");
    bodyBuilder.appendFormalLine(mappingBuilderLine.toString());
    bodyBuilder.newLine();
    // applyPagination(pageable, query, mapping);
    bodyBuilder.appendFormalLine(String.format("applyPagination(pageable, query, mapping);", pageable));
    // applyOrderById(query);
    bodyBuilder.appendFormalLine("applyOrderById(query);");
    bodyBuilder.newLine();
    buildQueryResult(bodyBuilder, pageable, entityVariable, projection, this.defaultReturnType);
    // Sets body to generated method
    methodBuilder.setBodyBuilder(bodyBuilder);
    // Build and return a MethodMetadata
    return methodBuilder.build();
// instance
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) Pair(org.apache.commons.lang3.tuple.Pair)

Example 82 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class RepositoryJpaMetadata method getFindAllIteratorMethod.

private MethodMetadata getFindAllIteratorMethod(JavaType entity, FieldMetadata identifierFieldMetadata) {
    // Define method parameter type and name
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JavaType.iterableOf(identifierFieldMetadata.getFieldType())));
    parameterNames.add(identifierFieldMetadata.getFieldName());
    MethodMetadata existingMethod = getGovernorMethod(FIND_ALL_ITERATOR_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, FIND_ALL_ITERATOR_METHOD_NAME, JavaType.listOf(entity), parameterTypes, parameterNames, null).build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata)

Example 83 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class RepositoryJpaMetadata method getSaveMethod.

private MethodMetadata getSaveMethod(JavaType entity) {
    // Define method parameter type and name
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(entity));
    parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(entity.getSimpleTypeName())));
    MethodMetadata existingMethod = getGovernorMethod(SAVE_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, SAVE_METHOD_NAME, entity, parameterTypes, parameterNames, null).build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata)

Example 84 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class PushInCommands method getAllPossibleMethods.

/**
 * Method that returns all defined methods for provided class on --class parameter.
 *
 * @param context context
 *            ShellContext used to obtain specified parameters
 * @return List with available methods. Empty List if class has not been specified.
 */
@CliOptionAutocompleteIndicator(command = "push-in", param = "method", validate = false, help = "Provides possible methods names if some class parameter has been specified")
public List<String> getAllPossibleMethods(ShellContext context) {
    List<String> allPossibleMethods = new ArrayList<String>();
    // Getting all introduces parameters
    Map<String, String> specifiedParameters = context.getParameters();
    String specifiedClass = specifiedParameters.get("class");
    // Check if class parameter has been specified
    if (StringUtils.isNotEmpty(specifiedClass)) {
        JavaType klass = getJavaTypeConverter().convertFromText(specifiedClass, JavaType.class, PROJECT);
        // TODO: Class details should be cached to prevent load MemberDetails everytime.
        // The problem is that if some element is cached, and then, new  method is added
        // to .aj file, this parameter will not autocomplete it.
        MemberDetails klassDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), typeLocationService.getTypeDetails(klass));
        if (klassDetails != null) {
            List<MethodMetadata> definedMethods = klassDetails.getMethods();
            for (MethodMetadata method : definedMethods) {
                // Check if method has been defined on current class and check
                // if current method has been pushed before.
                String declaredByMetadataID = method.getDeclaredByMetadataId();
                if (StringUtils.isNotBlank(declaredByMetadataID) && declaredByMetadataID.split("\\?").length > 1 && declaredByMetadataID.split("\\#").length > 0 && !declaredByMetadataID.split("\\#")[0].equals("MID:org.springframework.roo.classpath.PhysicalTypeIdentifier") && declaredByMetadataID.split("\\?")[1].equals(klass.getFullyQualifiedTypeName())) {
                    String methodName = method.getMethodName().getSymbolName();
                    List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
                    methodName = methodName.concat("(");
                    for (int i = 0; i < parameterTypes.size(); i++) {
                        String paramType = parameterTypes.get(i).getJavaType().getSimpleTypeName();
                        methodName = methodName.concat(paramType).concat(",");
                    }
                    if (!parameterTypes.isEmpty()) {
                        methodName = methodName.substring(0, methodName.length() - 1).concat(")");
                    } else {
                        methodName = methodName.concat(")");
                    }
                    allPossibleMethods.add(methodName);
                }
            }
        }
    }
    return allPossibleMethods;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Example 85 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class PushInOperationsImpl method methodMatch.

/**
 * This method checks if the provided methodName matches with the provided
 * regular expression
 *
 * @param methodName
 * @param regEx
 * @return
 */
private boolean methodMatch(MethodMetadata method, String regEx) {
    // Create regular expression using provided text
    Pattern pattern = Pattern.compile(regEx);
    Matcher matcher = pattern.matcher(method.getMethodName().getSymbolName());
    boolean matches = matcher.matches();
    // manually
    if (!matches && regEx.split("\\(").length > 1) {
        // Getting method name and parameter types
        String name = regEx.split("\\(")[0];
        String[] parameterTypes = regEx.split("\\(")[1].replaceAll("\\)", "").split(",");
        // Prevent errors with empty regular expressions
        if (StringUtils.isEmpty(name)) {
            return false;
        }
        if (method.getMethodName().equals(new JavaSymbolName(name))) {
            List<AnnotatedJavaType> methodParams = method.getParameterTypes();
            boolean sameParameterTypes = false;
            if (methodParams.size() == parameterTypes.length) {
                sameParameterTypes = true;
                for (int i = 0; i < methodParams.size(); i++) {
                    if (!methodParams.get(i).getJavaType().getSimpleTypeName().equals(parameterTypes[i])) {
                        sameParameterTypes = false;
                        break;
                    }
                }
            }
            // If the same method as provided, return true
            if (sameParameterTypes) {
                return true;
            }
        }
    }
    return matches;
}
Also used : Pattern(java.util.regex.Pattern) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) Matcher(java.util.regex.Matcher) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)

Aggregations

AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)130 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)117 ArrayList (java.util.ArrayList)116 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)108 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)99 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)81 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)71 JavaType (org.springframework.roo.model.JavaType)63 SpringJavaType (org.springframework.roo.model.SpringJavaType)46 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)41 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)25 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)23 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)16 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)15 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)12 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)10 Pair (org.apache.commons.lang3.tuple.Pair)7 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7