use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class FileTextResolver method createItem.
private ItemDTO createItem(long namespaceId, long itemId, String value) {
ItemDTO item = new ItemDTO();
item.setId(itemId);
item.setNamespaceId(namespaceId);
item.setValue(value);
item.setLineNum(1);
item.setKey(ConfigConsts.CONFIG_FILE_CONTENT_KEY);
return item;
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class PropertyResolver method deleteCommentAndBlankItem.
private void deleteCommentAndBlankItem(Map<Integer, ItemDTO> oldLineNumMapItem, Map<Integer, String> newLineNumMapItem, ItemChangeSets changeSets) {
for (Map.Entry<Integer, ItemDTO> entry : oldLineNumMapItem.entrySet()) {
int lineNum = entry.getKey();
ItemDTO oldItem = entry.getValue();
String newItem = newLineNumMapItem.get(lineNum);
// 2.old is comment by now is not exist or modified
if ((isBlankItem(oldItem) && !isBlankItem(newItem)) || isCommentItem(oldItem) && (newItem == null || !newItem.equals(oldItem.getComment()))) {
changeSets.addDeleteItem(oldItem);
}
}
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class PropertyResolver method resolve.
@Override
public ItemChangeSets resolve(long namespaceId, String configText, List<ItemDTO> baseItems) {
Map<Integer, ItemDTO> oldLineNumMapItem = BeanUtils.mapByKey("lineNum", baseItems);
Map<String, ItemDTO> oldKeyMapItem = BeanUtils.mapByKey("key", baseItems);
// remove comment and blank item map.
oldKeyMapItem.remove("");
String[] newItems = configText.split(ITEM_SEPARATOR);
Set<String> repeatKeys = new HashSet<>();
if (isHasRepeatKey(newItems, repeatKeys)) {
throw new BadRequestException(String.format("Config text has repeated keys: %s, please check your input.", repeatKeys.toString()));
}
ItemChangeSets changeSets = new ItemChangeSets();
// use for delete blank and comment item
Map<Integer, String> newLineNumMapItem = new HashMap<>();
int lineCounter = 1;
for (String newItem : newItems) {
newItem = newItem.trim();
newLineNumMapItem.put(lineCounter, newItem);
ItemDTO oldItemByLine = oldLineNumMapItem.get(lineCounter);
// comment item
if (isCommentItem(newItem)) {
handleCommentLine(namespaceId, oldItemByLine, newItem, lineCounter, changeSets);
// blank item
} else if (isBlankItem(newItem)) {
handleBlankLine(namespaceId, oldItemByLine, lineCounter, changeSets);
// normal item
} else {
handleNormalLine(namespaceId, oldKeyMapItem, newItem, lineCounter, changeSets);
}
lineCounter++;
}
deleteCommentAndBlankItem(oldLineNumMapItem, newLineNumMapItem, changeSets);
deleteNormalKVItem(oldKeyMapItem, changeSets);
return changeSets;
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class PropertyResolver method handleNormalLine.
private void handleNormalLine(Long namespaceId, Map<String, ItemDTO> keyMapOldItem, String newItem, int lineCounter, ItemChangeSets changeSets) {
String[] kv = parseKeyValueFromItem(newItem);
if (kv == null) {
throw new BadRequestException("line:" + lineCounter + " key value must separate by '='");
}
String newKey = kv[0];
// handle user input \n
String newValue = kv[1].replace("\\n", "\n");
ItemDTO oldItem = keyMapOldItem.get(newKey);
if (oldItem == null) {
// new item
changeSets.addCreateItem(buildNormalItem(0L, namespaceId, newKey, newValue, "", lineCounter));
} else if (!newValue.equals(oldItem.getValue()) || lineCounter != oldItem.getLineNum()) {
// update item
changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, newKey, newValue, oldItem.getComment(), lineCounter));
}
keyMapOldItem.remove(newKey);
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class ConfigServiceTest method mockBaseItemHas3Key.
/**
* a=b b=c c=d
*/
private List<ItemDTO> mockBaseItemHas3Key() {
ItemDTO item1 = new ItemDTO("a", "b", "", 1);
ItemDTO item2 = new ItemDTO("b", "c", "", 2);
ItemDTO item3 = new ItemDTO("c", "d", "", 3);
return Arrays.asList(item1, item2, item3);
}
Aggregations