Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-T08-1#85 from jovantanyk/fix-storage
Browse files Browse the repository at this point in the history
Fix storage
  • Loading branch information
NatLeong authored Mar 23, 2024
2 parents a1b2dc1 + db359dd commit 0586070
Show file tree
Hide file tree
Showing 24 changed files with 423 additions and 313 deletions.
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/commons/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class JsonUtil {
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.registerModule(new SimpleModule("SimpleModule")
.addSerializer(Level.class, new ToStringSerializer())
.addDeserializer(Level.class, new LevelDeserializer(Level.class)));
.addDeserializer(Level.class, new LevelDeserializer(Level.class))
.addSerializer(new OptionalSerializer()));

static <T> void serializeObjectToJsonFile(Path jsonFile, T objectToSerialize) throws IOException {
FileUtil.writeToFile(jsonFile, toJsonString(objectToSerialize));
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/seedu/address/commons/util/OptionalSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seedu.address.commons.util;

import java.io.IOException;
import java.util.Optional;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
* A Serializer for handling Optional values and converting them into
* proper JSON strings with correct null formatting.
*/
public class OptionalSerializer extends JsonSerializer<Optional<?>> {
@Override
public void serialize(Optional<?> optional, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
if (optional.isPresent()) {
jsonGenerator.writeObject(optional.get());
} else {
jsonGenerator.writeNull();
}
}
/**
* Avoids directly specifying Optional.class with generics
*/
public Class<Optional<?>> handledType() {
return (Class<Optional<?>>) (Class<?>) Optional.class;
}
}
34 changes: 9 additions & 25 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package seedu.address.logic;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import seedu.address.logic.parser.Prefix;
import seedu.address.model.person.Allergies;
import seedu.address.model.person.BloodType;
import seedu.address.model.person.Condition;
import seedu.address.model.person.Country;
import seedu.address.model.person.DateOfAdmission;
import seedu.address.model.person.Diagnosis;
import seedu.address.model.person.Email;
import seedu.address.model.person.Person;
import seedu.address.model.person.Symptom;

/**
* Container for user visible messages.
Expand All @@ -23,6 +14,7 @@ public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_NRIC_NOT_FOUND = "The NRIC provided is not found in the system";
public static final String MESSAGE_PERSON_NOT_FOUND = "The person provided was not found";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
Expand All @@ -45,14 +37,6 @@ public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePref
*/
public static String format(Person person) {
final StringBuilder builder = new StringBuilder();
Optional<Email> email = Optional.ofNullable(person.getEmail());
Optional<Country> country = Optional.ofNullable(person.getCountry());
Optional<Allergies> allergies = Optional.ofNullable(person.getAllergies());
Optional<BloodType> bloodType = Optional.ofNullable(person.getBloodType());
Optional<Condition> condition = Optional.ofNullable(person.getCondition());
Optional<DateOfAdmission> dateOfAdmission = Optional.ofNullable(person.getDateOfAdmission());
Optional<Diagnosis> diagnosis = Optional.ofNullable(person.getDiagnosis());
Optional<Symptom> symptom = Optional.ofNullable(person.getSymptom());
builder.append(person.getName())
.append("; NRIC: ")
.append(person.getNric())
Expand All @@ -67,21 +51,21 @@ public static String format(Person person) {
.append("; Status: ")
.append(person.getStatus())
.append("; Email: ")
.append(Optional.ofNullable(person.getEmail()).map(Object::toString).orElse("-"))
.append(person.getEmail().orElse("-"))
.append("; Country: ")
.append(Optional.ofNullable(person.getCountry()).map(Object::toString).orElse("-"))
.append(person.getCountry().orElse("-"))
.append("; Allergies: ")
.append(Optional.ofNullable(person.getAllergies()).map(Object::toString).orElse("-"))
.append(person.getAllergies().orElse("-"))
.append("; Blood Type: ")
.append(Optional.ofNullable(person.getBloodType()).map(Object::toString).orElse("-"))
.append(person.getBloodType().orElse("-"))
.append("; Condition: ")
.append(Optional.ofNullable(person.getCondition()).map(Object::toString).orElse("-"))
.append(person.getCondition().orElse("-"))
.append("; DOA: ")
.append(Optional.ofNullable(person.getDateOfAdmission()).map(Object::toString).orElse("-"))
.append(person.getDateOfAdmission().orElse("-"))
.append("; Diagnosis: ")
.append(Optional.ofNullable(person.getDiagnosis()).map(Object::toString).orElse("-"))
.append(person.getDiagnosis().orElse("-"))
.append("; Symptom: ")
.append(Optional.ofNullable(person.getSymptom()).map(Object::toString).orElse("-"))
.append(person.getSymptom().orElse("-"))
.append("; Tags: ");
person.getTags().forEach(builder::append);
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CommandResult execute(Model model) throws CommandException {

ObservableList<Person> persons = model.getFilteredPersonList();
if (!model.hasPerson(Person.createPersonWithNric(targetNric))) {
throw new CommandException(Messages.MESSAGE_PERSON_NOT_FOUND);
throw new CommandException(Messages.MESSAGE_NRIC_NOT_FOUND);
}
//Difference between filteredPersons.contains and model.hasPerson: first checks if the instance is in the list,
//second checks if the NRIC is in the list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public UpdatePersonDescriptor(UpdatePersonDescriptor toCopy) {
setDateOfBirth(toCopy.dateOfBirth);
setSex(toCopy.sex);
setStatus(toCopy.status);
// setTags(toCopy.tags);
setTags(toCopy.tags);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public CreateCommand parse(String args) throws ParseException {
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NRIC, PREFIX_NAME, PREFIX_PHONE, PREFIX_ADDRESS,
PREFIX_DATEOFBIRTH, PREFIX_SEX, PREFIX_STATUS);
PREFIX_DATEOFBIRTH, PREFIX_SEX);
Nric nric = ParserUtil.parseNric(argMultimap.getValue(PREFIX_NRIC).get());
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
DateOfBirth dob = ParserUtil.parseDateOfBirth(argMultimap.getValue(PREFIX_DATEOFBIRTH).get());
Sex sex = ParserUtil.parseSex(argMultimap.getValue(PREFIX_SEX).get());
Status status = ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).get());
//TODO (later): assersion to make sure optional values don't generate errors
//TODO (later): assersion to make sure optinal values don't generate errors
Person person = new Person(nric, name, phone, address, dob, sex, status);
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public UpdateCommand parse(String args) throws ParseException {
PREFIX_DIAGNOSIS);

UpdatePersonDescriptor updatePersonDescriptor = new UpdatePersonDescriptor();
updatePersonDescriptor.setNric(nric);

// Mandatory fields
if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
Expand All @@ -74,10 +73,6 @@ public UpdateCommand parse(String args) throws ParseException {
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
updatePersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}
if (argMultimap.getValue(PREFIX_DATEOFBIRTH).isPresent()) {
updatePersonDescriptor.setDateOfBirth(
ParserUtil.parseDateOfBirth(argMultimap.getValue(PREFIX_DATEOFBIRTH).get()));
}
if (argMultimap.getValue(PREFIX_SEX).isPresent()) {
updatePersonDescriptor.setSex(ParserUtil.parseSex(argMultimap.getValue(PREFIX_SEX).get()));
}
Expand Down
29 changes: 2 additions & 27 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,6 @@ public boolean isSamePerson(Person otherPerson) {
return otherPerson != null && otherPerson.getNric().equals(getNric());
}

/**
* Returns true if the person has all mandatory fields.
*/
public static boolean isValidPerson(Person person) {
return person.nric != null
&& person.name != null
&& person.phone != null
&& person.address != null
&& person.dateOfBirth != null
&& person.sex != null
&& person.status != null;
}

/**
* Returns true if both persons have the same identity and all data fields.
* This defines a stronger notion of equality between two persons.
Expand All @@ -266,31 +253,19 @@ public boolean equals(Object other) {
return false;
}
Person otherPerson = (Person) other;
if (!(isValidPerson(this) && isValidPerson(otherPerson))) {
return false;
}
return nric.equals(otherPerson.nric)
&& name.equals(otherPerson.name)
&& phone.equals(otherPerson.phone)
&& address.equals(otherPerson.address)
&& dateOfBirth.equals(otherPerson.dateOfBirth)
&& sex.equals(otherPerson.sex)
&& status.equals(otherPerson.status)
&& tags.equals(otherPerson.tags)
&& Objects.equals(email, otherPerson.email)
&& Objects.equals(country, otherPerson.country)
&& Objects.equals(allergies, otherPerson.allergies)
&& Objects.equals(bloodType, otherPerson.bloodType)
&& Objects.equals(condition, otherPerson.condition)
&& Objects.equals(dateOfAdmission, otherPerson.dateOfAdmission)
&& Objects.equals(diagnosis, otherPerson.diagnosis)
&& Objects.equals(symptom, otherPerson.symptom);
&& status.equals(otherPerson.status);
}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(name, phone, email, address, tags);
return Objects.hash(nric, name, phone, address, dateOfBirth, sex, status);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Phone {
public static final String MESSAGE_CONSTRAINTS =
"Phone numbers should only contain numbers, and it should be at least 8 digits long";
//TODO: change REGEX to give correct results
public static final String VALIDATION_REGEX = "\\d{8,}";
public static final String VALIDATION_REGEX = "\\d{8}";
private final String value;

/**
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
public class SampleDataUtil {
public static Person[] getSamplePersons() {
return new Person[] {
new Person(new Nric("A1234567B"), new Name("Alex Yeoh"), new Phone("87438807"),
new Person(new Nric("T0234567C"), new Name("Alex Yeoh"), new Phone("87438807"),
new Address("Blk 30 Geylang Street 29, #06-40"), new DateOfBirth("1977-04-03"),
new Sex("M"), new Status("HEALTHY")),
new Person(new Nric("A1234568B"), new Name("Bernice Yu"), new Phone("99272758"),
new Person(new Nric("S9234568N"), new Name("Bernice Yu"), new Phone("99272758"),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"), new DateOfBirth("1977-04-03"),
new Sex("F"), new Status("UNWELL")),
new Person(new Nric("A1234569B"), new Name("Charlotte Oliveiro"), new Phone("93210283"),
new Person(new Nric("S8934569Z"), new Name("Charlotte Oliveiro"), new Phone("93210283"),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"), new DateOfBirth("2001-04-03"),
new Sex("F"), new Status("HEALTHY")),
new Person(new Nric("A1234560B"), new Name("David Li"), new Phone("91031282"),
new Person(new Nric("T0134560A"), new Name("David Li"), new Phone("91031282"),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"), new DateOfBirth("1987-11-03"),
new Sex("M"), new Status("PENDING")),
new Person(new Nric("A1234561B"), new Name("Irfan Ibrahim"), new Phone("92492021"),
new Person(new Nric("T0534161B"), new Name("Irfan Ibrahim"), new Phone("92492021"),
new Address("Blk 47 Tampines Street 20, #17-35"), new DateOfBirth("1970-12-03"),
new Sex("M"), new Status("UNWELL")),
new Person(new Nric("A1234562B"), new Name("Roy Balakrishnan"), new Phone("92624417"),
new Person(new Nric("S9781662B"), new Name("Roy Balakrishnan"), new Phone("92624417"),
new Address("Blk 45 Aljunied Street 85, #11-31"), new DateOfBirth("1987-04-03"),
new Sex("M"), new Status("PENDING"))
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
{
"persons": [ {
"name": "Valid Person",
"phone": "9482424",
"email": "[email protected]",
"address": "4th street"
}, {
"name": "Person With Invalid Phone Field",
"phone": "948asdf2424",
"email": "[email protected]",
"address": "4th street"
} ]
"persons": [
{
"nric": "T0139571B",
"name": "Valid Person",
"phone": "94351253",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "T0139571B",
"name": "Invalid Person Phone Wrong",
"phone": "94eeqa!1253a",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
{
"persons": [ {
"name": "Person with invalid name field: Ha!ns Mu@ster",
"phone": "9482424",
"email": "[email protected]",
"address": "4th street"
} ]
"persons": [
{
"nric": "INVALID NRIC!!!!",
"name": "Alice Pauline",
"phone": "94351253",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
{
"persons": [ {
"name": "Alice Pauline",
"phone": "94351253",
"email": "[email protected]",
"address": "123, Jurong West Ave 6, #08-111",
"tags": [ "friends" ]
}, {
"name": "Alice Pauline",
"phone": "94351253",
"email": "[email protected]",
"address": "4th street"
} ]
"persons": [
{
"nric": "T0139571B",
"name": "Alice Pauline",
"phone": "94351253",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
},
{
"nric": "T0139571B",
"name": "Alice Pauline",
"phone": "94351253",
"address": "123, Jurong West Ave 6, #08-111",
"dateOfBirth": "2001-01-01",
"sex": "F",
"status": "HEALTHY",
"email": null,
"country": null,
"allergies": null,
"bloodType": null,
"condition": null,
"dateOfAdmission": null,
"diagnosis": null,
"symptom": null
}
]
}
Loading

0 comments on commit 0586070

Please sign in to comment.