I have the following XML
<result>
<group name="AA">
<record AA001="2" AA001="2" ... AA009="3"/>
<record AA011="2" AA011="2" ... AA019="3"/>
...
</group>
<group name="AB">
<record AB001="2" AB001="2" ... AA009="3"/>
...
which I have to map to single instance which contains all the attributes. As you can see the attribute names are unique so all these arrays can be mapped to a single instance:
public class FilteredDE {
protected String AA001;
...
protected String AA009;
protected String AA011;
...
protected String AA019;
protected String AB001;
...
The created JAXB class of record looks similar, it contains all the attribute names but only those ones are populated that are present in the XML.
In the 1st record the AA001, ..., AA009, in the 2nd the AA011, ..., AA019, etc
So crafted the following mapper:
@Mapping(target = ".", source = "group")
public FilteredDE map(Result re);
@Mapping(target = ".", source = "record")
public FilteredDE map(Group re, @MappingTarget FilteredDE filt);
public FilteredDE map(Record re, @MappingTarget FilteredDE filt);
I want to pass the same instance to each mapper so in the end one instance will be populated with all the attribute values.
However the map methods are not called from each other so the instance is not passed between them:
@Override
public FilteredDE map(Result re) {
if ( re == null ) {
return null;
}
FilteredDE.FilteredDEBuilder filteredDE = FilteredDE.builder();
return filteredDE.build();
}
@Override
public FilteredDE map(Group re, FilteredDE filt) {
if ( re == null ) {
return filt;
}
return filt;
}
@Override
public FilteredDE map(Record re, FilteredDE filt) {
if ( re == null ) {
return filt;
}
if ( re.getAA001() != null ) {
filt.setAA001( re.getAA001() );
}
...
if ( re.getAZ029() != null ) {
filt.setAZ029( re.getAZ029() );
}
return filt;
Any hint please how I could achieve this in mapstruct? Can it be?
Thanks!
I have the following XML
<result>
<group name="AA">
<record AA001="2" AA001="2" ... AA009="3"/>
<record AA011="2" AA011="2" ... AA019="3"/>
...
</group>
<group name="AB">
<record AB001="2" AB001="2" ... AA009="3"/>
...
which I have to map to single instance which contains all the attributes. As you can see the attribute names are unique so all these arrays can be mapped to a single instance:
public class FilteredDE {
protected String AA001;
...
protected String AA009;
protected String AA011;
...
protected String AA019;
protected String AB001;
...
The created JAXB class of record looks similar, it contains all the attribute names but only those ones are populated that are present in the XML.
In the 1st record the AA001, ..., AA009, in the 2nd the AA011, ..., AA019, etc
So crafted the following mapper:
@Mapping(target = ".", source = "group")
public FilteredDE map(Result re);
@Mapping(target = ".", source = "record")
public FilteredDE map(Group re, @MappingTarget FilteredDE filt);
public FilteredDE map(Record re, @MappingTarget FilteredDE filt);
I want to pass the same instance to each mapper so in the end one instance will be populated with all the attribute values.
However the map methods are not called from each other so the instance is not passed between them:
@Override
public FilteredDE map(Result re) {
if ( re == null ) {
return null;
}
FilteredDE.FilteredDEBuilder filteredDE = FilteredDE.builder();
return filteredDE.build();
}
@Override
public FilteredDE map(Group re, FilteredDE filt) {
if ( re == null ) {
return filt;
}
return filt;
}
@Override
public FilteredDE map(Record re, FilteredDE filt) {
if ( re == null ) {
return filt;
}
if ( re.getAA001() != null ) {
filt.setAA001( re.getAA001() );
}
...
if ( re.getAZ029() != null ) {
filt.setAZ029( re.getAZ029() );
}
return filt;
Any hint please how I could achieve this in mapstruct? Can it be?
Thanks!
Share Improve this question asked Mar 17 at 16:15 ViktorViktor 1,4972 gold badges26 silver badges52 bronze badges1 Answer
Reset to default 0Probably it is not the best solution but it is compact enough, it is what I ended up writing:
public FilteredDE map(Result re) {
FilteredDE filteredDE = new FilteredDE();
for (Group group : re.getGroup()) {
for (Record record : block.getRecord()) {
map(record, filteredDE);
}
}
return filteredDE;
}
@BeanMapping(
nullValuePropertyMappingStrategy= NullValuePropertyMappingStrategy.IGNORE,
nullValueCheckStrategy=NullValueCheckStrategy.ALWAYS)
public abstract FilteredDE map(Record re, @MappingTarget FilteredDE filt);
Is there any better please?