/*
 * @(#)FtpFile.java
 *
 * Copyright (c) 2001-2002 JScape
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * 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.inet.ftp;

import java.io.Serializable;

/**
 * Represents an item in a directory listing retrieved from FTP server.
 */
public class FtpFile
        implements Serializable {
    
    
    private String filename;
    private long filesize;
    private String owner;
    private String group;
    private String permission;
    private String fileDate;
    private String fileTime;
    private boolean isDirectory;
    private boolean isLink;
    private String linkTarget;
    private int style;
    private FtpFileParser ftpFileParser;
    private String line;
    private boolean isYearSet;
    
    /**
     * Creates a new FtpFile instance.
     *
     * @param filename   the name of the file
     * @param filesize   the size of the file
     * @param owner      the owner or owner id of the file
     * @param group      the group or group id of the file
     * @param permission the permissions for the file
     * @param fileDate   the last modified date of the file
     * @param fileTime   the last modified time of the file
     * @param directory  true if is a directory
     * @param link       true if is a symbolic link
     * @param linkTarget the target if a symbolic link
     * @param style      the directory listing style used
     * @param line       the original line used to generate this entry
     */
    public FtpFile(String filename,
                   long filesize,
                   String owner,
                   String group,
                   String permission,
                   String fileDate,
                   String fileTime,
                   boolean directory,
                   boolean link,
                   String linkTarget,
                   int style,
                   String line) {
        
        this.filename = filename;
        this.filesize = filesize;
        this.owner = owner;
        this.group = group;
        this.permission = permission;
        this.fileDate = fileDate;
        this.fileTime = fileTime;
        this.isDirectory = directory;
        this.isLink = link;
        this.linkTarget = linkTarget;
        this.style = style;
        this.line = line;
    }
    
    /**
     * Creates a new FtpFile instance.
     *
     * @param filename   the name of the file
     * @param filesize   the size of the file
     * @param owner      the owner or owner id of the file
     * @param group      the group or group id of the file
     * @param permission the permissions for the file
     * @param fileDate   the last modified date of the file
     * @param fileTime   the last modified time of the file
     * @param directory  true if is a directory
     * @param link       true if is a symbolic link
     * @param linkTarget the target if a symbolic link
     * @param style      the directory listing style used
     * @param line       the original line used to generate this entry
     */
    public FtpFile(String filename,
                   long filesize,
                   String owner,
                   String group,
                   String permission,
                   String fileDate,
                   String fileTime,
                   boolean directory,
                   boolean link,
                   String linkTarget,
                   int style,
                   String line,
                   boolean isYearSet) {
        
        this.filename = filename;
        this.filesize = filesize;
        this.owner = owner;
        this.group = group;
        this.permission = permission;
        this.fileDate = fileDate;
        this.fileTime = fileTime;
        this.isDirectory = directory;
        this.isLink = link;
        this.linkTarget = linkTarget;
        this.style = style;
        this.line = line;
        this.isYearSet = isYearSet;
    }
    
    /**
     * Creates a new FtpFile instance.
     *
     * @param filename the name of file
     */
    public FtpFile(String filename) {
        this.filename = filename;
    }
    
    /**
     * Creates a new FtpFile instance.
     *
     * @param filename the name of file
     * @param line     the original line sent by FTP server and used in parsing this <code>FtpFile</code>
     */
    public FtpFile(String filename,
                   String line,
                   FtpFileParser ftpFileParser) {
        this(filename);
        this.line = line;
        this.ftpFileParser = ftpFileParser;
    }
    
    /**
     * Creates a new FtpFile instance.
     *
     * @param filename      the name of file
     * @param ftpFileParser the FtpFileParser that created this FtpFile
     *
     * @see FtpFileParser
     */
    public FtpFile(String filename, FtpFileParser ftpFileParser) {
        this(filename);
        this.ftpFileParser = ftpFileParser;
    }
    
    /**
     * Is year set.
     *
     * @return
     */
    public boolean isYearSet() {
        return this.isYearSet;
    }
    
    /**
     * Sets filename for this <code>FtpFile</code>.
     *
     * @param filename the name of file
     */
    public void setFilename(String filename) {
        this.filename = filename;
    }
    
    /**
     * Gets filename for this <code>FtpFile</code>.
     *
     * @return filename
     */
    public String getFilename() {
        return filename;
    }
    
    /**
     * Sets file size for this <code>FtpFile</code>.
     *
     * @param size the size of file in bytes
     */
    public void setFilesize(String size) {
        try {
            this.filesize = Long.parseLong(size);
        } catch (NumberFormatException ne) {
            this.filesize = 0;
        }
    }
    
    /**
     * Gets file size for this <code>FtpFile</code>.
     *
     * @return size of file in bytes
     */
    public long getFilesize() {
        return filesize;
    }
    
    /**
     * Sets owner of this <code>FtpFile</code>.
     *
     * @param owner the name of owner
     */
    public void setOwner(String owner) {
        this.owner = owner;
    }
    
    /**
     * Gets owner of this <code>FtpFile</code>.
     *
     * @return name of owner
     */
    public String getOwner() {
        return owner;
    }
    
    /**
     * Sets group for this <code>FtpFile</code>.
     *
     * @param group the name of group
     */
    public void setGroup(String group) {
        this.group = group;
    }
    
    /**
     * Gets group for this <code>FtpFile</code>.
     *
     * @return name of group
     */
    public String getGroup() {
        return group;
    }
    
    /**
     * Sets permissions string for this <code>FtpFile</code>.
     *
     * @param permission a permissions string
     */
    public void setPermission(String permission) {
        if (permission.indexOf('d') != -1) {
            isDirectory = true;
        }
        
        if (permission.indexOf('l') != -1) {
            isLink = true;
        }
        
        this.permission = permission;
    }
    
    /**
     * Gets permissions string for this <code>FtpFile</code>.
     * The permissions string usually has the standard unix form,
     * such as:<br>
     * For a directory: <code> drwxr-xr-x </code> <br>
     * For a file: <code> -rw-rw-r-- </code> <br>
     * For a link: <code> lrw-r--r-- </code> <br>
     *
     * @return a permissions string
     *
     * @see #isDirectory
     * @see #isLink
     * @see #getLinkTarget
     */
    public String getPermission() {
        return permission;
    }
    
    /**
     * Test if this file represents a directory on the server.
     * Assumes is a directory if is a symbolic link and has no periods in filename.
     *
     * @return <code>true</code> if is a directory <code>false</code> otherwise
     *
     * @see #isLink
     */
    public boolean isDirectory() {
        return isDirectory;
    }
    
    /**
     * Denotes file as a directory.
     * Normally this will be set automatically by parsing the permissions for the file.
     * This method is useful in cases where permissions information is not included for example MS-DOS.
     *
     * @param isDirectory <code>true</code> if file is a directory <code>false</code> otherwise
     */
    public void setDirectory(boolean isDirectory) {
        this.isDirectory = isDirectory;
    }
    
    /**
     * Test wheather the entity represented by this FtpFile object
     * is a symbolic link.
     *
     * @return <CODE>true</CODE> if is a symbolic link <CODE>false</CODE> otherwise
     *
     * @see #getLinkTarget
     */
    public boolean isLink() {
        return isLink;
    }
    
    /**
     * Set the target if this entry is a link.
     *
     * @param s the link target path name
     */
    public void setLinkTarget(String s) {
        linkTarget = new String(s);
    }
    
    /**
     * If the directory listing entry for this file is a link
     * and link target is specified by the server, this method
     * can be used to retrieve the link target.
     *
     * @return the target of symbolic link
     *
     * @see #isLink
     */
    public String getLinkTarget() {
        return linkTarget;
    }
    
    /**
     * Sets date for this <code>FtpFile</code>.
     *
     * @param fileDate the date of the file
     */
    public void setDate(String fileDate) {
        this.fileDate = fileDate;
    }
    
    /**
     * Gets date for this <code>FtpFile</code>.
     *
     * @return a date
     */
    public String getDate() {
        return fileDate;
    }
    
    /**
     * Sets time for this <code>FtpFile</code>.
     *
     * @param fileTime of the file
     */
    public void setTime(String fileTime) {
        this.fileTime = fileTime;
    }
    
    /**
     * Gets time for this <code>FtpFile</code>.
     *
     * @return a time
     */
    public String getTime() {
        return fileTime;
    }
    
    
    /**
     * Gets string representation of this <code>FtpFile</code>.
     *
     * @return a String
     */
    public String toString() {
        return line;
    }
    
    /**
     * Checks where the filename is a hidden filename by checking to see if filename begins with a period.
     *
     * @return true if hidden, false otherwise
     */
    public boolean isHidden() {
        return getFilename().startsWith(".");
    }
    
    /**
     * Gets the FtpFileParser that created this FtpFile
     *
     * @return a FtpFileParser
     */
    public FtpFileParser getFtpFileParser() {
        return ftpFileParser;
    }
    
    public void setFtpFileParser(FtpFileParser value) {
        this.ftpFileParser = value;
    }
    
    /**
     * Sets the original line sent by FTP server and used in parsing this <code>FtpFile</code>
     *
     * @param line the original line
     */
    public void setLine(String line) {
        this.line = line;
    }
    
    /**
     * Gets the original line sent by FTP server and used in parsing this <code>FtpFile</code>
     *
     * @return the original line
     */
    public String getLine() {
        return line;
    }
}
