Search in sources :

Example 6 with Commit

use of com.ctrip.framework.apollo.biz.entity.Commit in project apollo by ctripcorp.

the class ItemController method findDeletedItems.

@GetMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/deleted")
public List<ItemDTO> findDeletedItems(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName) {
    // get latest release time
    Release latestActiveRelease = releaseService.findLatestActiveRelease(appId, clusterName, namespaceName);
    List<Commit> commits;
    if (Objects.nonNull(latestActiveRelease)) {
        commits = commitService.find(appId, clusterName, namespaceName, latestActiveRelease.getDataChangeCreatedTime(), null);
    } else {
        commits = commitService.find(appId, clusterName, namespaceName, null);
    }
    if (Objects.nonNull(commits)) {
        List<Item> deletedItems = commits.stream().map(item -> ConfigChangeContentBuilder.convertJsonString(item.getChangeSets()).getDeleteItems()).flatMap(Collection::stream).collect(Collectors.toList());
        return BeanUtils.batchTransform(ItemDTO.class, deletedItems);
    }
    return Collections.emptyList();
}
Also used : Item(com.ctrip.framework.apollo.biz.entity.Item) Commit(com.ctrip.framework.apollo.biz.entity.Commit) Release(com.ctrip.framework.apollo.biz.entity.Release) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 7 with Commit

use of com.ctrip.framework.apollo.biz.entity.Commit in project apollo by ctripcorp.

the class ItemController method update.

@PreAcquireNamespaceLock
@PutMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}")
public ItemDTO update(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @PathVariable("itemId") long itemId, @RequestBody ItemDTO itemDTO) {
    Item managedEntity = itemService.findOne(itemId);
    if (managedEntity == null) {
        throw new NotFoundException("item not found for itemId " + itemId);
    }
    Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
    // In case someone constructs an attack scenario
    if (namespace == null || namespace.getId() != managedEntity.getNamespaceId()) {
        throw new BadRequestException("Invalid request, item and namespace do not match!");
    }
    Item entity = BeanUtils.transform(Item.class, itemDTO);
    ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
    Item beforeUpdateItem = BeanUtils.transform(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.transform(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) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)

Aggregations

Commit (com.ctrip.framework.apollo.biz.entity.Commit)7 Item (com.ctrip.framework.apollo.biz.entity.Item)5 PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)3 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)3 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)3 AbstractIntegrationTest (com.ctrip.framework.apollo.biz.AbstractIntegrationTest)2 Release (com.ctrip.framework.apollo.biz.entity.Release)2 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)2 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)2 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)2 Test (org.junit.Test)2 Sql (org.springframework.test.context.jdbc.Sql)2 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)1 InstanceConfig (com.ctrip.framework.apollo.biz.entity.InstanceConfig)1 ReleaseHistory (com.ctrip.framework.apollo.biz.entity.ReleaseHistory)1 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1