package com.symcor.od.client.cmdline;

import java.util.*;
import java.io.*;
import javax.activation.*;
import com.symcor.od.client.soap.*;
import com.symcor.od.common.soap.message.*;
import com.symcor.od.common.config.ServerConfig;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class ODClient {

    public ODClient() {
    }
    public static void main(String[] args) {
        ODClient ODClient1 = new ODClient();
        if (args.length < 3) {
            System.out.println("usage: java soapServerURI serviceName methodName ");
            System.exit(-1);
        }

        String soapServerURI = args[0];
        String serviceName = args[1];
        String methodName = args[2];

        //logging
        System.out.println("soap server URI: " + soapServerURI);
        System.out.println("service name: " + serviceName);
        System.out.println("method name: " + methodName);

        SoapClient soapClient = new SoapClient(soapServerURI, serviceName);

        String configDir = ServerConfig.ARCHIVE_CONFIG_DIR;
        String appName = ServerConfig.ARCHIVE_APPLICATION_NAME;
        String serverName = ServerConfig.ARCHIVE_SERVER_NAME;
        String loginName = ServerConfig.ARCHIVE_LOGIN_NAME;
        String password = ServerConfig.ARCHIVE_LOGIN_PASSWORD;

        Vector folders = null;
        if (methodName.equals("logon")) {
            folders = soapClient.logon(configDir, appName, serverName, loginName, password);
            System.out.println("folders: \n" + folders);
        } else if (methodName.equals("openFolder")) {
            String folderName = "Retail Remittance JPEG"; // ServerConfig.ARCHIVE_FOLDER_NAME;
            Vector criteria = soapClient.openFolder(folderName);
            System.out.println("\n" + criteria);
        } else if (methodName.equals ("getFiles")) {
            String dataDir = "/home/jzhang/data/";
            Vector hits = soapClient.getFiles(dataDir);
            System.out.println("hits: \n" + hits);
        } else if (methodName.equals("search")) {
            Vector criteria = new Vector();

            Criterion processingDate = new Criterion("Processing Date [YYYY-MM-DD) [Required]");
            processingDate.setOperand(Criterion.OPBETWEEN);
            String[] dates = {"2002-10-01", "2002-10-04"};
            String[] fixedValues = {"2002-10-01", "2002-10-04"};
            String[] defaultValues = {"2002-10-01", "2002-10-04"};

            processingDate.setSearchValues(dates);
            processingDate.setDefaultValues(defaultValues);
            processingDate.setFixedValues(fixedValues);
            processingDate.setOperand(Criterion.OPBETWEEN);
            processingDate.setType(Criterion.TYPE_NON_LIST);
            criteria.add(processingDate);


            HitList hitList = soapClient.search(criteria);
//            HitList hitList = soapClient.search(new Vector());

            if (hitList == null) {
                System.out.println("hit list is null");
                System.exit(0);
            }
            Vector hits = hitList.getHits();
            System.out.println("hits: \n" + hits);
        } else if (methodName.equals("getDoc")) {
            String docId = "5687-5689-MIA1-3FAA-0-0-79827-79827-85-68-0-9-0";
            ImageDocument doc = soapClient.getDoc(docId);
            byte[] data = doc.getData();
            try {
                //write data back.
                String outputPath = "c:\\data.jpg";
                OutputStream os = new FileOutputStream(outputPath);
                os.write(data);
                os.flush();
                os.close();
                System.out.println("the retrieved image is saved to: " + outputPath);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

           // String folderName = "Retail Remittance JPEG"; ;
           // Vector criteria = soapClient.openFolder(folderName);
            //System.out.println("criteria: \n" + criteria);

        } else if (methodName.equals("getAtm")) {
            String docId = "5687-5689-MIA1-3FAA-0-0-79827-79827-85-68-0-9-0";
            ImageDocument doc = soapClient.getDocAsAtm(docId);
            try {
                DataHandler dh = doc.getDataHandler();
                DataSource ds = dh.getDataSource();
                InputStream is = ds.getInputStream();

                String outputPath = "c:\\dataAtm.jpg";
                OutputStream os = new FileOutputStream(outputPath);

                int c; //should be changed to use a buffer, size > 1.
                while ((c = is.read()) != -1)
                   os.write(c);


                os.flush();
                os.close();
                System.out.println("the retrieved image is saved to: " + outputPath);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }   else if (methodName.equals("getFile")) {
            String filePath = "/home/jzhang/data/earthx.jpg";

            ImageDocument doc = soapClient.getFile(filePath);
            DataHandler dh = doc.getDataHandler();
            DataSource ds = dh.getDataSource();
           try {
                //write data back.
                InputStream is = ds.getInputStream();
                String outputPath = "c:\\cheques2.gif";
                OutputStream os = new FileOutputStream(outputPath);

                byte[] buffer = new byte [4098];
                int count = -1;
                while ((count = is.read(buffer))>-1) {
                    os.write(buffer, 0, count);
                    os.flush();
                }
                os.close();
                System.out.println("the retrieved image is saved to: " + outputPath);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else if (methodName.equals("logoff")) {
            soapClient.logoff();
        } else {
            System.out.println("invalid method name. ");
            System.exit(0);
        }
    }
}
