package org.zstack.tag;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by lining on 2017/6/22.
 */
public class SystemTagUtils {

    public static String findTagValue(List<String> systemTags, PatternedSystemTag tag, String tagToken){
        if(systemTags == null || tag == null || tagToken == null ){
            throw new IllegalArgumentException("illegal argument");
        }

        if(systemTags.isEmpty()){
            return null;
        }

        Optional<String> opt = systemTags.stream().filter(s -> tag.isMatch(s)).findAny();
        String result = opt.isPresent() ? tag.getTokenByTag(opt.get(), tagToken) : null;
        return result;
    }

    public static String findTagValue(List<String> systemTags, PatternedSystemTag tag){
        if(systemTags == null || tag == null){
            throw new IllegalArgumentException("illegal argument");
        }

        if(systemTags.isEmpty()){
            return null;
        }

        Optional<String> opt = systemTags.stream().filter(s -> tag.isMatch(s)).findAny();
        String result = opt.isPresent() ? opt.get() : null;
        return result;
    }

    public static List<String> findTagValues(List<String> systemTags, PatternedSystemTag tag, String tagToken) {
        if(systemTags == null || tag == null || tagToken == null ){
            throw new IllegalArgumentException("illegal argument");
        }

        if(systemTags.isEmpty()){
            return null;
        }

        return systemTags.stream().filter(tag::isMatch).map(t -> tag.getTokenByTag(t, tagToken)).collect(Collectors.toList());
    }

    public static List<String> findTagValues(List<String> systemTags, PatternedSystemTag tag) {
        if(systemTags == null || tag == null){
            throw new IllegalArgumentException("illegal argument");
        }

        if(systemTags.isEmpty()){
            return null;
        }

        return systemTags.stream().filter(tag::isMatch).collect(Collectors.toList());
    }
}

@@ -29,85 +29,282 @@
 ort 
-java.util.List;%0Aimport java.util.Optional;%0Aimport java.util.stream.Collectors
+org.zstack.header.tag.SystemTagInventory;%0A%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Optional;%0Aimport java.util.stream.Collectors;%0A%0Aimport static org.zstack.utils.CollectionDSL.e;%0Aimport static org.zstack.utils.CollectionDSL.map
 ;%0A%0A/
@@ -2218,11 +2218,1471 @@
 );%0A    %7D
+%0A%0A    public static void cloneTag(PatternedSystemTag tag, String tagToken, String srcUuid, String destUuid)%7B%0A        if (tagToken == null) %7B%0A            return;%0A        %7D%0A        List%3CSystemTagInventory%3E srcTags = tag.getTagInventories(srcUuid);%0A%0A        if (srcTags == null %7C%7C srcTags.isEmpty()) %7B%0A            return;%0A        %7D%0A%0A        for(SystemTagInventory srcTag: srcTags) %7B%0A            SystemTagCreator creator = tag.newSystemTagCreator(destUuid);%0A            creator.setTagByTokens(map(e(tagToken, tag.getTokenByResourceUuid(srcUuid, tagToken))));%0A            creator.inherent = srcTag.isInherent();%0A            creator.ignoreIfExisting = true;%0A            creator.create();%0A        %7D%0A    %7D%0A%0A    public static void cloneTag(PatternedSystemTag tag, String srcUuid, String destUuid, String... tokens)%7B%0A        List%3CSystemTagInventory%3E srcTags = tag.getTagInventories(srcUuid);%0A%0A        if (srcTags == null %7C%7C srcTags.isEmpty()) %7B%0A            return;%0A        %7D%0A%0A        Map%3CString, String%3E tokenMap = new HashMap%3C%3E();%0A        for (String token : tokens) %7B%0A            tokenMap.put(token, tag.getTokenByResourceUuid(srcUuid, token));%0A        %7D%0A%0A        for(SystemTagInventory srcTag: srcTags) %7B%0A            SystemTagCreator creator = tag.newSystemTagCreator(destUuid);%0A            creator.setTagByTokens(tokenMap);%0A            creator.inherent = srcTag.isInherent();%0A            creator.ignoreIfExisting = true;%0A            creator.create();%0A        %7D%0A    %7D
 %0A%7D%0A

add clonetag to tagUtils