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);
}
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;
}
Aggregations