aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/tui/TemplateSetScreen.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/ui/tui/TemplateSetScreen.java')
-rw-r--r--src/main/ui/tui/TemplateSetScreen.java157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/main/ui/tui/TemplateSetScreen.java b/src/main/ui/tui/TemplateSetScreen.java
new file mode 100644
index 0000000..0f6df3a
--- /dev/null
+++ b/src/main/ui/tui/TemplateSetScreen.java
@@ -0,0 +1,157 @@
+package ui.tui;
+
+import model.asn1.exceptions.ParseException;
+import model.ca.Template;
+import model.x501.Name;
+
+/**
+ * The screen that modifies the properties of a single template and add it to the store.
+ */
+public class TemplateSetScreen implements UIHandler {
+ private final JCA session;
+
+ /**
+ * EFFECTS: Init with session.
+ */
+ public TemplateSetScreen(JCA session) {
+ this.session = session;
+ }
+
+ private Template template;
+
+ /**
+ * EFFECTS: Print help
+ */
+ @Override
+ public void help() {
+ System.out.println("show\tView the current template settings\n"
+ + "set\tSet key value\n"
+ + "commit\tSave the template\n"
+ + "exit\tDiscard changes\n"
+ + "help\tPrint this help message\n");
+ }
+
+ /**
+ * EFFECTS: Parse and set / unset the subject of the template
+ * MODIFIES: this
+ */
+ private void handleSetSubject(String val) {
+ try {
+ if (val == null) {
+ template = new Template(template.getName(), template.isEnabled(), (Name) null, template.getValidity());
+ } else {
+ template = new Template(template.getName(), template.isEnabled(), val, template.getValidity());
+ }
+ } catch (ParseException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+
+ /**
+ * EFFECTS: Set the validity of the template to the given integer
+ * MODIFIES: this
+ */
+ private void handleSetValidity(String val) {
+ if (val == null) {
+ System.out.println("Cannot unset validity");
+ return;
+ }
+ try {
+ long i = Long.parseLong(val);
+ if (i <= 0) {
+ System.out.println("Invalid validity days");
+ return;
+ }
+ template = new Template(template.getName(), template.isEnabled(), template.getSubject(), i);
+ } catch (NumberFormatException ignored) {
+ System.out.println("Invalid validity days");
+ }
+ }
+
+ /**
+ * EFFECTS: Handle the `set` command.
+ * MODIFIES: this
+ */
+ private void handleSet(String... args) {
+ if (args.length != 2 && args.length != 3) {
+ System.out.println("Usage: set <key> <value>");
+ System.out.println("Supported keys: subject validity");
+ return;
+ }
+ String val = args.length == 3 ? args[2] : null;
+ switch (args[1]) {
+ case "subject":
+ handleSetSubject(val);
+ break;
+ case "validity":
+ handleSetValidity(val);
+ break;
+ default:
+ System.out.println("Unknown key");
+ break;
+ }
+ }
+
+ /**
+ * EFFECTS: Add the template to store and switch to templates screen.
+ * MODIFIES: session
+ */
+ @Override
+ public void commit() {
+ session.getCa().addTemplate(template);
+ session.setUnsaved(true);
+ session.setScreen(Screen.TEMPLATES);
+ }
+
+ /**
+ * EFFECTS: Show template info.
+ */
+ @Override
+ public void show() {
+ System.out.println("Subject:\t" + template.getSubject());
+ System.out.println("Validity:\t" + template.getValidity() + " days");
+ }
+
+ /**
+ * EFFECTS: Handle commands
+ */
+ @Override
+ public void command(String... args) {
+ switch (args[0]) {
+ case "set":
+ handleSet(args);
+ break;
+ default:
+ case "help":
+ help();
+ break;
+ }
+ }
+
+ /**
+ * EFFECTS: Return to templates list and clear the current template in editing.
+ */
+ @Override
+ public Screen exit() {
+ template = null;
+ return Screen.TEMPLATES;
+ }
+
+ /**
+ * EFFECTS: yuuta@JCA /templates/name/ %
+ */
+ @Override
+ public String getPS1() {
+ return String.format("/templates/%s/ %%", template.getName());
+ }
+
+ /**
+ * EFFECT: Edit args[0].
+ * REQUIRES: args.length = 1; args[0] instanceof Template
+ * MODIFIES: args[0]
+ */
+ @Override
+ public void enter(Object... args) {
+ template = (Template) args[0];
+ }
+} \ No newline at end of file