Search in sources :

Example 21 with AnnotationMetadata

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

the class AbstractViewGenerationService method addMenu.

@Override
public void addMenu(String moduleName, ViewContext<T> ctx) {
    Map<String, MenuEntry> mapMenuEntries = new TreeMap<String, MenuEntry>();
    Set<ClassOrInterfaceTypeDetails> existingControllers = new HashSet<ClassOrInterfaceTypeDetails>();
    existingControllers.addAll(getControllerLocator().getControllers(null, ControllerType.COLLECTION, getType()));
    existingControllers.addAll(getControllerLocator().getControllers(null, ControllerType.SEARCH, getType()));
    Iterator<ClassOrInterfaceTypeDetails> it = existingControllers.iterator();
    while (it.hasNext()) {
        // Getting controller and its information
        ClassOrInterfaceTypeDetails controller = it.next();
        ControllerAnnotationValues controllerValues = new ControllerAnnotationValues(controller);
        JavaType entity = controllerValues.getEntity();
        // Obtain the entityMetadata
        JpaEntityMetadata entityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(getTypeLocationService().getTypeDetails(entity)));
        boolean isReadOnly = false;
        if (entityMetadata != null && entityMetadata.isReadOnly()) {
            isReadOnly = true;
        }
        // Get finders for each controller
        AnnotationMetadata controllerSearchAnnotation = controller.getAnnotation(RooJavaType.ROO_SEARCH);
        Map<String, String> finderNamesAndPaths = new HashMap<String, String>();
        if (controllerSearchAnnotation != null && controllerSearchAnnotation.getAttribute("finders") != null) {
            List<?> finders = (List<?>) controllerSearchAnnotation.getAttribute("finders").getValue();
            Iterator<?> iterator = finders.iterator();
            while (iterator.hasNext()) {
                StringAttributeValue attributeValue = (StringAttributeValue) iterator.next();
                String finderName = attributeValue.getValue();
                // Build URL path to get data
                String finderPath = "";
                if (StringUtils.startsWith(finderName, "count")) {
                    finderPath = StringUtils.removeStart(finderName, "count");
                } else if (StringUtils.startsWith(finderName, "find")) {
                    finderPath = StringUtils.removeStart(finderName, "find");
                } else if (StringUtils.startsWith(finderName, "query")) {
                    finderPath = StringUtils.removeStart(finderName, "query");
                } else if (StringUtils.startsWith(finderName, "read")) {
                    finderPath = StringUtils.removeStart(finderName, "read");
                } else {
                    finderPath = finderName;
                }
                finderPath = String.format("search/%s/search-form", StringUtils.uncapitalize(finderPath));
                finderNamesAndPaths.put(finderName, finderPath);
            }
        }
        // Getting pathPrefix
        String pathPrefix = StringUtils.defaultString(controllerValues.getPathPrefix(), "");
        // Generate path
        String path = getControllerOperations().getBaseUrlForController(controller);
        if (controllerSearchAnnotation != null) {
            // Prevent that /search will be included in every menu entry
            // The /search is already included in the step before only for
            // finder entries
            path = path.replace("/search", "");
        }
        // Create new menuEntry element for controller
        String keyThatRepresentsEntry = pathPrefix.concat(entity.getSimpleTypeName());
        // Add new menu entry to menuEntries list if doesn't exist
        MenuEntry menuEntry = null;
        if (controllerValues.getType() == ControllerType.SEARCH) {
            // Only add finder entry
            menuEntry = createMenuEntry(entity.getSimpleTypeName(), path, pathPrefix, FieldItem.buildLabel(entity.getSimpleTypeName(), ""), FieldItem.buildLabel(entity.getSimpleTypeName(), "plural"), finderNamesAndPaths, false, false, false);
        } else {
            // Add default menu entries
            menuEntry = createMenuEntry(entity.getSimpleTypeName(), path, pathPrefix, FieldItem.buildLabel(entity.getSimpleTypeName(), ""), FieldItem.buildLabel(entity.getSimpleTypeName(), "plural"), finderNamesAndPaths, false, true, isReadOnly);
        }
        if (mapMenuEntries.containsKey(keyThatRepresentsEntry)) {
            MenuEntry menuEntryInserted = mapMenuEntries.get(keyThatRepresentsEntry);
            if (menuEntryInserted.getFinderNamesAndPaths().isEmpty() && !menuEntry.getFinderNamesAndPaths().isEmpty()) {
                menuEntryInserted.setFinderNamesAndPaths(menuEntry.getFinderNamesAndPaths());
            }
            // Check the 'addDefaultEntries' attribute and add it if needed
            if (!menuEntryInserted.isAddDefaultEntries() && menuEntry.isAddDefaultEntries()) {
                menuEntryInserted.setAddDefaultEntries(menuEntry.isAddDefaultEntries());
            }
        } else {
            mapMenuEntries.put(keyThatRepresentsEntry, menuEntry);
        }
    }
    // Also, check web flow views in the views folder
    String viewsFolder = getViewsFolder(moduleName);
    List<String> webFlowViews = getWebFlowViewsFromDir(viewsFolder, null);
    // After obtain the webFlow views, add them to the menu
    for (String webFlowView : webFlowViews) {
        // Creating the menu entry
        MenuEntry menuEntry = createMenuEntry(webFlowView, webFlowView, "", FieldItem.buildLabel(webFlowView, ""), FieldItem.buildLabel(webFlowView, "plural"), null, true, false, false);
        mapMenuEntries.put(webFlowView, menuEntry);
    }
    // First of all, generate a list of MenuEntries based on existing
    // controllers
    List<MenuEntry> menuEntries = new ArrayList<MenuEntry>(mapMenuEntries.values());
    // Generate ids to search when merge new and existing doc
    List<String> requiredIds = new ArrayList<String>();
    for (MenuEntry entry : menuEntries) {
        requiredIds.add(entry.getPathPrefix().concat(entry.getEntityName()).concat("Entry"));
    }
    // Process elements to generate
    DOC newDoc = null;
    // Getting new viewName
    String viewName = getFragmentsFolder(moduleName).concat("/menu").concat(getViewsExtension());
    // Check if new view to generate exists or not
    if (existsFile(viewName)) {
        DOC existingDoc = loadExistingDoc(viewName);
        if (!isUserManagedDocument(existingDoc)) {
            newDoc = mergeMenu("fragments/menu", existingDoc, ctx, menuEntries);
        }
    } else {
        ctx.addExtraParameter("menuEntries", menuEntries);
        newDoc = process("fragments/menu", ctx);
    }
    // Write newDoc on disk
    writeDoc(newDoc, viewName);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) MenuEntry(org.springframework.roo.addon.web.mvc.views.components.MenuEntry) ControllerAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues) List(java.util.List) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) HashSet(java.util.HashSet)

Example 22 with AnnotationMetadata

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

the class AbstractViewGenerationService method getReferenceField.

/**
 * This method obtains all necessary configuration to be able to work with
 * reference fields. Complete provided FieldItem with extra fields. If some
 * extra configuration is not available, returns false to prevent that this
 * field will be added. If everything is ok, returns true to add this field to
 * generated view.
 *
 * @param fieldItem
 * @param typeDetails
 * @param allControllers
 * @return
 */
protected boolean getReferenceField(FieldItem fieldItem, ClassOrInterfaceTypeDetails typeDetails, ViewContext<T> viewContext) {
    // Set type as REFERENCE
    fieldItem.setType(FieldTypes.REFERENCE.toString());
    // Add referencedEntity to configuration
    fieldItem.addConfigurationElement("referencedEntity", typeDetails.getType().getSimpleTypeName());
    // Add the controllerPath related to the referencedEntity to
    // configuration
    final String controllerPrefix = viewContext.getViewMetadata().getControllerMetadata().getAnnotationValues().getPathPrefix();
    Collection<ClassOrInterfaceTypeDetails> allControllers = getControllerLocator().getControllers(typeDetails.getType(), ControllerType.COLLECTION, getType());
    Iterator<ClassOrInterfaceTypeDetails> it = allControllers.iterator();
    String referencedPath = "";
    ClassOrInterfaceTypeDetails referencedController = null;
    while (it.hasNext()) {
        ClassOrInterfaceTypeDetails controller = it.next();
        ControllerAnnotationValues values = new ControllerAnnotationValues(controller);
        AnnotationMetadata controllerAnnotation = controller.getAnnotation(RooJavaType.ROO_CONTROLLER);
        AnnotationAttributeValue<String> prefixAttr = controllerAnnotation.getAttribute("pathPrefix");
        if (StringUtils.equals(values.getPathPrefix(), controllerPrefix)) {
            // Get path
            referencedPath = getControllerOperations().getBaseUrlForController(controller);
            referencedController = controller;
            // Get target entity metadata to get identifier field
            ClassOrInterfaceTypeDetails relatedEntityCid = getTypeLocationService().getTypeDetails(typeDetails.getType());
            JpaEntityMetadata relatedEntityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(relatedEntityCid));
            fieldItem.addConfigurationElement("identifierField", relatedEntityMetadata.getCurrentIndentifierField().getFieldName().getSymbolName());
            break;
        }
    }
    if (referencedController == null) {
        return false;
    }
    fieldItem.addConfigurationElement("referencedPath", referencedPath);
    fieldItem.addConfigurationElement("referencedController", referencedController);
    return true;
}
Also used : ControllerAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 23 with AnnotationMetadata

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

the class AbstractViewGenerationService method addI18nLabelsFromEntityFormatExpressions.

/**
 * Locates `@EntityFormat` annotations at type-level and relation fields and
 * adds a new message to provided map if the annotation has the `message`
 * attribute.
 *
 * @param properties the Map<String, String> to be added to message bundles.
 * @param details the ClassOrInterfaceTypeDetails to check.
 */
private void addI18nLabelsFromEntityFormatExpressions(final Map<String, String> properties, ClassOrInterfaceTypeDetails details) {
    // Get all projection member details
    if (details != null) {
        MemberDetails memberDetails = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), details);
        // Search for @EntityFormat at type-level
        AnnotationMetadata entityFormatTypeLevelAnnotation = memberDetails.getAnnotation(SpringletsJavaType.SPRINGLETS_ENTITY_FORMAT);
        if (entityFormatTypeLevelAnnotation != null) {
            AnnotationAttributeValue<Object> messageAttribute = entityFormatTypeLevelAnnotation.getAttribute("message");
            if (messageAttribute != null) {
                properties.put((String) messageAttribute.getValue(), "TO BE MODIFIED BY DEVELOPER");
            }
        }
        // Search for @EntityFormat at relation fields
        for (FieldMetadata field : memberDetails.getFields()) {
            AnnotationMetadata entityFormatAnnotation = field.getAnnotation(SpringletsJavaType.SPRINGLETS_ENTITY_FORMAT);
            if (entityFormatAnnotation != null) {
                AnnotationAttributeValue<Object> messageAttribute = entityFormatAnnotation.getAttribute("message");
                if (messageAttribute != null) {
                    properties.put((String) messageAttribute.getValue(), "TO BE MODIFIED BY DEVELOPER");
                }
            }
        }
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 24 with AnnotationMetadata

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

the class ControllerMetadataProviderImpl method getEntityFromRelationField.

/**
 * Get the entity that represents a relation field
 *
 * @param relationField Array that represents the relation field.
 * @param entity Current entity to search the corresponding field
 * @param level Current array level to search
 * @return
 */
private JavaType getEntityFromRelationField(String[] relationField, JavaType entity, int level) {
    JavaType entityTypeToCalculate = null;
    MemberDetails memberDetails = getMemberDetails(entity);
    List<FieldMetadata> fields = memberDetails.getFields();
    for (FieldMetadata entityField : fields) {
        if (entityField.getFieldName().getSymbolName().equals(relationField[level])) {
            AnnotationMetadata oneToManyAnnotation = entityField.getAnnotation(JpaJavaType.ONE_TO_MANY);
            if (oneToManyAnnotation != null && (entityField.getFieldType().getFullyQualifiedTypeName().equals(JavaType.LIST.getFullyQualifiedTypeName()) || entityField.getFieldType().getFullyQualifiedTypeName().equals(JavaType.SET.getFullyQualifiedTypeName()))) {
                level++;
                if (relationField.length > level) {
                    entityTypeToCalculate = getEntityFromRelationField(relationField, entityField.getFieldType().getParameters().get(0), level);
                } else {
                    entityTypeToCalculate = entityField.getFieldType().getParameters().get(0);
                }
                break;
            }
        }
    }
    return entityTypeToCalculate;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 25 with AnnotationMetadata

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

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