package com.globalsight.tools;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;

import org.apache.commons.cli.CommandLine;

// TODO: add filters
public class ShowJobsCommand extends WebServiceCommand {

    @Override
    protected void execute(CommandLine command, UserData userData,
        WebService webService) throws Exception {
        List<String> args = command.getArgList();
        List<Job> jobs = webService.getJobs();
        if (args.size() == 0) {
            showAllJobs(jobs);
        }
        else {
            List<Job> jobsToShow = new ArrayList<Job>();
ARGLOOP:    
            for (String arg: args) {
                Long id = parseId(arg);
                if (id == null) {
                    System.out.println("Skipping '" + arg + "': not an id");
                    continue;
                }
                for (Job j : jobs) {
                    if (j.getId().equals(arg)) { // TODO: jobs need numeric IDs
                        jobsToShow.add(j);
                        continue ARGLOOP;
                    }
                }
                System.out.println("Skipping '" + arg + "': no such job");
            }
            if (jobsToShow.size() == 0) {
                return;
            }
            for (Job j : jobsToShow) {
                // XXX Only doing the first workflow
                Task t = webService.getCurrentTask(j.getWorkflows().get(0));
                System.out.println("Job " + j.getId() + ", task " + t.getId() +
                                ", " + t.getName() + " -- " + t.getState());
            }
        }
    }
    
    private Long parseId(String s) {
        try {
            return Long.valueOf(s);
        }
        catch (NumberFormatException e) {
            return null;
        }
    }

    void showAllJobs(List<Job> jobs) throws Exception {
        printHeader();
        for (Job job : jobs) {
            printShort(job);
        }
    }
    void printHeader() {
    	Formatter f = new Formatter(System.out);
    	f.format("%-8s%-8s%-12s%s\n", "ID", "WFID", "State", "Name");
    }
    void printShort(Job job) {
        Formatter f = new Formatter(System.out);
        // XXX Hack: just displaying the first WFId
        f.format("%-8s%-4s%-14s%s\n", job.getId(), 
        		job.getWorkflows().get(0).getId(), job.getDisplayState(), 
                 job.getName());
    }
    
    @Override
    public String getDescription() {
        return "List jobs for this company";
    }
}

@@ -1360,46 +1360,129 @@
     
-// XXX Only doing the first workflow%0D%0A
+System.out.println(%22Job %22 + j.getId() + %22: %22 + j.getName());%0D%0A                for (Workflow f : j.getWorkflows()) %7B%0D%0A    
     
@@ -1532,35 +1532,17 @@
 ask(
-j.getWorkflows().get(0));%0D%0A
+f);%0D%0A    
     
@@ -1577,55 +1577,115 @@
 ln(%22
-Job 
+%5Ct
 %22 + 
-j
+f
 .get
-Id() + %22, task %22 + t.getId() +%0D%0A  
+TargetLocale() + %0D%0A                                %22 - Task %22 + t.getId() + %22 %22 + t.getName() %0D%0A
     
@@ -1714,33 +1714,13 @@
     
-%22, %22 + t.getName()
+ 
  + %22
- --
  %22 +
@@ -1732,24 +1732,43 @@
 tState());%0D%0A
+                %7D%0D%0A
             

Print task info for all workflows