package cookery_db;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;


/**
 * Database backup provider for Derby databases.
 * @author Nick Apperley
 */
public class DerbyDBBackup extends TimerTask implements DBBackup
{
    private FileHandler logFile = null;
    private String backupLocation;
    private String backupName;
    private String databaseName;
    private String databaseAddress;

    /**
     * Creates a new DerbyDBBackup object.
     * @param backupName Name of the backup.
     * @param backupLocation Location of the backup via a path
     * (not including the backup name).
     * @param databaseName Name of the database.
     * @param databaseAddress Location of the database via a path
     * (not including the database name).
     */
    public DerbyDBBackup(String backupName, String backupLocation,
            String databaseName, String databaseAddress)
    {
        final boolean NEW_LOG_FILE = false;
        final String LOG_FILE_PATTERN = "Cookery Log";

        this.backupName = backupName;
        this.databaseName = databaseName;
        this.databaseAddress = databaseAddress;
        this.backupLocation = backupLocation;

        try
        {
            logFile = new FileHandler(LOG_FILE_PATTERN, NEW_LOG_FILE);
        } catch (IOException ex)
        {
            Logger.getLogger(DerbyDBBackup.class.getName()).log(
                    Level.WARNING, "Unable to create Cookery log file", ex);
        } catch (SecurityException ex)
        {
            Logger.getLogger(DerbyDBBackup.class.getName()).log(
                    Level.WARNING, "Unable to create Cookery log file due " +
                    "to security restrictions", ex);
        }
    }

    public String getBackupName()
    {
        return backupName;
    }

    public String getDatabaseName()
    {
        return databaseName;
    }

    public String getDatabaseAddress()
    {
        return databaseAddress;
    }

    public String getBackupLocation()
    {
        return backupLocation;
    }

    /**
     * Makes a backup of the database.
     */
    public void backup()
    {
        // Eg 10 April 2009 09:30
        final String PATTERN = "d MMMM yyyy k:m";
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN);
        File originalDatabase = new File(
                databaseAddress + File.separator + databaseName);
        File backupDatabase = new File(
                backupLocation + File.separator + backupName +
                " (" + dateFormat.format(new Date()) + ")");

        try
        {
            FileUtils.copyDirectory(originalDatabase, backupDatabase);
        } catch (IOException ex)
        {
            Logger.getLogger(DerbyDBBackup.class.getName()).log(
                    Level.WARNING, "Unable to backup Derby database", ex);
            logFile.publish(new LogRecord(Level.WARNING,
                    "Unable to backup Derby database"));
        }
    }

    @Override
    public void run()
    {
        backup();
    }
}
