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
This commit is contained in:
Julien Nicolas
2018-10-17 07:33:12 +00:00
parent 161172b3ff
commit de65cd9acd
10 changed files with 79 additions and 35 deletions
+8 -11
View File
@@ -19,18 +19,15 @@ under the License.
-->
<menus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ofbiz.apache.org/Widget-Menu" xsi:schemaLocation="http://ofbiz.apache.org/Widget-Menu http://ofbiz.apache.org/dtds/widget-menu.xsd">
<menu name="CommonAppBarMenu" default-menu-item-name="main" id="app-navigation" type="simple" title="${applicationTitle}&amp;nbsp;"
default-selected-style="selected" menu-container-style="button-bar tab-bar" selected-menuitem-context-field-name="headerItem">
<menu-item name="main" title="${uiLabelMap.CommonMain}">
<condition><not><if-empty field="userLogin"/></not></condition>
<link target="main" link-type="anchor"/>
</menu-item>
</menu>
<menu name="CommonAppBarMenu" extends="CommonAppBarMenu"
extends-resource="${groovy:CommonAppBarMenu?CommonAppBarMenu:'component://common-theme/widget/CommonMenus.xml'}"/>
<menu name="CommonTabBarMenu" selected-menuitem-context-field-name="tabButtonItem" type="simple"
menu-container-style="button-bar tab-bar" default-selected-style="selected"/>
<menu name="CommonTabBarMenu" extends="CommonTabBarMenu"
extends-resource="${groovy:CommonTabBarMenu?CommonTabBarMenu:'component://common-theme/widget/CommonMenus.xml'}"/>
<menu name="CommonButtonBarMenu" type="simple" menu-container-style="button-bar" default-widget-style="buttontext"/>
<menu name="CommonButtonBarMenu" extends="CommonButtonBarMenu"
extends-resource="${groovy:CommonButtonBarMenu?CommonButtonBarMenu:'component://common-theme/widget/CommonMenus.xml'}"/>
<menu name="CommonInlineBarMenu" type="simple" menu-container-style="in-line-bar" default-widget-style="linktext"/>
<menu name="CommonInlineBarMenu" extends="CommonInlineBarMenu"
extends-resource="${groovy:CommonInlineBarMenu?CommonInlineBarMenu:'component://common-theme/widget/CommonMenus.xml'}"/>
</menus>
@@ -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<String, ModelMenu> 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<String, ModelMenu> readMenuDocument(Document menuFileDoc, String menuLocation) {
public static Map<String, ModelMenu> readMenuDocument(Document menuFileDoc, String menuLocation, VisualTheme visualTheme) {
Map<String, ModelMenu> 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<String, ModelMenu> modelMenuMap = menuLocationCache.get(resourceName);
public static ModelMenu getMenuFromLocation(String resourceName, String menuName, VisualTheme visualTheme) throws IOException, SAXException, ParserConfigurationException {
String keyName = resourceName + "::" + visualTheme.getVisualThemeId();
Map<String, ModelMenu> 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;
}
}
}
@@ -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);
@@ -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<ModelAction> 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<String, String> 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<? extends Element> 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);
}
@@ -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);
@@ -74,6 +74,7 @@ public class ModelTheme implements Serializable {
//template rendering
private final Map<String, ModelTemplate> modelTemplateMap;
private final Map<String, String> modelCommonScreensMap;
private final Map<String, String> modelCommonMenusMap;
/**
* Only constructor to initialize a modelTheme from xml definition
@@ -86,6 +87,7 @@ public class ModelTheme implements Serializable {
Map<String, Object> initThemePropertiesMap = new HashMap<>();
Map<String, ModelTemplate> initModelTemplateMap = new HashMap<>();
Map<String, String> initModelCommonScreensMap = new HashMap<>();
Map<String, String> 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<String,String> getModelCommonScreens() {
return modelCommonScreensMap;
}
public Map<String,String> getModelCommonMenus() { return modelCommonMenusMap; }
/**
* the ModelTemplate class, manage the complexity of macro library definition and the rendering technology
+1 -1
View File
@@ -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>
+8
View File
@@ -166,4 +166,12 @@ under the License.
<screen name="ShowHelp" location="component://common-theme/widget/HelpScreens.xml"/>
</generic-screen>
</common-screens>
<common-menus><!--list all common menu to each application that each theme can be surcharge or not and use the menu present on common theme-->
<root-decorator default-location="component://common-theme/widget/CommonMenus.xml">
<menu name="CommonAppBarMenu"/>
<menu name="CommonTabBarMenu"/>
<menu name="CommonButtonBarMenu"/>
<menu name="CommonInlineBarMenu"/>
</root-decorator>
</common-menus>
</theme>
+1 -1
View File
@@ -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>
+1 -1
View File
@@ -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>