Skip to content

Improved: getConfigParam can't be configured at runtime (OFBIZ-12815) #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,117 @@ public static String getPropertyValueFromDelegatorName(String resource, String n
}
}

public static <T> Object getPropertyValue(String resource, String name, Object defaultValue, Delegator delegator, Class<T> clazz) {
Map<String, String> propMap = getSystemPropertyValue(resource, name, delegator);
if ("Y".equals(propMap.get("isExistInDb"))) {
String s = propMap.get("value");
if (UtilValidate.isEmpty(s)) {
return defaultValue;
}
try {
if (clazz.componentType() == Integer.class) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, since it is for trunk and java 17 is available, what do you think about using switch pattern matching (and Guarded Pattern)
ref : https://openjdk.org/jeps/406
Thanks and regards,
Gil

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, i got carried away discovering those new switch syntax :), simple switch are sufficient here, sorry.

Copy link
Author

@thahn27 thahn27 May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Gil,

i closed this PR and opened another one because the commit description was wrong. The new PR: #635
Also i will adjust the existing code with a switch in an upcoming commit in the new PR.

Thanks and regards,
Tobias

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to you Tobias :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @gilPts ,

i tried to implement the switch statement instead of the if statements and i can't really make it work. The pattern matching documentation in the ref you provided works with objects which have been instantiated before. Maybe stick to the if statements?
Maybe you could provide an example or explain how you would implement such switch statement.

Thanks and regards,
Tobias

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes pattern matching is not applicable here, hence my second comment, sorry for the misunderstanding. If in those cases are fine, since we are operating on Class object test...
Thanks

return Integer.valueOf(s);
}
if (clazz.componentType() == Double.class) {
return Double.valueOf(s);
}
if (clazz.componentType() == Long.class) {
return Long.valueOf(s);
}
if (clazz.componentType() == Float.class) {
return Float.valueOf(s);
}
if (clazz.componentType() == BigInteger.class) {
return Float.valueOf(s);
}
if (clazz.componentType() == BigDecimal.class) {
return Float.valueOf(s);
}
if (clazz.componentType() == Number.class) {
return Double.valueOf(s);
}
if (clazz.componentType() == Boolean.class) {
if ("Y".equals(s) || "true".equalsIgnoreCase(s)) {
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}
} catch (NumberFormatException e) {
return null;
}
}
return null;
}

public static Integer getPropertyAsInteger(String resource, String name, int defaultNumber, Delegator delegator) {
Object myObject = getPropertyValue(resource, name, String.valueOf(defaultNumber), delegator, Integer.class);
if (myObject == null) {
return UtilProperties.getPropertyAsInteger(resource, name, defaultNumber);
}
return (Integer) myObject;
}

public static Double getPropertyAsDouble(String resource, String name, double defaultNumber, Delegator delegator) {
Object myObject = getPropertyValue(resource, name, String.valueOf(defaultNumber), delegator, Double.class);
if (myObject == null) {
return UtilProperties.getPropertyAsDouble(resource, name, defaultNumber);
}
return (Double) myObject;
}

public static Long getPropertyAsLong(String resource, String name, long defaultNumber, Delegator delegator) {
Object myObject = getPropertyValue(resource, name, String.valueOf(defaultNumber), delegator, Long.class);
if (myObject == null) {
return UtilProperties.getPropertyAsLong(resource, name, defaultNumber);
}
return (Long) myObject;
}

public static Float getPropertyAsFloat(String resource, String name, float defaultNumber, Delegator delegator) {
Object myObject = getPropertyValue(resource, name, String.valueOf(defaultNumber), delegator, Float.class);
if (myObject == null) {
return UtilProperties.getPropertyAsFloat(resource, name, defaultNumber);
}
return (Float) myObject;
}

public static BigInteger getPropertyAsBigInteger(String resource, String name, BigInteger defaultNumber, Delegator delegator) {
Object myObject = getPropertyValue(resource, name, String.valueOf(defaultNumber), delegator, BigInteger.class);
if (myObject == null) {
return UtilProperties.getPropertyAsBigInteger(resource, name, defaultNumber);
}
return (BigInteger) myObject;
}

public static BigDecimal getPropertyAsBigDecimal(String resource, String name, BigDecimal defaultNumber, Delegator delegator) {
Object myObject = getPropertyValue(resource, name, String.valueOf(defaultNumber), delegator, BigDecimal.class);
if (myObject == null) {
return UtilProperties.getPropertyAsBigDecimal(resource, name, defaultNumber);
}
return (BigDecimal) myObject;
}

public static Double getPropertyNumber(String resource, String name, double defaultValue, Delegator delegator) {
Object myObject = getPropertyValue(resource, name, String.valueOf(defaultValue), delegator, Number.class);
if (myObject == null) {
return UtilProperties.getPropertyNumber(resource, name, defaultValue);
}
return (Double) myObject;
}

public static double getPropertyNumber(String resource, String name, Delegator delegator) {
return getPropertyNumber(resource, name, 0.00000, delegator);
}

public static Boolean getPropertyAsBoolean(String resource, String name, boolean defaultValue, Delegator delegator) {
Object myObject = getPropertyValue(resource, name, String.valueOf(defaultValue), delegator, Boolean.class);
if (myObject == null) {
return UtilProperties.getPropertyAsBoolean(resource, name, defaultValue);
}
return (Boolean) myObject;
}

public static double getPropertyNumber(String resource, String name, double defaultValue) {
return UtilProperties.getPropertyNumber(resource, name, defaultValue);
}
Expand Down