package org.ovirt.engine.core.bll.adbroker;

import java.util.EnumMap;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

public class SearchQueryFotmatter implements LdapQueryFormatter<LdapQueryExecution> {

    private final EnumMap<SearchLangageLDAPTokens, String> tokensToLDAPKeys;

    public SearchQueryFotmatter(EnumMap<SearchLangageLDAPTokens, String> tokensToLDAPKeys) {
        this.tokensToLDAPKeys = tokensToLDAPKeys;
    }

    /**
     * Replace the keywords generated by the SyntaxChecker class with the provider-type specific LDAP query. The
     * replacement is basically identifying Dollar sign, "$" as a prefix for the token and then fetching the token value
     * from the map. e.g. an search expression like this (&($LDAP_USER_ACCOUNT)($GIVENNAME=John)) should look
     * $LDAP_USER_ACCOUNT in
     */
    @Override
    public LdapQueryExecution format(LdapQueryMetadata queryMetadata) {

        String filter = (String) queryMetadata.getQueryData().getFilterParameters()[0];
        for (Entry<SearchLangageLDAPTokens, String> tokenEntry : tokensToLDAPKeys.entrySet()) {
            filter = StringUtils.replace(filter, tokenEntry.getKey().name(), tokenEntry.getValue());
        }
        String baseDN = queryMetadata.getBaseDN();

        return new LdapQueryExecution(filter,
                        filter, // The display filter in that case is like the filter
                baseDN,
                        queryMetadata.getContextMapper(),
                        queryMetadata.getSearchScope(),
                        queryMetadata.getReturningAttributes(),
                        queryMetadata.getQueryData().getDomain());
    }

    public static void main(String[] args) {
        String filter = "(&($LDAP_USER_ACCOUNT)($GIVENNAME=ggry))";
        EnumMap<SearchLangageLDAPTokens, String> map =
                new EnumMap<SearchLangageLDAPTokens, String>(SearchLangageLDAPTokens.class);
        map.put(SearchLangageLDAPTokens.$GIVENNAME, "givenname");
        map.put(SearchLangageLDAPTokens.$USER_ACCOUNT_TYPE, "samaccountname");

        for (Entry<SearchLangageLDAPTokens, String> tokenEntry : map.entrySet()) {
            filter = StringUtils.replace(filter, tokenEntry.getKey().name(), tokenEntry.getValue());
        }
        System.out.println(filter);
    }
}

@@ -1686,651 +1686,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        String filter = %22(&($LDAP_USER_ACCOUNT)($GIVENNAME=ggry))%22;%0A        EnumMap%3CSearchLangageLDAPTokens, String%3E map =%0A                new EnumMap%3CSearchLangageLDAPTokens, String%3E(SearchLangageLDAPTokens.class);%0A        map.put(SearchLangageLDAPTokens.$GIVENNAME, %22givenname%22);%0A        map.put(SearchLangageLDAPTokens.$USER_ACCOUNT_TYPE, %22samaccountname%22);%0A%0A        for (Entry%3CSearchLangageLDAPTokens, String%3E tokenEntry : map.entrySet()) %7B%0A            filter = StringUtils.replace(filter, tokenEntry.getKey().name(), tokenEntry.getValue());%0A        %7D%0A        System.out.println(filter);%0A    %7D
 %0A%7D%0A

Remove SearchQueryFormatter.main(String[])