135 lines
4.5 KiB
Java
135 lines
4.5 KiB
Java
package de.arminwolf.financeanalyzer.service;
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import de.arminwolf.financeanalyzer.conf.Configuration;
|
|
import de.arminwolf.financeanalyzer.dao.TransactionDAO;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
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.util.Arrays;
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
import java.util.stream.Stream;
|
|
|
|
@Service
|
|
public class FileService {
|
|
|
|
|
|
public static final String CONFIGURATION_JSON = "configuration.json";
|
|
@Autowired
|
|
private CacheService cacheService;
|
|
|
|
|
|
public Configuration getConfiguration() {
|
|
File workingDirectory = getOrCreateWorkingDirectory();
|
|
Path configurationFilePath = Paths.get(workingDirectory.toPath().toString(), CONFIGURATION_JSON);
|
|
|
|
System.out.printf("Loading '%s'\n", configurationFilePath);
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
try {
|
|
return objectMapper.readValue(configurationFilePath.toFile(), Configuration.class);
|
|
} catch (IOException e) {
|
|
System.out.printf("Error while accesing '%s'\n", configurationFilePath);
|
|
}
|
|
|
|
return new Configuration();
|
|
}
|
|
|
|
|
|
public Optional<String> getJsonFileName() {
|
|
File workingDirectory = getOrCreateWorkingDirectory();
|
|
final Path jsonFilePath = Paths.get(workingDirectory.toPath().toString(), "finance-analyzer.json");
|
|
if (!jsonFilePath.toFile().exists()) {
|
|
return Optional.empty();
|
|
} else {
|
|
String absolutePath = jsonFilePath.toAbsolutePath().toString();
|
|
System.out.println("Absolute Path of json file: " + absolutePath);
|
|
return Optional.of(absolutePath);
|
|
}
|
|
}
|
|
|
|
|
|
public boolean isJsonPresent() {
|
|
File orCreateWorkingDirectory = getOrCreateWorkingDirectory();
|
|
return getJsonFiles(orCreateWorkingDirectory).findFirst().isPresent();
|
|
}
|
|
|
|
|
|
private Stream<String> getJsonFiles(final File orCreateWorkingDirectory) {
|
|
return Arrays.stream(Objects.requireNonNull(orCreateWorkingDirectory.list((dir, name) -> name.endsWith(".json"))));
|
|
}
|
|
|
|
|
|
public TransactionDAO[] getJsonFile() {
|
|
File workingDirectory = getOrCreateWorkingDirectory();
|
|
final Path jsonFilePath = Paths.get(workingDirectory.toPath().toString(), "finance-analyzer.json");
|
|
if (!jsonFilePath.toFile().exists()) {
|
|
throw new IllegalStateException("Could not load json file.");
|
|
} else {
|
|
|
|
System.out.printf("Loading '%s'\n", jsonFilePath);
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
try {
|
|
return objectMapper.readValue(jsonFilePath.toFile(), new TypeReference<TransactionDAO[]>() {
|
|
});
|
|
} catch (IOException e) {
|
|
System.out.printf("Error while accesing '%s'\n", jsonFilePath);
|
|
}
|
|
}
|
|
|
|
return new TransactionDAO[0];
|
|
}
|
|
|
|
|
|
public File getOrCreateWorkingDirectory() {
|
|
if (Objects.isNull(cacheService.get("workingDirectory"))) {
|
|
final String userHomeAsString = System.getProperty("user.home");
|
|
System.out.printf("Looking for working Directory '.finance-analyser' in user home (%s).\n", userHomeAsString);
|
|
Path path = Paths.get(userHomeAsString.concat(File.separator).concat(".finance-analyser"));
|
|
boolean exists = Files.exists(path);
|
|
if (exists) {
|
|
return path.toFile();
|
|
} else {
|
|
Path directory = null;
|
|
try {
|
|
directory = Files.createDirectory(path);
|
|
cacheService.put("workingDirectory", directory.toFile());
|
|
return directory.toFile();
|
|
} catch (IOException e) {
|
|
System.err.println("Could not create directory in user home.. Permission issue?" + e.getMessage());
|
|
}
|
|
throw new IllegalStateException("Could not create working directory. Please fix the permissions.");
|
|
}
|
|
} else {
|
|
return (File) cacheService.get("workingDirectory");
|
|
}
|
|
}
|
|
|
|
|
|
public void saveConfiguration(final Configuration configuration) {
|
|
File workingDirectory = getOrCreateWorkingDirectory();
|
|
Path path = Paths.get(workingDirectory.getPath(), CONFIGURATION_JSON);
|
|
if (path.toFile().exists()) {
|
|
System.out.println("Configuration file already exists. Overwriting it.");
|
|
try {
|
|
Files.delete(path);
|
|
} catch (IOException e) {
|
|
System.out.println("Could not delete existing configuration file." + e.getMessage());
|
|
}
|
|
}
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
try {
|
|
objectMapper.writeValue(path.toFile(), configuration);
|
|
} catch (IOException e) {
|
|
System.err.println("Could not save configuration file." + e.getMessage());
|
|
}
|
|
}
|
|
|
|
}
|