Search in sources :

Example 16 with NotFoundException

use of com.ctrip.framework.apollo.common.exception.NotFoundException in project apollo by ctripcorp.

the class ItemSetService method updateSet.

@Transactional
public ItemChangeSets updateSet(String appId, String clusterName, String namespaceName, ItemChangeSets changeSet) {
    String operator = changeSet.getDataChangeLastModifiedBy();
    ConfigChangeContentBuilder configChangeContentBuilder = new ConfigChangeContentBuilder();
    if (!CollectionUtils.isEmpty(changeSet.getCreateItems())) {
        for (ItemDTO item : changeSet.getCreateItems()) {
            Item entity = BeanUtils.transform(Item.class, item);
            entity.setDataChangeCreatedBy(operator);
            entity.setDataChangeLastModifiedBy(operator);
            Item createdItem = itemService.save(entity);
            configChangeContentBuilder.createItem(createdItem);
        }
        auditService.audit("ItemSet", null, Audit.OP.INSERT, operator);
    }
    if (!CollectionUtils.isEmpty(changeSet.getUpdateItems())) {
        for (ItemDTO item : changeSet.getUpdateItems()) {
            Item entity = BeanUtils.transform(Item.class, item);
            Item managedItem = itemService.findOne(entity.getId());
            if (managedItem == null) {
                throw new NotFoundException(String.format("item not found.(key=%s)", entity.getKey()));
            }
            Item beforeUpdateItem = BeanUtils.transform(Item.class, managedItem);
            // protect. only value,comment,lastModifiedBy,lineNum can be modified
            managedItem.setValue(entity.getValue());
            managedItem.setComment(entity.getComment());
            managedItem.setLineNum(entity.getLineNum());
            managedItem.setDataChangeLastModifiedBy(operator);
            Item updatedItem = itemService.update(managedItem);
            configChangeContentBuilder.updateItem(beforeUpdateItem, updatedItem);
        }
        auditService.audit("ItemSet", null, Audit.OP.UPDATE, operator);
    }
    if (!CollectionUtils.isEmpty(changeSet.getDeleteItems())) {
        for (ItemDTO item : changeSet.getDeleteItems()) {
            Item deletedItem = itemService.delete(item.getId(), operator);
            configChangeContentBuilder.deleteItem(deletedItem);
        }
        auditService.audit("ItemSet", null, Audit.OP.DELETE, operator);
    }
    if (configChangeContentBuilder.hasContent()) {
        createCommit(appId, clusterName, namespaceName, configChangeContentBuilder.build(), changeSet.getDataChangeLastModifiedBy());
    }
    return changeSet;
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)16 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)8 Release (com.ctrip.framework.apollo.biz.entity.Release)7 Transactional (org.springframework.transaction.annotation.Transactional)7 Item (com.ctrip.framework.apollo.biz.entity.Item)4 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)4 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)3 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)3 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)3 PageRequest (org.springframework.data.domain.PageRequest)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)2 Commit (com.ctrip.framework.apollo.biz.entity.Commit)2 Instance (com.ctrip.framework.apollo.biz.entity.Instance)2 InstanceConfig (com.ctrip.framework.apollo.biz.entity.InstanceConfig)2 InstanceService (com.ctrip.framework.apollo.biz.service.InstanceService)2 ReleaseService (com.ctrip.framework.apollo.biz.service.ReleaseService)2 InstanceConfigDTO (com.ctrip.framework.apollo.common.dto.InstanceConfigDTO)2 InstanceDTO (com.ctrip.framework.apollo.common.dto.InstanceDTO)2 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)2