Migration to HRD - How to convert string-encoded k

2019-07-09 10:47发布

I have started to migrate from our M/S application to HRD. I wonder if you can recommend me how to convert old string encoded keys to new ones.

I know that string encoded keys holds the application name, although I am not certain if there are other details which I need to take care of. In addition, I could not find any forum-post, which has Java's code example of converting "string encoded keys" which looks quite strange to me (only python forums discuss it).

I will appreciate any help on this matter.

By the way I thought to run the following, although I'm afraid it will not work or cover all aspects:

    private static String convertKey(String encodedKey) {
    Key key = KeyFactory.stringToKey(encodedKey);       
    Key newKey = KeyFactory.createKey(key.getKind(), key.getId());      
    return KeyFactory.keyToString(newKey);
}

Thanks! Uri

3条回答
Lonely孤独者°
2楼-- · 2019-07-09 11:12

Why do you think you need to convert keys? The migration tool will copy over all your data, and in the process it will recreate the keys so that they correctly reference the new app.

查看更多
We Are One
3楼-- · 2019-07-09 11:18

The above code will work as long as the Entity is a Parent entity with no other Parents. If you have a Child Entity you will have to migrate the parent key as well.

For Example:

Key parentKey = key.getParent(); 
Key newParentKey = KeyFactory.createKey(parentKey.getKind(), parentKey.getId());

Only then you can use this code

Key newKey = KeyFactory.createKey(newParentKey,childKey.getKind(), childKey.getId());      

This example applies for only one parent, if you have multiple parents you will have to use this code a few times.

查看更多
Explosion°爆炸
4楼-- · 2019-07-09 11:27

With following recursive function generateKey(Key key) can be covered all cases (cascade parents too) for encoded string key conversions. Other two functions are wrappers for one end key collection conversions. http://www.geotrack24.com

private Key generateKey(Key key) {  
    Key parentKey = key.getParent();
    if(parentKey == null){
        return KeyFactory.createKey(key.getKind(), key.getId());  
    }else{          
        Key _newParentKey = generateKey(parentKey);         
        return KeyFactory.createKey(_newParentKey, key.getKind(), key.getId());  
    }  
}

private String convertKey(String encodedKey) {
    Key key = KeyFactory.stringToKey(encodedKey);      
    return KeyFactory.keyToString(generateKey(key));
}   

private Set<String> convertKeys(Set<String> keys){
    Set<String> newkeys = new HashSet<String>();
    for(String key: keys){
        newkeys.add(convertKey(key));
    }
    return newkeys;
}
查看更多
登录 后发表回答