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