| 1 | |
package com.ctrip.framework.apollo.biz.service; |
| 2 | |
|
| 3 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 4 | |
import org.springframework.stereotype.Service; |
| 5 | |
import org.springframework.transaction.annotation.Transactional; |
| 6 | |
import org.springframework.util.CollectionUtils; |
| 7 | |
|
| 8 | |
import com.ctrip.framework.apollo.biz.entity.Audit; |
| 9 | |
import com.ctrip.framework.apollo.biz.entity.Commit; |
| 10 | |
import com.ctrip.framework.apollo.biz.entity.Item; |
| 11 | |
import com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder; |
| 12 | |
import com.ctrip.framework.apollo.common.utils.BeanUtils; |
| 13 | |
import com.ctrip.framework.apollo.core.dto.ItemChangeSets; |
| 14 | |
import com.ctrip.framework.apollo.core.dto.ItemDTO; |
| 15 | |
import com.ctrip.framework.apollo.core.utils.StringUtils; |
| 16 | |
|
| 17 | |
|
| 18 | |
@Service |
| 19 | 1 | public class ItemSetService { |
| 20 | |
|
| 21 | |
@Autowired |
| 22 | |
private AuditService auditService; |
| 23 | |
|
| 24 | |
@Autowired |
| 25 | |
private CommitService commitService; |
| 26 | |
|
| 27 | |
@Autowired |
| 28 | |
private ItemService itemService; |
| 29 | |
|
| 30 | |
|
| 31 | |
@Transactional |
| 32 | |
public ItemChangeSets updateSet(String appId, String clusterName, |
| 33 | |
String namespaceName, ItemChangeSets changeSet) { |
| 34 | 0 | String operator = changeSet.getDataChangeLastModifiedBy(); |
| 35 | 0 | ConfigChangeContentBuilder configChangeContentBuilder = new ConfigChangeContentBuilder(); |
| 36 | |
|
| 37 | 0 | if (!CollectionUtils.isEmpty(changeSet.getCreateItems())) { |
| 38 | 0 | for (ItemDTO item : changeSet.getCreateItems()) { |
| 39 | 0 | Item entity = BeanUtils.transfrom(Item.class, item); |
| 40 | 0 | entity.setDataChangeCreatedBy(operator); |
| 41 | 0 | entity.setDataChangeLastModifiedBy(operator); |
| 42 | 0 | Item createdItem = itemService.save(entity); |
| 43 | 0 | configChangeContentBuilder.createItem(createdItem); |
| 44 | 0 | } |
| 45 | 0 | auditService.audit("ItemSet", null, Audit.OP.INSERT, operator); |
| 46 | |
} |
| 47 | |
|
| 48 | 0 | if (!CollectionUtils.isEmpty(changeSet.getUpdateItems())) { |
| 49 | 0 | for (ItemDTO item : changeSet.getUpdateItems()) { |
| 50 | 0 | Item entity = BeanUtils.transfrom(Item.class, item); |
| 51 | |
|
| 52 | 0 | Item beforeUpdateItem = itemService.findOne(entity.getId()); |
| 53 | 0 | if (beforeUpdateItem != null){ |
| 54 | 0 | beforeUpdateItem = BeanUtils.transfrom(Item.class, beforeUpdateItem); |
| 55 | |
} |
| 56 | |
|
| 57 | 0 | entity.setDataChangeLastModifiedBy(operator); |
| 58 | 0 | Item updatedItem = itemService.update(entity); |
| 59 | 0 | configChangeContentBuilder.updateItem(beforeUpdateItem, updatedItem); |
| 60 | |
|
| 61 | 0 | } |
| 62 | 0 | auditService.audit("ItemSet", null, Audit.OP.UPDATE, operator); |
| 63 | |
} |
| 64 | |
|
| 65 | 0 | if (!CollectionUtils.isEmpty(changeSet.getDeleteItems())) { |
| 66 | 0 | for (ItemDTO item : changeSet.getDeleteItems()) { |
| 67 | 0 | Item deletedItem = itemService.delete(item.getId(), operator); |
| 68 | 0 | configChangeContentBuilder.deleteItem(deletedItem); |
| 69 | 0 | } |
| 70 | 0 | auditService.audit("ItemSet", null, Audit.OP.DELETE, operator); |
| 71 | |
} |
| 72 | |
|
| 73 | 0 | String configChangeContent = configChangeContentBuilder.build(); |
| 74 | 0 | if (!StringUtils.isEmpty(configChangeContent)) { |
| 75 | 0 | createCommit(appId, clusterName, namespaceName, configChangeContentBuilder.build(), |
| 76 | 0 | changeSet.getDataChangeLastModifiedBy()); |
| 77 | |
} |
| 78 | |
|
| 79 | 0 | return changeSet; |
| 80 | |
|
| 81 | |
} |
| 82 | |
|
| 83 | |
private void createCommit(String appId, String clusterName, String namespaceName, String configChangeContent, |
| 84 | |
String operator) { |
| 85 | |
|
| 86 | 0 | Commit commit = new Commit(); |
| 87 | 0 | commit.setAppId(appId); |
| 88 | 0 | commit.setClusterName(clusterName); |
| 89 | 0 | commit.setNamespaceName(namespaceName); |
| 90 | 0 | commit.setChangeSets(configChangeContent); |
| 91 | 0 | commit.setDataChangeCreatedBy(operator); |
| 92 | 0 | commit.setDataChangeLastModifiedBy(operator); |
| 93 | 0 | commitService.save(commit); |
| 94 | 0 | } |
| 95 | |
|
| 96 | |
} |