Search in sources :

Example 56 with AnnotationMetadata

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

the class JmsOperationsImpl method addJmsReceiver.

@Override
public void addJmsReceiver(String destinationName, JavaType endpointService, String jndiConnectionFactory, String profile, boolean force) {
    boolean isApplicationModule = false;
    // Check that the module of the service is type application
    Collection<Pom> modules = getTypeLocationService().getModules(ModuleFeatureName.APPLICATION);
    for (Pom pom : modules) {
        if (pom.getModuleName().equals(endpointService.getModule())) {
            isApplicationModule = true;
        }
    }
    if (!isApplicationModule) {
        LOGGER.log(Level.SEVERE, String.format("The module selected where JMS service will be configured must be of type application"));
        return;
    }
    // Check if Service already exists and --force is included
    final String serviceFilePathIdentifier = getPathResolver().getCanonicalPath(endpointService.getModule(), Path.SRC_MAIN_JAVA, endpointService);
    if (getFileManager().exists(serviceFilePathIdentifier) && force) {
        getFileManager().delete(serviceFilePathIdentifier);
    } else if (getFileManager().exists(serviceFilePathIdentifier)) {
        throw new IllegalArgumentException(String.format("Endpoint '%s' already exists and cannot be created. Try to use a " + "different name on --endpoint parameter or use this command with '--force' " + "to overwrite the current service.", endpointService));
    }
    // Set destionation property name
    StringBuffer destinationNamePropertyName = new StringBuffer(JMS_PROPERTY_DESTINATION_NAME_PREFIX);
    destinationNamePropertyName.append(destinationName.replaceAll("/", "."));
    destinationNamePropertyName.append(JMS_PROPERTY_DESTINATION_NAME_SUFIX);
    // Set properties
    setProperties(destinationName, destinationNamePropertyName.toString(), jndiConnectionFactory, endpointService.getModule(), profile, force);
    // Create service
    createReceiverJmsService(endpointService, destinationNamePropertyName.toString());
    // Add jms dependecy in module
    getProjectOperations().addDependency(endpointService.getModule(), DEPENDENCY_JMS);
    // Add annotation @EnableJms to application class of the module
    Set<ClassOrInterfaceTypeDetails> applicationClasses = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(SpringJavaType.SPRING_BOOT_APPLICATION);
    for (ClassOrInterfaceTypeDetails applicationClass : applicationClasses) {
        if (applicationClass.getType().getModule().equals(endpointService.getModule())) {
            // Check if annotation exists
            boolean annotationNotExists = true;
            for (AnnotationMetadata annotation : applicationClass.getAnnotations()) {
                if (annotation.getAnnotationType().equals(SpringJavaType.ENABLE_JMS)) {
                    annotationNotExists = false;
                    break;
                }
            }
            if (annotationNotExists) {
                ClassOrInterfaceTypeDetailsBuilder builder = new ClassOrInterfaceTypeDetailsBuilder(applicationClass);
                builder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.ENABLE_JMS));
                getTypeManagementService().createOrUpdateTypeOnDisk(builder.build());
            }
            break;
        }
    }
}
Also used : ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Pom(org.springframework.roo.project.maven.Pom)

Example 57 with AnnotationMetadata

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

the class JspViewManager method createFieldsForCreateAndUpdate.

private void createFieldsForCreateAndUpdate(final List<FieldMetadata> formFields, final Document document, final Element root, final boolean isCreate) {
    for (final FieldMetadata field : formFields) {
        final String fieldName = field.getFieldName().getSymbolName();
        JavaType fieldType = field.getFieldType();
        AnnotationMetadata annotationMetadata;
        // Ignoring java.util.Map field types (see ROO-194)
        if (fieldType.equals(new JavaType(Map.class.getName()))) {
            continue;
        }
        // separately to the field list
        if (field.getCustomData().keySet().contains(CustomDataKeys.EMBEDDED_ID_FIELD)) {
            continue;
        }
        fieldType = getJavaTypeForField(field);
        final JavaTypeMetadataDetails typeMetadataHolder = relatedDomainTypes.get(fieldType);
        JavaTypePersistenceMetadataDetails typePersistenceMetadataHolder = null;
        if (typeMetadataHolder != null) {
            typePersistenceMetadataHolder = typeMetadataHolder.getPersistenceDetails();
        }
        Element fieldElement = null;
        if (fieldType.getFullyQualifiedTypeName().equals(Boolean.class.getName()) || fieldType.getFullyQualifiedTypeName().equals(boolean.class.getName())) {
            fieldElement = document.createElement("field:checkbox");
        // Handle enum fields
        } else if (typeMetadataHolder != null && typeMetadataHolder.isEnumType()) {
            fieldElement = new XmlElementBuilder("field:select", document).addAttribute("items", "${" + typeMetadataHolder.getPlural().toLowerCase() + "}").addAttribute("path", getPathForType(fieldType)).build();
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.ONE_TO_MANY_FIELD)) {
            // accordingly
            if (typePersistenceMetadataHolder != null) {
                fieldElement = new XmlElementBuilder("field:simple", document).addAttribute("messageCode", "entity_reference_not_managed").addAttribute("messageCodeAttribute", new JavaSymbolName(fieldType.getSimpleTypeName()).getReadableSymbolName()).build();
            } else {
                continue;
            }
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_ONE_FIELD) || field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_MANY_FIELD) || field.getCustomData().keySet().contains(CustomDataKeys.ONE_TO_ONE_FIELD)) {
            final JavaType referenceType = getJavaTypeForField(field);
            final JavaTypeMetadataDetails referenceTypeMetadata = relatedDomainTypes.get(referenceType);
            if (referenceType != null && referenceTypeMetadata != null && referenceTypeMetadata.isApplicationType() && typePersistenceMetadataHolder != null) {
                fieldElement = new XmlElementBuilder("field:select", document).addAttribute("items", "${" + referenceTypeMetadata.getPlural().toLowerCase() + "}").addAttribute("itemValue", typePersistenceMetadataHolder.getIdentifierField().getFieldName().getSymbolName()).addAttribute("path", "/" + getPathForType(getJavaTypeForField(field))).build();
                if (field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_MANY_FIELD)) {
                    fieldElement.setAttribute("multiple", "true");
                }
            }
        } else if (fieldType.equals(DATE) || fieldType.equals(CALENDAR)) {
            if (fieldName.equals(CREATED)) {
                continue;
            }
            // Only include the date picker for styles supported by Dojo
            // (SMALL & MEDIUM)
            fieldElement = new XmlElementBuilder("field:datetime", document).addAttribute("dateTimePattern", "${" + entityName + "_" + fieldName.toLowerCase() + "_date_format}").build();
            if (null != MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), FUTURE)) {
                fieldElement.setAttribute("future", "true");
            } else if (null != MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), PAST)) {
                fieldElement.setAttribute("past", "true");
            }
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.LOB_FIELD)) {
            fieldElement = new XmlElementBuilder("field:textarea", document).build();
        }
        if ((annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), SIZE)) != null) {
            final AnnotationAttributeValue<?> max = annotationMetadata.getAttribute(new JavaSymbolName("max"));
            if (max != null) {
                final int maxValue = (Integer) max.getValue();
                if (fieldElement == null && maxValue > 30) {
                    fieldElement = new XmlElementBuilder("field:textarea", document).build();
                }
            }
        }
        // Use a default input field if no other criteria apply
        if (fieldElement == null) {
            fieldElement = document.createElement("field:input");
        }
        addCommonAttributes(field, fieldElement);
        fieldElement.setAttribute("field", fieldName);
        fieldElement.setAttribute("id", XmlUtils.convertId("c:" + formBackingType.getFullyQualifiedTypeName() + "." + field.getFieldName().getSymbolName()));
        // If identifier manually assigned, then add 'required=true'
        if (formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().equals(field.getFieldName()) && field.getAnnotation(JpaJavaType.GENERATED_VALUE) == null) {
            fieldElement.setAttribute("required", "true");
        }
        fieldElement.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(fieldElement));
        root.appendChild(fieldElement);
    }
}
Also used : JavaTypePersistenceMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) Element(org.w3c.dom.Element) JavaTypeMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypeMetadataDetails) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 58 with AnnotationMetadata

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

the class ControllerCommands method getDetailFieldsRelatedToEntity.

/**
 * Get a field list that can be selected to do a detail controller.
 *
 * @param entity
 *            Entity on which create the detail controller
 * @param parentFieldName
 *            The parent's field name used to construct the field name
 *            related with the original entity
 * @return the related field list
 */
private List<String> getDetailFieldsRelatedToEntity(ClassOrInterfaceTypeDetails entity, String parentFieldName) {
    List<String> results = new ArrayList<String>();
    MemberDetails entityDetails = getMemberDetailsScanner().getMemberDetails(entity.getType().getSimpleTypeName(), entity);
    List<FieldMetadata> fields = entityDetails.getFields();
    for (FieldMetadata field : fields) {
        AnnotationMetadata oneToManyAnnotation = field.getAnnotation(JpaJavaType.ONE_TO_MANY);
        AnnotationMetadata manyToManyAnnotation = field.getAnnotation(JpaJavaType.MANY_TO_MANY);
        if ((oneToManyAnnotation != null || manyToManyAnnotation != null) && (field.getFieldType().getFullyQualifiedTypeName().equals(JavaType.LIST.getFullyQualifiedTypeName()) || field.getFieldType().getFullyQualifiedTypeName().equals(JavaType.SET.getFullyQualifiedTypeName()))) {
            results.add(parentFieldName.concat(field.getFieldName().getSymbolName()));
        }
    }
    return results;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 59 with AnnotationMetadata

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

the class SecurityFiltersMetadataProviderImpl 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> prefilterMethods = new LinkedHashMap<MethodMetadata, String>();
    Map<MethodMetadata, String> postfilterMethods = new LinkedHashMap<MethodMetadata, String>();
    // Get methods defined in each annotation @RooSecurityFilter
    AnnotationMetadata annotationFilters = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
    AnnotationAttributeValue<?> attributeFilters = annotationFilters.getAttribute("filters");
    List<?> values = (List<?>) attributeFilters.getValue();
    if (values != null && !values.isEmpty()) {
        Iterator<?> valuesIt = values.iterator();
        while (valuesIt.hasNext()) {
            NestedAnnotationAttributeValue filterAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
            // Get attributes from the annotation @RooSecurityFilter
            String methodName = (String) filterAnnotation.getValue().getAttribute("method").getValue();
            String when = (String) filterAnnotation.getValue().getAttribute("when").getValue();
            List<?> methodParameters = (List<?>) filterAnnotation.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 (filterAnnotation.getValue().getAttribute("roles") != null) {
                        roles = (String) filterAnnotation.getValue().getAttribute("roles").getValue();
                    }
                    String usernames = null;
                    if (filterAnnotation.getValue().getAttribute("usernames") != null) {
                        usernames = (String) filterAnnotation.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);
                    if (PRE_FILTER.equals(when)) {
                        prefilterMethods.put(method, secAnnotationValue);
                    } else {
                        if (POST_FILTER.equals(when)) {
                            postfilterMethods.put(method, secAnnotationValue);
                        }
                    }
                    break;
                }
            }
        }
    }
    return new SecurityFiltersMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, prefilterMethods, postfilterMethods);
}
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 60 with AnnotationMetadata

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

the class SecurityOperationsImpl method generateAuthorizeAnnotations.

@Override
public void generateAuthorizeAnnotations(JavaType klass, String methodName, String roles, String usernames) {
    Validate.notNull(klass, "ERROR: klass parameter is mandatory on 'generateAuthorizeAnnotations' method");
    Validate.notNull(methodName, "ERROR: method parameter is mandatory on 'generateAuthorizeAnnotations' method");
    // Get methods to annotate.
    // With the last parameter to false, we avoid that push in action occurs.
    List<Object> pushedElements = getPushInOperations().pushIn(klass.getPackage(), klass, methodName, false);
    List<AnnotationAttributeValue<?>> rooSecurityAuthorizationsToAdd = new ArrayList<AnnotationAttributeValue<?>>();
    for (Object pushedElement : pushedElements) {
        if (pushedElement instanceof DefaultMethodMetadata) {
            DefaultMethodMetadata method = (DefaultMethodMetadata) pushedElement;
            // Get parameters
            List<AnnotationAttributeValue<?>> lstParamTypes = new ArrayList<AnnotationAttributeValue<?>>();
            List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
            Iterator<AnnotatedJavaType> iterParamTypes = parameterTypes.iterator();
            while (iterParamTypes.hasNext()) {
                ClassAttributeValue parameterAttributeValue = new ClassAttributeValue(new JavaSymbolName("value"), iterParamTypes.next().getJavaType());
                lstParamTypes.add(parameterAttributeValue);
            }
            // Generate new annotations @RooSecurityAuthorization
            NestedAnnotationAttributeValue newFilter = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getRooSecurityAuthorizationsAnnotation(method.getMethodName().getSymbolName(), lstParamTypes, roles, usernames).build());
            rooSecurityAuthorizationsToAdd.add(newFilter);
        }
    }
    // Get actual values of @RooSecurityAuthorizations
    ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(klass);
    ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceDetails);
    // Check annotation @RooSecurityAuthorizations to delete defined annotations
    // that will be redefined
    AnnotationMetadata annotationAuthorizations = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
    AnnotationMetadataBuilder annotationAuthorizationsMetadataBuilder;
    if (annotationAuthorizations != null) {
        // Getting authorizations from annotation
        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();
                if (checkRooSecurityAuthorizationMaintainAnnotation(rooSecurityAuthorizationsToAdd, authorizationAnnotation)) {
                    // Maintain annotation if 'method' or 'parameters' are different
                    rooSecurityAuthorizationsToAdd.add(authorizationAnnotation);
                }
            }
        }
        annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(annotationAuthorizations);
        // remove annotation
        cidBuilder.removeAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
    } else {
        // Doesn't exist @RooSecurityAuthorizations, create it
        annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
    }
    // Add authorizations attribute
    ArrayAttributeValue<AnnotationAttributeValue<?>> newAuthorizations = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("authorizations"), rooSecurityAuthorizationsToAdd);
    annotationAuthorizationsMetadataBuilder.addAttribute(newAuthorizations);
    // Include new @RooSecurityAuthorizations annotation
    cidBuilder.addAnnotation(annotationAuthorizationsMetadataBuilder);
    // Write on disk
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    // Add Spring Security dependency
    getProjectOperations().addDependency(klass.getModule(), SPRING_SECURITY_CORE, false);
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) DefaultMethodMetadata(org.springframework.roo.classpath.details.DefaultMethodMetadata) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)109 JavaType (org.springframework.roo.model.JavaType)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)52 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)47 ArrayList (java.util.ArrayList)40 RooJavaType (org.springframework.roo.model.RooJavaType)34 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)30 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)24 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)21 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)20 List (java.util.List)19 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)18 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)17 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)16 SpringJavaType (org.springframework.roo.model.SpringJavaType)15 JdkJavaType (org.springframework.roo.model.JdkJavaType)12 JpaJavaType (org.springframework.roo.model.JpaJavaType)12 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)11 LinkedHashMap (java.util.LinkedHashMap)10