ru.curs.celesta.score.GrainSaver Maven / Gradle / Ivy
The newest version!
package ru.curs.celesta.score;
import ru.curs.celesta.score.io.Resource;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
/**
* Persists grain to a writable resource.
*
* @author Pavel Perminov ([email protected])
* @since 2019-03-10
*/
public final class GrainSaver {
/**
* Saves metadata content of score back to SQL-files rewriting their content.
*
* @param score Score to save
* @param scorePath path to save the score to
*/
public void save(AbstractScore score, Resource scorePath) throws IOException {
for (Grain g : score.getGrains().values()) {
save(g, scorePath);
}
}
/**
* Saves grain to file(s).
*
* @param grain grain to save
* @param scorePath path to save the grain to
*/
public void save(Grain grain, Resource scorePath) throws IOException {
final String grainName = grain.getName();
Resource output = scorePath.createRelative(grain.getNamespace())
.createRelative(grainName + ".sql");
for (GrainPart gp : grain.getGrainParts()) {
Resource source = gp.getSource();
if (scorePath.contains(source) && !source.equals(output)) {
source.delete();
}
}
OutputStream outputStream = output.getOutputStream();
if (outputStream == null) {
throw new IOException(String.format(
"Cannot save '%s' grain script to resouce %s. The resource is not writable!",
grainName, output));
}
try (PrintWriter pw = new PrintWriter(
new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
CelestaSerializer serializer = new CelestaSerializer(pw);
serializer.save(grain);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy