Search in sources :

Example 1 with ConfigChangeContentBuilder

use of com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder in project apollo by ctripcorp.

the class ItemController method create.

@PreAcquireNamespaceLock
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
public ItemDTO create(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto) {
    Item entity = BeanUtils.transfrom(Item.class, dto);
    ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
    Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
    if (managedEntity != null) {
        throw new BadRequestException("item already exist");
    } else {
        entity = itemService.save(entity);
        builder.createItem(entity);
    }
    dto = BeanUtils.transfrom(ItemDTO.class, entity);
    Commit commit = new Commit();
    commit.setAppId(appId);
    commit.setClusterName(clusterName);
    commit.setNamespaceName(namespaceName);
    commit.setChangeSets(builder.build());
    commit.setDataChangeCreatedBy(dto.getDataChangeLastModifiedBy());
    commit.setDataChangeLastModifiedBy(dto.getDataChangeLastModifiedBy());
    commitService.save(commit);
    return dto;
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) Commit(com.ctrip.framework.apollo.biz.entity.Commit) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ConfigChangeContentBuilder

use of com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder in project apollo by ctripcorp.

the class ItemController method delete.

@PreAcquireNamespaceLock
@RequestMapping(path = "/items/{itemId}", method = RequestMethod.DELETE)
public void delete(@PathVariable("itemId") long itemId, @RequestParam String operator) {
    Item entity = itemService.findOne(itemId);
    if (entity == null) {
        throw new NotFoundException("item not found for itemId " + itemId);
    }
    itemService.delete(entity.getId(), operator);
    Namespace namespace = namespaceService.findOne(entity.getNamespaceId());
    Commit commit = new Commit();
    commit.setAppId(namespace.getAppId());
    commit.setClusterName(namespace.getClusterName());
    commit.setNamespaceName(namespace.getNamespaceName());
    commit.setChangeSets(new ConfigChangeContentBuilder().deleteItem(entity).build());
    commit.setDataChangeCreatedBy(operator);
    commit.setDataChangeLastModifiedBy(operator);
    commitService.save(commit);
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) Commit(com.ctrip.framework.apollo.biz.entity.Commit) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ConfigChangeContentBuilder

use of com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder 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.transfrom(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.transfrom(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.transfrom(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)

Example 4 with ConfigChangeContentBuilder

use of com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder in project apollo by ctripcorp.

the class ItemController method update.

@PreAcquireNamespaceLock
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}", method = RequestMethod.PUT)
public ItemDTO update(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @PathVariable("itemId") long itemId, @RequestBody ItemDTO itemDTO) {
    Item entity = BeanUtils.transfrom(Item.class, itemDTO);
    ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
    Item managedEntity = itemService.findOne(itemId);
    if (managedEntity == null) {
        throw new BadRequestException("item not exist");
    }
    Item beforeUpdateItem = BeanUtils.transfrom(Item.class, managedEntity);
    // protect. only value,comment,lastModifiedBy can be modified
    managedEntity.setValue(entity.getValue());
    managedEntity.setComment(entity.getComment());
    managedEntity.setDataChangeLastModifiedBy(entity.getDataChangeLastModifiedBy());
    entity = itemService.update(managedEntity);
    builder.updateItem(beforeUpdateItem, entity);
    itemDTO = BeanUtils.transfrom(ItemDTO.class, entity);
    if (builder.hasContent()) {
        Commit commit = new Commit();
        commit.setAppId(appId);
        commit.setClusterName(clusterName);
        commit.setNamespaceName(namespaceName);
        commit.setChangeSets(builder.build());
        commit.setDataChangeCreatedBy(itemDTO.getDataChangeLastModifiedBy());
        commit.setDataChangeLastModifiedBy(itemDTO.getDataChangeLastModifiedBy());
        commitService.save(commit);
    }
    return itemDTO;
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) Commit(com.ctrip.framework.apollo.biz.entity.Commit) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Item (com.ctrip.framework.apollo.biz.entity.Item)4 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)4 PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)3 Commit (com.ctrip.framework.apollo.biz.entity.Commit)3 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)2 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)2 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)1 Transactional (org.springframework.transaction.annotation.Transactional)1