forked from se-edu/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from cs2113-ay1819s2-t08-2/master
merge
- Loading branch information
Showing
7 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/seedu/address/model/task/exceptions/DuplicateTaskException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package seedu.address.model.task.exceptions; | ||
|
||
public class DuplicateTaskException extends RuntimeException { | ||
public DuplicateTaskException() { | ||
super("Operation would result in duplicate tasks"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package seedu.address.storage; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.model.person.*; | ||
import seedu.address.model.tag.Tag; | ||
import seedu.address.model.task.DeadlineDate; | ||
import seedu.address.model.task.DeadlineTime; | ||
import seedu.address.model.task.Task; | ||
import seedu.address.model.task.TaskName; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class JsonAdaptedTask { | ||
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Task's %s field is missing!"; | ||
|
||
private final String taskName; | ||
private final String deadlineDate; | ||
private final String deadlineTime; | ||
|
||
private final List<JsonAdaptedTag> tagged = new ArrayList<>(); | ||
|
||
/** | ||
* Constructs a {@code JsonAdaptedPerson} with the given person details. | ||
*/ | ||
@JsonCreator | ||
public JsonAdaptedTask(@JsonProperty("taskName") String taskName, @JsonProperty("deadlineDate") String deadlineDate, | ||
@JsonProperty("deadlineTime") String deadlineTime, | ||
@JsonProperty("tagged") List<JsonAdaptedTag> tagged) { | ||
this.taskName = taskName; | ||
this.deadlineDate = deadlineDate; | ||
this.deadlineTime = deadlineTime; | ||
if (tagged != null) { | ||
this.tagged.addAll(tagged); | ||
} | ||
} | ||
|
||
/** | ||
* Converts a given {@code Person} into this class for Jackson use. | ||
*/ | ||
public JsonAdaptedTask(Task source) { | ||
taskName = source.getTaskName().fullName; | ||
deadlineTime = source.getDeadlineTime().value; | ||
deadlineDate = source.getDeadlineDate().value; | ||
|
||
tagged.addAll(source.getTags().stream() | ||
.map(JsonAdaptedTag::new) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
/** | ||
* Converts this Jackson-friendly adapted person object into the model's {@code Person} object. | ||
* | ||
* @throws IllegalValueException if there were any data constraints violated in the adapted person. | ||
*/ | ||
public Task toModelType() throws IllegalValueException { | ||
final List<Tag> taskTags = new ArrayList<>(); | ||
for (JsonAdaptedTag tag : tagged) { | ||
taskTags.add(tag.toModelType()); | ||
} | ||
|
||
if (taskName == null) { | ||
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName())); | ||
} | ||
if (!TaskName.isValidName(taskName)) { | ||
throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS); | ||
} | ||
final TaskName modelTaskName = new TaskName(taskName); | ||
|
||
if (deadlineDate == null) { | ||
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Phone.class.getSimpleName())); | ||
} | ||
// if (!DeadlineDate.(deadlineDate)) { | ||
// throw new IllegalValueException(Phone.MESSAGE_CONSTRAINTS); | ||
// } | ||
final DeadlineDate modelDeadlineDate = new DeadlineDate(deadlineDate); | ||
|
||
if (deadlineTime == null) { | ||
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Email.class.getSimpleName())); | ||
} | ||
// if (!Email.isValidEmail(deadlineTime)) { | ||
// throw new IllegalValueException(Email.MESSAGE_CONSTRAINTS); | ||
// } | ||
final DeadlineTime modelDeadlineTime = new DeadlineTime(deadlineTime); | ||
|
||
final Set<Tag> modelTags = new HashSet<>(taskTags); | ||
return new Task(modelTaskName,modelDeadlineTime, modelDeadlineDate, modelTags); | ||
} | ||
|
||
} | ||
|
||
|
64 changes: 64 additions & 0 deletions
64
src/main/java/seedu/address/storage/JsonSerializableTaskList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package seedu.address.storage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonRootName; | ||
|
||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.model.AddressBook; | ||
import seedu.address.model.ReadOnlyAddressBook; | ||
import seedu.address.model.ReadOnlyTaskList; | ||
import seedu.address.model.TaskList; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.task.Task; | ||
|
||
/** | ||
* An Immutable AddressBook that is serializable to JSON format. | ||
*/ | ||
@JsonRootName(value = "tasklist") | ||
class JsonSerializableTaskList { | ||
|
||
public static final String MESSAGE_DUPLICATE_PERSON = "Persons list contains duplicate person(s)."; | ||
|
||
private final List<JsonAdaptedTask> tasks = new ArrayList<>(); | ||
|
||
/** | ||
* Constructs a {@code JsonSerializableAddressBook} with the given persons. | ||
*/ | ||
@JsonCreator | ||
public JsonSerializableTaskList(@JsonProperty("tasks") List<JsonAdaptedTask> | ||
tasks) { | ||
this.tasks.addAll(tasks); | ||
} | ||
|
||
/** | ||
* Converts a given {@code ReadOnlyAddressBook} into this class for Jackson use. | ||
* | ||
* @param source future changes to this will not affect the created {@code JsonSerializableAddressBook}. | ||
*/ | ||
public JsonSerializableTaskList(ReadOnlyTaskList source) { | ||
tasks.addAll(source.getTaskList().stream().map(JsonAdaptedTask::new).collect(Collectors.toList())); | ||
} | ||
|
||
/** | ||
* Converts this address book into the model's {@code AddressBook} object. | ||
* | ||
* @throws IllegalValueException if there were any data constraints violated. | ||
*/ | ||
public TaskList toModelType() throws IllegalValueException { | ||
TaskList taskList = new TaskList(); | ||
for (JsonAdaptedTask jsonAdaptedTask : tasks) { | ||
Task task = jsonAdaptedTask.toModelType(); | ||
if (taskList.hasTask(task)) { | ||
throw new IllegalValueException(MESSAGE_DUPLICATE_PERSON); | ||
} | ||
taskList.addTask(task); | ||
} | ||
return taskList; | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/seedu/address/storage/JsonTaskListStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package seedu.address.storage; | ||
|
||
import com.fasterxml.jackson.databind.JsonSerializable; | ||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.commons.exceptions.DataConversionException; | ||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.commons.util.FileUtil; | ||
import seedu.address.commons.util.JsonUtil; | ||
import seedu.address.model.ReadOnlyAddressBook; | ||
import seedu.address.model.ReadOnlyTaskList; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import java.util.logging.Logger; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class JsonTaskListStorage implements TaskListStorage { | ||
|
||
private static final Logger logger = LogsCenter.getLogger(JsonAddressBookStorage.class); | ||
private Path filePath; | ||
|
||
public JsonTaskListStorage(Path filePath) {this.filePath = filePath; } | ||
|
||
public Path getTaskListFilePath(){ return filePath; } | ||
@Override | ||
public Optional<ReadOnlyTaskList> readTaskList() throws DataConversionException{ | ||
return readTaskList(filePath); | ||
} | ||
|
||
public Optional<ReadOnlyTaskList> readTaskList(Path filePath) throws DataConversionException { | ||
requireNonNull(filePath); | ||
Optional<JsonSerializableTaskList> jsonTaskList = JsonUtil.readJsonFile(filePath, JsonSerializableTaskList.class); | ||
|
||
if (!jsonTaskList.isPresent()) { | ||
return Optional.empty(); | ||
} | ||
try { | ||
return Optional.of(jsonTaskList.get().toModelType()); | ||
} catch (IllegalValueException ive) { | ||
logger.info("Illegal values found in " + filePath + ": " + ive.getMessage()); | ||
throw new DataConversionException(ive); | ||
} | ||
} | ||
|
||
@Override | ||
public void saveTaskList(ReadOnlyTaskList taskList) throws IOException { | ||
saveTaskList(taskList, filePath); | ||
} | ||
|
||
/** | ||
* Similar to {@link #saveTaskList(ReadOnlyTaskList)}. | ||
* | ||
* @param filePath location of the data. Cannot be null. | ||
*/ | ||
public void saveTaskList(ReadOnlyTaskList taskList, Path filePath) throws IOException { | ||
requireNonNull(taskList); | ||
requireNonNull(filePath); | ||
|
||
FileUtil.createIfMissing(filePath); | ||
JsonUtil.saveJsonFile(new JsonSerializableTaskList(taskList), filePath); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package seedu.address.storage; | ||
|
||
import seedu.address.commons.exceptions.DataConversionException; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import seedu.address.model.ReadOnlyTaskList; | ||
|
||
public interface TaskListStorage { | ||
Path getTaskListFilePath(); | ||
|
||
Optional<ReadOnlyTaskList> readTaskList() throws DataConversionException, IOException; | ||
Optional<ReadOnlyTaskList> readTaskList(Path filePath) throws DataConversionException, IOException; | ||
void saveTaskList(ReadOnlyTaskList taskList) throws IOException; | ||
void saveTaskList(ReadOnlyTaskList taskList, Path filePath) throws IOException; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package seedu.address.testutil; | ||
import seedu.address.model.TaskList; | ||
import seedu.address.model.task.Task; | ||
|
||
public class TypicalTasks { | ||
// public static final Task taskOne = | ||
|
||
public static TaskList getTypicalTaskList(){ | ||
TaskList tl = new TaskList(); | ||
return tl; | ||
} | ||
|
||
} |