Search in sources :

Example 96 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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 97 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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 98 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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 99 with MethodMetadata

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

the class SecurityAuthorizationsMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Getting the annotated entity type
    JavaType annotatedService = governorPhysicalTypeMetadata.getType();
    // Getting the service details
    MemberDetails serviceDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), getTypeLocationService().getTypeDetails(annotatedService));
    Map<MethodMetadata, String> preAuthorizationMethods = new LinkedHashMap<MethodMetadata, String>();
    // Get methods defined in each annotation @RooSecurityAuthorization
    AnnotationMetadata annotationAuthorizations = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
    AnnotationAttributeValue<?> attributeAuthorizations = annotationAuthorizations.getAttribute("authorizations");
    List<?> values = (List<?>) attributeAuthorizations.getValue();
    if (values != null && !values.isEmpty()) {
        Iterator<?> valuesIt = values.iterator();
        while (valuesIt.hasNext()) {
            NestedAnnotationAttributeValue authorizationAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
            // Get attributes from the annotation @RooSecurityAuthorization
            String methodName = (String) authorizationAnnotation.getValue().getAttribute("method").getValue();
            List<?> methodParameters = (List<?>) authorizationAnnotation.getValue().getAttribute("parameters").getValue();
            List<MethodMetadata> methods = serviceDetails.getMethods(new JavaSymbolName(methodName));
            for (MethodMetadata method : methods) {
                // check the parameters to get corresponding method
                if (checkParameters(method, methodParameters)) {
                    String roles = null;
                    if (authorizationAnnotation.getValue().getAttribute("roles") != null) {
                        roles = (String) authorizationAnnotation.getValue().getAttribute("roles").getValue();
                    }
                    String usernames = null;
                    if (authorizationAnnotation.getValue().getAttribute("usernames") != null) {
                        usernames = (String) authorizationAnnotation.getValue().getAttribute("usernames").getValue();
                    }
                    // Create the content based on defined roles and
                    // usernames to include it in the security annotation
                    String secAnnotationValue = getSecurityOperations().getSpringSecurityAnnotationValue(roles, usernames);
                    preAuthorizationMethods.put(method, secAnnotationValue);
                    break;
                }
            }
        }
    }
    return new SecurityAuthorizationsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, preAuthorizationMethods);
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) LinkedHashMap(java.util.LinkedHashMap) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) List(java.util.List) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 100 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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)

Aggregations

MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)149 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)119 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)115 ArrayList (java.util.ArrayList)111 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)110 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)73 JavaType (org.springframework.roo.model.JavaType)72 SpringJavaType (org.springframework.roo.model.SpringJavaType)40 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)39 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 RooJavaType (org.springframework.roo.model.RooJavaType)22 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)18 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)17 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)16 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15