aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/TemplateSetScreen.java
blob: 42f393b3bc72a512bdbf1ff090c4b27f0e7995b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package ui;

import model.asn1.exceptions.ParseException;
import model.ca.Template;

/**
 * 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#template
     */
    private void handleSetSubject(String val) {
        try {
            template.setSubject(val);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * EFFECTS: Set the validity of the template to the given integer
     * MODIFIES: this#template
     */
    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.setValidity(i);
        } catch (NumberFormatException ignored) {
            System.out.println("Invalid validity days");
        }
    }

    /**
     * EFFECTS: Handle the `set` command.
     * MODIFIES: this#template
     */
    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.getTemplates().add(template);
        session.setScreen(Screen.TEMPLATES);
        session.log("A new template is added.");
    }

    /**
     * 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];
    }
}