Search in sources :

Example 1 with MenuEntry

use of org.springframework.roo.addon.web.mvc.views.components.MenuEntry 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 2 with MenuEntry

use of org.springframework.roo.addon.web.mvc.views.components.MenuEntry in project spring-roo by spring-projects.

the class ThymeleafViewGeneratorServiceImpl method mergeMenu.

@Override
public Document mergeMenu(String templateName, Document loadExistingDoc, ViewContext<ThymeleafMetadata> ctx, List<MenuEntry> menuEntries) {
    for (MenuEntry menuEntry : menuEntries) {
        // Get field code if data-z attribute value is equals to
        // user-managed
        Element elementMenu = loadExistingDoc.getElementById(menuEntry.getId().concat("-entry"));
        if (elementMenu != null && elementMenu.hasAttr("data-z") && elementMenu.attr("data-z").equals("user-managed")) {
            menuEntry.setUserManaged(true);
            menuEntry.setCodeManaged(elementMenu.outerHtml());
        }
    }
    ctx.addExtraParameter("userManagedComponents", mergeStructure(loadExistingDoc));
    ctx.addExtraParameter("menuEntries", menuEntries);
    Document newDoc = process(templateName, ctx);
    return newDoc;
}
Also used : MenuEntry(org.springframework.roo.addon.web.mvc.views.components.MenuEntry) Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document)

Aggregations

MenuEntry (org.springframework.roo.addon.web.mvc.views.components.MenuEntry)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)1 ControllerAnnotationValues (org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues)1 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)1 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)1 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)1 JavaType (org.springframework.roo.model.JavaType)1 JdkJavaType (org.springframework.roo.model.JdkJavaType)1 JpaJavaType (org.springframework.roo.model.JpaJavaType)1 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)1 RooJavaType (org.springframework.roo.model.RooJavaType)1 SpringJavaType (org.springframework.roo.model.SpringJavaType)1