| 1 | |
package com.ctrip.apollo.biz.service; |
| 2 | |
|
| 3 | |
import java.util.Collections; |
| 4 | |
import java.util.List; |
| 5 | |
|
| 6 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 7 | |
import org.springframework.stereotype.Service; |
| 8 | |
import org.springframework.transaction.annotation.Transactional; |
| 9 | |
|
| 10 | |
import com.ctrip.apollo.biz.entity.Audit; |
| 11 | |
import com.ctrip.apollo.biz.entity.Item; |
| 12 | |
import com.ctrip.apollo.biz.entity.Namespace; |
| 13 | |
import com.ctrip.apollo.biz.repository.ItemRepository; |
| 14 | |
import com.ctrip.apollo.biz.repository.NamespaceRepository; |
| 15 | |
import com.ctrip.apollo.common.utils.BeanUtils; |
| 16 | |
import com.ctrip.apollo.core.exception.NotFoundException; |
| 17 | |
|
| 18 | |
@Service |
| 19 | 1 | public class ItemService { |
| 20 | |
|
| 21 | |
@Autowired |
| 22 | |
private ItemRepository itemRepository; |
| 23 | |
|
| 24 | |
@Autowired |
| 25 | |
private NamespaceRepository namespaceRepository; |
| 26 | |
|
| 27 | |
@Autowired |
| 28 | |
private AuditService auditService; |
| 29 | |
|
| 30 | |
@Transactional |
| 31 | |
public void delete(long id, String owner) { |
| 32 | 0 | itemRepository.delete(id); |
| 33 | |
|
| 34 | 0 | auditService.audit(Item.class.getSimpleName(), id, Audit.OP.DELETE, owner); |
| 35 | 0 | } |
| 36 | |
|
| 37 | |
public Item findOne(String appId, String clusterName, String namespaceName, String key) { |
| 38 | 0 | Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, |
| 39 | |
clusterName, namespaceName); |
| 40 | 0 | if (namespace == null) { |
| 41 | 0 | throw new NotFoundException( |
| 42 | 0 | String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName)); |
| 43 | |
} |
| 44 | 0 | Item item = itemRepository.findByNamespaceIdAndKey(namespace.getId(), key); |
| 45 | 0 | return item; |
| 46 | |
} |
| 47 | |
|
| 48 | |
public Item findOne(long itemId) { |
| 49 | 0 | Item item = itemRepository.findOne(itemId); |
| 50 | 0 | return item; |
| 51 | |
} |
| 52 | |
|
| 53 | |
public List<Item> findItems(Long namespaceId) { |
| 54 | 0 | List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespaceId); |
| 55 | 0 | if (items == null) { |
| 56 | 0 | return Collections.emptyList(); |
| 57 | |
} |
| 58 | 0 | return items; |
| 59 | |
} |
| 60 | |
|
| 61 | |
public List<Item> findItems(String appId, String clusterName, String namespaceName) { |
| 62 | 0 | Namespace group = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName, |
| 63 | |
namespaceName); |
| 64 | 0 | if (group != null) { |
| 65 | 0 | return findItems(group.getId()); |
| 66 | |
} else { |
| 67 | 0 | return Collections.emptyList(); |
| 68 | |
} |
| 69 | |
} |
| 70 | |
|
| 71 | |
@Transactional |
| 72 | |
public Item save(Item entity) { |
| 73 | 0 | Item item = itemRepository.save(entity); |
| 74 | |
|
| 75 | 0 | auditService.audit(Item.class.getSimpleName(), item.getId(), Audit.OP.INSERT, |
| 76 | 0 | item.getDataChangeCreatedBy()); |
| 77 | |
|
| 78 | 0 | return item; |
| 79 | |
} |
| 80 | |
|
| 81 | |
@Transactional |
| 82 | |
public Item update(Item item) { |
| 83 | 0 | Item managedItem = itemRepository.findOne(item.getId()); |
| 84 | 0 | BeanUtils.copyEntityProperties(item, managedItem); |
| 85 | 0 | managedItem = itemRepository.save(managedItem); |
| 86 | |
|
| 87 | 0 | auditService.audit(Item.class.getSimpleName(), managedItem.getId(), Audit.OP.UPDATE, |
| 88 | 0 | managedItem.getDataChangeLastModifiedBy()); |
| 89 | |
|
| 90 | 0 | return managedItem; |
| 91 | |
} |
| 92 | |
|
| 93 | |
} |