From de65cd9acd02a3eeb72f0e2699da20a3382ecfdb Mon Sep 17 00:00:00 2001 From: Julien Nicolas Date: Wed, 17 Oct 2018 07:33:12 +0000 Subject: [PATCH] Improved: Change the Menu extends-resource management to allow structure changes by themes (OFBIZ-10600) Thanks: Nicolas MALIN, Leila MEKIKA and Gil PORTENSEIGNE git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1844068 13f79535-47bb-0310-9956-ffa450edef68 --- framework/common/widget/CommonMenus.xml | 19 ++++++------- .../ofbiz/widget/model/MenuFactory.java | 28 ++++++++++++------- .../ofbiz/widget/model/ModelFormField.java | 3 +- .../apache/ofbiz/widget/model/ModelMenu.java | 23 +++++++++------ .../ofbiz/widget/model/ModelScreenWidget.java | 3 +- .../apache/ofbiz/widget/model/ModelTheme.java | 24 ++++++++++++++++ themes/bluelight/template/AppBarOpen.ftl | 2 +- themes/common-theme/widget/Theme.xml | 8 ++++++ themes/tomahawk/template/AppBarClose.ftl | 2 +- themes/tomahawk/template/AppBarOpen.ftl | 2 +- 10 files changed, 79 insertions(+), 35 deletions(-) diff --git a/framework/common/widget/CommonMenus.xml b/framework/common/widget/CommonMenus.xml index bcb38682b7..b48f86746f 100644 --- a/framework/common/widget/CommonMenus.xml +++ b/framework/common/widget/CommonMenus.xml @@ -19,18 +19,15 @@ under the License. --> - - - - - - + - + - + - + diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MenuFactory.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MenuFactory.java index f98d258494..3f4b6c52d1 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MenuFactory.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MenuFactory.java @@ -32,6 +32,7 @@ import org.apache.ofbiz.base.util.UtilHttp; import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.base.util.UtilXml; import org.apache.ofbiz.base.util.cache.UtilCache; +import org.apache.ofbiz.widget.renderer.VisualTheme; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; @@ -50,7 +51,13 @@ public class MenuFactory { public static ModelMenu getMenuFromWebappContext(String resourceName, String menuName, HttpServletRequest request) throws IOException, SAXException, ParserConfigurationException { String webappName = UtilHttp.getApplicationName(request); - String cacheKey = webappName + "::" + resourceName; + VisualTheme visualTheme = ThemeFactory.resolveVisualTheme(request); + String location = webappName + "::" + resourceName; + String cacheKey = location; + + if (UtilValidate.isNotEmpty(visualTheme)) { + cacheKey += "::" + visualTheme.getVisualThemeId(); + } Map modelMenuMap = menuWebappCache.get(cacheKey); if (modelMenuMap == null) { @@ -58,7 +65,7 @@ public class MenuFactory { URL menuFileUrl = servletContext.getResource(resourceName); Document menuFileDoc = UtilXml.readXmlDocument(menuFileUrl, true, true); - modelMenuMap = readMenuDocument(menuFileDoc, cacheKey); + modelMenuMap = readMenuDocument(menuFileDoc, location, visualTheme); menuWebappCache.putIfAbsent(cacheKey, modelMenuMap); modelMenuMap = menuWebappCache.get(cacheKey); } @@ -74,7 +81,7 @@ public class MenuFactory { return modelMenu; } - public static Map readMenuDocument(Document menuFileDoc, String menuLocation) { + public static Map readMenuDocument(Document menuFileDoc, String menuLocation, VisualTheme visualTheme) { Map modelMenuMap = new HashMap<>(); if (menuFileDoc != null) { // read document and construct ModelMenu for each menu element @@ -83,21 +90,22 @@ public class MenuFactory { rootElement = UtilXml.firstChildElement(rootElement, "menus"); } for (Element menuElement: UtilXml.childElementList(rootElement, "menu")){ - ModelMenu modelMenu = new ModelMenu(menuElement, menuLocation); + ModelMenu modelMenu = new ModelMenu(menuElement, menuLocation, visualTheme); modelMenuMap.put(modelMenu.getName(), modelMenu); } } return modelMenuMap; } - public static ModelMenu getMenuFromLocation(String resourceName, String menuName) throws IOException, SAXException, ParserConfigurationException { - Map modelMenuMap = menuLocationCache.get(resourceName); + public static ModelMenu getMenuFromLocation(String resourceName, String menuName, VisualTheme visualTheme) throws IOException, SAXException, ParserConfigurationException { + String keyName = resourceName + "::" + visualTheme.getVisualThemeId(); + Map modelMenuMap = menuLocationCache.get(keyName); if (modelMenuMap == null) { URL menuFileUrl = FlexibleLocation.resolveLocation(resourceName); Document menuFileDoc = UtilXml.readXmlDocument(menuFileUrl, true, true); - modelMenuMap = readMenuDocument(menuFileDoc, resourceName); - menuLocationCache.putIfAbsent(resourceName, modelMenuMap); - modelMenuMap = menuLocationCache.get(resourceName); + modelMenuMap = readMenuDocument(menuFileDoc, resourceName, visualTheme); + menuLocationCache.putIfAbsent(keyName, modelMenuMap); + modelMenuMap = menuLocationCache.get(keyName); } if (UtilValidate.isEmpty(modelMenuMap)) { @@ -110,4 +118,4 @@ public class MenuFactory { } return modelMenu; } -} +} \ No newline at end of file diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java index 73f3aeb1b1..fbb3d2497d 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java @@ -75,6 +75,7 @@ import org.apache.ofbiz.widget.renderer.FormRenderer; import org.apache.ofbiz.widget.renderer.FormStringRenderer; import org.apache.ofbiz.widget.renderer.MenuStringRenderer; import org.apache.ofbiz.widget.renderer.ScreenRenderer; +import org.apache.ofbiz.widget.renderer.VisualTheme; import org.codehaus.groovy.control.CompilationFailedException; import org.w3c.dom.Element; @@ -3267,7 +3268,7 @@ public class ModelFormField { String location = this.getMenuLocation(context); ModelMenu modelMenu = null; try { - modelMenu = MenuFactory.getMenuFromLocation(location, name); + modelMenu = MenuFactory.getMenuFromLocation(location, name, (VisualTheme) context.get("visualTheme")); } catch (Exception e) { String errMsg = "Error rendering menu named [" + name + "] at location [" + location + "]: "; Debug.logError(e, errMsg, module); diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelMenu.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelMenu.java index 7eec1aa379..e9b1122248 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelMenu.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelMenu.java @@ -31,6 +31,7 @@ import org.apache.ofbiz.base.util.UtilXml; import org.apache.ofbiz.base.util.collections.FlexibleMapAccessor; import org.apache.ofbiz.base.util.string.FlexibleStringExpander; import org.apache.ofbiz.widget.renderer.MenuStringRenderer; +import org.apache.ofbiz.widget.renderer.VisualTheme; import org.w3c.dom.Element; /** @@ -102,7 +103,7 @@ public class ModelMenu extends ModelWidget { private final String type; /** XML Constructor */ - public ModelMenu(Element menuElement, String menuLocation) { + public ModelMenu(Element menuElement, String menuLocation, VisualTheme visualTheme) { super(menuElement); ArrayList actions = new ArrayList<>(); String defaultAlign = ""; @@ -139,22 +140,26 @@ public class ModelMenu extends ModelWidget { if (!parentMenu.isEmpty()) { if (!parentResource.isEmpty()) { try { - parent = MenuFactory.getMenuFromLocation(parentResource, parentMenu); + FlexibleStringExpander parentResourceExp = FlexibleStringExpander.getInstance(parentResource); + Map visualRessources = visualTheme.getModelTheme().getModelCommonMenus(); + parentResource = parentResourceExp.expandString(visualRessources); + parent = MenuFactory.getMenuFromLocation(parentResource, parentMenu, visualTheme); } catch (Exception e) { - Debug.logError(e, "Failed to load parent menu definition '" + parentMenu + "' at resource '" + parentResource - + "'", module); + Debug.logError(e, "Failed to load parent menu definition '" + parentMenu + "' at resource '" + parentResource, module); } } else { parentResource = menuLocation; // try to find a menu definition in the same file Element rootElement = menuElement.getOwnerDocument().getDocumentElement(); List menuElements = UtilXml.childElementList(rootElement, "menu"); - for (Element menuElementEntry : menuElements) { - if (menuElementEntry.getAttribute("name").equals(parentMenu)) { - parent = new ModelMenu(menuElementEntry, parentResource); - break; - } + Element parentEntry = menuElements.stream() + .filter(elem -> elem.getAttribute("name").equals(parentMenu)) + .findFirst() + .orElse(null); + if (parentEntry != null) { + parent = new ModelMenu(parentEntry, parentResource, visualTheme); } + if (parent == null) { Debug.logError("Failed to find parent menu definition '" + parentMenu + "' in same document.", module); } diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java index feb1220404..99e1746fcf 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java @@ -55,6 +55,7 @@ import org.apache.ofbiz.widget.renderer.MenuStringRenderer; import org.apache.ofbiz.widget.renderer.ScreenRenderer; import org.apache.ofbiz.widget.renderer.ScreenStringRenderer; import org.apache.ofbiz.widget.renderer.TreeStringRenderer; +import org.apache.ofbiz.widget.renderer.VisualTheme; import org.w3c.dom.Element; import org.xml.sax.SAXException; @@ -1599,7 +1600,7 @@ public abstract class ModelScreenWidget extends ModelWidget { String location = this.getLocation(context); ModelMenu modelMenu = null; try { - modelMenu = MenuFactory.getMenuFromLocation(location, name); + modelMenu = MenuFactory.getMenuFromLocation(location, name, (VisualTheme) context.get("visualTheme")); } catch (Exception e) { String errMsg = "Error rendering included menu named [" + name + "] at location [" + location + "]: "; Debug.logError(e, errMsg, module); diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelTheme.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelTheme.java index 0309b10835..ae1bf13f19 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelTheme.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelTheme.java @@ -74,6 +74,7 @@ public class ModelTheme implements Serializable { //template rendering private final Map modelTemplateMap; private final Map modelCommonScreensMap; + private final Map modelCommonMenusMap; /** * Only constructor to initialize a modelTheme from xml definition @@ -86,6 +87,7 @@ public class ModelTheme implements Serializable { Map initThemePropertiesMap = new HashMap<>(); Map initModelTemplateMap = new HashMap<>(); Map initModelCommonScreensMap = new HashMap<>(); + Map initModelCommonMenusMap = new HashMap<>(); // first resolve value from the origin theme Element originThemeElement = UtilXml.firstChildElement(themeElement, "extends"); @@ -121,6 +123,9 @@ public class ModelTheme implements Serializable { if (originTheme.modelCommonScreensMap != null) { initModelCommonScreensMap = UtilMisc.makeMapWritable(originTheme.modelCommonScreensMap); } + if (originTheme.modelCommonMenusMap != null) { + initModelCommonMenusMap = UtilMisc.makeMapWritable(originTheme.modelCommonMenusMap); + } } //second collect value from XML and surcharge @@ -167,6 +172,23 @@ public class ModelTheme implements Serializable { } } break; + case "common-menus": + for (Element menuPurpose : UtilXml.childElementList(childElement)) { + String defaultLocation = menuPurpose.getAttribute("default-location"); + for (Element menu : UtilXml.childElementList(menuPurpose)) { + String name = menu.getAttribute("name"); + String location = menu.getAttribute("location"); + if (UtilValidate.isEmpty(location)) { + location = defaultLocation; + } + if (UtilValidate.isEmpty(location)) { + Debug.logWarning("We can resolve the menu location " + name + " in the theme " + this.name + " so no added it", module); + continue; + } + initModelCommonMenusMap.put(name, location); + } + } + break; } } @@ -186,6 +208,7 @@ public class ModelTheme implements Serializable { this.themePropertiesMap = Collections.unmodifiableMap(initThemePropertiesMap); this.modelTemplateMap = Collections.unmodifiableMap(initModelTemplateMap); this.modelCommonScreensMap = Collections.unmodifiableMap(initModelCommonScreensMap); + this.modelCommonMenusMap = Collections.unmodifiableMap(initModelCommonMenusMap); } public String getName() { @@ -370,6 +393,7 @@ public class ModelTheme implements Serializable { public Map getModelCommonScreens() { return modelCommonScreensMap; } + public Map getModelCommonMenus() { return modelCommonMenusMap; } /** * the ModelTemplate class, manage the complexity of macro library definition and the rendering technology diff --git a/themes/bluelight/template/AppBarOpen.ftl b/themes/bluelight/template/AppBarOpen.ftl index f9c049d290..fdd2a7be21 100644 --- a/themes/bluelight/template/AppBarOpen.ftl +++ b/themes/bluelight/template/AppBarOpen.ftl @@ -23,7 +23,7 @@ under the License. <#assign displayApps = Static["org.apache.ofbiz.webapp.control.LoginWorker"].getAppBarWebInfos(security, userLogin, ofbizServerName, "main")> <#assign displaySecondaryApps = Static["org.apache.ofbiz.webapp.control.LoginWorker"].getAppBarWebInfos(security, userLogin, ofbizServerName, "secondary")> -<#assign appModelMenu = Static["org.apache.ofbiz.widget.model.MenuFactory"].getMenuFromLocation(applicationMenuLocation,applicationMenuName)> +<#assign appModelMenu = Static["org.apache.ofbiz.widget.model.MenuFactory"].getMenuFromLocation(applicationMenuLocation,applicationMenuName,visualTheme)> <#if appModelMenu.getModelMenuItemByName(headerItem)??> <#if headerItem!="main"> <#assign show_last_menu = true> diff --git a/themes/common-theme/widget/Theme.xml b/themes/common-theme/widget/Theme.xml index 1ac0e2f2cc..9025383c86 100644 --- a/themes/common-theme/widget/Theme.xml +++ b/themes/common-theme/widget/Theme.xml @@ -166,4 +166,12 @@ under the License. + + + + + + + + diff --git a/themes/tomahawk/template/AppBarClose.ftl b/themes/tomahawk/template/AppBarClose.ftl index d71b7bf25f..d36bf2ab88 100644 --- a/themes/tomahawk/template/AppBarClose.ftl +++ b/themes/tomahawk/template/AppBarClose.ftl @@ -16,7 +16,7 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> -<#assign appModelMenu = Static["org.apache.ofbiz.widget.model.MenuFactory"].getMenuFromLocation(applicationMenuLocation,applicationMenuName)> +<#assign appModelMenu = Static["org.apache.ofbiz.widget.model.MenuFactory"].getMenuFromLocation(applicationMenuLocation,applicationMenuName,visualTheme)> <#if person?has_content> <#assign userName = (person.firstName!) + " " + (person.middleName!) + " " + person.lastName!> <#elseif partyGroup?has_content> diff --git a/themes/tomahawk/template/AppBarOpen.ftl b/themes/tomahawk/template/AppBarOpen.ftl index f71579c63e..e89250a908 100644 --- a/themes/tomahawk/template/AppBarOpen.ftl +++ b/themes/tomahawk/template/AppBarOpen.ftl @@ -24,7 +24,7 @@ under the License. <#assign displayApps = Static["org.apache.ofbiz.webapp.control.LoginWorker"].getAppBarWebInfos(security, userLogin, ofbizServerName, "main")> <#assign displaySecondaryApps = Static["org.apache.ofbiz.webapp.control.LoginWorker"].getAppBarWebInfos(security, userLogin, ofbizServerName, "secondary")> -<#assign appModelMenu = Static["org.apache.ofbiz.widget.model.MenuFactory"].getMenuFromLocation(applicationMenuLocation,applicationMenuName)> +<#assign appModelMenu = Static["org.apache.ofbiz.widget.model.MenuFactory"].getMenuFromLocation(applicationMenuLocation,applicationMenuName,visualTheme)> <#if appModelMenu.getModelMenuItemByName(headerItem)??> <#if headerItem!="main"> <#assign show_last_menu = true>