Commit 58c1a20c by Yiming Liu

Merge conflicts

parent 7ccb2447
...@@ -4,11 +4,10 @@ import com.ctrip.apollo.core.dto.ItemChangeSets; ...@@ -4,11 +4,10 @@ import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.utils.StringUtils; import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.util.BeanUtils; import com.ctrip.apollo.portal.util.BeanUtils;
import com.sun.tools.javac.util.Assert;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -43,7 +42,7 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -43,7 +42,7 @@ public class PropertyResolver implements ConfigTextResolver {
ItemChangeSets changeSets = new ItemChangeSets(); ItemChangeSets changeSets = new ItemChangeSets();
result.setChangeSets(changeSets); result.setChangeSets(changeSets);
Map<Integer, String> newLineNumMapItem = new HashedMap();//use for delete blank and comment item Map<Integer, String> newLineNumMapItem = new HashMap();//use for delete blank and comment item
int lineCounter = 1; int lineCounter = 1;
for (String newItem : newItems) { for (String newItem : newItems) {
newItem = newItem.trim(); newItem = newItem.trim();
...@@ -82,13 +81,13 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -82,13 +81,13 @@ public class PropertyResolver implements ConfigTextResolver {
String oldComment = oldItemByLine == null ? "" : oldItemByLine.getComment(); String oldComment = oldItemByLine == null ? "" : oldItemByLine.getComment();
//create comment. implement update comment by delete old comment and create new comment //create comment. implement update comment by delete old comment and create new comment
if (!(isCommentItem(oldItemByLine) && newItem.equals(oldComment))) { if (!(isCommentItem(oldItemByLine) && newItem.equals(oldComment))) {
changeSets.addCreatedItem(buildCommentItem(0l, newItem, lineCounter)); changeSets.addCreateItem(buildCommentItem(0l, newItem, lineCounter));
} }
} }
private void handleBlankLine(ItemDTO oldItem, int lineCounter, ItemChangeSets changeSets) { private void handleBlankLine(ItemDTO oldItem, int lineCounter, ItemChangeSets changeSets) {
if (!isBlankItem(oldItem)) { if (!isBlankItem(oldItem)) {
changeSets.addCreatedItem(buildBlankItem(0l, lineCounter)); changeSets.addCreateItem(buildBlankItem(0l, lineCounter));
} }
} }
...@@ -110,7 +109,7 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -110,7 +109,7 @@ public class PropertyResolver implements ConfigTextResolver {
ItemDTO oldItem = keyMapOldItem.get(newKey); ItemDTO oldItem = keyMapOldItem.get(newKey);
if (oldItem == null) {//new item if (oldItem == null) {//new item
changeSets.addCreatedItem(buildNormalItem(0l, newKey, newValue, "", lineCounter)); changeSets.addCreateItem(buildNormalItem(0l, newKey, newValue, "", lineCounter));
} else if (!newValue.equals(oldItem.getValue())){//update item } else if (!newValue.equals(oldItem.getValue())){//update item
changeSets.addUpdateItem( changeSets.addUpdateItem(
buildNormalItem(oldItem.getId(), newKey, newValue, oldItem.getComment(), buildNormalItem(oldItem.getId(), newKey, newValue, oldItem.getComment(),
...@@ -139,7 +138,7 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -139,7 +138,7 @@ public class PropertyResolver implements ConfigTextResolver {
private void deleteNormalKVItem(Map<String, ItemDTO> baseKeyMapItem, ItemChangeSets changeSets) { private void deleteNormalKVItem(Map<String, ItemDTO> baseKeyMapItem, ItemChangeSets changeSets) {
//surplus item is to be deleted //surplus item is to be deleted
for (Map.Entry<String, ItemDTO> entry : baseKeyMapItem.entrySet()) { for (Map.Entry<String, ItemDTO> entry : baseKeyMapItem.entrySet()) {
changeSets.addDeletedItem(entry.getValue()); changeSets.addDeleteItem(entry.getValue());
} }
} }
...@@ -156,7 +155,7 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -156,7 +155,7 @@ public class PropertyResolver implements ConfigTextResolver {
//2.old is comment by now is not exist or modified //2.old is comment by now is not exist or modified
if ((isBlankItem(oldItem) && !isBlankItem(newItem)) if ((isBlankItem(oldItem) && !isBlankItem(newItem))
|| isCommentItem(oldItem) && (newItem == null || !newItem.equals(oldItem))) { || isCommentItem(oldItem) && (newItem == null || !newItem.equals(oldItem))) {
changeSets.addDeletedItem(oldItem); changeSets.addDeleteItem(oldItem);
} }
} }
} }
......
package com.ctrip.apollo.portal.service.txtresolver;
import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.util.BeanUtils;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* config item format is K:V##C
*
* @Autor lepdou
*/
@Component
public class SimpleKVCResolver implements ConfigTextResolver {
private static final String KV_SEPARATOR = ":";
private static final String VC_SEPARATOR = "##";
private static final String ITEM_SEPARATOR = "\n";
@Override
public TextResolverResult resolve(String configText, List<ItemDTO> baseItems) {
TextResolverResult result = new TextResolverResult();
if (StringUtils.isEmpty(configText)) {
result.setCode(TextResolverResult.Code.SIMPLE_KVC_TEXT_EMPTY);
return result;
}
Map<String, ItemDTO> baseKeyMapItem = BeanUtils.mapByKey("key", baseItems);
String[] items = configText.split(ITEM_SEPARATOR);
ItemChangeSets changeSets = new ItemChangeSets();
int lineCounter = 1;
int kvSeparator, vcSeparator;
String key, value, comment;
for (String item : items) {
kvSeparator = item.indexOf(KV_SEPARATOR);
vcSeparator = item.indexOf(VC_SEPARATOR);
if (kvSeparator == -1 || vcSeparator == -1) {
result.setCode(TextResolverResult.Code.SIMPLTE_KVC_INVALID_FORMAT);
result.setExtensionMsg(" line:" + lineCounter);
return result;
}
key = item.substring(0, kvSeparator).trim();
value = item.substring(kvSeparator + 1, vcSeparator).trim();
comment = item.substring(vcSeparator + 2, item.length()).trim();
ItemDTO baseItem = baseKeyMapItem.get(key);
if (baseItem == null) {//new item
changeSets.addCreateItem(buildItem(key, value, comment));
} else if (!value.equals(baseItem.getValue()) || !comment.equals(baseItem.getComment())) {//update item
changeSets.addUpdateItem(buildItem(key, value, comment));
}
//deleted items:items in baseItems but not in configText
baseKeyMapItem.remove(key);
lineCounter ++;
}
//deleted items
for (Map.Entry<String, ItemDTO> entry : baseKeyMapItem.entrySet()) {
changeSets.addDeleteItem(entry.getValue());
}
result.setCode(TextResolverResult.Code.OK);
result.setChangeSets(changeSets);
return result;
}
private ItemDTO buildItem(String key, String value, String comment) {
ItemDTO item = new ItemDTO();
item.setKey(key);
item.setValue(value);
item.setComment(comment);
return item;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment