//Java Application Code

import java.util.Date;
import java.util.Vector;
import watt.util.Values;
import watt.app.server.Service;
import watt.app.server.ServiceThread;

public class Competitor extends Service
{
    public static final String BASE_IFC = "Analyst.widls";
    public static final String[] services = {"Gartner","Forrester",
                                            "IDC","GigaWeb","Yankee"};

    public static final String[] serviceUrls = {"www.gartner.com",
                                               "www.forrester.com",
                                               "www.idc.com",
                                               "www.gigaweb.com",
                                               "www.yankee.com"};

        public static Values search(Values session, Values in)
        {
            Vector queries = new Vector();

            String[] stmp = (String[])in.get("svcList");
            if (stmp == null) return throwError("No services selected");
            int[] svcs = convertToInt(stmp);

            String[] qtmp = (String[])in.get("queryList");
            if (qtmp == null) return throwError("No search terms entered");

            for (int i=0; i<qtmp.length; i++)
                if (qtmp[i].trim().length() > 0)
                    queries.addElement(qtmp[i]);

                    int svcCount = svcs.length;
                    int queryCount = queries.size();

                    // start service threads
                    ServiceThread st[][] = 
                        new ServiceThread[svcCount][queryCount];
                    for (int i=0; i<svcCount; i++)
            {
                for (int j=0; j<queryCount; j++)
                {
                    Values args = new Values();
                    args.put("query",(String)queries.elementAt(j));
	        st[i][j] = startThread(args,session,services[svcs[i]]);
                }
            }

            // join service threads
            Values[] results = new Values[svcCount];

            for (int i=0; i<svcCount; i++)
            {
                Values[] q = new Values[queryCount];

                for (int j=0; j<queryCount; j++)
                q[j] = getData(services[svcs[i]], session, st[i][j]);

                results[i] = new Values();
                results[i].put("queries", q);
                results[i].put("name",services[svcs[i]]);
                results[i].put("url",serviceUrls[svcs[i]]);
            }

            Values out = new Values();
                   out.put("date", new Date().toString());
                   out.put("results",results);
                   return out;
        }
    static ServiceThread startThread(Values in, Values session, 
                                     String serviceName)
    {
            return doThreadInvoke(BASE_IFC, serviceName,      
                                              session, in);
    }
            static Values getData(String key, Values session, 
                                  ServiceThread st)
    {
         Values out = null;
         try
         {
             out = st.getData();
         }
             catch (Exception e)
         {
             out = throwError(e);
             out.put("url",key);
         }
         return out;
    }
    private static int[] convertToInt(String[] s)
    {
        int[] result = new int[s.length];
        for (int i=0; i<s.length; i++)
            result[i] = Integer.parseInt(s[i]);
        return result;
    }
}