Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update loading of data to ensure that sex attribute gender matches with gender of relationship roles #307

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException("Attribute " + attributeName + " already exists.");
}
if (attributeName.equalsIgnoreCase("sex")) {
model.genderCheck(model, uuid, attributeValue);
model.genderCheck(uuidToUse, attributeValue);
}
Attribute attribute = AttributeUtil.createAttribute(attributeName, attributeValue);
person.updateAttribute(attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CommandResult execute(Model model) throws CommandException {
}

if (attributeName.equalsIgnoreCase("sex")) {
model.genderCheck(model, uuid, attributeValue);
model.genderCheck(uuidToUse, attributeValue);
}

Attribute attribute = AttributeUtil.createAttribute(attributeName, attributeValue);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ public RoleBasedRelationship checkSiblingsSpousesGender(Model model, String orig
rolePerson2, isSiblings);
}

public void genderCheck(Model model, String uuid, String gender) throws CommandException {
relationships.genderCheck(model, uuid, gender);
public void genderCheck(UUID uuid, String gender) throws CommandException {
relationships.genderCheck(uuid, gender);
}

public void genderMatch(String rolePerson1, String uuid, String shortUuid) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ RoleBasedRelationship checkSiblingsSpousesGender(Model model, String originUuid,
String rolePerson1, String rolePerson2,
Boolean isSiblings) throws CommandException;

void genderCheck(Model model, String uuid, String gender) throws CommandException;
void genderCheck(UUID uuid, String gender) throws CommandException;

void genderMatch(String rolePerson1, String uuid, String shortUuid);
}
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ public RoleBasedRelationship checkSiblingsSpousesGender(Model model, String orig
}

@Override
public void genderCheck(Model model, String uuid, String gender) throws CommandException {
addressBook.genderCheck(model, uuid, gender);
public void genderCheck(UUID uuid, String gender) throws CommandException {
addressBook.genderCheck(uuid, gender);
}

@Override
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ public void genderMatch(String gender, String uuid) {
} else {
genderRole = "FEMALE";
}
String attributeGender = attributes.get("Sex").getValueAsString();
if (!attributeGender.equals(genderRole)) {
throw new IllegalArgumentException("Sex attribute of " + uuid + " does not match the gender of your "
+ "inputted role.\nIf you'd like to change the gender of the person, please change the sex "
+ "attribute by deleting and re-adding it as the gender you want.");
if (attributes.containsKey("Sex")) {
String attributeGender = attributes.get("Sex").getValueAsString();
if (!attributeGender.equals(genderRole)) {
throw new IllegalArgumentException("Sex attribute of " + uuid + " does not match the gender of your "
+ "inputted role.\nIf you'd like to change the gender of the person, please change the sex "
+ "attribute by deleting and re-adding it as the gender you want.");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
public class RelationshipUtil {
protected static ArrayList<ArrayList<String>> roleBasedDescriptors = new ArrayList<>(Arrays.asList(
new ArrayList<>(Arrays.asList("siblings", "brother", "sister")),
new ArrayList<>(Arrays.asList("siblings", "brother", "brother")),
new ArrayList<>(Arrays.asList("siblings", "sister", "sister")),
new ArrayList<>(Arrays.asList("spouses", "husband", "wife")),
new ArrayList<>(Arrays.asList("spouses", "husband", "husband")),
new ArrayList<>(Arrays.asList("spouses", "wife", "wife")),
new ArrayList<>(Arrays.asList("bioparents", "parent", "child"))
));
protected static ArrayList<String> rolelessDescriptors = new ArrayList<>(
Expand Down Expand Up @@ -689,15 +693,14 @@ private void genderError(String gender, String role, String uuid) throws Command
/**
* Checks if the inputted gender of a person matches the gender inferred from existing relationship roles.
*
* @param model The model containing the relationships.
* @param uuid The last 4 digits of the UUID of a person.
* @param fulluuid The full uuid of a person.
* @param gender The gender input of a person.
* @throws CommandException If the roles provided for the persons are incompatible with their genders as inferred
* from existing relationships.
*/
public void genderCheck(Model model, String uuid, String gender) throws CommandException {
UUID fulluuid = model.getFullUuid(uuid.toString());
ObservableList<Relationship> allRelationships = model.getFilteredRelationshipList();
public void genderCheck(UUID fulluuid, String gender) throws CommandException {
String uuid = fulluuid.toString().substring(fulluuid.toString().length() - 4);
ObservableList<Relationship> allRelationships = this.internalUnmodifiableList;
String genderMatch = null;
for (Relationship r : allRelationships) {
if ((r.getRelationshipDescriptor().equalsIgnoreCase("Siblings")
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/seedu/address/storage/JsonAdaptedAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.AttributeUtil;
import seedu.address.model.person.attribute.Attribute;
import seedu.address.model.person.attribute.NameAttribute;
import seedu.address.model.person.attribute.StringAttribute;

/**
* Jackson-friendly version of {@link Attribute}.
Expand Down Expand Up @@ -46,10 +44,10 @@ public Attribute toModelType() throws IllegalValueException {
if (!name.matches("[a-zA-Z]+")) {
throw new IllegalValueException("Attribute name contains more than one word.");
}
if (name.equals("Name") || name.equals("name")) {
return new NameAttribute("Name", value);
} else {
return new StringAttribute(AttributeUtil.capitalizeAttributeName(name), value);
try {
return AttributeUtil.createAttribute(name, value);
} catch (Exception e) {
throw new IllegalValueException("Invalid attribute type or value in JSON.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public AddressBook toModelType() throws IllegalValueException {
SiblingRelationship siblingRelationship = (SiblingRelationship) relationship;
UUID person1Uuid = siblingRelationship.getPerson1();
UUID person2Uuid = siblingRelationship.getPerson2();
String uuid1 = person1Uuid.toString();
String uuid2 = person2Uuid.toString();
String rolePerson1 = siblingRelationship.getRole(person1Uuid);
String rolePerson2 = siblingRelationship.getRole(person2Uuid);

Expand All @@ -117,11 +119,27 @@ public AddressBook toModelType() throws IllegalValueException {

personGenders.put(person1Uuid, person1Gender);
personGenders.put(person2Uuid, person2Gender);
try {
addressBook.genderMatch(rolePerson1, uuid1, uuid1.substring(uuid1.length() - 4));
} catch (Exception e) {
throw new IllegalValueException(
"Relationship role " + rolePerson1 + " does not match with "
+ uuid1 + "'s sex attribute gender");
}
try {
addressBook.genderMatch(rolePerson2, uuid2, uuid2.substring(uuid2.length() - 4));
} catch (Exception e) {
throw new IllegalValueException(
"Relationship role " + rolePerson2 + " does not match with "
+ uuid2 + "'s sex attribute gender");
}
}
if (relationship instanceof SpousesRelationship) {
SpousesRelationship spousesRelationship = (SpousesRelationship) relationship;
UUID person1Uuid = spousesRelationship.getPerson1();
UUID person2Uuid = spousesRelationship.getPerson2();
String uuid1 = person1Uuid.toString();
String uuid2 = person2Uuid.toString();
String rolePerson1 = spousesRelationship.getRole(person1Uuid);
String rolePerson2 = spousesRelationship.getRole(person2Uuid);

Expand All @@ -137,10 +155,36 @@ public AddressBook toModelType() throws IllegalValueException {

personGenders.put(person1Uuid, person1Gender);
personGenders.put(person2Uuid, person2Gender);
try {
addressBook.genderMatch(rolePerson1, uuid1, uuid1.substring(uuid1.length() - 4));
} catch (Exception e) {
throw new IllegalValueException(
"Relationship role " + rolePerson1 + " does not match with "
+ uuid1 + "'s sex attribute gender");
}
try {
addressBook.genderMatch(rolePerson2, uuid2, uuid2.substring(uuid2.length() - 4));
} catch (Exception e) {
throw new IllegalValueException(
"Relationship role " + rolePerson2 + " does not match with "
+ uuid2 + "'s sex attribute gender");
}
}
if (addressBook.hasRelationship(relationship)) {
throw new IllegalValueException("Duplicate relationship found.");
}
// Check sex attribute gender with existing relationships (if it exists)
for (Person person : addressBook.getPersonList()) {
if (person.hasAttribute("Sex")) {
try {
addressBook.genderCheck(person.getUuid(),
person.getAttribute("Sex").getValueAsString());
} catch (Exception e) {
throw new IllegalValueException("Sex attribute gender of" + person.getUuid().toString()
+ " does not match with existing relationships");
}
}
}
addressBook.addRelationship(relationship);
}
if (pairOfDescriptors != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public RoleBasedRelationship checkSiblingsSpousesGender(Model model, String orig
}

@Override
public void genderCheck(Model model, String uuid, String gender) {
public void genderCheck(UUID uuid, String gender) throws CommandException {
throw new AssertionError("This method should not be called.");
}

Expand Down
Loading