| 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.Cluster; |
| 13 | |
import com.ctrip.apollo.biz.repository.ClusterRepository; |
| 14 | |
import com.ctrip.apollo.common.utils.BeanUtils; |
| 15 | |
import com.ctrip.apollo.core.ConfigConsts; |
| 16 | |
import com.ctrip.apollo.core.exception.ServiceException; |
| 17 | |
import com.google.common.base.Strings; |
| 18 | |
|
| 19 | |
@Service |
| 20 | 1 | public class ClusterService { |
| 21 | |
|
| 22 | |
@Autowired |
| 23 | |
private ClusterRepository clusterRepository; |
| 24 | |
|
| 25 | |
@Autowired |
| 26 | |
private AuditService auditService; |
| 27 | |
|
| 28 | |
public boolean isClusterNameUnique(String appId, String clusterName) { |
| 29 | 4 | Objects.requireNonNull(appId, "AppId must not be null"); |
| 30 | 4 | Objects.requireNonNull(clusterName, "ClusterName must not be null"); |
| 31 | 4 | return Objects.isNull(clusterRepository.findByAppIdAndName(appId, clusterName)); |
| 32 | |
} |
| 33 | |
|
| 34 | |
public Cluster findOne(String appId, String name) { |
| 35 | 0 | return clusterRepository.findByAppIdAndName(appId, name); |
| 36 | |
} |
| 37 | |
|
| 38 | |
public List<Cluster> findClusters(String appId) { |
| 39 | 3 | if (Strings.isNullOrEmpty(appId)) { |
| 40 | 0 | return Collections.emptyList(); |
| 41 | |
} |
| 42 | |
|
| 43 | 3 | List<Cluster> clusters = clusterRepository.findByAppId(appId); |
| 44 | 3 | if (clusters == null) { |
| 45 | 0 | return Collections.emptyList(); |
| 46 | |
} |
| 47 | 3 | return clusters; |
| 48 | |
} |
| 49 | |
|
| 50 | |
@Transactional |
| 51 | |
public Cluster save(Cluster entity) { |
| 52 | 0 | if (!isClusterNameUnique(entity.getAppId(), entity.getName())) { |
| 53 | 0 | throw new ServiceException("cluster not unique"); |
| 54 | |
} |
| 55 | 0 | Cluster cluster = clusterRepository.save(entity); |
| 56 | |
|
| 57 | 0 | auditService.audit(Cluster.class.getSimpleName(), cluster.getId(), Audit.OP.INSERT, |
| 58 | 0 | cluster.getDataChangeCreatedBy()); |
| 59 | |
|
| 60 | 0 | return cluster; |
| 61 | |
} |
| 62 | |
|
| 63 | |
@Transactional |
| 64 | |
public void delete(long id, String owner) { |
| 65 | 0 | clusterRepository.delete(id); |
| 66 | |
|
| 67 | 0 | auditService.audit(Cluster.class.getSimpleName(), id, Audit.OP.DELETE, owner); |
| 68 | 0 | } |
| 69 | |
|
| 70 | |
@Transactional |
| 71 | |
public Cluster update(Cluster cluster) { |
| 72 | 0 | Cluster managedCluster = |
| 73 | 0 | clusterRepository.findByAppIdAndName(cluster.getAppId(), cluster.getName()); |
| 74 | 0 | BeanUtils.copyEntityProperties(cluster, managedCluster); |
| 75 | 0 | managedCluster = clusterRepository.save(managedCluster); |
| 76 | |
|
| 77 | 0 | auditService.audit(Cluster.class.getSimpleName(), managedCluster.getId(), Audit.OP.UPDATE, |
| 78 | 0 | managedCluster.getDataChangeLastModifiedBy()); |
| 79 | |
|
| 80 | 0 | return managedCluster; |
| 81 | |
} |
| 82 | |
|
| 83 | |
@Transactional |
| 84 | |
public void createDefaultCluster(String appId, String createBy) { |
| 85 | 4 | if (!isClusterNameUnique(appId, ConfigConsts.CLUSTER_NAME_DEFAULT)) { |
| 86 | 0 | throw new ServiceException("cluster not unique"); |
| 87 | |
} |
| 88 | 4 | Cluster cluster = new Cluster(); |
| 89 | 4 | cluster.setName(ConfigConsts.CLUSTER_NAME_DEFAULT); |
| 90 | 4 | cluster.setAppId(appId); |
| 91 | 4 | cluster.setDataChangeCreatedBy(createBy); |
| 92 | 4 | cluster.setDataChangeLastModifiedBy(createBy); |
| 93 | 4 | clusterRepository.save(cluster); |
| 94 | |
|
| 95 | 4 | auditService.audit(Cluster.class.getSimpleName(), cluster.getId(), Audit.OP.INSERT, createBy); |
| 96 | 4 | } |
| 97 | |
} |