/*
 * @(#)FileTransferUploadEvent.java
 *
 * Copyright (c) 1999-2007 JSCAPE LLC
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JSCAPE.
 *
 */
package com.jscape.filetransfer;

import java.util.HashMap;
import java.util.Map;

/**
 * Fired when file is uploaded to server.
 */
public class FileTransferUploadEvent
        extends FileTransferEvent {

    private final String filename;
    private final String path;
    private final String localPath;
    private final long size;
    private final long time;
    private final Map<String, Object> attributes;

    /**
     * Creates a new event instance.
     *
     * @param source    the source of the event
     * @param filename  the file uploaded
     * @param path      the remote path the file was uploaded to
     * @param localPath the local file path
     * @param size      the size of file in bytes
     * @param time      the time to transfer file in milliseconds
     * @param attributes transfer-specific attributes
     */
    public FileTransferUploadEvent(FileTransfer source,
                                   String filename,
                                   String path,
                                   String localPath,
                                   long size,
                                   long time,
                                   Map<String, Object> attributes) {
        super(source);

        this.filename = filename;
        this.path = path;
        this.localPath = localPath;
        this.size = size;
        this.time = time;
        this.attributes = attributes;
    }

    /**
     * Creates a new event instance.
     *
     * @param source    the source of the event
     * @param filename  the file uploaded
     * @param path      the remote path the file was uploaded to
     * @param localPath the local file path
     * @param size      the size of file in bytes
     * @param time      the time to transfer file in milliseconds
     */
    public FileTransferUploadEvent(FileTransfer source,
                                   String filename,
                                   String path,
                                   String localPath,
                                   long size,
                                   long time) {
        this(source, filename, path, localPath, size, time, new HashMap<String, Object>());
    }

    /**
     * Gets name of file uploaded.
     *
     * @return filename
     */
    public String getFilename() {
        return this.filename;
    }

    /**
     * Gets remote path the file was uploaded to.
     *
     * @return remote directory path
     */
    public String getPath() {
        return this.path;
    }

    /**
     * Returns uploaded file local path.
     *
     * @return uploaded file local path
     */
    public String getLocalPath() {
        return this.localPath;
    }

    /**
     * Gets total bytes transmitted.
     *
     * @return bytes transmitted
     */
    public long getSize() {
        return this.size;
    }

    /**
     * Gets total time to transfer file measured in milliseconds.
     *
     * @return total time
     */
    public long getTime() {
        return this.time;
    }

    public Object attributeFor(String name) {
        return this.attributes.get(name);
    }
    
    public Object attributeFor(String name, Object defaultValue) {
        Object value = attributeFor(name);
        return value != null ? value : defaultValue;
    }
    
    /**
     * {@inheritDoc}
     */
    public void accept(FileTransferListener listener) {
        listener.upload(this);
    }
    
    @Override
    public String toString() {
        return "FileTransferUploadEvent {"
               + "filename='" + this.filename + '\''
               + ", path='" + this.path + '\''
               + ", localPath='" + this.localPath + '\''
               + ", size=" + this.size
               + ", time=" + this.time
               + ", attributes=" + this.attributes
               + '}';
    }
}
