Data import: more info on how the Enum mapping/import works?

So I’ve got the Data Import add-on installed, and I tried it out on a simple case, an entity with just a description and a two-value Enum, and the import went just fine…except the Enum attribute was left null.

The Enum is a simple one built in Studio:

public enum OrthoticOrProsthetic implements EnumClass<String> {

    Orthotic("O"),
    Prosthetic("P");

    private String id;

    OrthoticOrProsthetic(String value) {
        this.id = value;
    }

    public String getId() {
        return id;
    }

    @Nullable
    public static OrthoticOrProsthetic fromId(String id) {
        for (OrthoticOrProsthetic at : OrthoticOrProsthetic.values()) {
            if (at.getId().equals(id)) {
                return at;
            }
        }
        return null;
    }
}

All of the values in the column in the CSV file are either O or P so I believe it should have worked.

Where should I start looking to find out why it failed?

In fact, I just noticed in the logs:

11:48:20.766 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: O for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.803 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: O for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.816 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: O for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.829 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: O for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.841 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: O for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.852 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: O for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.865 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: O for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.878 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: P for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.890 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: P for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.904 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: P for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.916 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: P for Enum: OrthoticOrProsthetic. Will be ignored
11:48:20.929 INFO  d.d.c.d.binding.DatatypeFactory         - Enum value could not be found: P for Enum: OrthoticOrProsthetic. Will be ignored

hi,

this is working as expected :slight_smile:

Here you can read about how enum binding works: GitHub - mariodavid/cuba-component-data-import: CUBA component for easy data import

The CSV needs to contain either Orthotic or Prosthetic as values - not their IDs.

If you cannot change the CSV you can adjust the binding behavior by instead of using the automatic binding behavior switch for this column mapping to custom binding behavior: GitHub - mariodavid/cuba-component-data-import: CUBA component for easy data import.

In the script it should be something like this:

if (rawValue == "O") {
  return OrthoticOrProsthetic.Orthotic
}
else {
  return OrthoticOrProsthetic.Prosthetic
}

I hope this helps.

Cheers
Mario

Aha. I was confused between the concept of ID/value, and to further the confusion - the IDE/Studio labels the O/P as “value” in the source panel.

Setting that as a script causes an exception for each row:

12:29:10.245 ERROR d.d.c.d.b.CustomScriptAttributeBinder   - Error while executing custom binding script: class groovy.lang.MissingPropertyException
groovy.lang.MissingPropertyException: No such property: OrthoticOrProsthetic for class: Script1
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:65) ~[groovy-2.5.8.jar:2.5.8]
	at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51) ~[groovy-2.5.8.jar:2.5.8]
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:309) ~[groovy-2.5.8.jar:2.5.8]
	at Script1.run(Script1.groovy:3) ~[na:na]
	at com.haulmont.cuba.core.sys.AbstractScripting.evaluateGroovy(AbstractScripting.java:206) ~[cuba-global-7.2.1.jar:7.2.1]
	at com.haulmont.cuba.core.sys.AbstractScripting.evaluateGroovy(AbstractScripting.java:234) ~[cuba-global-7.2.1.jar:7.2.1]
	at com.haulmont.cuba.core.global.Scripting$evaluateGroovy.call(Unknown Source) ~[na:na]
	at de.diedavids.cuba.dataimport.binding.CustomScriptAttributeBinder.getValue(CustomScriptAttributeBinder.groovy:43) ~[dataimport-core-0.12.0.jar:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43) ~[groovy-2.5.8.jar:2.5.8]
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:190) ~[groovy-2.5.8.jar:2.5.8]
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:58) ~[groovy-2.5.8.jar:2.5.8]
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:176) ~[groovy-2.5.8.jar:2.5.8]
	at de.diedavids.cuba.dataimport.binding.CustomScriptAttributeBinder.tryToExecuteCustomScriptAndSetValue(CustomScriptAttributeBinder.groovy:23) ~[dataimport-core-0.12.0.jar:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43) ~[groovy-2.5.8.jar:2.5.8]
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:190) ~[groovy-2.5.8.jar:2.5.8]
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:58) ~[groovy-2.5.8.jar:2.5.8]
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:184) ~[groovy-2.5.8.jar:2.5.8]
	at de.diedavids.cuba.dataimport.binding.CustomScriptAttributeBinder.bindAttribute(CustomScriptAttributeBinder.groovy:18) ~[dataimport-core-0.12.0.jar:na]

(The trace goes on longer.)

Does the script need an import or similar for the Enum?

Jupp, just add an import statement on the top…

I did try that, but then instead I get an NPE. :frowning:

Out of the office until tomorrow AM now, so I can’t cut and paste the NPE.

NM; further exploration happening.

'Tis all working now; thanks @mario.

1 Like