diff --git a/idempiere-plugin-template/com.ingeint.template.targetplatform/template/target-platform-template.xml b/idempiere-plugin-template/com.ingeint.template.targetplatform/template/target-platform-template.xml index dd5c9ab..5e8302f 100755 --- a/idempiere-plugin-template/com.ingeint.template.targetplatform/template/target-platform-template.xml +++ b/idempiere-plugin-template/com.ingeint.template.targetplatform/template/target-platform-template.xml @@ -1,5 +1,5 @@ - + diff --git a/idempiere-plugin-template/com.ingeint.template.test/META-INF/MANIFEST.MF b/idempiere-plugin-template/com.ingeint.template.test/META-INF/MANIFEST.MF index 02390d9..ad4dac2 100755 --- a/idempiere-plugin-template/com.ingeint.template.test/META-INF/MANIFEST.MF +++ b/idempiere-plugin-template/com.ingeint.template.test/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: iDempiere Plugin Template Test Bundle-SymbolicName: com.ingeint.template.test -Bundle-Version: 9.0.0.qualifier +Bundle-Version: 10.0.0.qualifier Bundle-Vendor: INGEINT Fragment-Host: com.ingeint.template Bundle-ClassPath: ., diff --git a/idempiere-plugin-template/com.ingeint.template/META-INF/MANIFEST.MF b/idempiere-plugin-template/com.ingeint.template/META-INF/MANIFEST.MF index d327e44..0f31a58 100755 --- a/idempiere-plugin-template/com.ingeint.template/META-INF/MANIFEST.MF +++ b/idempiere-plugin-template/com.ingeint.template/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-Category: idempiere-plugin Bundle-ManifestVersion: 2 Bundle-Name: iDempiere Plugin Template Bundle-SymbolicName: com.ingeint.template -Bundle-Version: 9.0.0.qualifier +Bundle-Version: 10.0.0.qualifier Bundle-Vendor: INGEINT Bundle-ActivationPolicy: lazy Require-Bundle: org.apache.commons.lang3, diff --git a/template/plugin.symbolic.name.targetplatform/template/target-platform-template.xml b/template/plugin.symbolic.name.targetplatform/template/target-platform-template.xml index 43839b1..61bd9c0 100644 --- a/template/plugin.symbolic.name.targetplatform/template/target-platform-template.xml +++ b/template/plugin.symbolic.name.targetplatform/template/target-platform-template.xml @@ -25,6 +25,11 @@ 4.8.138 jar + + + + + diff --git a/template/plugin.symbolic.name.test/src/plugin.root/util/FileTemplateBuilderTest.java b/template/plugin.symbolic.name.test/src/plugin.root/util/FileTemplateBuilderTest.java index 9e575ae..792e0e6 100755 --- a/template/plugin.symbolic.name.test/src/plugin.root/util/FileTemplateBuilderTest.java +++ b/template/plugin.symbolic.name.test/src/plugin.root/util/FileTemplateBuilderTest.java @@ -21,6 +21,7 @@ import static ${plugin.root}.test.util.RandomTestUtil.*; import static org.assertj.core.api.Assertions.assertThat; +import java.text.NumberFormat; import java.util.Arrays; import java.util.List; @@ -51,10 +52,11 @@ public void setup() { @Test public void createFileTemplate() { + String priceStr = NumberFormat.getInstance().format(randomPrice); String result = builder.file("resources/xml/xml-invoice.xml").inject("invoice", invoice).build(); assertThat(result).contains("" + randomName + ""); assertThat(result).contains("" + randomId + ""); - assertThat(result).contains(""); + assertThat(result).contains(""); } public class Invoice { diff --git a/template/plugin.symbolic.name/src/plugin.root/annotation/ColumnCallout.java b/template/plugin.symbolic.name/src/plugin.root/annotation/ColumnCallout.java new file mode 100644 index 0000000..3c6dd45 --- /dev/null +++ b/template/plugin.symbolic.name/src/plugin.root/annotation/ColumnCallout.java @@ -0,0 +1,14 @@ +package ${plugin.root}.annotation; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Retention(RUNTIME) +@Target(METHOD) +public @interface ColumnCallout { + public String[] columnName() default ""; + public int order() default 0; +} diff --git a/template/plugin.symbolic.name/src/plugin.root/base/CustomCallout.java b/template/plugin.symbolic.name/src/plugin.root/base/CustomCallout.java index 1f6521b..75a83d4 100644 --- a/template/plugin.symbolic.name/src/plugin.root/base/CustomCallout.java +++ b/template/plugin.symbolic.name/src/plugin.root/base/CustomCallout.java @@ -18,24 +18,32 @@ package ${plugin.root}.base; +import java.lang.reflect.Method; import java.util.Properties; +import java.util.Arrays; +import java.util.Optional; import org.adempiere.base.IColumnCallout; import org.compiere.model.GridField; import org.compiere.model.GridTab; +import org.adempiere.exceptions.AdempiereException; +import org.compiere.util.Util; + +import ${plugin.root}.annotation.ColumnCallout; +import ${plugin.root}.util.StringUtils; /** * Custom IColumnCallout */ public abstract class CustomCallout implements IColumnCallout { - + private Properties ctx; private int WindowNo; private GridTab mTab; private GridField mField; private Object value; private Object oldValue; - + /** * Gets the context * @@ -44,7 +52,7 @@ public abstract class CustomCallout implements IColumnCallout { public Properties getCtx() { return ctx; } - + /** * Gets the Window No * @@ -53,7 +61,7 @@ public Properties getCtx() { public int getWindowNo() { return WindowNo; } - + /** * Gets the Tab * @@ -62,7 +70,7 @@ public int getWindowNo() { public GridTab getTab() { return mTab; } - + /** * Gets the field * @@ -71,7 +79,7 @@ public GridTab getTab() { public GridField getField() { return mField; } - + /** * Gets de current value of field * @@ -80,7 +88,7 @@ public GridField getField() { public Object getValue() { return value; } - + /** * Gets the old value of field * @@ -89,7 +97,7 @@ public Object getValue() { public Object getOldValue() { return oldValue; } - + /** * Gets the table name * @@ -98,7 +106,7 @@ public Object getOldValue() { public String getTableName() { return mTab.getTableName(); } - + /** * Gets the column name * @@ -107,7 +115,7 @@ public String getTableName() { public String getColumnName() { return mField.getColumnName(); } - + /** * Set a new value to the current column * @@ -117,7 +125,7 @@ public String getColumnName() { public String setValue(Object newValue) { return mTab.setValue(getColumnName(), newValue); } - + /** * Set a new value to the selected column * @@ -128,7 +136,103 @@ public String setValue(Object newValue) { public String setValue(String columnName, Object newValue) { return mTab.setValue(columnName, newValue); } - + + /** + * + * @return the int value + */ + public int getValueAsInt() { + return Optional.ofNullable((Integer) value) + .orElse(0); + } + + /** + * + * @return the old value as int + */ + public int getOldValueAsInt() { + return Optional.ofNullable((Integer) oldValue) + .orElse(0); + } + + /** + * + * @param columnName Column to get as int + * @return the int value + */ + public int getValueAsInt(String columnName) { + return Optional.ofNullable((Integer) getValue(columnName)) + .orElse(0); + } + + /** + * + * @param columnName Column to get + * @return the value + */ + public Object getValue(String columnName) { + return getTab().getValue(columnName); + } + + /** + * + * @return the value as boolean + */ + public boolean getValueAsBoolean() { + if (value instanceof String) + return "Y".equals(value); + return Optional.ofNullable((Boolean) value) + .orElse(false); + } + + /** + * + * @return the old value as boolean + */ + public boolean getOldValueAsBoolean() { + if (oldValue instanceof String) + return "Y".equals(oldValue); + return Optional.ofNullable((Boolean) oldValue) + .orElse(false); + } + + /** + * + * @param columnName Column to get + * @return the value as boolean + */ + public boolean getValueAsBoolean(String columnName) { + return getTab().getValueAsBoolean(columnName); + } + + /** + * + * @param columnName Column to get + * @return the value as string + */ + public String getValueAsString(String columnName) { + return Optional.ofNullable((String) getValue(columnName)) + .orElse(""); + } + + /** + * + * @return the value as string + */ + public String getValueAsString() { + return Optional.ofNullable((String) value) + .orElse(""); + } + + /** + * + * @return the old value as string + */ + public String getOldValueAsString() { + return Optional.ofNullable((String) oldValue) + .orElse(""); + } + @Override public String start(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value, Object oldValue) { this.ctx = ctx; @@ -139,12 +243,46 @@ public String start(Properties ctx, int WindowNo, GridTab mTab, GridField mField this.oldValue = oldValue; return start(); } - + /** * Custom event execution * * @return null if no error */ - protected abstract String start(); - + protected String start() { + String columnName = getColumnName(); + + Method[] methods = Arrays.stream(getClass().getDeclaredMethods()) + .filter(method -> method.isAnnotationPresent(ColumnCallout.class) + && StringUtils.isValuePresent(method.getAnnotation(ColumnCallout.class).columnName(), columnName) + && method.getReturnType().equals(String.class)) + .sorted(CustomCallout::sort) + .toArray(Method[]::new); + + for (Method method: methods) + { + String error = execute(method); + + if (!Util.isEmpty(error, true)) + return error; + } + + return ""; + } + + private static int sort(Method method1, Method method2) { + ColumnCallout columnCallout1 = method1.getAnnotation(ColumnCallout.class); + ColumnCallout columnCallout2 = method2.getAnnotation(ColumnCallout.class); + + return columnCallout1.order() > columnCallout2.order() ? 1 + : (columnCallout1.order() == columnCallout2.order() ? 0 : -1); + } + + private String execute(Method method) { + try { + return (String) method.invoke(this); + } catch (Exception e) { + throw new AdempiereException(e.getLocalizedMessage(), e); + } + } } diff --git a/template/plugin.symbolic.name/src/plugin.root/util/StringUtils.java b/template/plugin.symbolic.name/src/plugin.root/util/StringUtils.java new file mode 100644 index 0000000..4fd8198 --- /dev/null +++ b/template/plugin.symbolic.name/src/plugin.root/util/StringUtils.java @@ -0,0 +1,13 @@ +package ${plugin.root}.util; + +import java.util.Arrays; + +public class StringUtils { + + public static boolean isValuePresent(String[] strs, String value) { + return Arrays.stream(strs) + .filter(str -> str != null && str.equals(value)) + .findFirst() + .isPresent(); + } +}