Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

java - Add comment for audit bunch with Javers

I have Spring Boot app with javers-spring-boot-starter-sql and use it in a simple way:

@Override
@JaversAuditable
public Company save(Company company) {
    return companyRepository.save(company);
}

@Repository
@JaversSpringDataAuditable
public interface CompanyRepository
        extends JpaRepository<Company, Long>, JpaSpecificationExecutor<Company> {
}

And return reduced action log by Change.commitMetadata.id like

QueryBuilder jqlQuery = QueryBuilder.byClass(Account.class);
List<Change> changes = javers.findChanges(jqlQuery.build());
Set<ChangeBunch> bunches = changes.stream().collect(Collectors.groupingBy(this::getId))
        .values().stream()
        .map(item ->
                ChangeBunch.builder().changes(item).comment("My comment").build()
        ).collect(Collectors.toSet());
return javers.getJsonConverter().toJson(bunches);


private long getId(Change change) {
    return change.getCommitMetadata().orElseThrow().getId().getMajorId();
}

And get the output:

[
  {
    "comment": "My comment",
    "changes": [
      {
        "changeType": "ValueChange",
        "globalId": {
          "entity": "com.pravvich.demo.model.Account",
          "cdoId": 1
        },
        "commitMetadata": {
          "author": "unauthenticated",
          "properties": [],
          "commitDate": "2021-01-27T02:59:54.361",
          "commitDateInstant": "2021-01-26T23:59:54.361277500Z",
          "id": 7.00
        },
        "property": "number",
        "propertyChangeType": "PROPERTY_VALUE_CHANGED",
        "left": 6,
        "right": 100
      },
      {
        "changeType": "ValueChange",
        "globalId": {
          "entity": "com.pravvich.demo.model.Account",
          "cdoId": 1
        },
        "commitMetadata": {
          "author": "unauthenticated",
          "properties": [],
          "commitDate": "2021-01-27T02:59:54.361",
          "commitDateInstant": "2021-01-26T23:59:54.361277500Z",
          "id": 7.00
        },
        "property": "balance",
        "propertyChangeType": "PROPERTY_VALUE_CHANGED",
        "left": 10.8,
        "right": 200
      }
    ]
  }
]

But the comment is hardcoded I need to save the comment somehow in the database prefer just in javers table as metadata but how I can extend it? Perhaps I have another way to leave a comment for the change bunch?

question from:https://stackoverflow.com/questions/65918812/add-comment-for-audit-bunch-with-javers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...