Skip to content
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

Add support for version 10 #6

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><?pde version="3.8"?>
<target name="idempiere-9.0.0" sequenceNumber="91">
<target name="idempiere-10.0.0" sequenceNumber="91">
<locations>
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="slicer" includeSource="true" type="InstallableUnit">
<unit id="org.adempiere.base.feature.feature.group"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://www.ingeint.com>
Fragment-Host: com.ingeint.template
Bundle-ClassPath: .,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://www.ingeint.com>
Bundle-ActivationPolicy: lazy
Require-Bundle: org.apache.commons.lang3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<version>4.8.138</version>
<type>jar</type>
</location>
<location includeMode="slicer" includeAllPlatforms="false" includeSource="true" includeConfigurePhase="true" type="InstallableUnit">
<unit id="org.apache.httpcomponents.httpclient" version="4.5.13.v20210128-2225"/>
<unit id="org.apache.httpcomponents.httpcore" version="4.4.14.v20210128-2225"/>
<repository id="eclipse" location="https://download.eclipse.org/eclipse/updates/4.20"/>
</location>
</locations>
<targetJRE path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</target>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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("<name>" + randomName + "</name>");
assertThat(result).contains("<id>" + randomId + "</id>");
assertThat(result).contains("<product name=\"" + randomProduct + "\" price=\"" + randomPrice + "\"/>");
assertThat(result).contains("<product name=\"" + randomProduct + "\" price=\"" + priceStr + "\"/>");
}

public class Invoice {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
168 changes: 153 additions & 15 deletions template/plugin.symbolic.name/src/plugin.root/base/CustomCallout.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -44,7 +52,7 @@ public abstract class CustomCallout implements IColumnCallout {
public Properties getCtx() {
return ctx;
}

/**
* Gets the Window No
*
Expand All @@ -53,7 +61,7 @@ public Properties getCtx() {
public int getWindowNo() {
return WindowNo;
}

/**
* Gets the Tab
*
Expand All @@ -62,7 +70,7 @@ public int getWindowNo() {
public GridTab getTab() {
return mTab;
}

/**
* Gets the field
*
Expand All @@ -71,7 +79,7 @@ public GridTab getTab() {
public GridField getField() {
return mField;
}

/**
* Gets de current value of field
*
Expand All @@ -80,7 +88,7 @@ public GridField getField() {
public Object getValue() {
return value;
}

/**
* Gets the old value of field
*
Expand All @@ -89,7 +97,7 @@ public Object getValue() {
public Object getOldValue() {
return oldValue;
}

/**
* Gets the table name
*
Expand All @@ -98,7 +106,7 @@ public Object getOldValue() {
public String getTableName() {
return mTab.getTableName();
}

/**
* Gets the column name
*
Expand All @@ -107,7 +115,7 @@ public String getTableName() {
public String getColumnName() {
return mField.getColumnName();
}

/**
* Set a new value to the current column
*
Expand All @@ -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
*
Expand All @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}