45 lines
1.8 KiB
Java
45 lines
1.8 KiB
Java
package com.molnarandrei.sopa.properties;
|
|
|
|
import com.molnarandrei.sopa.Utils;
|
|
import com.molnarandrei.sopa.data.RepositoryException;
|
|
|
|
import java.nio.file.Path;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
/** Properties are not undoable: their files are written apart from the yml of their project or testcase. */
|
|
public class PropertiesService {
|
|
private static final Logger log = Logger.getLogger(PropertiesService.class.getName());
|
|
|
|
/** The properties of the project or testcase whose yml is {@code ymlPath}; empty and alerting when unreadable. */
|
|
public static Map<String, String> read(Path ymlPath) {
|
|
try {
|
|
return PropertiesRepository.get(PropertiesRepository.of(ymlPath));
|
|
} catch (RepositoryException cannotRead) {
|
|
log.log(Level.WARNING, "Cannot read properties of %s.".formatted(ymlPath), cannotRead);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se han podido leer las propiedades de:\n%s".formatted(ymlPath),
|
|
cannotRead.getMessage()
|
|
);
|
|
return new HashMap<>();
|
|
}
|
|
}
|
|
|
|
/** Writes the properties of the project or testcase whose yml is {@code ymlPath}, alerting on failure. */
|
|
public static void write(Path ymlPath, Map<String, String> properties) {
|
|
try {
|
|
PropertiesRepository.update(PropertiesRepository.of(ymlPath), properties);
|
|
} catch (RepositoryException cannotWrite) {
|
|
log.log(Level.SEVERE, "Cannot write properties of %s.".formatted(ymlPath), cannotWrite);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se han podido guardar las propiedades de:\n%s".formatted(ymlPath),
|
|
cannotWrite.getMessage()
|
|
);
|
|
}
|
|
}
|
|
}
|