| 1 | |
package com.ctrip.apollo.biz.service; |
| 2 | |
|
| 3 | |
import java.util.Collections; |
| 4 | |
import java.util.List; |
| 5 | |
import java.util.Objects; |
| 6 | |
|
| 7 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 8 | |
import org.springframework.stereotype.Service; |
| 9 | |
import org.springframework.transaction.annotation.Transactional; |
| 10 | |
|
| 11 | |
import com.ctrip.apollo.biz.entity.Audit; |
| 12 | |
import com.ctrip.apollo.biz.entity.Namespace; |
| 13 | |
import com.ctrip.apollo.biz.repository.NamespaceRepository; |
| 14 | |
import com.ctrip.apollo.common.utils.BeanUtils; |
| 15 | |
import com.ctrip.apollo.core.ConfigConsts; |
| 16 | |
import com.ctrip.apollo.core.exception.ServiceException; |
| 17 | |
|
| 18 | |
@Service |
| 19 | 1 | public class NamespaceService { |
| 20 | |
|
| 21 | |
@Autowired |
| 22 | |
private NamespaceRepository namespaceRepository; |
| 23 | |
|
| 24 | |
@Autowired |
| 25 | |
private AuditService auditService; |
| 26 | |
|
| 27 | |
public boolean isNamespaceUnique(String appId, String cluster, String namespace) { |
| 28 | 4 | Objects.requireNonNull(appId, "AppId must not be null"); |
| 29 | 4 | Objects.requireNonNull(cluster, "Cluster must not be null"); |
| 30 | 4 | Objects.requireNonNull(namespace, "Namespace must not be null"); |
| 31 | 8 | return Objects.isNull( |
| 32 | 4 | namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, cluster, namespace)); |
| 33 | |
} |
| 34 | |
|
| 35 | |
@Transactional |
| 36 | |
public void delete(long id, String owner) { |
| 37 | 0 | namespaceRepository.delete(id); |
| 38 | |
|
| 39 | 0 | auditService.audit(Namespace.class.getSimpleName(), id, Audit.OP.DELETE, owner); |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
public Namespace findOne(Long namespaceId) { |
| 43 | 0 | return namespaceRepository.findOne(namespaceId); |
| 44 | |
} |
| 45 | |
|
| 46 | |
public Namespace findOne(String appId, String clusterName, String namespaceName) { |
| 47 | 0 | return namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName, |
| 48 | |
namespaceName); |
| 49 | |
} |
| 50 | |
|
| 51 | |
public List<Namespace> findNamespaces(String appId, String clusterName) { |
| 52 | 3 | List<Namespace> groups = namespaceRepository.findByAppIdAndClusterName(appId, clusterName); |
| 53 | 3 | if (groups == null) { |
| 54 | 0 | return Collections.emptyList(); |
| 55 | |
} |
| 56 | 3 | return groups; |
| 57 | |
} |
| 58 | |
|
| 59 | |
@Transactional |
| 60 | |
public Namespace save(Namespace entity) { |
| 61 | 0 | if (!isNamespaceUnique(entity.getAppId(), entity.getClusterName(), entity.getNamespaceName())) { |
| 62 | 0 | throw new ServiceException("namespace not unique"); |
| 63 | |
} |
| 64 | 0 | Namespace namespace = namespaceRepository.save(entity); |
| 65 | |
|
| 66 | 0 | auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.INSERT, |
| 67 | 0 | namespace.getDataChangeCreatedBy()); |
| 68 | |
|
| 69 | 0 | return namespace; |
| 70 | |
} |
| 71 | |
|
| 72 | |
@Transactional |
| 73 | |
public Namespace update(Namespace namespace) { |
| 74 | 0 | Namespace managedNamespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName( |
| 75 | 0 | namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName()); |
| 76 | 0 | BeanUtils.copyEntityProperties(namespace, managedNamespace); |
| 77 | 0 | managedNamespace = namespaceRepository.save(managedNamespace); |
| 78 | |
|
| 79 | 0 | auditService.audit(Namespace.class.getSimpleName(), managedNamespace.getId(), Audit.OP.UPDATE, |
| 80 | 0 | managedNamespace.getDataChangeLastModifiedBy()); |
| 81 | |
|
| 82 | 0 | return managedNamespace; |
| 83 | |
} |
| 84 | |
|
| 85 | |
@Transactional |
| 86 | |
public void createDefaultNamespace(String appId, String createBy) { |
| 87 | 4 | if (!isNamespaceUnique(appId, ConfigConsts.CLUSTER_NAME_DEFAULT, appId)) { |
| 88 | 0 | throw new ServiceException("namespace not unique"); |
| 89 | |
} |
| 90 | |
|
| 91 | 4 | Namespace ns = new Namespace(); |
| 92 | 4 | ns.setAppId(appId); |
| 93 | 4 | ns.setClusterName(ConfigConsts.CLUSTER_NAME_DEFAULT); |
| 94 | 4 | ns.setNamespaceName(ConfigConsts.NAMESPACE_DEFAULT); |
| 95 | 4 | ns.setDataChangeCreatedBy(createBy); |
| 96 | 4 | ns.setDataChangeLastModifiedBy(createBy); |
| 97 | 4 | namespaceRepository.save(ns); |
| 98 | |
|
| 99 | 4 | auditService.audit(Namespace.class.getSimpleName(), ns.getId(), Audit.OP.INSERT, createBy); |
| 100 | 4 | } |
| 101 | |
} |