package org.beanmaker.util;

import org.apache.commons.fileupload.FileItem;

import java.io.File;

public class DbBeanFileCreator {

    private final String defaultUploadDir;
    private final DbBeanFileStoredFilenameCalculator storedFilenameCalculator;

    public DbBeanFileCreator(final String defaultUploadDir) {
        this(defaultUploadDir, new DbBeanFileIdentityStoredFilenameCalculator());
    }

    public DbBeanFileCreator(
            final String defaultUploadDir,
            final DbBeanFileStoredFilenameCalculator storedFilenameCalculator)
    {
        this.defaultUploadDir = defaultUploadDir;
        this.storedFilenameCalculator = storedFilenameCalculator;
    }

    public DbBeanFile create(final DbBeanFile dbBeanFile, final FileItem fileItem) {
        boolean newRecord = false;

        dbBeanFile.setOrigFilename(fileItem.getName());
        final String filename = storedFilenameCalculator.calc(fileItem.getName());
        if (filename.equals(dbBeanFile.getOrigFilename()))
            dbBeanFile.setStoredFilename(null);
        else
            dbBeanFile.setStoredFilename(filename);

        if (dbBeanFile.getId() == 0) {
            dbBeanFile.updateDB();
            newRecord = true;
        }

        try {
            fileItem.write(new File(getOrCreateUploadDirectory(dbBeanFile, defaultUploadDir), filename));
        } catch (final Exception ex) {  // function write() is actually marked as throwing Exception !!!
            if (newRecord)
                dbBeanFile.delete();
            throw new RuntimeException(ex);
        }

        if (!newRecord)
            dbBeanFile.updateDB();

        return dbBeanFile;
    }

    private File getOrCreateUploadDirectory(final DbBeanFile dbBeanFile, final String defaultUploadDir) {
        final File uploadDirectory = getUploadDirectory(dbBeanFile, defaultUploadDir);

        if (!uploadDirectory.exists()) {
            if (!uploadDirectory.mkdir())
                throw new IllegalArgumentException("Could not create directory: " + uploadDirectory.getAbsolutePath());
        } else if (!uploadDirectory.isDirectory())
            throw new IllegalArgumentException(uploadDirectory.getAbsolutePath() + " is not a directory");

        return uploadDirectory;
    }

    public static File getUploadDirectory(final DbBeanFile dbBeanFile, final String defaultUploadDir) {
        if (dbBeanFile.isAltDirEmpty())
            return new File(defaultUploadDir, Long.toString(dbBeanFile.getId()));

        return new File(dbBeanFile.getAltDir(), Long.toString(dbBeanFile.getId()));
    }
}

@@ -165,24 +165,69 @@
 tUploadDir;%0A
+    private final String alternateUploadDir;%0A
     private 
@@ -619,35 +619,511 @@
 this
-.defaultUploadDir = default
+(defaultUploadDir, null, storedFilenameCalculator);%0A    %7D%0A%0A    public DbBeanFileCreator(%0A            final String defaultUploadDir,%0A            final String alternateUploadDir,%0A            final DbBeanFileStoredFilenameCalculator storedFilenameCalculator)%0A    %7B%0A        this.defaultUploadDir = defaultUploadDir;%0A        if (alternateUploadDir == null %7C%7C alternateUploadDir.equals(defaultUploadDir))%0A            this.alternateUploadDir = null;%0A        else%0A            this.alternateUploadDir = alternate
 Uplo
@@ -1631,24 +1631,119 @@
 filename);%0A%0A
+        if (alternateUploadDir != null)%0A            dbBeanFile.setAltDir(alternateUploadDir);%0A%0A
         if (

Make it easier to upload files elsewhere than in the default directory