##### Coverage Report - com.ctrip.framework.apollo.adminservice.controller.ItemController

Classes in this File | Line Coverage | Branch Coverage | Complexity
ItemController |
16% | 10/60
14% | 2/14
| 3.0;3

1 |  |
     package com.ctrip.framework.apollo.adminservice.controller;
2 |  |
3 |  |
     import java.util.LinkedList;
4 |  |
     import java.util.List;
5 |  |
6 |  |
     import org.springframework.beans.factory.annotation.Autowired;
7 |  |
     import org.springframework.web.bind.annotation.PathVariable;
8 |  |
     import org.springframework.web.bind.annotation.RequestBody;
9 |  |
     import org.springframework.web.bind.annotation.RequestMapping;
10 |  |
     import org.springframework.web.bind.annotation.RequestMethod;
11 |  |
     import org.springframework.web.bind.annotation.RequestParam;
12 |  |
     import org.springframework.web.bind.annotation.RestController;
13 |  |
14 |  |
     import com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock;
15 |  |
     import com.ctrip.framework.apollo.biz.entity.Commit;
16 |  |
     import com.ctrip.framework.apollo.biz.entity.Item;
17 |  |
     import com.ctrip.framework.apollo.biz.entity.Namespace;
18 |  |
     import com.ctrip.framework.apollo.biz.service.CommitService;
19 |  |
     import com.ctrip.framework.apollo.biz.service.ItemService;
20 |  |
     import com.ctrip.framework.apollo.biz.service.NamespaceService;
21 |  |
     import com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder;
22 |  |
     import com.ctrip.framework.apollo.common.utils.BeanUtils;
23 |  |
     import com.ctrip.framework.apollo.core.dto.ItemDTO;
24 |  |
     import com.ctrip.framework.apollo.core.exception.NotFoundException;
25 |  |
26 |  |
     @RestController
27 | 1 |
     public class ItemController {
28 |  |
29 |  |
       @Autowired
30 |  |
       private ItemService itemService;
31 |  |
       @Autowired
32 |  |
       private NamespaceService namespaceService;
33 |  |
       @Autowired
34 |  |
       private CommitService commitService;
35 |  |
36 |  |
       @PreAcquireNamespaceLock
37 |  |
       @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
38 |  |
       public ItemDTO createOrUpdate(@PathVariable("appId") String appId,
39 |  |
                                     @PathVariable("clusterName") String clusterName,
40 |  |
                                     @PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto) {
41 | 0 |
         Item entity = BeanUtils.transfrom(Item.class, dto);
42 |  |
43 | 0 |
         ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
44 | 0 |
         Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
45 | 0 |
         if (managedEntity != null) {
46 | 0 |
           Item beforeUpdateItem = BeanUtils.transfrom(Item.class, managedEntity);
47 | 0 |
           BeanUtils.copyEntityProperties(entity, managedEntity);
48 | 0 |
           entity = itemService.update(managedEntity);
49 | 0 |
           builder.updateItem(beforeUpdateItem, entity);
50 | 0 |
         } else {
51 | 0 |
           Item lastItem = itemService.findLastOne(appId, clusterName, namespaceName);
52 | 0 |
           int lineNum = 1;
53 | 0 |
           if (lastItem != null) {
54 | 0 |
             Integer lastItemNum = lastItem.getLineNum();
55 | 0 |
             lineNum = lastItemNum == null ? 1 : lastItemNum + 1;
56 |  |
           }
57 | 0 |
           entity.setLineNum(lineNum);
58 | 0 |
           entity = itemService.save(entity);
59 | 0 |
           builder.createItem(entity);
60 |  |
         }
61 | 0 |
         dto = BeanUtils.transfrom(ItemDTO.class, entity);
62 |  |
63 | 0 |
         Commit commit = new Commit();
64 | 0 |
         commit.setAppId(appId);
65 | 0 |
         commit.setClusterName(clusterName);
66 | 0 |
         commit.setNamespaceName(namespaceName);
67 | 0 |
         commit.setChangeSets(builder.build());
68 | 0 |
         commit.setDataChangeCreatedBy(dto.getDataChangeLastModifiedBy());
69 | 0 |
         commit.setDataChangeLastModifiedBy(dto.getDataChangeLastModifiedBy());
70 | 0 |
         commitService.save(commit);
71 |  |
72 | 0 |
         return dto;
73 |  |
       }
74 |  |
75 |  |
       @PreAcquireNamespaceLock
76 |  |
       @RequestMapping(path = "/items/{itemId}", method = RequestMethod.DELETE)
77 |  |
       public void delete(@PathVariable("itemId") long itemId, @RequestParam String operator) {
78 | 0 |
         Item entity = itemService.findOne(itemId);
79 | 0 |
         if (entity == null) {
80 | 0 |
           throw new NotFoundException("item not found for itemId " + itemId);
81 |  |
         }
82 | 0 |
         itemService.delete(entity.getId(), operator);
83 |  |
84 | 0 |
         Namespace namespace = namespaceService.findOne(entity.getNamespaceId());
85 |  |
86 | 0 |
         Commit commit = new Commit();
87 | 0 |
         commit.setAppId(namespace.getAppId());
88 | 0 |
         commit.setClusterName(namespace.getClusterName());
89 | 0 |
         commit.setNamespaceName(namespace.getNamespaceName());
90 | 0 |
         commit.setChangeSets(new ConfigChangeContentBuilder().deleteItem(entity).build());
91 | 0 |
         commit.setDataChangeCreatedBy(operator);
92 | 0 |
         commit.setDataChangeLastModifiedBy(operator);
93 | 0 |
         commitService.save(commit);
94 | 0 |
       }
95 |  |
96 |  |
       @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
97 |  |
       public List<ItemDTO> findItems(@PathVariable("appId") String appId,
98 |  |
                                      @PathVariable("clusterName") String clusterName,
99 |  |
                                      @PathVariable("namespaceName") String namespaceName) {
100 | 3 |
         List<Item> items = itemService.findItems(appId, clusterName, namespaceName);
101 | 3 |
         List<ItemDTO> itemDTOs = new LinkedList<>();
102 |  |
103 | 3 |
         for (Item item : items) {
104 | 9 |
           ItemDTO itemDTO = BeanUtils.transfrom(ItemDTO.class, item);
105 | 9 |
           itemDTO.setLastModifiedBy(item.getDataChangeLastModifiedBy());
106 | 9 |
           itemDTO.setLastModifiedTime(item.getDataChangeLastModifiedTime());
107 | 9 |
           itemDTOs.add(itemDTO);
108 | 9 |
         }
109 | 3 |
         return itemDTOs;
110 |  |
       }
111 |  |
112 |  |
       @RequestMapping("/items/{itemId}")
113 |  |
       public ItemDTO get(@PathVariable("itemId") long itemId) {
114 | 0 |
         Item item = itemService.findOne(itemId);
115 | 0 |
         if (item == null) {
116 | 0 |
           throw new NotFoundException("item not found for itemId " + itemId);
117 |  |
         }
118 | 0 |
         return BeanUtils.transfrom(ItemDTO.class, item);
119 |  |
       }
120 |  |
121 |  |
       @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key:.+}")
122 |  |
       public ItemDTO get(@PathVariable("appId") String appId,
123 |  |
                          @PathVariable("clusterName") String clusterName,
124 |  |
                          @PathVariable("namespaceName") String namespaceName, @PathVariable("key") String key) {
125 | 0 |
         Item item = itemService.findOne(appId, clusterName, namespaceName, key);
126 | 0 |
         if (item == null) {
127 | 0 |
           throw new NotFoundException(
128 | 0 |
               String.format("item not found for %s %s %s %s", appId, clusterName, namespaceName, key));
129 |  |
         }
130 | 0 |
         return BeanUtils.transfrom(ItemDTO.class, item);
131 |  |
       }
132 |  |
     }