ModelWidgetAction.java code formatting, no functional change.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1649280 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian Crum
2015-01-04 01:40:36 +00:00
parent 88a71146d1
commit 73cc6c2cbf
@@ -64,19 +64,6 @@ import org.w3c.dom.Element;
public abstract class ModelWidgetAction implements Serializable {
public static final String module = ModelWidgetAction.class.getName();
protected ModelWidget modelWidget;
protected ModelWidgetAction() {}
public ModelWidgetAction(ModelWidget modelWidget, Element actionElement) {
this.modelWidget = modelWidget;
if (Debug.verboseOn()) Debug.logVerbose("Reading widget action with name: " + actionElement.getNodeName(), module);
}
public abstract void runAction(Map<String, Object> context) throws GeneralException;
public abstract void accept(ModelActionVisitor visitor);
public static ModelWidgetAction newInstance(ModelWidget modelWidget, Element actionElement) {
if ("set".equals(actionElement.getNodeName())) {
return new SetField(modelWidget, actionElement);
@@ -102,18 +89,19 @@ public abstract class ModelWidgetAction implements Serializable {
throw new IllegalArgumentException("Action element not supported with name: " + actionElement.getNodeName());
}
}
public static List<ModelWidgetAction> readSubActions(ModelWidget modelWidget, Element parentElement) {
List<? extends Element> actionElementList = UtilXml.childElementList(parentElement);
List<ModelWidgetAction> actions = new ArrayList<ModelWidgetAction>(actionElementList.size());
for (Element actionElement: actionElementList) {
for (Element actionElement : actionElementList) {
actions.add(newInstance(modelWidget, actionElement));
}
return Collections.unmodifiableList(actions);
}
public static void runSubActions(List<ModelWidgetAction> actions, Map<String, Object> context) {
if (actions == null) return;
if (actions == null)
return;
for (ModelWidgetAction action : actions) {
if (Debug.verboseOn())
Debug.logVerbose("Running action " + action.getClass().getName(), module);
@@ -125,184 +113,245 @@ public abstract class ModelWidgetAction implements Serializable {
}
}
public static class SetField extends ModelWidgetAction {
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;
protected ModelWidget modelWidget;
public SetField(ModelWidget modelWidget, Element setElement) {
super (modelWidget, 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 = FlexibleStringExpander.getInstance(setElement.getAttribute("default-value"));
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 widget");
}
}
protected ModelWidgetAction() {
}
@SuppressWarnings("rawtypes")
@Override
public void runAction(Map<String, Object> context) {
String globalStr = this.globalExdr.expandString(context);
// default to false
boolean global = "true".equals(globalStr);
protected ModelWidgetAction(ModelWidget modelWidget, Element actionElement) {
this.modelWidget = modelWidget;
if (Debug.verboseOn())
Debug.logVerbose("Reading widget action with name: " + actionElement.getNodeName(), module);
}
Object newValue = null;
if (this.fromScope != null && this.fromScope.equals("user")) {
if (!this.fromField.isEmpty()) {
HttpSession session = (HttpSession) context.get("session");
newValue = getInMemoryPersistedFromField(session, context);
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.expand(context);
}
} else if (this.fromScope != null && this.fromScope.equals("application")) {
if (!this.fromField.isEmpty()) {
ServletContext servletContext = (ServletContext) context.get("application");
newValue = getInMemoryPersistedFromField(servletContext, context);
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("Getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module);
} else if (!this.valueExdr.isEmpty()) {
newValue = this.valueExdr.expand(context);
}
}
public abstract void accept(ModelActionVisitor visitor);
// If newValue is still empty, use the default value
if (ObjectType.isEmpty(newValue) && !this.defaultExdr.isEmpty()) {
newValue = this.defaultExdr.expand(context);
}
public abstract void runAction(Map<String, Object> context) throws GeneralException;
if (UtilValidate.isNotEmpty(this.type)) {
if ("NewMap".equals(this.type)) {
newValue = new HashMap();
} else if ("NewList".equals(this.type)) {
newValue = new LinkedList();
} 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);
}
}
}
public static class EntityAnd extends ModelWidgetAction {
protected ByAndFinder finder;
if (this.toScope != null && this.toScope.equals("user")) {
String originalName = this.field.getOriginalName();
List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_"));
String newKey = "";
if (currentWidgetTrail != null) {
newKey = StringUtil.join(currentWidgetTrail, "|");
}
if (UtilValidate.isNotEmpty(newKey)) {
newKey += "|";
}
newKey += 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();
List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_"));
String newKey = "";
if (currentWidgetTrail != null) {
newKey = StringUtil.join(currentWidgetTrail, "|");
}
if (UtilValidate.isNotEmpty(newKey)) {
newKey += "|";
}
newKey += 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 {
// only do this if it is not global, if global ONLY put it in the global context
if (!global) {
if (Debug.verboseOn()) Debug.logVerbose("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);
} else {
this.field.put(context, 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 Object getInMemoryPersistedFromField(Object storeAgent, Map<String, Object> context) {
Object newValue = null;
String originalName = this.fromField.getOriginalName();
List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_"));
List<String> trailList = new ArrayList<String>();
if (currentWidgetTrail != null) {
trailList.addAll(currentWidgetTrail);
}
for (int i=trailList.size(); i >= 0; i--) {
List<String> subTrail = trailList.subList(0,i);
String newKey = null;
if (subTrail.size() > 0)
newKey = StringUtil.join(subTrail, "|") + "|" + originalName;
else
newKey = originalName;
if (storeAgent instanceof ServletContext) {
newValue = ((ServletContext)storeAgent).getAttribute(newKey);
} else if (storeAgent instanceof HttpSession) {
newValue = ((HttpSession)storeAgent).getAttribute(newKey);
}
if (newValue != null) {
break;
}
}
return newValue;
public EntityAnd(ModelWidget modelWidget, Element entityAndElement) {
super(modelWidget, entityAndElement);
finder = new ByAndFinder(entityAndElement);
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
public ByAndFinder getFinder() {
return this.finder;
}
@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 ModelWidgetAction {
ByConditionFinder finder;
public EntityCondition(ModelWidget modelWidget, Element entityConditionElement) {
super(modelWidget, entityConditionElement);
finder = new ByConditionFinder(entityConditionElement);
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
public ByConditionFinder getFinder() {
return this.finder;
}
@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 EntityOne extends ModelWidgetAction {
protected PrimaryKeyFinder finder;
public EntityOne(ModelWidget modelWidget, Element entityOneElement) {
super(modelWidget, entityOneElement);
finder = new PrimaryKeyFinder(entityOneElement);
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
public PrimaryKeyFinder getFinder() {
return this.finder;
}
@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 GetRelated extends ModelWidgetAction {
protected FlexibleMapAccessor<List<GenericValue>> listNameAcsr;
protected FlexibleMapAccessor<Map<String, Object>> mapAcsr;
protected FlexibleMapAccessor<List<String>> orderByListAcsr;
protected String relationName;
protected boolean useCache;
protected FlexibleMapAccessor<Object> valueNameAcsr;
public GetRelated(ModelWidget modelWidget, Element getRelatedElement) {
super(modelWidget, getRelatedElement);
this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("value-field"));
if (this.valueNameAcsr.isEmpty())
this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("value-name"));
this.listNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("list"));
if (this.listNameAcsr.isEmpty())
this.listNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("list-name"));
this.relationName = getRelatedElement.getAttribute("relation-name");
this.mapAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("map"));
if (this.mapAcsr.isEmpty())
this.mapAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("map-name"));
this.orderByListAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("order-by-list"));
if (this.orderByListAcsr.isEmpty())
this.orderByListAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("order-by-list-name"));
this.useCache = "true".equals(getRelatedElement.getAttribute("use-cache"));
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
public String getRelationName() {
return this.relationName;
}
@Override
public void runAction(Map<String, Object> context) {
Object valueObject = valueNameAcsr.get(context);
if (valueObject == null) {
Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module);
return;
}
if (!(valueObject instanceof GenericValue)) {
String errMsg = "Env variable for value-name " + valueNameAcsr.toString()
+ " is not a GenericValue object; for the relation-name: " + relationName + "]";
Debug.logError(errMsg, module);
throw new IllegalArgumentException(errMsg);
}
GenericValue value = (GenericValue) valueObject;
List<String> orderByNames = null;
if (!orderByListAcsr.isEmpty()) {
orderByNames = orderByListAcsr.get(context);
}
Map<String, Object> constraintMap = null;
if (!mapAcsr.isEmpty()) {
constraintMap = mapAcsr.get(context);
}
try {
listNameAcsr.put(context, value.getRelated(relationName, constraintMap, orderByNames, useCache));
} catch (GenericEntityException e) {
String errMsg = "Problem getting related from entity with name " + value.getEntityName()
+ " for the relation-name: " + relationName + ": " + e.getMessage();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
}
}
public static class GetRelatedOne extends ModelWidgetAction {
protected String relationName;
protected FlexibleMapAccessor<Object> toValueNameAcsr;
protected boolean useCache;
protected FlexibleMapAccessor<Object> valueNameAcsr;
public GetRelatedOne(ModelWidget modelWidget, Element getRelatedOneElement) {
super(modelWidget, getRelatedOneElement);
this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("value-field"));
if (this.valueNameAcsr.isEmpty())
this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("value-name"));
this.toValueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("to-value-field"));
if (this.toValueNameAcsr.isEmpty())
this.toValueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("to-value-name"));
this.relationName = getRelatedOneElement.getAttribute("relation-name");
this.useCache = "true".equals(getRelatedOneElement.getAttribute("use-cache"));
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
public String getRelationName() {
return this.relationName;
}
@Override
public void runAction(Map<String, Object> context) {
Object valueObject = valueNameAcsr.get(context);
if (valueObject == null) {
Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module);
return;
}
if (!(valueObject instanceof GenericValue)) {
String errMsg = "Env variable for value-name " + valueNameAcsr.toString()
+ " is not a GenericValue object; for the relation-name: " + relationName + "]";
Debug.logError(errMsg, module);
throw new IllegalArgumentException(errMsg);
}
GenericValue value = (GenericValue) valueObject;
try {
toValueNameAcsr.put(context, value.getRelatedOne(relationName, useCache));
} catch (GenericEntityException e) {
String errMsg = "Problem getting related one from entity with name " + value.getEntityName()
+ " for the relation-name: " + relationName + ": " + e.getMessage();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
}
}
public static class PropertyMap extends ModelWidgetAction {
protected FlexibleStringExpander resourceExdr;
protected FlexibleMapAccessor<ResourceBundleMapWrapper> mapNameAcsr;
protected FlexibleStringExpander globalExdr;
protected FlexibleMapAccessor<ResourceBundleMapWrapper> mapNameAcsr;
protected FlexibleStringExpander resourceExdr;
public PropertyMap(ModelWidget modelWidget, Element setElement) {
super (modelWidget, setElement);
super(modelWidget, 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 accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
@Override
public void runAction(Map<String, Object> context) {
String globalStr = this.globalExdr.expandString(context);
@@ -344,25 +393,20 @@ public abstract class ModelWidgetAction implements Serializable {
}
}
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
}
public static class PropertyToField extends ModelWidgetAction {
protected FlexibleStringExpander resourceExdr;
protected FlexibleStringExpander propertyExdr;
protected FlexibleMapAccessor<Object> fieldAcsr;
protected FlexibleStringExpander defaultExdr;
protected boolean noLocale;
protected FlexibleMapAccessor<List<? extends Object>> argListAcsr;
protected FlexibleStringExpander defaultExdr;
protected FlexibleMapAccessor<Object> fieldAcsr;
protected FlexibleStringExpander globalExdr;
protected boolean noLocale;
protected FlexibleStringExpander propertyExdr;
protected FlexibleStringExpander resourceExdr;
public PropertyToField(ModelWidget modelWidget, Element setElement) {
super (modelWidget, setElement);
super(modelWidget, setElement);
this.resourceExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("resource"));
this.propertyExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("property"));
this.fieldAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("field"));
@@ -372,6 +416,11 @@ public abstract class ModelWidgetAction implements Serializable {
this.globalExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("global"));
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
@Override
public void runAction(Map<String, Object> context) {
//String globalStr = this.globalExdr.expandString(context);
@@ -405,11 +454,6 @@ public abstract class ModelWidgetAction implements Serializable {
}
fieldAcsr.put(context, value);
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
}
public static class Script extends ModelWidgetAction {
@@ -417,12 +461,17 @@ public abstract class ModelWidgetAction implements Serializable {
protected String method;
public Script(ModelWidget modelWidget, Element scriptElement) {
super (modelWidget, scriptElement);
super(modelWidget, scriptElement);
String scriptLocation = scriptElement.getAttribute("location");
this.location = WidgetWorker.getScriptLocation(scriptLocation);
this.method = WidgetWorker.getScriptMethodName(scriptLocation);
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
@Override
public void runAction(Map<String, Object> context) throws GeneralException {
if (location.endsWith(".xml")) {
@@ -440,26 +489,31 @@ public abstract class ModelWidgetAction implements Serializable {
ScriptUtil.executeScript(this.location, this.method, context);
}
}
}
public static class Service extends ModelWidgetAction {
protected FlexibleStringExpander autoFieldMapExdr;
protected Map<FlexibleMapAccessor<Object>, Object> fieldMap;
protected FlexibleMapAccessor<Map<String, Object>> resultMapNameAcsr;
protected FlexibleStringExpander serviceNameExdr;
public Service(ModelWidget modelWidget, Element serviceElement) {
super(modelWidget, serviceElement);
this.serviceNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("service-name"));
this.resultMapNameAcsr = FlexibleMapAccessor.getInstance(serviceElement.getAttribute("result-map"));
if (this.resultMapNameAcsr.isEmpty())
this.resultMapNameAcsr = FlexibleMapAccessor.getInstance(serviceElement.getAttribute("result-map-name"));
this.autoFieldMapExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("auto-field-map"));
this.fieldMap = EntityFinderUtil.makeFieldMap(serviceElement);
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
}
public static class Service extends ModelWidgetAction {
protected FlexibleStringExpander serviceNameExdr;
protected FlexibleMapAccessor<Map<String, Object>> resultMapNameAcsr;
protected FlexibleStringExpander autoFieldMapExdr;
protected Map<FlexibleMapAccessor<Object>, Object> fieldMap;
public Service(ModelWidget modelWidget, Element serviceElement) {
super (modelWidget, serviceElement);
this.serviceNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("service-name"));
this.resultMapNameAcsr = FlexibleMapAccessor.getInstance(serviceElement.getAttribute("result-map"));
if (this.resultMapNameAcsr.isEmpty()) this.resultMapNameAcsr = FlexibleMapAccessor.getInstance(serviceElement.getAttribute("result-map-name"));
this.autoFieldMapExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("auto-field-map"));
this.fieldMap = EntityFinderUtil.makeFieldMap(serviceElement);
public FlexibleStringExpander getServiceNameExdr() {
return this.serviceNameExdr;
}
@Override
@@ -487,7 +541,8 @@ public abstract class ModelWidgetAction implements Serializable {
FlexibleMapAccessor<Object> fieldFma = FlexibleMapAccessor.getInstance(autoFieldMapString);
Map<String, Object> autoFieldMap = UtilGenerics.toMap(fieldFma.get(context));
if (autoFieldMap != null) {
serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, autoFieldMap);
serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext()
.makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, autoFieldMap);
}
}
if (serviceContext == null) {
@@ -502,7 +557,7 @@ public abstract class ModelWidgetAction implements Serializable {
if (!this.resultMapNameAcsr.isEmpty()) {
this.resultMapNameAcsr.put(context, result);
String queryString = (String)result.get("queryString");
String queryString = (String) result.get("queryString");
context.put("queryString", queryString);
context.put("queryStringMap", result.get("queryStringMap"));
if (UtilValidate.isNotEmpty(queryString)) {
@@ -522,211 +577,185 @@ public abstract class ModelWidgetAction implements Serializable {
throw new IllegalArgumentException(errMsg);
}
}
public FlexibleStringExpander getServiceNameExdr() {
return this.serviceNameExdr;
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
}
public static class EntityOne extends ModelWidgetAction {
protected PrimaryKeyFinder finder;
public static class SetField extends ModelWidgetAction {
protected FlexibleStringExpander defaultExdr;
protected FlexibleMapAccessor<Object> field;
protected FlexibleMapAccessor<Object> fromField;
protected String fromScope;
protected FlexibleStringExpander globalExdr;
protected String toScope;
protected String type;
protected FlexibleStringExpander valueExdr;
public EntityOne(ModelWidget modelWidget, Element entityOneElement) {
super (modelWidget, 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 SetField(ModelWidget modelWidget, Element setElement) {
super(modelWidget, 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 = FlexibleStringExpander.getInstance(setElement.getAttribute("default-value"));
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 widget");
}
}
public PrimaryKeyFinder getFinder() {
return this.finder;
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
}
public static class EntityAnd extends ModelWidgetAction {
protected ByAndFinder finder;
public Object getInMemoryPersistedFromField(Object storeAgent, Map<String, Object> context) {
Object newValue = null;
String originalName = this.fromField.getOriginalName();
List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_"));
List<String> trailList = new ArrayList<String>();
if (currentWidgetTrail != null) {
trailList.addAll(currentWidgetTrail);
}
public EntityAnd(ModelWidget modelWidget, Element entityAndElement) {
super (modelWidget, entityAndElement);
finder = new ByAndFinder(entityAndElement);
for (int i = trailList.size(); i >= 0; i--) {
List<String> subTrail = trailList.subList(0, i);
String newKey = null;
if (subTrail.size() > 0)
newKey = StringUtil.join(subTrail, "|") + "|" + originalName;
else
newKey = originalName;
if (storeAgent instanceof ServletContext) {
newValue = ((ServletContext) storeAgent).getAttribute(newKey);
} else if (storeAgent instanceof HttpSession) {
newValue = ((HttpSession) storeAgent).getAttribute(newKey);
}
if (newValue != null) {
break;
}
}
return newValue;
}
@SuppressWarnings("rawtypes")
@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);
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()) {
HttpSession session = (HttpSession) context.get("session");
newValue = getInMemoryPersistedFromField(session, context);
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.expand(context);
}
} else if (this.fromScope != null && this.fromScope.equals("application")) {
if (!this.fromField.isEmpty()) {
ServletContext servletContext = (ServletContext) context.get("application");
newValue = getInMemoryPersistedFromField(servletContext, context);
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("Getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue,
module);
} else if (!this.valueExdr.isEmpty()) {
newValue = this.valueExdr.expand(context);
}
}
}
public ByAndFinder getFinder() {
return this.finder;
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
}
public static class EntityCondition extends ModelWidgetAction {
ByConditionFinder finder;
public EntityCondition(ModelWidget modelWidget, Element entityConditionElement) {
super (modelWidget, 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);
// If newValue is still empty, use the default value
if (ObjectType.isEmpty(newValue) && !this.defaultExdr.isEmpty()) {
newValue = this.defaultExdr.expand(context);
}
}
public ByConditionFinder getFinder() {
return this.finder;
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
}
public static class GetRelatedOne extends ModelWidgetAction {
protected FlexibleMapAccessor<Object> valueNameAcsr;
protected FlexibleMapAccessor<Object> toValueNameAcsr;
protected String relationName;
protected boolean useCache;
public GetRelatedOne(ModelWidget modelWidget, Element getRelatedOneElement) {
super (modelWidget, getRelatedOneElement);
this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("value-field"));
if (this.valueNameAcsr.isEmpty()) this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("value-name"));
this.toValueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("to-value-field"));
if (this.toValueNameAcsr.isEmpty()) this.toValueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("to-value-name"));
this.relationName = getRelatedOneElement.getAttribute("relation-name");
this.useCache = "true".equals(getRelatedOneElement.getAttribute("use-cache"));
}
@Override
public void runAction(Map<String, Object> context) {
Object valueObject = valueNameAcsr.get(context);
if (valueObject == null) {
Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module);
return;
if (UtilValidate.isNotEmpty(this.type)) {
if ("NewMap".equals(this.type)) {
newValue = new HashMap();
} else if ("NewList".equals(this.type)) {
newValue = new LinkedList();
} 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 (!(valueObject instanceof GenericValue)) {
String errMsg = "Env variable for value-name " + valueNameAcsr.toString() + " is not a GenericValue object; for the relation-name: " + relationName + "]";
Debug.logError(errMsg, module);
throw new IllegalArgumentException(errMsg);
}
GenericValue value = (GenericValue) valueObject;
try {
toValueNameAcsr.put(context, value.getRelatedOne(relationName, useCache));
} catch (GenericEntityException e) {
String errMsg = "Problem getting related one from entity with name " + value.getEntityName() + " for the relation-name: " + relationName + ": " + e.getMessage();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
}
public String getRelationName() {
return this.relationName;
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
}
}
public static class GetRelated extends ModelWidgetAction {
protected FlexibleMapAccessor<Object> valueNameAcsr;
protected FlexibleMapAccessor<List<GenericValue>> listNameAcsr;
protected FlexibleMapAccessor<Map<String, Object>> mapAcsr;
protected FlexibleMapAccessor<List<String>> orderByListAcsr;
protected String relationName;
protected boolean useCache;
public GetRelated(ModelWidget modelWidget, Element getRelatedElement) {
super (modelWidget, getRelatedElement);
this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("value-field"));
if (this.valueNameAcsr.isEmpty()) this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("value-name"));
this.listNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("list"));
if (this.listNameAcsr.isEmpty()) this.listNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("list-name"));
this.relationName = getRelatedElement.getAttribute("relation-name");
this.mapAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("map"));
if (this.mapAcsr.isEmpty()) this.mapAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("map-name"));
this.orderByListAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("order-by-list"));
if (this.orderByListAcsr.isEmpty()) this.orderByListAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("order-by-list-name"));
this.useCache = "true".equals(getRelatedElement.getAttribute("use-cache"));
}
@Override
public void runAction(Map<String, Object> context) {
Object valueObject = valueNameAcsr.get(context);
if (valueObject == null) {
Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module);
return;
if (this.toScope != null && this.toScope.equals("user")) {
String originalName = this.field.getOriginalName();
List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_"));
String newKey = "";
if (currentWidgetTrail != null) {
newKey = StringUtil.join(currentWidgetTrail, "|");
}
if (UtilValidate.isNotEmpty(newKey)) {
newKey += "|";
}
newKey += 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();
List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_"));
String newKey = "";
if (currentWidgetTrail != null) {
newKey = StringUtil.join(currentWidgetTrail, "|");
}
if (UtilValidate.isNotEmpty(newKey)) {
newKey += "|";
}
newKey += 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 {
// only do this if it is not global, if global ONLY put it in the global context
if (!global) {
if (Debug.verboseOn())
Debug.logVerbose("Setting field [" + this.field.getOriginalName() + "] to value: " + newValue, module);
this.field.put(context, newValue);
}
}
if (!(valueObject instanceof GenericValue)) {
String errMsg = "Env variable for value-name " + valueNameAcsr.toString() + " is not a GenericValue object; for the relation-name: " + relationName + "]";
Debug.logError(errMsg, module);
throw new IllegalArgumentException(errMsg);
}
GenericValue value = (GenericValue) valueObject;
List<String> orderByNames = null;
if (!orderByListAcsr.isEmpty()) {
orderByNames = orderByListAcsr.get(context);
}
Map<String, Object> constraintMap = null;
if (!mapAcsr.isEmpty()) {
constraintMap = mapAcsr.get(context);
}
try {
listNameAcsr.put(context, value.getRelated(relationName, constraintMap, orderByNames, useCache));
} catch (GenericEntityException e) {
String errMsg = "Problem getting related from entity with name " + value.getEntityName() + " for the relation-name: " + relationName + ": " + e.getMessage();
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
}
public String getRelationName() {
return this.relationName;
}
if (global) {
Map<String, Object> globalCtx = UtilGenerics.checkMap(context.get("globalContext"));
if (globalCtx != null) {
this.field.put(globalCtx, newValue);
} else {
this.field.put(context, newValue);
}
}
@Override
public void accept(ModelActionVisitor visitor) {
visitor.visit(this);
// 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);
}
}
}
}