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
|
package ui;
import model.ca.Template;
import model.x501.Name;
/**
* The screen that allows users to list templates and manage them.
*/
public class TemplatesScreen implements UIHandler {
private final JCA session;
/**
* EFFECTS: Init with the session.
*/
public TemplatesScreen(JCA session) {
this.session = session;
}
/**
* EFFECTS: Print help.
*/
@Override
public void help() {
System.out.println("show\tList templates\n"
+ "add\tCreate a new template\n"
+ "enable\tEnable a template\n"
+ "disable\tDisable a template\n"
+ "delete\tDelete a template\n"
+ "exit\tGo to main menu\n"
+ "help\tPrint this message");
}
/**
* EFFECTS: List templates in Name[ENABLED / DISABLED] Subject Validity format.
*/
@Override
public void show() {
session.getCa().getTemplates().forEach(tem ->
System.out.printf("%s[%s]\t%s\t%d Days\n",
tem.getName(),
tem.isEnabled() ? "ENABLED" : "DISABLED",
tem.getSubject(),
tem.getValidity()));
}
/**
* EFFECTS: Create a new template with the given name and switch to the template set screen.
*/
private void handleAdd(String... args) {
if (args.length <= 1) {
System.out.println("Usage: add <name>");
return;
}
if (session.getCa().findTemplate(args[1], false) != null) {
System.out.println("The template already exists.");
return;
}
session.setScreen(Screen.TEMPLATE_SET,
new Template(args[1], false, (Name) null, 30));
}
/**
* EFFECTS: Handle the enable / disable commands.
* MODIFIES: session
*/
private void handleEnableDisable(boolean enable, String... args) {
if (args.length <= 1) {
System.out.printf("Usage: %s <template>\n", enable ? "enable" : "disable");
return;
}
Template tmp = session.getCa().findTemplate(args[1], false);
if (tmp == null) {
System.out.println("Cannot find the template specified");
return;
}
session.getCa().setTemplateEnable(tmp, enable);
session.setUnsaved(true);
}
/**
* EFFECTS: Handle the delete command
* MODIFIES: session
*/
private void handleDelete(String... args) {
if (args.length <= 1) {
System.out.println("Usage: delete <template>");
return;
}
Template tmp = session.getCa().findTemplate(args[1], true);
if (tmp == null) {
System.out.println("Cannot find the template specified");
return;
}
session.getCa().removeTemplate(tmp);
session.setUnsaved(true);
}
/**
* EFFECTS: Handle commands.
*/
@Override
public void command(String... args) {
switch (args[0]) {
case "add":
handleAdd(args);
break;
case "enable":
handleEnableDisable(true, args);
break;
case "disable":
handleEnableDisable(false, args);
break;
case "delete":
handleDelete(args);
break;
default:
help();
break;
}
}
/**
* EFFECTS: Go to main menu.
*/
@Override
public Screen exit() {
return Screen.MAIN;
}
/**
* EFFECTS: Return "/templates/ %"
*/
@Override
public String getPS1() {
return "/templates/ %";
}
}
|