Search in sources :

Example 6 with ItemDTO

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;
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO)

Example 7 with ItemDTO

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);
        }
    }
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with ItemDTO

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;
}
Also used : HashMap(java.util.HashMap) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) HashSet(java.util.HashSet)

Example 9 with ItemDTO

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);
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException)

Example 10 with ItemDTO

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);
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO)

Aggregations

ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)44 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)14 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)12 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)11 Test (org.junit.Test)11 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)7 Item (com.ctrip.framework.apollo.biz.entity.Item)6 OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)6 HashMap (java.util.HashMap)6 ItemBO (com.ctrip.framework.apollo.portal.entity.bo.ItemBO)5 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)4 ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)4 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Sql (org.springframework.test.context.jdbc.Sql)4 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)3 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)3 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)3 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)3 RestTemplate (org.springframework.web.client.RestTemplate)3