Coverage Report - com.ctrip.framework.apollo.biz.service.ItemService
 
Classes in this File Line Coverage Branch Coverage Complexity
ItemService
1%
1/53
0%
0/18
2.6
 
 1  
 package com.ctrip.framework.apollo.biz.service;
 2  
 
 3  
 import com.ctrip.framework.apollo.biz.entity.Audit;
 4  
 import com.ctrip.framework.apollo.biz.entity.Item;
 5  
 import com.ctrip.framework.apollo.biz.entity.Namespace;
 6  
 import com.ctrip.framework.apollo.biz.repository.ItemRepository;
 7  
 import com.ctrip.framework.apollo.biz.repository.NamespaceRepository;
 8  
 import com.ctrip.framework.apollo.common.utils.BeanUtils;
 9  
 import com.ctrip.framework.apollo.core.exception.BadRequestException;
 10  
 import com.ctrip.framework.apollo.core.exception.NotFoundException;
 11  
 import com.ctrip.framework.apollo.core.utils.StringUtils;
 12  
 
 13  
 import org.springframework.beans.factory.annotation.Autowired;
 14  
 import org.springframework.stereotype.Service;
 15  
 import org.springframework.transaction.annotation.Transactional;
 16  
 
 17  
 import java.util.Collections;
 18  
 import java.util.List;
 19  
 
 20  
 @Service
 21  1
 public class ItemService {
 22  
 
 23  
   @Autowired
 24  
   private ItemRepository itemRepository;
 25  
 
 26  
   @Autowired
 27  
   private NamespaceRepository namespaceRepository;
 28  
 
 29  
   @Autowired
 30  
   private AuditService auditService;
 31  
 
 32  
   @Autowired
 33  
   private ServerConfigService serverConfigService;
 34  
 
 35  
   @Transactional
 36  
   public Item delete(long id, String operator) {
 37  0
     Item item = itemRepository.findOne(id);
 38  0
     if (item == null) {
 39  0
       throw new IllegalArgumentException("item not exist. ID:" + id);
 40  
     }
 41  
 
 42  0
     item.setDeleted(true);
 43  0
     item.setDataChangeLastModifiedBy(operator);
 44  0
     Item deletedItem = itemRepository.save(item);
 45  
 
 46  0
     auditService.audit(Item.class.getSimpleName(), id, Audit.OP.DELETE, operator);
 47  0
     return deletedItem;
 48  
   }
 49  
 
 50  
   public Item findOne(String appId, String clusterName, String namespaceName, String key) {
 51  0
     Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
 52  
         clusterName, namespaceName);
 53  0
     if (namespace == null) {
 54  0
       throw new NotFoundException(
 55  0
           String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
 56  
     }
 57  0
     Item item = itemRepository.findByNamespaceIdAndKey(namespace.getId(), key);
 58  0
     return item;
 59  
   }
 60  
 
 61  
   public Item findLastOne(String appId, String clusterName, String namespaceName) {
 62  0
     Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
 63  
                                                                                         clusterName, namespaceName);
 64  0
     if (namespace == null) {
 65  0
       throw new NotFoundException(
 66  0
           String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
 67  
     }
 68  0
     Item item = itemRepository.findFirst1ByNamespaceIdOrderByLineNumDesc(namespace.getId());
 69  0
     return item;
 70  
   }
 71  
 
 72  
   public Item findOne(long itemId) {
 73  0
     Item item = itemRepository.findOne(itemId);
 74  0
     return item;
 75  
   }
 76  
 
 77  
   public List<Item> findItems(Long namespaceId) {
 78  0
     List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespaceId);
 79  0
     if (items == null) {
 80  0
       return Collections.emptyList();
 81  
     }
 82  0
     return items;
 83  
   }
 84  
   
 85  
   public List<Item> findItems(String appId, String clusterName, String namespaceName) {
 86  0
     Namespace group = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName,
 87  
         namespaceName);
 88  0
     if (group != null) {
 89  0
       return findItems(group.getId());
 90  
     } else {
 91  0
       return Collections.emptyList();
 92  
     }
 93  
   }
 94  
   
 95  
   @Transactional
 96  
   public Item save(Item entity) {
 97  0
     checkItemKeyLength(entity.getKey());
 98  0
     checkItemValueLength(entity.getValue());
 99  
 
 100  0
     entity.setId(0);//protection
 101  0
     Item item = itemRepository.save(entity);
 102  
 
 103  0
     auditService.audit(Item.class.getSimpleName(), item.getId(), Audit.OP.INSERT,
 104  0
         item.getDataChangeCreatedBy());
 105  
 
 106  0
     return item;
 107  
   }
 108  
 
 109  
   @Transactional
 110  
   public Item update(Item item) {
 111  0
     checkItemValueLength(item.getValue());
 112  0
     Item managedItem = itemRepository.findOne(item.getId());
 113  0
     BeanUtils.copyEntityProperties(item, managedItem);
 114  0
     managedItem = itemRepository.save(managedItem);
 115  
 
 116  0
     auditService.audit(Item.class.getSimpleName(), managedItem.getId(), Audit.OP.UPDATE,
 117  0
         managedItem.getDataChangeLastModifiedBy());
 118  
 
 119  0
     return managedItem;
 120  
   }
 121  
 
 122  
   private boolean checkItemValueLength(String value){
 123  0
     int lengthLimit = Integer.valueOf(serverConfigService.getValue("item.value.length.limit", "20000"));
 124  0
     if (!StringUtils.isEmpty(value) && value.length() > lengthLimit){
 125  0
       throw new BadRequestException("value too long. length limit:" + lengthLimit);
 126  
     }
 127  0
     return true;
 128  
   }
 129  
 
 130  
   private boolean checkItemKeyLength(String key){
 131  0
     int lengthLimit = Integer.valueOf(serverConfigService.getValue("item.key.length.limit", "128"));
 132  0
     if (!StringUtils.isEmpty(key) && key.length() > lengthLimit){
 133  0
       throw new BadRequestException("key too long. length limit:" + lengthLimit);
 134  
     }
 135  0
     return true;
 136  
   }
 137  
 
 138  
 }