Skip to content

Commit

Permalink
Implemented: Service parameter default-value attribut can support fle…
Browse files Browse the repository at this point in the history
…xible string

(OFBIZ-11180)

When you define a service with default-value, you can't set simple dynamic resolution for the value.
The improvement extend the default-value attribut on service parameter to support the FlexibleStringExpander syntax and realize the expand on service context.

Example :
     <service name=createPicklist default-entity-name=Picklist engine=entity-auto invoke=create auth=true>
        <auto-attributes include=pk mode=OUT optional=false/>
        <auto-attributes include=nonpk mode=IN optional=true/>
        <override name=statusId default-value=PICKLIST_INPUT/>
        <override name=picklistDate default-value=/>
     </service>

Thanks Jacques Le Roux and Swapnil M Mane for the review

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1866380 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
nmalin committed Sep 4, 2019
1 parent c66a030 commit 5fb61ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.Serializable;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

import javax.wsdl.Definition;
Expand All @@ -31,6 +32,7 @@
import org.apache.ofbiz.base.util.ObjectType;
import org.apache.ofbiz.base.util.UtilProperties;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.string.FlexibleStringExpander;

/**
* Generic Service Model Parameter
Expand Down Expand Up @@ -77,7 +79,7 @@ public class ModelParam implements Serializable {
public List<ModelParamValidator> validators;

/** Default value */
private String defaultValue = null;
private FlexibleStringExpander defaultValue = null;

/** Is this Parameter required or optional? Default to false, or required */
public boolean optional = false;
Expand Down Expand Up @@ -109,7 +111,7 @@ public ModelParam(ModelParam param) {
this.stringListSuffix = param.stringListSuffix;
this.validators = param.validators;
if (param.defaultValue != null) {
this.setDefaultValue(param.defaultValue);
this.setDefaultValue(param.defaultValue.getOriginal());
}
this.optional = param.optional;
this.overrideOptional = param.overrideOptional;
Expand Down Expand Up @@ -178,11 +180,15 @@ public boolean isOptional() {
return this.optional;
}

public Object getDefaultValue() {
public FlexibleStringExpander getDefaultValue() {
return this.defaultValue;
}

public Object getDefaultValue(Map<String, Object> context) {
Object defaultValueObj = null;
if (this.type != null) {
try {
defaultValueObj = ObjectType.simpleTypeOrObjectConvert(this.defaultValue, this.type, null, null, false);
defaultValueObj = ObjectType.simpleTypeOrObjectConvert(this.defaultValue.expandString(context), this.type, null, null, false);
} catch (Exception e) {
Debug.logWarning(e, "Service attribute [" + name + "] default value could not be converted to type [" + type + "]: " + e.toString(), module);
}
Expand All @@ -196,14 +202,14 @@ public Object getDefaultValue() {
return defaultValueObj;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
this.defaultValue = FlexibleStringExpander.getInstance(defaultValue);
if (this.defaultValue != null) {
this.optional = true;
}
if (Debug.verboseOn()) Debug.logVerbose("Default value for attribute [" + this.name + "] set to [" + this.defaultValue + "]", module);
}
public void copyDefaultValue(ModelParam param) {
this.setDefaultValue(param.defaultValue);
this.setDefaultValue(param.defaultValue.getOriginal());
}

public boolean equals(ModelParam model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,9 @@ public int getDefinedOutCount() {
public void updateDefaultValues(Map<String, Object> context, String mode) {
List<ModelParam> params = this.getModelParamList();
for (ModelParam param: params) {
if (IN_OUT_PARAM.equals(param.mode) || mode.equals(param.mode)) {
Object defaultValueObj = param.getDefaultValue();
if (param.getDefaultValue() != null
&& (IN_OUT_PARAM.equals(param.mode) || mode.equals(param.mode))) {
Object defaultValueObj = param.getDefaultValue(context);
if (defaultValueObj != null && context.get(param.name) == null) {
context.put(param.name, defaultValueObj);
Debug.logInfo("Set default value [" + defaultValueObj + "] for parameter [" + param.name + "]", module);
Expand Down

0 comments on commit 5fb61ec

Please sign in to comment.