头图

The previous article "Introduction to the EBean ORM Framework-2. Field Encryption, Update Log and History" introduces many feature annotations, this article will introduce the entity draft function

The draft function is mainly used for some important content releases that require process approval or repeated revisions. First, draft the draft to confirm that there are no problems before publishing.

The biggest difference between this implementation plan and the previous completely independent implementation is that the draft and the official version of the data structure are independent of each other, but the program logic is integrated

One, set up a draft

@Draftable
@Builder
@Data
@Entity
@ToString(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "customer")
public class Customer extends BaseModel {

    public static final CustomerFinder find = new CustomerFinder();

    private String name;

    @DraftOnly
    Timestamp whenPublish;

    @DraftDirty
    Boolean draft;

    @DraftReset
    String author;

    @OneToMany(cascade = {CascadeType.ALL})
    List<Group> groupList;
}

@DraftableElement
@Entity
@Table(name = "c_group")
public class Group extends BaseModel {
    private String name;

}

1. Draft annotations

Provided on the main solid @Draftable annotations marked entity has a draft function; fruiting body at the head @DraftableElement annotation, the synchronization marker with the host entity maintains a draft state.

@DraftableElement supports @OneToMany and @ManyToMany relationship settings

2. @DraftOnly

This comment means that it only exists in the draft, and there is no such field in the data structure of the official version. Can be used for process approval records, draft modification records and other scenarios

3. DraftDirty

This annotation can be placed on a boolean attribute and only exists in the draft. When the draft is saved, it is automatically set to true ; when the draft is published, it is automatically set to false . This annotation can be used for whether the data is published

4. DraftReset

The value of this annotation attribute will be automatically set to NULL after the draft is published, and the value of the official version will not change

Second, the draft implementation

1. New

@Test
public void create() {
  List<Group> list = new ArrayList<>();
  list.add(Group.builder().name("abc").build());
  list.add(Group.builder().name("def").build());

  Customer customer = Customer.builder()
    .name("abc")
    .author("hypier")
    .groupList(list)
    .whenPublish(new Timestamp(System.currentTimeMillis()))
    .build();

  customer.save();
}

When the main entity and the sub-entity are saved at the same time, a draft database table will be created for the two data tables

When draft comments are set, the new operation will only be written to the xxx_draft table, the official version has no data

2. Inquiry

Customer customer = Customer.find.query()
              .where().idEq(1L)
              .asDraft()
              .findOne();

List<Customer> customers = Customer.find.query()
                .where().eq("draft", true)
                .asDraft()
                .findList();
asDraft() : Indicates the query draft data, and the official version data is removed

3. Update

@Test
public void update() {
    Customer customer = Customer.find.query()
            .where().idEq(1L)
            .asDraft()
            .findOne();

    customer.setName(UUID.randomUUID().toString());
    customer.update();
}

The updated source data must be a draft and cannot be directly modified on the official version data, otherwise an error will be reported

Draft content can be modified multiple times, but it does not affect the official version that has been released

4. Release

@Test
public void publish(){

    Database database = DB.getDefault();
    Customer customer = database.publish(Customer.class, 1L);

    System.err.println(customer);
}
After release, the draft data is synchronously written into the data structure of the official version

5. Recovery

@Test
public void restore(){
    Database database = DB.getDefault();
    Customer customer = database.draftRestore(Customer.class, 1L);

    System.err.println(customer);
}
When the draft has been modified many times and you don’t want to update it, you can use the restore command to restore the latest official version data

3. Summary

The code in the article is not a complete logic due to the length of the omission. If you are interested, please Fork the source code https://gitee.com/hypier/barry-ebean/tree/master/ebean-section-3

4. Please pay attention to my official account

请关注我的公众号


barry的异想世界
20 声望3 粉丝

引用和评论

0 条评论