From cbc4c6b6b9cdf6373caecdf54dc53b062f6f0f54 Mon Sep 17 00:00:00 2001 From: "Matt Harrah (frizbog)" Date: Sat, 3 Sep 2016 23:01:43 -0400 Subject: [PATCH] Issue #122 - New class/method to create an individual --- .../gedcom4j/factory/IndividualFactory.java | 133 ++++++++++++++++++ src/main/java/org/gedcom4j/factory/Sex.java | 66 +++++++++ .../org/gedcom4j/factory/package-info.java | 32 +++++ .../factory/IndividualFactoryTest.java | 72 ++++++++++ 4 files changed, 303 insertions(+) create mode 100644 src/main/java/org/gedcom4j/factory/IndividualFactory.java create mode 100644 src/main/java/org/gedcom4j/factory/Sex.java create mode 100644 src/main/java/org/gedcom4j/factory/package-info.java create mode 100644 src/test/java/org/gedcom4j/factory/IndividualFactoryTest.java diff --git a/src/main/java/org/gedcom4j/factory/IndividualFactory.java b/src/main/java/org/gedcom4j/factory/IndividualFactory.java new file mode 100644 index 00000000..15dfaa9f --- /dev/null +++ b/src/main/java/org/gedcom4j/factory/IndividualFactory.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2009-2016 Matthew R. Harrah + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ +package org.gedcom4j.factory; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import org.gedcom4j.model.Gedcom; +import org.gedcom4j.model.Individual; +import org.gedcom4j.model.IndividualEvent; +import org.gedcom4j.model.IndividualEventType; +import org.gedcom4j.model.PersonalName; +import org.gedcom4j.model.Place; +import org.gedcom4j.model.StringWithCustomTags; + +/** + * @author frizbog + * + */ +public class IndividualFactory { + + /** + * Create an {@link Individual} and add them to the {@link Gedcom} + * + * @param g + * the gedcom to add the individual to + * @param givenName + * the given name of the new individual - optional + * @param surname + * the surname of the new individual - optional + * @param sex + * the sex of the new individual - optional + * @param birth + * the birthdate of the new individual - optional + * @param birthPlace + * the place of birth for the new individual - optional + * @param death + * the death date of the new individual - optional + * @param deathPlace + * the place of death for the new individual - optional + * @return the individual. As a side effect, the individual is already added to the gedcom for you. + */ + @SuppressWarnings("checkstyle:ParameterNumber") + public Individual create(Gedcom g, String givenName, String surname, Sex sex, Date birth, String birthPlace, Date death, + String deathPlace) { + Individual result = new Individual(); + + // Make an xref and add to gedcom + for (int xref = g.getIndividuals().size(); !g.getIndividuals().containsKey("@I" + xref + "@") && result + .getXref() == null; xref++) { + result.setXref("@I" + xref + "@"); + g.getIndividuals().put(result.getXref(), result); + } + + // Set sex + if (sex != null) { + result.setSex(new StringWithCustomTags(sex.getCode())); + } + + // Set name + StringBuilder basicName = new StringBuilder(); + if (givenName != null) { + basicName.append(givenName).append(" /"); + } + if (surname != null) { + basicName.append(surname); + } + basicName.append("/"); + PersonalName n = new PersonalName(); + n.setBasic(basicName.toString()); + result.getNames(true).add(n); + + // Set birth event + if (birth != null || birthPlace != null) { + IndividualEvent b = new IndividualEvent(); + b.setType(IndividualEventType.BIRTH); + if (birth != null) { + b.setDate(new StringWithCustomTags(new SimpleDateFormat("d MMM yyyy", Locale.US).format(birth).toUpperCase( + Locale.US))); + } + if (birthPlace != null) { + Place place = new Place(); + place.setPlaceName(birthPlace); + b.setPlace(place); + } + result.getEvents(true).add(b); + } + + // Set death event + if (death != null || deathPlace != null) { + IndividualEvent b = new IndividualEvent(); + b.setType(IndividualEventType.DEATH); + if (death != null) { + b.setDate(new StringWithCustomTags(new SimpleDateFormat("d MMM yyyy", Locale.US).format(death).toUpperCase( + Locale.US))); + } + if (deathPlace != null) { + Place place = new Place(); + place.setPlaceName(deathPlace); + b.setPlace(place); + } + result.getEvents(true).add(b); + } + + return result; + } + +} diff --git a/src/main/java/org/gedcom4j/factory/Sex.java b/src/main/java/org/gedcom4j/factory/Sex.java new file mode 100644 index 00000000..9c28a39b --- /dev/null +++ b/src/main/java/org/gedcom4j/factory/Sex.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2009-2016 Matthew R. Harrah + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ +package org.gedcom4j.factory; + +/** + * Sex of an individual + * + * @author frizbog + */ +public enum Sex { + /** Male */ + MALE("M"), + /** Female */ + FEMALE("F"), + /** Unknown */ + UNKNOWN("U"); + + /** + * The code for the sex of the individual + */ + private final String code; + + /** + * Constructor + * + * @param code + * the code for the sex of the individual + */ + Sex(String code) { + this.code = code; + } + + /** + * Get the code + * + * @return the code + */ + public String getCode() { + return code; + } + +} diff --git a/src/main/java/org/gedcom4j/factory/package-info.java b/src/main/java/org/gedcom4j/factory/package-info.java new file mode 100644 index 00000000..daa79bcb --- /dev/null +++ b/src/main/java/org/gedcom4j/factory/package-info.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2009-2016 Matthew R. Harrah + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ +/** + * Classes to assist with creation of objects in the data model. + * + * @author frizbog + */ +package org.gedcom4j.factory; \ No newline at end of file diff --git a/src/test/java/org/gedcom4j/factory/IndividualFactoryTest.java b/src/test/java/org/gedcom4j/factory/IndividualFactoryTest.java new file mode 100644 index 00000000..a8ffd4f2 --- /dev/null +++ b/src/test/java/org/gedcom4j/factory/IndividualFactoryTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2009-2016 Matthew R. Harrah + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ +package org.gedcom4j.factory; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; + +import java.util.Calendar; +import java.util.Date; + +import org.gedcom4j.model.Gedcom; +import org.gedcom4j.model.Individual; +import org.gedcom4j.model.IndividualEventType; +import org.junit.Test; + +/** + * Test for {@link IndividualFactory} + * + * @author frizbog + */ +public class IndividualFactoryTest { + + /** + * Test for + * {@link IndividualFactory#create(org.gedcom4j.model.Gedcom, String, String, Sex, java.util.Date, String, java.util.Date, String)} + */ + @Test + public void testCreate() { + Gedcom g = new Gedcom(); + @SuppressWarnings("deprecation") + Individual i = new IndividualFactory().create(g, "Robert", "Tarantino", Sex.MALE, new Date(67, Calendar.MAY, 1), "Idaho", + new Date(99, Calendar.OCTOBER, 31), "Virginia"); + + assertNotNull(i); + assertNotNull(i.getXref()); + + Individual i2 = g.getIndividuals().get(i.getXref()); + assertNotNull(i2); + assertSame(i, i2); + + assertEquals("M", i.getSex().getValue()); + + assertEquals("1 MAY 1967", i.getEventsOfType(IndividualEventType.BIRTH).get(0).getDate().getValue()); + assertEquals("31 OCT 1999", i.getEventsOfType(IndividualEventType.DEATH).get(0).getDate().getValue()); + } + +}