
/*
 * $Id: ClockingEntryConnectionHelper.java,v 1.2 2003/06/16 13:31:27 tbayer Exp $
 *
 * Copyright (c) 2002 Orientation in Objects GmbH
 * Weinheimer Str. 68, D - 68309 Mannheim, Germany
 * All rights reserved.
 *
 * Reverse Engineering, Aendern und Erweitern dieser Software ist nur
 * mit ausdruecklicher schriftlicher Genehmigung von Orientation in
 * Objects gestattet.
 */

/*
   $Log: ClockingEntryConnectionHelper.java,v $
   Revision 1.2  2003/06/16 13:31:27  tbayer
   Umstellung auf JDK 1.4 Homepage

   Revision 1.1  2002/04/03 14:47:10  tbayer
   *** empty log message ***

 */
package de.oio.zeiterfassung.midp;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 * Hilfsklasse zum Senden von ClockingEntries an ein Servlet per HTTP.
 *
 * @author $Author: tbayer $
 * @version $Revision: 1.2 $<br/>
 * $Date: 2003/06/16 13:31:27 $<br/>
 */
public class ClockingEntryConnectionHelper {

    private static ClockingEntryConnectionHelper instance        = null;
    private String                               url             = "";
    private Vector                               clockingEntries = new Vector();

    private ClockingEntryConnectionHelper() {}

    public static ClockingEntryConnectionHelper getInstance() {

        if (instance == null) {
            instance = new ClockingEntryConnectionHelper();
        }

        return instance;
    }

    public String getURL() {
        return url;
    }

    public void setURL(String url) {
        this.url = url;
    }

    public void setClockingEntries(Vector clockingEntries) {
        this.clockingEntries = clockingEntries;
    }

    public String postClockingEntries() {

        StringBuffer     result        = new StringBuffer();
        long             contentLength = 0;
        int              currentChar   = 0;
        HttpConnection   c             = null;
        DataOutputStream dos           = null;
        InputStream      is            = null;

        try {
            c = (HttpConnection) Connector.open(url);

            c.setRequestMethod(HttpConnection.POST);

            dos = new DataOutputStream(c.openOutputStream());

            dos.writeInt(clockingEntries.size());

            for (int i = 0; i < clockingEntries.size(); i++) {
                dos.writeBoolean(((ClockingEntry) clockingEntries.elementAt(i)).isInEntry());
                dos.writeLong(((ClockingEntry) clockingEntries.elementAt(i)).getTime());
            }

            dos.close();

            is            = c.openInputStream();
            contentLength = c.getLength();

            if (contentLength != -1) {
                for (int i = 0; i < contentLength; i++) {
                    if ((currentChar = is.read()) != -1) {
                        result.append((char) currentChar);
                    }
                }
            } else {
                while ((currentChar = is.read()) != -1) {
                    result.append((char) currentChar);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            result.delete(0, result.length());
            result.insert(0, e.toString());
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (c != null) {
                    c.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result.toString().trim();
    }
}    // ClockingEntryConnectionHelper


