blob: a624c370d7a226ca70d6cafed267c0976fd8dcb2 [file] [log] [blame]
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.ofbiz.widget.menu;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javolution.util.FastList;
import javolution.util.FastMap;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.ofbiz.base.util.BshUtil;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.GroovyUtil;
import org.ofbiz.base.util.ObjectType;
import org.ofbiz.base.util.UtilFormatOut;
import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
import org.ofbiz.base.util.collections.ResourceBundleMapWrapper;
import org.ofbiz.base.util.string.FlexibleStringExpander;
import org.ofbiz.entity.finder.ByAndFinder;
import org.ofbiz.entity.finder.ByConditionFinder;
import org.ofbiz.entity.finder.PrimaryKeyFinder;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.ModelService;
import org.ofbiz.widget.WidgetWorker;
import org.w3c.dom.Element;
/**
* Widget Library - Screen model class
*/
public abstract class ModelMenuAction {
public static final String module = ModelMenuAction.class.getName();
protected ModelMenu modelMenu;
protected ModelMenuItem modelMenuItem;
public ModelMenuAction(ModelMenu modelMenu, Element actionElement) {
this.modelMenu = modelMenu;
if (Debug.verboseOn()) Debug.logVerbose("Reading Screen action with name: " + actionElement.getNodeName(), module);
}
public ModelMenuAction(ModelMenuItem modelMenuItem, Element actionElement) {
this.modelMenuItem = modelMenuItem;
this.modelMenu = modelMenuItem.getModelMenu();
if (Debug.verboseOn()) Debug.logVerbose("Reading Screen action with name: " + actionElement.getNodeName(), module);
}
public abstract void runAction(Map<String, Object> context);
public static List<ModelMenuAction> readSubActions(ModelMenuItem modelMenuItem, Element parentElement) {
return readSubActions(modelMenuItem.getModelMenu(), parentElement);
}
public static List<ModelMenuAction> readSubActions(ModelMenu modelMenu, Element parentElement) {
List<? extends Element> actionElementList = UtilXml.childElementList(parentElement);
ArrayList<ModelMenuAction> actions = new ArrayList<ModelMenuAction>(actionElementList.size());
for (Element actionElement : actionElementList) {
if ("set".equals(actionElement.getNodeName())) {
actions.add(new SetField(modelMenu, actionElement));
} else if ("property-map".equals(actionElement.getNodeName())) {
actions.add(new PropertyMap(modelMenu, actionElement));
} else if ("property-to-field".equals(actionElement.getNodeName())) {
actions.add(new PropertyToField(modelMenu, actionElement));
} else if ("script".equals(actionElement.getNodeName())) {
actions.add(new Script(modelMenu, actionElement));
} else if ("service".equals(actionElement.getNodeName())) {
actions.add(new Service(modelMenu, actionElement));
} else if ("entity-one".equals(actionElement.getNodeName())) {
actions.add(new EntityOne(modelMenu, actionElement));
} else if ("entity-and".equals(actionElement.getNodeName())) {
actions.add(new EntityAnd(modelMenu, actionElement));
} else if ("entity-condition".equals(actionElement.getNodeName())) {
actions.add(new EntityCondition(modelMenu, actionElement));
} else {
throw new IllegalArgumentException("Action element not supported with name: " + actionElement.getNodeName());
}
}
actions.trimToSize();
return actions;
}
public static void runSubActions(List<ModelMenuAction> actions, Map<String, Object> context) {
if (actions == null) return;
for (ModelMenuAction action : actions) {
if (Debug.verboseOn()) Debug.logVerbose("Running screen action " + action.getClass().getName(), module);
action.runAction(context);
}
}
public static class SetField extends ModelMenuAction {
protected FlexibleMapAccessor<Object> field;
protected FlexibleMapAccessor<Object> fromField;
protected FlexibleStringExpander valueExdr;
protected FlexibleStringExpander defaultExdr;
protected FlexibleStringExpander globalExdr;
protected String type;
protected String toScope;
protected String fromScope;
public SetField(ModelMenu modelMenu, Element setElement) {
super (modelMenu, setElement);
this.field = FlexibleMapAccessor.getInstance(setElement.getAttribute("field"));
this.fromField = FlexibleMapAccessor.getInstance(setElement.getAttribute("from-field"));
this.valueExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("value"));
this.defaultExdr = UtilValidate.isNotEmpty(setElement.getAttribute("default-value")) ? FlexibleStringExpander.getInstance(setElement.getAttribute("default-value")) : null;
this.globalExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("global"));
this.type = setElement.getAttribute("type");
this.toScope = setElement.getAttribute("to-scope");
this.fromScope = setElement.getAttribute("from-scope");
if (!this.fromField.isEmpty() && !this.valueExdr.isEmpty()) {
throw new IllegalArgumentException("Cannot specify a from-field [" + setElement.getAttribute("from-field") + "] and a value [" + setElement.getAttribute("value") + "] on the set action in a screen widget");
}
}
@Override
public void runAction(Map<String, Object> context) {
String globalStr = this.globalExdr.expandString(context);
// default to false
boolean global = "true".equals(globalStr);
Object newValue = null;
if (this.fromScope != null && this.fromScope.equals("user")) {
if (!this.fromField.isEmpty()) {
String originalName = this.fromField.getOriginalName();
String currentWidgetTrail = (String)context.get("_WIDGETTRAIL_");
String newKey = currentWidgetTrail + "|" + originalName;
HttpSession session = (HttpSession)context.get("session");
newValue = session.getAttribute(newKey);
if (Debug.verboseOn()) Debug.logVerbose("In user getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module);
} else if (!this.valueExdr.isEmpty()) {
newValue = this.valueExdr.expandString(context);
}
} else if (this.fromScope != null && this.fromScope.equals("application")) {
if (!this.fromField.isEmpty()) {
String originalName = this.fromField.getOriginalName();
String currentWidgetTrail = (String)context.get("_WIDGETTRAIL_");
String newKey = currentWidgetTrail + "|" + originalName;
ServletContext servletContext = (ServletContext)context.get("application");
newValue = servletContext.getAttribute(newKey);
if (Debug.verboseOn()) Debug.logVerbose("In application getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module);
} else if (!this.valueExdr.isEmpty()) {
newValue = this.valueExdr.expandString(context);
}
} else {
if (!this.fromField.isEmpty()) {
newValue = this.fromField.get(context);
if (Debug.verboseOn()) Debug.logVerbose("In screen getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module);
} else if (!this.valueExdr.isEmpty()) {
newValue = this.valueExdr.expandString(context);
}
}
// If newValue is still empty, use the default value
if (this.defaultExdr != null) {
if (ObjectType.isEmpty(newValue)) {
newValue = this.defaultExdr.expandString(context);
}
}
if (UtilValidate.isNotEmpty(this.type)) {
if ("NewMap".equals(this.type)) {
newValue = FastMap.newInstance();
} else if ("NewList".equals(this.type)) {
newValue = FastList.newInstance();
} else {
try {
newValue = ObjectType.simpleTypeConvert(newValue, this.type, null, (TimeZone) context.get("timeZone"), (Locale) context.get("locale"), true);
} catch (GeneralException e) {
String errMsg = "Could not convert field value for the field: [" + this.field.getOriginalName() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.toString();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
}
}
if (this.toScope != null && this.toScope.equals("user")) {
String originalName = this.field.getOriginalName();
String currentWidgetTrail = (String)context.get("_WIDGETTRAIL_");
String newKey = currentWidgetTrail + "|" + originalName;
HttpSession session = (HttpSession)context.get("session");
session.setAttribute(newKey, newValue);
if (Debug.verboseOn()) Debug.logVerbose("In user setting value for field from [" + this.field.getOriginalName() + "]: " + newValue, module);
} else if (this.toScope != null && this.toScope.equals("application")) {
String originalName = this.field.getOriginalName();
String currentWidgetTrail = (String)context.get("_WIDGETTRAIL_");
String newKey = currentWidgetTrail + "|" + originalName;
ServletContext servletContext = (ServletContext)context.get("application");
servletContext.setAttribute(newKey, newValue);
if (Debug.verboseOn()) Debug.logVerbose("In application setting value for field from [" + this.field.getOriginalName() + "]: " + newValue, module);
} else {
if (Debug.verboseOn()) Debug.logVerbose("In screen setting field [" + this.field.getOriginalName() + "] to value: " + newValue, module);
this.field.put(context, newValue);
}
if (global) {
Map<String, Object> globalCtx = UtilGenerics.checkMap(context.get("globalContext"));
if (globalCtx != null) {
this.field.put(globalCtx, newValue);
}
}
// this is a hack for backward compatibility with the JPublish page object
Map<String, Object> page = UtilGenerics.checkMap(context.get("page"));
if (page != null) {
this.field.put(page, newValue);
}
}
}
public static class PropertyMap extends ModelMenuAction {
protected FlexibleStringExpander resourceExdr;
protected FlexibleMapAccessor<ResourceBundleMapWrapper> mapNameAcsr;
protected FlexibleStringExpander globalExdr;
public PropertyMap(ModelMenu modelMenu, Element setElement) {
super (modelMenu, setElement);
this.resourceExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("resource"));
this.mapNameAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("map-name"));
this.globalExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("global"));
}
@Override
public void runAction(Map<String, Object> context) {
String globalStr = this.globalExdr.expandString(context);
// default to false
boolean global = "true".equals(globalStr);
Locale locale = (Locale) context.get("locale");
String resource = this.resourceExdr.expandString(context, locale);
ResourceBundleMapWrapper existingPropMap = this.mapNameAcsr.get(context);
if (existingPropMap == null) {
this.mapNameAcsr.put(context, UtilProperties.getResourceBundleMap(resource, locale, context));
} else {
try {
existingPropMap.addBottomResourceBundle(resource);
} catch (IllegalArgumentException e) {
// log the error, but don't let it kill everything just for a typo or bad char in an l10n file
Debug.logError(e, "Error adding resource bundle [" + resource + "]: " + e.toString(), module);
}
}
if (global) {
Map<String, Object> globalCtx = UtilGenerics.checkMap(context.get("globalContext"));
if (globalCtx != null) {
ResourceBundleMapWrapper globalExistingPropMap = this.mapNameAcsr.get(globalCtx);
if (globalExistingPropMap == null) {
this.mapNameAcsr.put(globalCtx, UtilProperties.getResourceBundleMap(resource, locale, context));
} else {
// is it the same object? if not add it in here too...
if (existingPropMap != globalExistingPropMap) {
try {
globalExistingPropMap.addBottomResourceBundle(resource);
} catch (IllegalArgumentException e) {
// log the error, but don't let it kill everything just for a typo or bad char in an l10n file
Debug.logError(e, "Error adding resource bundle [" + resource + "]: " + e.toString(), module);
}
}
}
}
}
}
}
public static class PropertyToField extends ModelMenuAction {
protected FlexibleStringExpander resourceExdr;
protected FlexibleStringExpander propertyExdr;
protected FlexibleMapAccessor<Object> fieldAcsr;
protected FlexibleStringExpander defaultExdr;
protected boolean noLocale;
protected FlexibleMapAccessor<List<? extends Object>> argListAcsr;
protected FlexibleStringExpander globalExdr;
public PropertyToField(ModelMenu modelMenu, Element setElement) {
super (modelMenu, setElement);
this.resourceExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("resource"));
this.propertyExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("property"));
this.fieldAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("field"));
this.defaultExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("default"));
noLocale = "true".equals(setElement.getAttribute("no-locale"));
this.argListAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("arg-list-name"));
this.globalExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("global"));
}
@Override
public void runAction(Map<String, Object> context) {
// default to false
Locale locale = (Locale) context.get("locale");
String resource = this.resourceExdr.expandString(context, locale);
String property = this.propertyExdr.expandString(context, locale);
String value = null;
if (noLocale) {
value = UtilProperties.getPropertyValue(resource, property);
} else {
value = UtilProperties.getMessage(resource, property, locale);
}
if (UtilValidate.isEmpty(value)) {
value = this.defaultExdr.expandString(context);
}
// note that expanding the value string here will handle defaultValue and the string from
// the properties file; if we decide later that we don't want the string from the properties
// file to be expanded we should just expand the defaultValue at the beginning of this method.
value = FlexibleStringExpander.expandString(value, context);
if (!argListAcsr.isEmpty()) {
List<? extends Object> argList = argListAcsr.get(context);
if (UtilValidate.isNotEmpty(argList)) {
value = MessageFormat.format(value, argList.toArray());
}
}
fieldAcsr.put(context, value);
}
}
public static class Script extends ModelMenuAction {
protected static final Object[] EMPTY_ARGS = {};
protected String location;
protected String method;
public Script(ModelMenu modelMenu, Element scriptElement) {
super (modelMenu, scriptElement);
String scriptLocation = scriptElement.getAttribute("location");
this.location = WidgetWorker.getScriptLocation(scriptLocation);
this.method = WidgetWorker.getScriptMethodName(scriptLocation);
}
@Override
public void runAction(Map<String, Object> context) {
if (location.endsWith(".bsh")) {
try {
BshUtil.runBshAtLocation(location, context);
} catch (GeneralException e) {
String errMsg = "Error running BSH script at location [" + location + "]: " + e.toString();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
} else if (location.endsWith(".groovy")) {
try {
groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
if (UtilValidate.isEmpty(method)) {
script.run();
} else {
script.invokeMethod(method, EMPTY_ARGS);
}
} catch (GeneralException e) {
String errMsg = "Error running Groovy script at location [" + location + "]: " + e.toString();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
} else {
throw new IllegalArgumentException("For screen script actions the script type is not yet support for location:" + location);
}
}
}
public static class Service extends ModelMenuAction {
protected FlexibleStringExpander serviceNameExdr;
protected FlexibleMapAccessor<Map<String, Object>> resultMapNameAcsr;
protected FlexibleStringExpander autoFieldMapExdr;
protected Map<FlexibleMapAccessor<Object>, FlexibleMapAccessor<Object>> fieldMap;
public Service(ModelMenu modelMenu, Element serviceElement) {
super (modelMenu, serviceElement);
this.serviceNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("service-name"));
this.resultMapNameAcsr = FlexibleMapAccessor.getInstance(serviceElement.getAttribute("result-map-name"));
this.autoFieldMapExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("auto-field-map"));
List<? extends Element> fieldMapElementList = UtilXml.childElementList(serviceElement, "field-map");
if (fieldMapElementList.size() > 0) {
this.fieldMap = FastMap.newInstance();
for (Element fieldMapElement: fieldMapElementList) {
// set the env-name for each field-name, noting that if no field-name is specified it defaults to the env-name
this.fieldMap.put(
FlexibleMapAccessor.getInstance(UtilFormatOut.checkEmpty(fieldMapElement.getAttribute("field-name"), fieldMapElement.getAttribute("env-name"))),
FlexibleMapAccessor.getInstance(fieldMapElement.getAttribute("env-name")));
}
}
}
@Override
public void runAction(Map<String, Object> context) {
String serviceNameExpanded = this.serviceNameExdr.expandString(context);
if (UtilValidate.isEmpty(serviceNameExpanded)) {
throw new IllegalArgumentException("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal());
}
String autoFieldMapString = this.autoFieldMapExdr.expandString(context);
boolean autoFieldMapBool = !"false".equals(autoFieldMapString);
try {
Map<String, Object> serviceContext = null;
if (autoFieldMapBool) {
serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context);
} else {
serviceContext = FastMap.newInstance();
}
if (this.fieldMap != null) {
for (Map.Entry<FlexibleMapAccessor<Object>, FlexibleMapAccessor<Object>> entry: this.fieldMap.entrySet()) {
FlexibleMapAccessor<Object> serviceContextFieldAcsr = entry.getKey();
FlexibleMapAccessor<Object> contextEnvAcsr = entry.getValue();
serviceContextFieldAcsr.put(serviceContext, contextEnvAcsr.get(context));
}
}
Map<String, Object> result = WidgetWorker.getDispatcher(context).runSync(serviceNameExpanded, serviceContext);
if (!this.resultMapNameAcsr.isEmpty()) {
this.resultMapNameAcsr.put(context, result);
} else {
context.putAll(result);
}
} catch (GenericServiceException e) {
String errMsg = "Error calling service with name " + serviceNameExpanded + ": " + e.toString();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
}
}
public static class EntityOne extends ModelMenuAction {
protected PrimaryKeyFinder finder;
public EntityOne(ModelMenu modelMenu, Element entityOneElement) {
super (modelMenu, entityOneElement);
finder = new PrimaryKeyFinder(entityOneElement);
}
@Override
public void runAction(Map<String, Object> context) {
try {
finder.runFind(context, WidgetWorker.getDelegator(context));
} catch (GeneralException e) {
String errMsg = "Error doing entity query by condition: " + e.toString();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
}
}
public static class EntityAnd extends ModelMenuAction {
protected ByAndFinder finder;
public EntityAnd(ModelMenu modelMenu, Element entityAndElement) {
super (modelMenu, entityAndElement);
finder = new ByAndFinder(entityAndElement);
}
@Override
public void runAction(Map<String, Object> context) {
try {
finder.runFind(context, WidgetWorker.getDelegator(context));
} catch (GeneralException e) {
String errMsg = "Error doing entity query by condition: " + e.toString();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
}
}
public static class EntityCondition extends ModelMenuAction {
ByConditionFinder finder;
public EntityCondition(ModelMenu modelMenu, Element entityConditionElement) {
super (modelMenu, entityConditionElement);
finder = new ByConditionFinder(entityConditionElement);
}
@Override
public void runAction(Map<String, Object> context) {
try {
finder.runFind(context, WidgetWorker.getDelegator(context));
} catch (GeneralException e) {
String errMsg = "Error doing entity query by condition: " + e.toString();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
}
}
}