249 lines
10 KiB
Java
249 lines
10 KiB
Java
package com.molnarandrei.sopa.step;
|
|
|
|
|
|
import com.molnarandrei.sopa.data.yaml.Kind;
|
|
import com.molnarandrei.sopa.trash.TrashService;
|
|
import com.molnarandrei.sopa.Utils;
|
|
import com.molnarandrei.sopa.data.RepositoryException;
|
|
import com.molnarandrei.sopa.data.yaml.Step;
|
|
import com.molnarandrei.sopa.ui.lib.Packing;
|
|
import javafx.scene.control.TextInputDialog;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import static com.molnarandrei.sopa.Utils.highestIndexOf;
|
|
import static com.molnarandrei.sopa.Utils.renameIndexed;
|
|
|
|
public class StepService {
|
|
private static final Logger log = Logger.getLogger(StepService.class.getName());
|
|
public static final String COPY_SUFFIX = " (copia)";
|
|
|
|
public static Step read(Path stepPath) {
|
|
try {
|
|
return StepRepository.get(stepPath);
|
|
} catch (RepositoryException readWriteOrParseErr) {
|
|
log.log(Level.SEVERE, "Cannot read step %s.".formatted(stepPath), readWriteOrParseErr);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se ha podido abrir el paso:\n%s"
|
|
.formatted(stepPath),
|
|
readWriteOrParseErr.getMessage()
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static void create(Path testCasePath) {
|
|
var input = Packing.pack(new TextInputDialog());
|
|
input.setTitle("Crear paso en prueba");
|
|
input.setHeaderText("Nombre del paso");
|
|
var name = input.showAndWait().orElse("");
|
|
if (name.isBlank()) return;
|
|
|
|
var stepFolderPath = testCasePath.getParent().resolve("step-1");
|
|
if (Files.exists(stepFolderPath))
|
|
stepFolderPath = renameIndexed(stepFolderPath);
|
|
try {
|
|
Files.createDirectory(stepFolderPath);
|
|
} catch (IOException cannotCreate) {
|
|
log.log(Level.SEVERE, "Cannot create step directory %s.".formatted(stepFolderPath), cannotCreate);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se ha podido crear el paso:\n%s"
|
|
.formatted(stepFolderPath),
|
|
cannotCreate.getMessage()
|
|
);
|
|
return;
|
|
}
|
|
var step = new Step(name);
|
|
write(stepFolderPath.resolve("step.yml"), step);
|
|
}
|
|
|
|
/** Writes the step, alerting on failure. Returns whether it was written. */
|
|
public static boolean write(Path stepPath, Step step) {
|
|
step.getHeaders().entrySet().removeIf(entry ->
|
|
entry.getKey() == null || entry.getKey().isBlank() ||
|
|
entry.getValue() == null || entry.getValue().isBlank()
|
|
);
|
|
try {
|
|
StepRepository.update(stepPath, step);
|
|
return true;
|
|
} catch (RepositoryException cannotWrite) {
|
|
log.log(Level.SEVERE, "Cannot write step %s.".formatted(stepPath), cannotWrite);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se ha podido guardar el paso:\n%s"
|
|
.formatted(stepPath),
|
|
cannotWrite.getMessage()
|
|
);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Writes a new step with the contents of {@code stepPath} after the last step of
|
|
* {@code toTestCasePath}, its name followed by {@code nameSuffix}. Only the step itself is
|
|
* copied, not its call history: the copy is a different step.
|
|
* Returns the path of the new step.yml, null when it could not be copied.
|
|
*/
|
|
public static Path copyInto(Path stepPath, Path toTestCasePath, String nameSuffix) {
|
|
log.info("Copying step %s into testcase %s.".formatted(stepPath, toTestCasePath));
|
|
var step = read(stepPath);
|
|
if (step == null) return null;
|
|
|
|
var toTestCaseFolderPath = toTestCasePath.getParent();
|
|
Path copiedStepFolderPath = null;
|
|
try {
|
|
var highestIndex = highestIndexOf(toTestCaseFolderPath, "step");
|
|
copiedStepFolderPath = renameIndexed(toTestCaseFolderPath.resolve("step-" + (highestIndex + 1)));
|
|
Files.createDirectory(copiedStepFolderPath);
|
|
} catch (IOException cannotCreate) {
|
|
log.log(Level.SEVERE, "Cannot create step directory in %s.".formatted(toTestCaseFolderPath), cannotCreate);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se ha podido crear el paso en:\n%s"
|
|
.formatted(toTestCaseFolderPath),
|
|
cannotCreate.getMessage()
|
|
);
|
|
return null;
|
|
}
|
|
|
|
step.setName(step.getName() + nameSuffix);
|
|
var copiedStepPath = copiedStepFolderPath.resolve("step.yml");
|
|
if (write(copiedStepPath, step))
|
|
return copiedStepPath;
|
|
|
|
try {
|
|
StepRepository.delete(copiedStepPath);
|
|
} catch (RepositoryException leftover) {
|
|
log.log(Level.WARNING, "Cannot clean up failed step copy %s.".formatted(copiedStepPath), leftover);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Pastes the step at the end of {@code toTestCasePath}. A cut step is erased once its copy
|
|
* is written, and keeps its name.
|
|
*/
|
|
public static void pasteOnTestCase(Path stepPath, Path toTestCasePath, boolean cut) {
|
|
log.info("Pasting %s step %s onto testcase %s.".formatted(cut? "cut": "copied", stepPath, toTestCasePath));
|
|
var copiedStepPath = copyInto(stepPath, toTestCasePath, cut? "": COPY_SUFFIX);
|
|
if (copiedStepPath == null) return;
|
|
if (cut) erase(stepPath);
|
|
}
|
|
|
|
/**
|
|
* Pastes the step right below {@code toStepPath}, in its testcase. A cut step is erased
|
|
* once its copy is written, and keeps its name. Cutting a step onto itself does nothing.
|
|
*/
|
|
public static void pasteBelow(Path stepPath, Path toStepPath, boolean cut) {
|
|
log.info("Pasting %s step %s below step %s.".formatted(cut? "cut": "copied", stepPath, toStepPath));
|
|
if (cut && stepPath.equals(toStepPath)) {
|
|
log.info("Step %s cut onto itself, not pasting.".formatted(stepPath));
|
|
return;
|
|
}
|
|
|
|
var toTestCasePath = toStepPath.getParent().getParent().resolve("testcase.yml");
|
|
var copiedStepPath = copyInto(stepPath, toTestCasePath, cut? "": COPY_SUFFIX);
|
|
if (copiedStepPath == null) return;
|
|
// Erasing does not rename the other step folders, so toStepPath stays valid; the
|
|
// reordering below then closes the gap it leaves.
|
|
if (cut) erase(stepPath);
|
|
|
|
try {
|
|
StepRepository.moveReordering(copiedStepPath, toStepPath, false);
|
|
} catch (RepositoryException cannotReorder) {
|
|
log.log(Level.SEVERE, "Cannot place step %s below %s.".formatted(copiedStepPath, toStepPath), cannotReorder);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se ha podido colocar el paso pegado tras:\n%s"
|
|
.formatted(toStepPath),
|
|
cannotReorder.getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public static void toggleActive(Path stepPath) {
|
|
log.info("Toggling step %s.".formatted(stepPath));
|
|
var step = read(stepPath);
|
|
if (step == null) return;
|
|
|
|
step.setActive(! step.isActive());
|
|
write(stepPath, step);
|
|
}
|
|
|
|
public static void duplicate(Path stepPath) {
|
|
log.info("Duplicating step %s.".formatted(stepPath));
|
|
|
|
var stepFolderPath = stepPath.getParent();
|
|
var copiedStepFolderPath = Utils.renameIndexed(stepFolderPath);
|
|
var copiedStepPath = copiedStepFolderPath.resolve("step.yml");
|
|
try {
|
|
Utils.copyDirectory(stepFolderPath, copiedStepFolderPath);
|
|
var step = StepRepository.get(stepPath);
|
|
step.setName(step.getName() + " (copia)");
|
|
StepRepository.update(copiedStepPath, step);
|
|
} catch (IOException readWriteParseError) {
|
|
log.log(Level.SEVERE, "Cannot copy step %s.".formatted(stepPath), readWriteParseError);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se ha podido copiar el paso:\n%s"
|
|
.formatted(stepPath),
|
|
readWriteParseError.getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/** Moves the step to the trash: {@link TrashService#undo()} restores it. */
|
|
public static void delete(Path stepPath) {
|
|
log.info("Deleting step %s.".formatted(stepPath));
|
|
try {
|
|
TrashService.remember(Kind.step, stepPath, StepRepository.trash(stepPath));
|
|
} catch (RepositoryException readWriteParseError) {
|
|
log.log(Level.SEVERE, "Cannot delete step %s.".formatted(stepPath), readWriteParseError);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se ha podido eliminar el paso:\n%s"
|
|
.formatted(stepPath),
|
|
readWriteParseError.getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/** Puts back a step moved to the trash by {@link #delete(Path)}. */
|
|
public static boolean restore(Path stepPath, Path kept) {
|
|
log.info("Restoring step %s.".formatted(stepPath));
|
|
try {
|
|
StepRepository.restore(stepPath, kept);
|
|
return true;
|
|
} catch (RepositoryException cannotRestore) {
|
|
log.log(Level.WARNING, "Cannot restore step %s.".formatted(stepPath), cannotRestore);
|
|
Utils.alert(
|
|
"No se puede deshacer",
|
|
"No se ha podido restaurar el paso:\n%s".formatted(stepPath),
|
|
cannotRestore.getMessage()
|
|
);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/** Erases a cut step once pasted: it is not kept in the trash, its copy is where it went. */
|
|
private static void erase(Path stepPath) {
|
|
try {
|
|
StepRepository.delete(stepPath);
|
|
} catch (RepositoryException readWriteParseError) {
|
|
log.log(Level.SEVERE, "Cannot erase cut step %s.".formatted(stepPath), readWriteParseError);
|
|
Utils.alert(
|
|
"Fallo de fichero",
|
|
"No se ha podido eliminar el paso cortado:\n%s"
|
|
.formatted(stepPath),
|
|
readWriteParseError.getMessage()
|
|
);
|
|
}
|
|
}
|
|
}
|