Softwareprojektmanagement/src/main/java/de/hsel/spm/baudas/web/DatasetManagement.java

127 lines
3.7 KiB
Java

package de.hsel.spm.baudas.web;
import de.hsel.spm.baudas.BauDas;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Stream;
/**
* manages data about .csv files.
*
* @author Johannes Theiner
* @version 0.1
* @since 1.0
**/
public class DatasetManagement {
@Getter
private static ConcurrentMap<String, List<SavedFile>> files = new ConcurrentHashMap<>();
private DatasetManagement() {}
/**
* 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, @NotNull String session) {
UUID uuid = UUID.randomUUID();
if (!files.containsKey(session))
files.put(session, new ArrayList<>());
if (files.get(session).size() >= 5) {
SavedFile file = files.get(session).iterator().next();
try {
Files.delete(get(file.getUuid()).toPath());
} catch (IOException e) {
BauDas.getLogger().throwing(DatasetManagement.class.getName(), DatasetManagement.class.getEnclosingMethod().getName(), e);
}
files.get(session).remove(file);
}
files.get(session).add(new SavedFile(uuid, name, LocalDateTime.now()));
return Paths.get(getFileName(uuid));
}
/**
* delete all files for session.
*
* @param session session id
*/
static void delete(@NotNull String session) {
if (!files.containsKey(session)) return;
for (SavedFile file : files.get(session)) {
try {
Files.delete(get(file.getUuid()).toPath());
} catch (IOException e) {
BauDas.getLogger().throwing(DatasetManagement.class.getName(), DatasetManagement.class.getEnclosingMethod().getName(), e);
}
}
files.remove(session);
//deleting all files older than x days.
int days = 30;
try (Stream<Path> stream = Files.list(Paths.get(""))) {
stream.forEach(path -> {
if (path.toFile().getName().endsWith("*.csv")) {
long diff = new Date().getTime() - path.toFile().lastModified();
if (diff > (long) days * 24 * 60 * 60 * 1000) {
try {
Files.delete(path);
} catch (IOException e) {
BauDas.getLogger().throwing(DatasetManagement.class.getName(), DatasetManagement.class.getEnclosingMethod().getName(), e);
}
}
}
});
} catch (IOException ex) {
BauDas.getLogger().throwing(DatasetManagement.class.getName(), DatasetManagement.class.getEnclosingMethod().getName(), ex);
}
}
/**
* 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 System.getProperty("catalina.base") + "/" + uuid + ".csv";
}
}