##### Coverage Report - com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder

Classes in this File | Line Coverage | Branch Coverage | Complexity
ConfigChangeContentBuilder |
0% | 0/23
0% | 0/6
| 1.6;1.6
ConfigChangeContentBuilder$ItemPair |
0% | 0/4
N/A | N/A
| 1.6;1.6

1 |  |
     package com.ctrip.framework.apollo.biz.utils;
2 |  |
3 |  |
     import com.google.gson.Gson;
4 |  |
     import com.google.gson.GsonBuilder;
5 |  |
6 |  |
     import com.ctrip.framework.apollo.biz.entity.Item;
7 |  |
     import com.ctrip.framework.apollo.common.utils.BeanUtils;
8 |  |
     import com.ctrip.framework.apollo.core.dto.ItemChangeSets;
9 |  |
     import com.ctrip.framework.apollo.core.dto.ItemDTO;
10 |  |
11 |  |
     import java.util.Date;
12 |  |
     import java.util.LinkedList;
13 |  |
     import java.util.List;
14 |  |
15 |  |
16 | 0 |
     public class ConfigChangeContentBuilder {
17 |  |
18 | 0 |
       private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
19 |  |
20 | 0 |
       private List<Item> createItems = new LinkedList<>();
21 | 0 |
       private List<ItemPair> updateItems = new LinkedList<>();
22 | 0 |
       private List<Item> deleteItems = new LinkedList<>();
23 |  |
24 |  |
25 |  |
       public ConfigChangeContentBuilder createItem(Item item) {
26 | 0 |
         createItems.add(item);
27 | 0 |
         return this;
28 |  |
       }
29 |  |
30 |  |
       public ConfigChangeContentBuilder updateItem(Item oldItem, Item newItem) {
31 | 0 |
         ItemPair itemPair = new ItemPair(oldItem, newItem);
32 | 0 |
         updateItems.add(itemPair);
33 | 0 |
         return this;
34 |  |
       }
35 |  |
36 |  |
       public ConfigChangeContentBuilder deleteItem(Item item) {
37 | 0 |
         deleteItems.add(item);
38 | 0 |
         return this;
39 |  |
       }
40 |  |
41 |  |
       public String build() {
42 |  |
         //因为事务第一段提交并没有更新时间,所以build时统一更新
43 | 0 |
         for (Item item : createItems) {
44 | 0 |
           item.setDataChangeLastModifiedTime(new Date());
45 | 0 |
         }
46 |  |
47 | 0 |
         for (ItemPair item : updateItems) {
48 | 0 |
           item.newItem.setDataChangeLastModifiedTime(new Date());
49 | 0 |
         }
50 |  |
51 | 0 |
         for (Item item : deleteItems) {
52 | 0 |
           item.setDataChangeLastModifiedTime(new Date());
53 | 0 |
         }
54 | 0 |
         return gson.toJson(this);
55 |  |
       }
56 |  |
57 | 0 |
       class ItemPair {
58 |  |
59 |  |
         Item oldItem;
60 |  |
         Item newItem;
61 |  |
62 | 0 |
         public ItemPair(Item oldItem, Item newItem) {
63 | 0 |
           this.oldItem = oldItem;
64 | 0 |
           this.newItem = newItem;
65 | 0 |
         }
66 |  |
       }
67 |  |
68 |  |
     }