|
4 | 4 | import java.util.HashSet; |
5 | 5 | import java.util.List; |
6 | 6 | import java.util.Set; |
| 7 | +import java.util.logging.Logger; |
7 | 8 | import java.util.stream.Collectors; |
8 | 9 |
|
9 | 10 | import com.fasterxml.jackson.annotation.JsonCreator; |
10 | 11 | import com.fasterxml.jackson.annotation.JsonProperty; |
11 | 12 |
|
| 13 | +import seedu.address.commons.core.LogsCenter; |
12 | 14 | import seedu.address.commons.exceptions.IllegalValueException; |
13 | 15 | import seedu.address.model.lab.Lab; |
14 | 16 | import seedu.address.model.student.Email; |
|
25 | 27 | class JsonAdaptedStudent { |
26 | 28 |
|
27 | 29 | public static final String MISSING_FIELD_MESSAGE_FORMAT = "Person's %s field is missing!"; |
| 30 | + private static final Logger logger = LogsCenter.getLogger(JsonAdaptedStudent.class); |
28 | 31 |
|
29 | 32 | private final String name; |
30 | 33 | private final String email; |
@@ -89,68 +92,101 @@ public JsonAdaptedStudent(Student source) { |
89 | 92 | * @throws IllegalValueException if there were any data constraints violated in the adapted student. |
90 | 93 | */ |
91 | 94 | public Student toModelType() throws IllegalValueException { |
92 | | - final List<Tag> personTags = new ArrayList<>(); |
93 | | - for (JsonAdaptedTag tag : tagged) { |
94 | | - personTags.add(tag.toModelType()); |
95 | | - } |
96 | | - final List<Lab> personLabs = new ArrayList<>(); |
| 95 | + final List<Tag> personTags = deserializeTags(); |
| 96 | + |
| 97 | + final List<Lab> personLabs = deserializeLabs(); |
| 98 | + |
| 99 | + final Name modelName = deserializeName(); |
| 100 | + |
| 101 | + final Email modelEmail = deserializeEmail(); |
| 102 | + |
| 103 | + final GithubUsername modelUsername = deserializeGithub(); |
| 104 | + |
| 105 | + final Telegram modelTelegram = deserializeTelegram(); |
| 106 | + |
| 107 | + final StudentId modelId = deserializeStudentId(); |
| 108 | + |
| 109 | + final Set<Tag> modelTags = new HashSet<>(personTags); |
| 110 | + |
| 111 | + Student s = new Student(modelName, modelEmail, modelTags, modelUsername, modelTelegram, modelId); |
| 112 | + s.getLabs().setLabs(personLabs); |
| 113 | + |
| 114 | + return s; |
| 115 | + } |
97 | 116 |
|
| 117 | + private List<Lab> deserializeLabs() throws IllegalValueException { |
| 118 | + List<Lab> personLabs = new ArrayList<>(); |
98 | 119 | for (int i = 0; i < labNumbers.size(); i++) { |
99 | 120 | // Incase of wrong lab status format we will just add the lab with LabStatus.UNSUBMITTED |
100 | 121 | try { |
101 | 122 | personLabs.add(labNumbers.get(i).toModelType().of(labStatuses.get(i).getLabStatus())); |
102 | 123 | } catch (IllegalArgumentException e) { |
| 124 | + logger.info("Illegal lab status found when converting labs " + e); |
103 | 125 | personLabs.add(labNumbers.get(i).toModelType()); |
104 | 126 | } |
105 | 127 | } |
| 128 | + return personLabs; |
| 129 | + } |
| 130 | + |
| 131 | + private List<Tag> deserializeTags() throws IllegalValueException { |
| 132 | + List<Tag> personTags = new ArrayList<>(); |
| 133 | + for (JsonAdaptedTag tag : tagged) { |
| 134 | + personTags.add(tag.toModelType()); |
| 135 | + } |
| 136 | + return personTags; |
| 137 | + } |
106 | 138 |
|
| 139 | + private Name deserializeName() throws IllegalValueException { |
107 | 140 | if (name == null) { |
108 | 141 | throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName())); |
109 | 142 | } |
110 | 143 | if (!Name.isValidName(name)) { |
111 | 144 | throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS); |
112 | 145 | } |
113 | | - final Name modelName = new Name(name); |
| 146 | + return new Name(name); |
| 147 | + } |
114 | 148 |
|
| 149 | + private Email deserializeEmail() throws IllegalValueException { |
115 | 150 | if (email == null) { |
116 | 151 | throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Email.class.getSimpleName())); |
117 | 152 | } |
118 | 153 | if (!Email.isValidEmail(email)) { |
119 | 154 | throw new IllegalValueException(Email.MESSAGE_CONSTRAINTS); |
120 | 155 | } |
121 | | - final Email modelEmail = new Email(email); |
| 156 | + return new Email(email); |
| 157 | + } |
122 | 158 |
|
| 159 | + private GithubUsername deserializeGithub() throws IllegalValueException { |
123 | 160 | if (githubUsername == null) { |
124 | 161 | throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, |
125 | 162 | GithubUsername.class.getSimpleName())); |
126 | 163 | } |
127 | 164 | if (!GithubUsername.isValidGithubUsername(githubUsername)) { |
128 | 165 | throw new IllegalValueException(GithubUsername.MESSAGE_CONSTRAINTS); |
129 | 166 | } |
130 | | - final GithubUsername modelUsername = new GithubUsername(githubUsername); |
| 167 | + return new GithubUsername(githubUsername); |
| 168 | + } |
131 | 169 |
|
| 170 | + private Telegram deserializeTelegram() throws IllegalValueException { |
132 | 171 | if (telegram == null) { |
133 | 172 | throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, |
134 | 173 | Telegram.class.getSimpleName())); |
135 | 174 | } |
136 | 175 | if (!Telegram.isValidTelegram(telegram)) { |
137 | 176 | throw new IllegalValueException(Telegram.MESSAGE_CONSTRAINTS); |
138 | 177 | } |
139 | | - final Telegram modelTelegram = new Telegram(telegram); |
| 178 | + return new Telegram(telegram); |
| 179 | + } |
140 | 180 |
|
| 181 | + private StudentId deserializeStudentId() throws IllegalValueException { |
141 | 182 | if (studentId == null) { |
142 | 183 | throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, |
143 | 184 | StudentId.class.getSimpleName())); |
144 | 185 | } |
145 | 186 | if (!StudentId.isValidStudentId(studentId)) { |
146 | 187 | throw new IllegalValueException(StudentId.MESSAGE_CONSTRAINTS); |
147 | 188 | } |
148 | | - final StudentId modelId = new StudentId(studentId); |
149 | | - |
150 | | - final Set<Tag> modelTags = new HashSet<>(personTags); |
151 | | - Student s = new Student(modelName, modelEmail, modelTags, modelUsername, modelTelegram, modelId); |
152 | | - s.getLabs().setLabs(personLabs); |
153 | | - return s; |
| 189 | + return new StudentId(studentId); |
154 | 190 | } |
155 | 191 |
|
156 | 192 | } |
0 commit comments