package org.analogweb.acf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import static org.analogweb.acf.CommonsFileUploadModulesConfig.PLUGIN_MESSAGE_RESOURCE;
import org.analogweb.ApplicationProperties;
import org.analogweb.MultipartFile;
import org.analogweb.RequestContext;
import org.analogweb.util.ApplicationPropertiesHolder;
import org.analogweb.util.IOUtils;
import org.analogweb.util.logging.Log;
import org.analogweb.util.logging.Logs;

public class TemporaryUploadFolder implements Serializable {

	private static final long serialVersionUID = 1950934123601946837L;

	private static final Log log = Logs.getLog(TemporaryUploadFolder.class);

	protected static final String TMP_DIR = TemporaryUploadFolder.class
			.getPackage().getName() + "_" + "TMP_DIR";

	private File baseDir;

	public static TemporaryUploadFolder current(RequestContext context) {
		Object tmpDir = context.getRequest().getAttribute(TMP_DIR);
		if (!(tmpDir instanceof TemporaryUploadFolder)) {
			ApplicationProperties props = ApplicationPropertiesHolder.current();
			TemporaryUploadFolder newFolder = new TemporaryUploadFolder(
					new File(createCurrentDirName(props, context)));
			context.getRequest().setAttribute(TMP_DIR, newFolder);
			tmpDir = context.getRequest().getAttribute(TMP_DIR);
			log.log(PLUGIN_MESSAGE_RESOURCE, "DACF000005", newFolder
					.getBaseDir().toString());
		}
		return (TemporaryUploadFolder) tmpDir;
	}

	private static synchronized String createCurrentDirName(
			ApplicationProperties props, RequestContext context) {
		return props.getTempDir().getPath() + "/"
				+ TemporaryUploadFolder.class.getName() + "_"
				+ System.currentTimeMillis() + "_" + context.hashCode();
	}

	protected TemporaryUploadFolder(File baseDir) {
		this.baseDir = baseDir;
	}

	protected File getBaseDir() {
		return this.baseDir;
	}

	public File require(MultipartFile multipartFile) {
		if(!getBaseDir().exists()){
			getBaseDir().mkdirs();
		}
		File file = new File(getBaseDir().getPath() + "/"
				+ multipartFile.getParameterName());
		if (!file.exists()) {
			FileOutputStream out = null;
			InputStream in = multipartFile.getInputStream();
			try {
				out = new FileOutputStream(file);
				IOUtils.copy(in, out);
				log.log(PLUGIN_MESSAGE_RESOURCE, "DACF000006",
						file.getPath());
			} catch (IOException e) {
				log.log(PLUGIN_MESSAGE_RESOURCE, "WACF000002",
						multipartFile.getParameterName());
				log.log(PLUGIN_MESSAGE_RESOURCE, "WACF000003", e, getBaseDir());
				return null;
			} finally {
				IOUtils.closeQuietly(in);
				IOUtils.closeQuietly(out);
			}
		} else {
			log.log(PLUGIN_MESSAGE_RESOURCE, "DACF000007",
					file.getPath());
		}
		return file;
	}

	public static File require(RequestContext context,
			MultipartFile multipartFile) {
		return current(context).require(multipartFile);
	}

	public void dispose() {
		File baseDir = getBaseDir();
		if(baseDir != null){
			deleteRecurciverely(baseDir);
			baseDir.delete();
			log.log(PLUGIN_MESSAGE_RESOURCE, "DACF000008",
					baseDir.getPath());
		}
	}

	private void deleteRecurciverely(File dir) {
		File[] listFiles = dir.listFiles();
		if(listFiles != null){
			for (File f : listFiles) {
				if (f.isDirectory()) {
					deleteRecurciverely(f);
				} else {
					f.delete();
				}
			}
		}
	}

}

@@ -1375,100 +1375,8 @@
 R);%0A
-%09%09%09log.log(PLUGIN_MESSAGE_RESOURCE, %22DACF000005%22, newFolder%0A%09%09%09%09%09.getBaseDir().toString());%0A
 %09%09%7D%0A
@@ -1890,24 +1890,44 @@
 %7B%0A%09%09
-if(!getBaseDir()
+File base = getBaseDir();%0A%09%09if(!base
 .exi
@@ -1937,28 +1937,20 @@
 ())%7B%0A%09%09%09
-getBaseDir()
+base
 .mkdirs(
@@ -1952,16 +1952,84 @@
 dirs();%0A
+%09%09%09log.log(PLUGIN_MESSAGE_RESOURCE, %22DACF000005%22, base.toString());%0A
 %09%09%7D%0A%09%09Fi
@@ -2047,28 +2047,20 @@
 ew File(
-getBaseDir()
+base
 .getPath
@@ -2540,28 +2540,20 @@
 03%22, e, 
-getBaseDir()
+base
 );%0A%09%09%09%09r
@@ -2963,24 +2963,44 @@
 eDir != null
+ && baseDir.exists()
 )%7B%0A%09%09%09delete

Improve log.