97 lines
2.4 KiB
Java
97 lines
2.4 KiB
Java
package de.hsel.spm.baudas.web;
|
|
|
|
import lombok.Getter;
|
|
import org.jetbrains.annotations.Contract;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.time.LocalDateTime;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
|
|
/**
|
|
* manages data about .csv files.
|
|
*
|
|
* @author Johannes Theiner
|
|
* @version 0.1
|
|
* @since 0.1
|
|
**/
|
|
public class Data {
|
|
|
|
@Getter
|
|
private static ConcurrentLinkedQueue<SavedFile> files = new ConcurrentLinkedQueue<>();
|
|
|
|
|
|
/**
|
|
* adds file to list and generates a new filename.
|
|
*
|
|
* @param name File name
|
|
* @return path to save file to
|
|
*/
|
|
@NotNull
|
|
static Path add(@NotNull String name) {
|
|
//cleanup old files
|
|
if (files.isEmpty()) {
|
|
try {
|
|
Files.list(Paths.get("")).forEach(path -> {
|
|
if (path.toFile().getName().endsWith(".csv")) {
|
|
try {
|
|
Files.delete(path);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
|
|
Path path;
|
|
UUID uuid = UUID.randomUUID();
|
|
|
|
if (files.size() >= 5) {
|
|
//remove last and add new one
|
|
SavedFile file = files.poll();
|
|
if (!get(file.getUuid()).delete()) {
|
|
System.out.println("failed to delete file...");
|
|
}
|
|
path = Paths.get(getFileName(uuid));
|
|
files.offer(new SavedFile(uuid, name, LocalDateTime.now()));
|
|
|
|
|
|
} else {
|
|
files.add(new SavedFile(uuid, name, LocalDateTime.now()));
|
|
path = Paths.get(getFileName(uuid));
|
|
}
|
|
return path;
|
|
}
|
|
|
|
/**
|
|
* generates File from uuid.
|
|
*
|
|
* @param uuid uuid
|
|
* @return file
|
|
*/
|
|
@Contract
|
|
@NotNull
|
|
public static File get(@NotNull UUID uuid) {
|
|
return new File(getFileName(uuid));
|
|
}
|
|
|
|
/**
|
|
* generates file name from uuid.
|
|
*
|
|
* @param uuid uuid
|
|
* @return file name
|
|
*/
|
|
@Contract(pure = true)
|
|
@NotNull
|
|
private static String getFileName(@NotNull UUID uuid) {
|
|
return uuid + ".csv";
|
|
}
|
|
} |