|
| 1 | +package seedu.address.storage; |
| 2 | + |
| 3 | +import java.time.LocalTime; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 11 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 12 | + |
| 13 | +import seedu.address.commons.exceptions.IllegalValueException; |
| 14 | +import seedu.address.model.person.Activity; |
| 15 | +import seedu.address.model.person.Day; |
| 16 | +import seedu.address.model.person.Duration; |
| 17 | +import seedu.address.model.person.Exercise; |
| 18 | +import seedu.address.model.person.Lesson; |
| 19 | +import seedu.address.model.person.Name; |
| 20 | +import seedu.address.model.person.Routine; |
| 21 | +import seedu.address.model.person.Slot; |
| 22 | +import seedu.address.model.tag.Tag; |
| 23 | + |
| 24 | +/** |
| 25 | + * Jackson-friendly version of {@link Slot}. |
| 26 | + */ |
| 27 | +public class JsonAdaptedSlot { |
| 28 | + public static final String TYPE_ROUTINE = "routine"; |
| 29 | + public static final String TYPE_LESSON = "lesson"; |
| 30 | + |
| 31 | + public static final String MISSING_FIELD_MESSAGE_FORMAT = "Slot's %s field is missing!"; |
| 32 | + |
| 33 | + private final String activityName; |
| 34 | + private final String type; |
| 35 | + private final String day; |
| 36 | + private final String duration; |
| 37 | + private final List<JsonAdaptedTag> tagsForLesson = new ArrayList<>(); |
| 38 | + private final List<JsonAdaptedExercise> exercisesForRoutine = new ArrayList<>(); |
| 39 | + |
| 40 | + /** |
| 41 | + * Constructs a {@code JsonAdaptedSlot} with the given slot details. |
| 42 | + */ |
| 43 | + @JsonCreator |
| 44 | + public JsonAdaptedSlot(@JsonProperty("activityName") String activityName, |
| 45 | + @JsonProperty("type") String type, |
| 46 | + @JsonProperty("day") String day, |
| 47 | + @JsonProperty("duration") String duration, |
| 48 | + @JsonProperty("tagsForLesson") List<JsonAdaptedTag> tags, |
| 49 | + @JsonProperty("exercisesForRoutine") List<JsonAdaptedExercise> exercises) { |
| 50 | + this.activityName = activityName; |
| 51 | + this.type = type; |
| 52 | + this.day = day; |
| 53 | + this.duration = duration; |
| 54 | + if (type.equals(TYPE_LESSON)) { |
| 55 | + if (tags != null) { |
| 56 | + tagsForLesson.addAll(tags); |
| 57 | + } |
| 58 | + } else { |
| 59 | + if (exercises != null) { |
| 60 | + exercisesForRoutine.addAll(exercises); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Converts a given {@code Slot} into this class for Jackson use. |
| 67 | + */ |
| 68 | + public JsonAdaptedSlot(Slot source) { |
| 69 | + activityName = source.getActivity().getName().fullName; |
| 70 | + type = source.getActivity() instanceof Routine |
| 71 | + ? TYPE_ROUTINE |
| 72 | + : TYPE_LESSON; |
| 73 | + day = source.getDay().toString(); |
| 74 | + duration = source.getDuration().toString(); |
| 75 | + if (type.equals(TYPE_LESSON)) { |
| 76 | + Lesson lesson = (Lesson) source.getActivity(); |
| 77 | + tagsForLesson.addAll(lesson.getTags() |
| 78 | + .stream() |
| 79 | + .map(JsonAdaptedTag::new) |
| 80 | + .collect(Collectors.toList())); |
| 81 | + } else { |
| 82 | + Routine routine = (Routine) source.getActivity(); |
| 83 | + exercisesForRoutine.addAll(routine.getExercises() |
| 84 | + .stream() |
| 85 | + .map(JsonAdaptedExercise::new) |
| 86 | + .collect(Collectors.toList())); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Converts this Jackson-friendly adapted slot object into the model's {@code Slot} object. |
| 92 | + * |
| 93 | + * @throws IllegalValueException if there were any data constraints violated in the adapted exercise. |
| 94 | + */ |
| 95 | + public Slot toModelType() throws IllegalValueException { |
| 96 | + |
| 97 | + if (activityName == null) { |
| 98 | + throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName())); |
| 99 | + } |
| 100 | + if (!Name.isValidName(activityName)) { |
| 101 | + throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS); |
| 102 | + } |
| 103 | + if (Day.isUnknownDay(Day.getDayEnum(day))) { |
| 104 | + throw new IllegalValueException(Day.MESSAGE_CONSTRAINTS); |
| 105 | + } |
| 106 | + if (!Duration.isValidDuration(duration)) { |
| 107 | + throw new IllegalValueException(Duration.MESSAGE_CONSTRAINTS_FORMAT); |
| 108 | + } |
| 109 | + |
| 110 | + final Name modelName = new Name(activityName); |
| 111 | + |
| 112 | + final Activity modelActivity; |
| 113 | + if (type.equals(TYPE_LESSON)) { |
| 114 | + final List<Tag> lessonTags = new ArrayList<>(); |
| 115 | + for (JsonAdaptedTag tag : tagsForLesson) { |
| 116 | + lessonTags.add(tag.toModelType()); |
| 117 | + } |
| 118 | + final Set<Tag> modelTags = new HashSet<>(lessonTags); |
| 119 | + modelActivity = new Lesson(modelName, modelTags); |
| 120 | + } else { |
| 121 | + final List<Exercise> routineExercises = new ArrayList<>(); |
| 122 | + for (JsonAdaptedExercise exercise : exercisesForRoutine) { |
| 123 | + routineExercises.add(exercise.toModelType()); |
| 124 | + } |
| 125 | + final Set<Exercise> modelExercise = new HashSet<>(routineExercises); |
| 126 | + Routine modelRoutine = new Routine(modelName); |
| 127 | + for (Exercise exercise : modelExercise) { |
| 128 | + modelRoutine.addExercise(exercise); |
| 129 | + } |
| 130 | + modelActivity = modelRoutine; |
| 131 | + } |
| 132 | + |
| 133 | + final Day modelDay = Day.getDayEnum(day); |
| 134 | + |
| 135 | + final Duration modelDuration = parseDuration(duration); |
| 136 | + |
| 137 | + return new Slot(modelActivity, modelDay, modelDuration); |
| 138 | + } |
| 139 | + |
| 140 | + private Duration parseDuration(String toParse) throws IllegalValueException { |
| 141 | + String[] timeSplit = toParse.split("-"); |
| 142 | + |
| 143 | + int startHour = Integer.parseInt(timeSplit[0].substring(0, 2)); |
| 144 | + int startMinute = Integer.parseInt(timeSplit[0].substring(2, 4)); |
| 145 | + LocalTime startTime = LocalTime.of(startHour, startMinute); |
| 146 | + |
| 147 | + int endHour = Integer.parseInt(timeSplit[1].substring(0, 2)); |
| 148 | + int endMinute = Integer.parseInt(timeSplit[1].substring(2, 4)); |
| 149 | + LocalTime endTime = LocalTime.of(endHour, endMinute); |
| 150 | + |
| 151 | + if (!Duration.isValidDuration(startTime, endTime)) { |
| 152 | + throw new IllegalValueException(Duration.MESSAGE_CONSTRAINTS_ORDER); |
| 153 | + } |
| 154 | + |
| 155 | + return new Duration(startTime, endTime); |
| 156 | + } |
| 157 | +} |
0 commit comments