0

我是 Bean-IO 的新手,当文件中存在特定记录类型时,我试图为组的出现配置验证逻辑。例如,如果平面文件中有三个记录,如下所示。

560866
670972
57086659

我正在尝试设置以下逻辑

  1. 56 行和 67 行一起形成多行记录
  2. 56&67 记录可以独立于记录 57,但 57 记录不能没有 56&67。

我成功地在 @record 注释中使用 minOccurs 属性创建了第一个验证,但无法使用组对 56&67 执行相同的操作。

请在下面找到示例代码设置。

HeaderRecord 类保存 56&67 条记录详细信息

@Group
public class HeaderRecord {
    @Record(minOccurs = 1)
    public TX56 tx56;
    @Record(minOccurs = 1)
    public TX67 tx67;
}

RecordObject 用于保存标题和行项目

public class RecordObject {
@Group(collection = List.class, minOccurs = 1)
    List<HeaderRecord> headerRecords;
    @Record(collection = List.class)
    List<TX57> tx57s;
}

@Record(maxLength = 10, name = "TX56")
public class TX56 {
    @Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "56", trim = true)
    protected int id;
    @Field(ordinal = 1, at = 2, length = 4, trim = true)
    protected int number;
}

@Record(maxLength = 31, name = "TX67")
public class TX67 {
    @Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "67", trim = true)
    protected int id;
    @Field(ordinal = 1, at = 2, length = 4, trim = true)
    protected int number;
}

@Record(maxLength = 71, name = "TX57")
public class TX57 {
    @Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "57", trim = true)
    protected int id;
    @Field(ordinal = 1, at = 2, length = 4, trim = true)
    protected int number;
}

使用上面的配置,当我尝试使用下面给出的记录解析文件时,它会抛出 UnexpectedRecordException。
560866
670972
57086659

堆栈跟踪:

2018-07-17 15:22:07,778[http-nio-8080-exec-2]ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]-Servlet。 service()for servlet [dispatcherServlet] 在上下文中与路径 [] 引发异常 [请求处理失败;嵌套异常是 org.beanio.UnexpectedRecordException:到达流结束,预期记录 'tx56'] 根本原因 org.beanio.UnexpectedRecordException:到达流的末尾,预计在 org.beanio.internal.parser.UnmarshallingContext.newUnsatisfiedRecordException(UnmarshallingContext.java:367)~[beanio-2.1.0.jar:2.1.0] 处记录“tx56”。 parser.Group.unmarshal(Group.java:127)~[beanio-.1.0.jar:2.1.0] at org.beanio.internal.parser.DelegatingParser.unmarshal(DelegatingParser.java:39)~[beanio-2.1. 0.jar:2.1.0] 在 org.beanio.internal.parser。RecordCollection.unmarshal(RecordCollection.java:42)~[beanio-2.1.0.jar:2.1.0] at org.beanio.internal.parser.Group.unmarshal(Group.java:118)~[beanio-2.1.0 .jar:2.1.0] 在 org.beanio.internal.parser.BeanReaderImpl.internalRead(BeanReaderImpl.java:106)~[beanio-2.1.0.jar:2.1.0] 在 org.beanio.internal.parser.BeanReaderImpl .read(BeanReaderImpl.java:67)~[beanio-2.1.0.jar:2.1.0] 在 dk.coop.integration.fileconversion.service.sampleapplication.createFixedLengthFile(sampleapplication.java:32)~[classes/:? ]67)~[beanio-2.1.0.jar:2.1.0] 在 dk.coop.integration.fileconversion.service.sampleapplication.createFixedLengthFile(sampleapplication.java:32)~[classes/:?]67)~[beanio-2.1.0.jar:2.1.0] 在 dk.coop.integration.fileconversion.service.sampleapplication.createFixedLengthFile(sampleapplication.java:32)~[classes/:?]

注意:使用上述配置,以下场景有效

  • 56&67独立
    来 560866
    670972
  • 57 不能独立来
    57086659:此平面文件失败,但有适当的异常
  • 56&67 应该总是作为一个单一的记录出现。
    这也很好用。

其他详细信息:示例平面文件

560866
670972
560866
670972
560866
670972
57086659
57086659
57086659
57086659
52022
560866
670972
57086659

如上所示,在平面文件中,多个标头记录和 TX57 记录有可能作为一个实体出现。也可能有其他类型的记录介于两者之间,在这种情况下,我必须将第二次出现的 TX56,67 和 57 视为不同的项目。
在上面的示例中,前 10 条记录将形成一个记录对象,然后这些记录的第二次出现将形成第二个记录对象。很抱歉没有早点分享,但是还有另一个包含记录对象列表的包装类。

我在下面给出了工作中的 maven 项目 Github URL。https://github.com/Jayadeep2308/FlatFileParser

4

1 回答 1

0

编辑:在 2018 年 8 月 5 日完成所有要求后更新

我已经private在类中创建了所有字段,并假设您有 getter + setter。

@Group我已经尝试了和注释上的各种设置组合,@Record因此下面的代码可能不是最佳的,但应该可以工作。

WrapperObject首先是包含所有数据的主要组 ( ):

@Group(minOccurs = 1, maxOccurs = 1)
public class WrapperObject {

  @Group(minOccurs = 0, maxOccurs = 1, collection = List.class)
  private List<RecordObject> recordObjectList;
  @Record(minOccurs = 0, maxOccurs = -1, collection = List.class)
  private List<TX52> tx52s;
}

编辑RecordObject更新以保存列表HeaderRecord,也更改了@Group值。

@Group(minOccurs = 0, maxOccurs = -1)
public class RecordObject {

  @Group(minOccurs = 0, maxOccurs = -1, collection = List.class)
  private List<HeaderRecord> headerRecords;
  @Record(minOccurs = 0, maxOccurs = -1, collection = List.class)
  private List<TX57> tx57s;
}

@Group(minOccurs = 0, maxOccurs = -1)
public class HeaderRecord {

  @Record(minOccurs = 1, maxOccurs = 1)
  private TX56 tx56;
  @Record(minOccurs = 1, maxOccurs = 1)
  private TX67 tx67;
}

在各个 TX 记录上,我已在您的记录标识符字段的注释 required=true上添加了属性。编辑:添加 TX52@Field

@Record(maxLength = 74, name = "TX52")
public class TX52 {

  @Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "52", trim = true, required = true)
  private int id;
  @Field(ordinal = 1, at = 2, length = 3, trim = true)
  private int number;
}

@Record(maxLength = 10, name = "TX56")
public class TX56 {

  @Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "56", trim = true, required = true)
  private int id;
  @Field(ordinal = 1, at = 2, length = 4, trim = true, required = true)
  private int number;
}

@Record(maxLength = 31, name = "TX67")
public class TX67 {

  @Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "67", trim = true, required = true)
  private int id;
  @Field(ordinal = 1, at = 2, length = 4, trim = true)
  private int number;
}

@Record(maxLength = 71, name = "TX57")
public class TX57 {

  @Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "57", trim = true, required = true)
  private int id;
  @Field(ordinal = 1, at = 2, length = 4, trim = true)
  private int number;
}

最后,我的测试代码:(编辑:更新的测试数据)

@Test
public void test() {

  final StreamFactory factory = StreamFactory.newInstance();

  final StreamBuilder builder = new StreamBuilder("Jaydeep23")
    .format("fixedlength")
    .parser(new FixedLengthParserBuilder())
    .addGroup(WrapperObject.class);

  factory.define(builder);

  final String scenario1 = "560866\n670972\n560866\n670972\n560866\n670972";
  final String scenario2 = "560866\n670972\n560866\n670972\n560866\n670972\n57086659\n57086659\n57086659\n" +
  "57086659\n560866\n670972\n57086659\n560866\n670972";
  // invalid
  final String scenario3 = "57086659\n57086659\n57086659\n57086659\n57086659";
  final String scenario4 = "52022\n52066\n52054\n52120";
  final String scenario5 = scenario1;
  final String scenario6 = "560866\n670972\n560866\n670972\n560866\n670972\n57086659\n57086659\n57086659\n" +
  "57086659\n52021\n52022\n52023\n560866\n670972\n57086659\n52023";

  final String message = scenario1;
  BeanReader beanReader = null;
  Object object = null;
  try (final Reader in = new BufferedReader(new StringReader(message))) {
    beanReader = factory.createReader("Jaydeep23", in);
    beanReader.setErrorHandler(new LoggingBeanReaderErrorHandler());
    while ((object = beanReader.read()) != null) {
      System.out.println("Object = " + object);
    }
  } catch (final Exception e) {
    fail(e.getMessage());
  } finally {
    if (beanReader != null) {
      beanReader.close();
    }
  }
}

生成此输出:(编辑:使用您的toString()方法)

Scenario 1 = [[Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
]null]null

Scenario 2 = [[Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
][Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
]]null

Scenario 3 - gives this error (which is correct according as TX57 is not allowed on its own:
Expected record/group 'tx56' at line 6

Scenario 4 = null[Record Type = 52, Store Number = 22
, Record Type = 52, Store Number = 66
, Record Type = 52, Store Number = 54
, Record Type = 52, Store Number = 120
]

Scenario 5 = [[Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
]null]null

Scenario 6 = [[Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
][Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
]][Record Type = 52, Store Number = 21
, Record Type = 52, Store Number = 22
, Record Type = 52, Store Number = 23
]

希望这可以帮助。

让我知道它现在是否适合您。

于 2018-07-17T21:53:47.310 回答