120 lines
3.8 KiB
Java
120 lines
3.8 KiB
Java
package name.nathanmcrae.numbersstation;
|
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
import com.leakyabstractions.result.api.Result;
|
|
import com.leakyabstractions.result.core.Results;
|
|
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.ArrayList;
|
|
import java.util.logging.Logger;
|
|
import java.util.logging.Level;
|
|
|
|
public class MainSettings {
|
|
private static final Logger logger = Logger.getLogger(Main.class.getName());
|
|
|
|
private int digitsPerGroup;
|
|
private int messageGenerationAttempts;
|
|
private String username;
|
|
private String selectedStationName;
|
|
|
|
private ArrayList<StationSettings> stations;
|
|
|
|
public MainSettings() {
|
|
stations = new ArrayList<>();
|
|
stations.add(new StationSettings("Station 1"));
|
|
messageGenerationAttempts = 5;
|
|
}
|
|
|
|
public static Path getSettingsFilePath() {
|
|
return Main.getConfigPath().resolve("main-settings.xml");
|
|
}
|
|
|
|
public ArrayList<StationSettings> getStations() {
|
|
return stations;
|
|
}
|
|
|
|
public int getMessageGenerationAttempts() {
|
|
return messageGenerationAttempts;
|
|
}
|
|
|
|
public void setMessageGenerationAttempts(int messageGenerationAttempts) {
|
|
this.messageGenerationAttempts = messageGenerationAttempts;
|
|
}
|
|
|
|
public String getUsername() {
|
|
return username;
|
|
}
|
|
|
|
public void setUsername(String username) {
|
|
this.username = username;
|
|
}
|
|
|
|
public String getSelectedStationName() {
|
|
return selectedStationName;
|
|
}
|
|
|
|
public void setSelectedStationName(String selectedStationName) {
|
|
this.selectedStationName = selectedStationName;
|
|
}
|
|
|
|
public static Result<MainSettings, Exception> load() {
|
|
XmlMapper xmlMapper = new XmlMapper();
|
|
xmlMapper.registerModule(new JavaTimeModule());
|
|
String userHome = System.getProperty("user.home");
|
|
|
|
MainSettings settings;
|
|
|
|
Path filePath = MainSettings.getSettingsFilePath();
|
|
Path directoryPath = filePath.getParent();
|
|
try {
|
|
if (!Files.exists(directoryPath)) {
|
|
Files.createDirectories(directoryPath);
|
|
}
|
|
|
|
if (!Files.exists(filePath)) {
|
|
settings = new MainSettings();
|
|
|
|
xmlMapper.writeValue(new File(filePath.toString()), settings);
|
|
} else {
|
|
settings = xmlMapper.readValue(new File(filePath.toString()), MainSettings.class);
|
|
for (StationSettings station : settings.getStations()) {
|
|
if (station.getDigitsPerGroup() == 0) {
|
|
station.setDigitsPerGroup(4);
|
|
}
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
logger.log(Level.SEVERE, "Failed to load settings from " + filePath.toString(), e);
|
|
return Results.failure(e);
|
|
}
|
|
|
|
return Results.success(settings);
|
|
}
|
|
|
|
public void save() {
|
|
XmlMapper xmlMapper = new XmlMapper();
|
|
xmlMapper.registerModule(new JavaTimeModule());
|
|
xmlMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
|
xmlMapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false);
|
|
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
try {
|
|
Path filePath = getSettingsFilePath();
|
|
Path directoryPath = filePath.getParent();
|
|
|
|
if (!Files.exists(directoryPath)) {
|
|
Files.createDirectories(directoryPath);
|
|
}
|
|
|
|
xmlMapper.writeValue(new File(filePath.toString()), this);
|
|
logger.info("Settings saved to " + filePath.toString());
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|