use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class NamespaceBranchService method calculateBranchChangeSet.
private ItemChangeSets calculateBranchChangeSet(String appId, Env env, String clusterName, String namespaceName, String branchName, String operator) {
NamespaceBO parentNamespace = namespaceService.loadNamespaceBO(appId, env, clusterName, namespaceName);
if (parentNamespace == null) {
throw new BadRequestException("base namespace not existed");
}
if (parentNamespace.getItemModifiedCnt() > 0) {
throw new BadRequestException("Merge operation failed. Because master has modified items");
}
List<ItemDTO> masterItems = itemService.findItems(appId, env, clusterName, namespaceName);
List<ItemDTO> branchItems = itemService.findItems(appId, env, branchName, namespaceName);
ItemChangeSets changeSets = itemsComparator.compareIgnoreBlankAndCommentItem(parentNamespace.getBaseInfo().getId(), masterItems, branchItems);
changeSets.setDeleteItems(Collections.emptyList());
changeSets.setDataChangeLastModifiedBy(operator);
return changeSets;
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class NamespaceService method parseDeletedItems.
private List<ItemBO> parseDeletedItems(List<ItemDTO> newItems, Map<String, String> releaseItems, Map<String, ItemDTO> deletedItemDTOs) {
Map<String, ItemDTO> newItemMap = BeanUtils.mapByKey("key", newItems);
List<ItemBO> deletedItems = new LinkedList<>();
for (Map.Entry<String, String> entry : releaseItems.entrySet()) {
String key = entry.getKey();
if (newItemMap.get(key) == null) {
ItemBO deletedItem = new ItemBO();
deletedItem.setDeleted(true);
ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO());
deletedItemDto.setKey(key);
String oldValue = entry.getValue();
deletedItem.setItem(deletedItemDto);
deletedItemDto.setValue(oldValue);
deletedItem.setModified(true);
deletedItem.setOldValue(oldValue);
deletedItem.setNewValue("");
deletedItems.add(deletedItem);
}
}
return deletedItems;
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class NamespaceService method transformNamespace2BO.
private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace) {
NamespaceBO namespaceBO = new NamespaceBO();
namespaceBO.setBaseInfo(namespace);
String appId = namespace.getAppId();
String clusterName = namespace.getClusterName();
String namespaceName = namespace.getNamespaceName();
fillAppNamespaceProperties(namespaceBO);
List<ItemBO> itemBOs = new LinkedList<>();
namespaceBO.setItems(itemBOs);
// latest Release
ReleaseDTO latestRelease;
Map<String, String> releaseItems = new HashMap<>();
Map<String, ItemDTO> deletedItemDTOs = new HashMap<>();
latestRelease = releaseService.loadLatestRelease(appId, env, clusterName, namespaceName);
if (latestRelease != null) {
releaseItems = GSON.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
}
// not Release config items
List<ItemDTO> items = itemService.findItems(appId, env, clusterName, namespaceName);
additionalUserInfoEnrichService.enrichAdditionalUserInfo(items, BaseDtoUserInfoEnrichedAdapter::new);
int modifiedItemCnt = 0;
for (ItemDTO itemDTO : items) {
ItemBO itemBO = transformItem2BO(itemDTO, releaseItems);
if (itemBO.isModified()) {
modifiedItemCnt++;
}
itemBOs.add(itemBO);
}
// deleted items
itemService.findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> {
deletedItemDTOs.put(item.getKey(), item);
});
List<ItemBO> deletedItems = parseDeletedItems(items, releaseItems, deletedItemDTOs);
itemBOs.addAll(deletedItems);
modifiedItemCnt += deletedItems.size();
namespaceBO.setItemModifiedCnt(modifiedItemCnt);
return namespaceBO;
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class ItemService method revokeItem.
public void revokeItem(String appId, Env env, String clusterName, String namespaceName) {
NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
if (namespace == null) {
throw new BadRequestException("namespace:" + namespaceName + " not exist in env:" + env + ", cluster:" + clusterName);
}
long namespaceId = namespace.getId();
Map<String, String> releaseItemDTOs = new HashMap<>();
ReleaseDTO latestRelease = releaseAPI.loadLatestRelease(appId, env, clusterName, namespaceName);
if (latestRelease != null) {
releaseItemDTOs = GSON.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
}
List<ItemDTO> baseItems = itemAPI.findItems(appId, env, clusterName, namespaceName);
Map<String, ItemDTO> oldKeyMapItem = BeanUtils.mapByKey("key", baseItems);
Map<String, ItemDTO> deletedItemDTOs = new HashMap<>();
// deleted items for comment
findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> {
deletedItemDTOs.put(item.getKey(), item);
});
ItemChangeSets changeSets = new ItemChangeSets();
AtomicInteger lineNum = new AtomicInteger(1);
releaseItemDTOs.forEach((key, value) -> {
ItemDTO oldItem = oldKeyMapItem.get(key);
if (oldItem == null) {
ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO());
changeSets.addCreateItem(buildNormalItem(0L, namespaceId, key, value, deletedItemDto.getComment(), lineNum.get()));
} else if (!oldItem.getValue().equals(value) || lineNum.get() != oldItem.getLineNum()) {
changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, key, value, oldItem.getComment(), lineNum.get()));
}
oldKeyMapItem.remove(key);
lineNum.set(lineNum.get() + 1);
});
oldKeyMapItem.forEach((key, value) -> changeSets.addDeleteItem(oldKeyMapItem.get(key)));
changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId());
updateItems(appId, env, clusterName, namespaceName, changeSets);
String formatStr = String.format("%s+%s+%s+%s", appId, env, clusterName, namespaceName);
Tracer.logEvent(TracerEventType.MODIFY_NAMESPACE_BY_TEXT, formatStr);
Tracer.logEvent(TracerEventType.MODIFY_NAMESPACE, formatStr);
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class ItemController method create.
@PreAcquireNamespaceLock
@PostMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
public ItemDTO create(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto) {
Item entity = BeanUtils.transform(Item.class, dto);
ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
if (managedEntity != null) {
throw new BadRequestException("item already exists");
}
entity = itemService.save(entity);
builder.createItem(entity);
dto = BeanUtils.transform(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;
}
Aggregations