javadozerj8583

Converting j8583 object having one getter with params in Dozer


I have an IsoMessage object (https://github.com/chochos/j8583/blob/master/src/main/java/com/solab/iso8583/IsoMessage.java) that has an internal array which I can only access through a getField(int) method.

public class IsoMessage {

    @SuppressWarnings("rawtypes")
    private IsoValue[] fields = new IsoValue[129];
    .........
    .........
    .........

    /** Returns the IsoValue for the specified field. First real field is 2. */
    @SuppressWarnings("unchecked")
    public <T> IsoValue<T> getField(int field) {
        return fields[field];
    }

I need to read all the attributes stored on the fields array, by calling getField(param Number), and move them to a new object that has a Map, and want to achieve this using dozer.

the object that I need to translate to:

public class TransactionInstance implements Serializable {

    private static final long serialVersionUID = 3429335891821913088L;
    private String transactionName;
    private Map<String, String> parameters;

I was experimenting with this dozer configuration hoping to get the field 1 from my isoMessage object

<mapping map-id="a">
    <class-a>com.solab.iso8583.IsoMessage</class-a>
    <class-b>j8583.example.TransactionInstance</class-b>
    <field>
        <a get-method="getField" key="1">field</a>
        <b map-set-method="put">parameters</b>
    </field>
</mapping>

But I'm stuck at getting the value from the original object with this exception:

Exception in thread "main" org.dozer.MappingException: No read or write method found for field (field) in class (class com.solab.iso8583.IsoMessage)
    at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.determinePropertyType(GetterSetterPropertyDescriptor.java:319)
    at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.getPropertyType(GetterSetterPropertyDescriptor.java:76)
    at org.dozer.fieldmap.MapFieldMap.determineActualPropertyType(MapFieldMap.java:170)
    at org.dozer.fieldmap.MapFieldMap.getSrcFieldValue(MapFieldMap.java:95)

I was checking this post https://github.com/DozerMapper/dozer/issues/111 and How to pass `this` to Dozer field mapping? bus still stuck in the same place, also I was wondering if I can achieve this by using the API so I can tell dynamically which fields I want to get from the original bean


Solution

  • I finally foud the rigth mapping using the capability that dozer has to access the internal objects directly (http://dozer.sourceforge.net/documentation/custommethods.html).

    <mapping>
        <class-a is-accessible="true">com.solab.iso8583.IsoMessage</class-a>
        <class-b>j8583.example.TransactionInstance</class-b>
        <field>
            <a>fields[3]</a>
            <b set-method="addParameter" map-set-method="addParameter" key="field3">parameters
            </b>
        </field>
    
    </mapping>