How to map abstract class or interface as record or segment

48 views
Skip to first unread message

Giulio Buz

unread,
May 12, 2021, 9:03:38 AM5/12/21
to beanio-users
Hi Everyone!

How to use BeanIO annotations to define a segment of an interface? In my example of BeanWriter, I can only obtain the lineResponse with only @Field information, @Segment is not visible.

ParserWriter result:

2021051211362850OK Tutto bene

Desidered result of 1 line:

2021051211362850EPPDNY81 D91138414510 20210512 F OK Tutto bene

Response.class that contains list of LineResponse.class

@Data
@NoArgsConstructor
@Group(minOccurs=0, maxOccurs= -1, type = Response.class)
public class Response {
@Record(minOccurs=1, collection = List.class, name="responseLineList", type=LineResponse.class)
List<LineResponse> responseLines;
}

In the following class LineResponse.class, it is present a @Segment with an interface.

@Data
@NoArgsConstructor
@Record
public class LineResponse implements Serializable {
@Field(ordinal = 1, required = true, length=16, format = "yyyyMMddhhmmssSS")
Date elaboratedOn;
@Segment(ordinal =2, name="requestLine", minOccurs = 1, maxOccurs = 1)
RequestLine line;
@Field(ordinal = 3, required = true, length=9)
String errorCode;
@Field(ordinal = 4, length=36)
String errorMessage;
}

Code of interface:

public interface RequestLine {}

Code of concrete TestLine class:

@Data
@NoArgsConstructor
@Record
public class TestLine implements RequestLine {
@Field(required = true, length=4, maxLength=4)
private String recordType;
@Field(required = true, length=1, maxLength=1)
private String messageKO;
@Field(required = true, length=1, maxLength=1)
private String messageOK;
@Field(required = true, length=1, maxLength=1)
private String divisionCode;
@Field(required = true, length=13, maxLength=13)
private Integer progressive;
@Field(required = true, length=1, maxLength=1, defaultValue = "D")
private String prefixItemCode;
@Field(required = true, length=9, maxLength=9)
private String itemCode;
}

Code of concrete TestLineB class:

@Data
@NoArgsConstructor
@Record
public class TestLineB implements RequestLine {
@Field(required = true, length=9, maxLength=9)
private String itemCode;
@Field(required = true, length=7, maxLength=7)
private BigDecimal quantity;
@Field(required = false, length=1, maxLength=1)
private String cancellation;
@Field(required = true, length=3, maxLength=3)
private String productionLine;
}

This is the code of my test with StreamFactory

// Filling mock Response with lines
LineResponse lineResponse = new LineResponse();
lineResponse .setElaboratedOn(new Date());
lineResponse .setLine(testLine);
lineResponse .setErrorCode("OK");
lineResponse .setErrorMessage("Tutto bene");
List<LineResponse> lineResponseList = new ArrayList<>();
lineResponseList .add(lineResponse );
Response response = new Response();
response.setResponseLine(lineResponseList);
Writer writer = new StringWriter();
String writerName = "ResponseFile";
// create a StreamFactory
StreamFactory factory = StreamFactory.newInstance();
factory.define( new StreamBuilder(writerName)
          .writeOnly()
          .format("fixedlength")
          .parser(new FixedLengthParserBuilder())
          .addGroup(Response.class) );
BeanWriter out = factory.createWriter(writerName, writer);
// JAVA to Writer
out.write(response);

Thanks for help
Reply all
Reply to author
Forward
0 new messages