Search in sources :

Example 71 with ClassOrInterfaceTypeDetails

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

the class ControllerOperationsImpl method checkDetailControllerExists.

/**
 * Check if detail controller exists for the values entity, responseType,
 * controllerPackage, pathPrefix and relationField provided by parameters
 *
 * @param entity
 *            Detail controller entity
 * @param responseType
 * @param controllerPackage
 * @param pathPrefix
 * @param relationField
 * @return
 */
private boolean checkDetailControllerExists(JavaType entity, ControllerMVCResponseService responseType, JavaPackage controllerPackage, String pathPrefix, String relationField) {
    Collection<ClassOrInterfaceTypeDetails> detailControllers = getControllerLocator().getControllers(entity, ControllerType.DETAIL, responseType.getAnnotation());
    for (ClassOrInterfaceTypeDetails existingController : detailControllers) {
        if (existingController.getType().getPackage().equals(controllerPackage)) {
            ControllerAnnotationValues values = new ControllerAnnotationValues(existingController);
            AnnotationAttributeValue<String> relationFieldAttr = existingController.getAnnotation(RooJavaType.ROO_DETAIL).getAttribute("relationField");
            if (StringUtils.equals(pathPrefix, values.getPathPrefix())) {
                if (relationFieldAttr == null) {
                    LOGGER.warning(String.format("Controller %s is defined as @%.type = DETAIL but @%s is missing!", existingController.getType().getFullyQualifiedTypeName(), RooJavaType.ROO_CONTROLLER, RooJavaType.ROO_DETAIL));
                } else if (relationField.equals(relationFieldAttr.getValue())) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 72 with ClassOrInterfaceTypeDetails

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

the class ControllerOperationsImpl method exportOperation.

/**
 * Generate the operations selected in the controller indicated
 *
 * @param controller
 *            Controller where the operations will be created
 * @param operations
 *            Service operations names that will be created
 */
public void exportOperation(JavaType controller, List<String> operations) {
    ClassOrInterfaceTypeDetails controllerDetails = getTypeLocationService().getTypeDetails(controller);
    // Check if provided controller exists on current project
    Validate.notNull(controllerDetails, "ERROR: You must provide an existing controller");
    // Check if provided controller has been annotated with @RooController
    Validate.notNull(controllerDetails.getAnnotation(RooJavaType.ROO_CONTROLLER), "ERROR: You must provide a controller annotated with @RooController");
    // Check parameter operations
    Validate.notEmpty(operations, "INFO: Don't exist operations to publish");
    ClassOrInterfaceTypeDetailsBuilder controllerBuilder = new ClassOrInterfaceTypeDetailsBuilder(controllerDetails);
    AnnotationMetadata operationsAnnotation = controllerDetails.getAnnotation(RooJavaType.ROO_OPERATIONS);
    // Create an array with new attributes array
    List<StringAttributeValue> operationsToAdd = new ArrayList<StringAttributeValue>();
    if (operationsAnnotation == null) {
        // Add Operations annotation
        AnnotationMetadataBuilder opAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_OPERATIONS);
        controllerBuilder.addAnnotation(opAnnotation);
        // set operations from command
        for (String operation : operations) {
            operationsToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), operation));
        }
        opAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("operations"), operationsToAdd));
        // Write changes on provided controller
        getTypeManagementService().createOrUpdateTypeOnDisk(controllerBuilder.build());
    } else {
        List<String> operationsNames = new ArrayList<String>();
        boolean operationsAdded = false;
        AnnotationAttributeValue<Object> attributeOperations = operationsAnnotation.getAttribute("operations");
        if (attributeOperations != null) {
            List<StringAttributeValue> existingOperations = (List<StringAttributeValue>) attributeOperations.getValue();
            Iterator<StringAttributeValue> it = existingOperations.iterator();
            // new ones
            while (it.hasNext()) {
                StringAttributeValue attributeValue = (StringAttributeValue) it.next();
                operationsToAdd.add(attributeValue);
                operationsNames.add(attributeValue.getValue());
            }
            // Add new finders to new attributes array
            for (String operation : operations) {
                if (!operationsNames.contains(operation)) {
                    operationsToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), operation));
                    operationsAdded = true;
                }
            }
            if (operationsAdded) {
                AnnotationMetadataBuilder opAnnotation = new AnnotationMetadataBuilder(operationsAnnotation);
                opAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("operations"), operationsToAdd));
                controllerBuilder.updateTypeAnnotation(opAnnotation);
                // Write changes on provided controller
                getTypeManagementService().createOrUpdateTypeOnDisk(controllerBuilder.build());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) List(java.util.List) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 73 with ClassOrInterfaceTypeDetails

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

the class ControllerOperationsImpl method getRelationInfoFor.

@Override
public List<RelationInfoExtended> getRelationInfoFor(final JavaType entity, final String path) {
    Validate.notNull(entity, "entity is required");
    Validate.notBlank(path, "path is required");
    ClassOrInterfaceTypeDetails cid = getTypeLocationService().getTypeDetails(entity);
    Validate.notNull(cid, "%s not found", entity.getFullyQualifiedTypeName());
    JpaEntityMetadata entityMetada = getMetadataService().get(JpaEntityMetadata.createIdentifier(cid));
    Validate.notNull(entityMetada, "Can't get Entity metadata information for %s", entity.getFullyQualifiedTypeName());
    return getRelationInfoFor(entityMetada, path);
}
Also used : ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 74 with ClassOrInterfaceTypeDetails

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

the class JsonControllerIntegrationTestMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final JsonControllerIntegrationTestAnnotationValues annotationValues = new JsonControllerIntegrationTestAnnotationValues(governorPhysicalTypeMetadata);
    // Get JSON controller target class
    final JavaType jsonController = annotationValues.getTargetClass();
    // Get the controller managed entity
    ClassOrInterfaceTypeDetails controllerCid = getTypeLocationService().getTypeDetails(jsonController);
    AnnotationMetadata rooControllerAnnotation = controllerCid.getAnnotation(RooJavaType.ROO_CONTROLLER);
    Validate.notNull(rooControllerAnnotation, "The target class must be annotated with @RooController.");
    Validate.notNull(rooControllerAnnotation.getAttribute("entity"), "The @RooController must have an 'entity' attribute, targeting managed entity.");
    final JavaType managedEntity = (JavaType) rooControllerAnnotation.getAttribute("entity").getValue();
    // Get the entity factory of managed entity
    final JavaType entityFactory = getJpaEntityFactoryLocator().getFirstJpaEntityFactoryForEntity(managedEntity);
    // Get the service related to managed entity
    final JavaType entityService = getServiceLocator().getFirstService(managedEntity).getType();
    return new JsonControllerIntegrationTestMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, jsonController, managedEntity, entityFactory, entityService);
}
Also used : JavaType(org.springframework.roo.model.JavaType) RooJavaType(org.springframework.roo.model.RooJavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 75 with ClassOrInterfaceTypeDetails

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

the class ThymeleafMVCViewResponseService method annotate.

/**
 * This operation annotates a controller with the THYMELEAF annotation
 *
 * @param controller JavaType with the controller to be annotated.
 */
@Override
public void annotate(JavaType controller) {
    Validate.notNull(controller, "ERROR: You must provide a valid controller");
    ClassOrInterfaceTypeDetails controllerDetails = getTypeLocationService().getTypeDetails(controller);
    // Check if provided controller exists on current project
    Validate.notNull(controllerDetails, "ERROR: You must provide an existing controller");
    // Check if provided controller has been annotated with @RooController
    Validate.notNull(controllerDetails.getAnnotation(RooJavaType.ROO_CONTROLLER), "ERROR: You must provide a controller annotated with @RooController");
    // Add Thymeleaf annotation
    ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(controllerDetails);
    typeBuilder.addAnnotation(new AnnotationMetadataBuilder(getAnnotation()));
    // Write changes on provided controller
    getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
}
Also used : ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)232 JavaType (org.springframework.roo.model.JavaType)114 ArrayList (java.util.ArrayList)87 RooJavaType (org.springframework.roo.model.RooJavaType)86 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)52 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)43 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)42 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)39 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)36 JpaJavaType (org.springframework.roo.model.JpaJavaType)34 SpringJavaType (org.springframework.roo.model.SpringJavaType)28 CliOptionAutocompleteIndicator (org.springframework.roo.shell.CliOptionAutocompleteIndicator)24 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)23 JdkJavaType (org.springframework.roo.model.JdkJavaType)22 Test (org.junit.Test)20 File (java.io.File)19 List (java.util.List)19 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)17