package com.atlassian.sal.core.search.query;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.atlassian.sal.api.search.parameter.SearchParameter;
import com.atlassian.sal.api.search.query.SearchQuery;
import com.atlassian.sal.core.search.parameter.BasicSearchParameter;
import com.atlassian.sal.core.util.URIUtil;

/**
 */
public class DefaultSearchQuery implements SearchQuery
{
    private StringBuffer searchString = new StringBuffer();
    private Map<String, SearchParameter> parameters = new LinkedHashMap<String, SearchParameter>();

    public DefaultSearchQuery(String query)
	{
        append(query);
	}

    public SearchQuery setParameter(String name, String value)
    {
    	parameters.put(name, new BasicSearchParameter(name, value));
        return this;
    }

	public String getParameter(String name)
	{
		final SearchParameter value = parameters.get(name);
		
		return value == null ? null : value.getValue();
	}

	public String buildQueryString()
	{
		StringBuilder builder = new StringBuilder(searchString);
		for (SearchParameter parameter : parameters.values())
		{
			builder.append('&');
			builder.append(parameter.buildQueryString());
		}
		return builder.toString();
	}

	public SearchQuery append(String query)
	{
        if (StringUtils.isEmpty(query))
        {
            throw new IllegalArgumentException("Cannot parse empty query string!");
        }
        if (query.indexOf(SearchQuery.PARAMETER_SEPARATOR) == -1)
        {
            //looks like there's no params.
            searchString.append(query);
            return this;
        }
        
        final String[] strings = query.split(SearchQuery.PARAMETER_SEPARATOR);
        searchString.append(strings[0]);
        for (int i = 1; i < strings.length; i++)
        {
            String string = strings[i];
            BasicSearchParameter searchParam = new BasicSearchParameter(string);
            parameters.put(searchParam.getName(), searchParam);
        }
		return this;
	}

	public String getSearchString()
	{
		return URIUtil.decode(searchString.toString());
	}

	public int getParameter(String name, int defaultValue)
	{
		try
		{
			return Integer.parseInt(getParameter(name));
		}
        catch (NumberFormatException e)
		{
			// ignore
		}
		return defaultValue;
	}
	
}

@@ -2036,25 +2036,28 @@
 n this;%0A%09%7D%0A%0A
-%09
+    
 public Strin
@@ -2076,21 +2076,30 @@
 tring()%0A
-%09%7B%0A%09%09
+    %7B%0A        
 return U
@@ -2138,21 +2138,27 @@
 ing());%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public i
@@ -2208,24 +2208,48 @@
 ue)%0A
-%09%7B%0A%09%09try%0A%09%09%7B%0A%09%09%09
+    %7B%0A        try%0A        %7B%0A            
 retu
@@ -2289,18 +2289,24 @@
 name));%0A
-%09%09
+        
 %7D%0A      
@@ -2343,31 +2343,58 @@
  e)%0A
-%09%09%7B%0A%09%09%09// ignore%0A%09%09%7D%0A%09%09
+        %7B%0A            // ignore%0A        %7D%0A        
 retu
@@ -2410,15 +2410,17 @@
 tValue;%0A
-%09
+    
 %7D%0A
-%09
 %0A%7D%0A

Add a new Request.execute(ResponseHandler) method that returns a generic result Fixed indenting.