package org.opentosca.csarrepo.filesystem;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Provides the file system functionality.
 * 
 * @author Fabian Toth, Dennis Przytarski
 */
public class FileSystem {

	private static final Logger LOGGER = LogManager.getLogger(FileSystem.class);
	private static final String BASE_PATH = System.getProperty("java.io.tmpdir") + File.pathSeparator + "csarrepo"
			+ File.pathSeparator;

	public boolean saveToFileSystem(long id, File file) {
		try {
			Files.move(file.toPath(), Paths.get(BASE_PATH + id + ".csar"), StandardCopyOption.ATOMIC_MOVE);
		} catch (IOException e) {
			LOGGER.error(e.getMessage(), e);
			return false;
		}
		return true;
	}

	public File getFile(long id) {
		File file = new File(BASE_PATH + id + ".csar");
		if (file.exists() && file.isFile()) {
			return file;
		}
		return null;
	}

	public boolean deleteFile(long id) {
		File file = new File(BASE_PATH + id + ".csar");
		if (file.exists() && file.isFile()) {
			return file.delete();
		}
		return false;
	}

}

@@ -73,27 +73,121 @@
 java.io.
-IOException
+FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.OutputStream
 ;%0Aimport
@@ -276,16 +276,39 @@
 yOption;
+%0Aimport java.util.UUID;
 %0A%0Aimport
@@ -724,16 +724,71 @@
 parator;
+%0A%09private static final String FILE_EXTENSION = %22.csar%22;
 %0A%0A%09publi
@@ -789,23 +789,22 @@
 %09public 
-boolean
+String
  saveToF
@@ -817,29 +817,167 @@
 tem(
-long id, File file) %7B
+File file) throws IOException %7B%0A%0A%09%09String generatedFileName = UUID.randomUUID().toString();%0A%09%09String absPath = BASE_PATH + generatedFileName + FILE_EXTENSION;%0A
 %0A%09%09t
@@ -1024,32 +1024,15 @@
 get(
-BASE_PATH + id + %22.csar%22
+absPath
 ), S
@@ -1134,44 +1134,229 @@
 %0A%09%09%09
-return false;%0A%09%09%7D%0A%09%09return true;%0A%09%7D%0A
+throw e;%0A%09%09%7D%0A%0A%09%09return absPath;%0A%09%7D%0A%0A%09/**%0A%09 * returns the file object represented by pathname%0A%09 * %0A%09 * @param pathname%0A%09 *            may not be null%0A%09 * @return the file or %3Ccode%3Enull%3C/code%3E if the file doesn't exist%0A%09 */
 %0A%09pu
@@ -1369,31 +1369,39 @@
 ile getFile(
-long id
+String pathname
 ) %7B%0A%09%09File f
@@ -1419,32 +1419,16 @@
 ile(
-BASE_PATH + id + %22.csar%22
+pathname
 );%0A%09
@@ -1505,16 +1505,196 @@
 ll;%0A%09%7D%0A%0A
+%09/**%0A%09 * delete the file specified by pathname%0A%09 * %0A%09 * @param pathname%0A%09 *            of the file to be deleted%0A%09 * @return %3Ccode%3Etrue%3C/code%3E if the deletions was successful%0A%09 */%0A
 %09public 
@@ -1716,15 +1716,23 @@
 ile(
-long id
+String pathname
 ) %7B%0A
@@ -1758,32 +1758,16 @@
 ile(
-BASE_PATH + id + %22.csar%22
+pathname
 );%0A%09
@@ -1854,10 +1854,707 @@
 se;%0A%09%7D%0A%0A
+%09/**%0A%09 * Creates a temporary file on the HDD%0A%09 * %0A%09 * @param fis%0A%09 *            FileInputStream%0A%09 * @return FileObject representing the created file%0A%09 * @throws IOException%0A%09 *             if error occurred%0A%09 */%0A%09public File saveTempFile(FileInputStream fis) throws IOException %7B%0A%09%09File tmpFile = File.createTempFile(%22tmpCSAR%22, %22.tmp%22);%0A%09%09tmpFile.deleteOnExit();%0A%0A%09%09OutputStream outputStream = new FileOutputStream(tmpFile);%0A%09%09int read = 0;%0A%09%09byte%5B%5D bytes = new byte%5B1024%5D;%0A%0A%09%09while ((read = fis.read(bytes)) != -1) %7B%0A%09%09%09outputStream.write(bytes, 0, read);%0A%09%09%7D%0A%09%09outputStream.flush();%0A%09%09outputStream.close();%0A%0A%09%09LOGGER.info(%22tmp file created: %22 + tmpFile.getAbsolutePath());%0A%09%09return tmpFile;%0A%09%7D%0A%0A
 %7D%0A

refactor FileSystem