Skip to content

Commit

Permalink
Finding and filling out a tiny bit of PersonPart
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Friis Østergaard committed May 25, 2011
1 parent df4e040 commit 68dc604
Show file tree
Hide file tree
Showing 24 changed files with 385 additions and 63 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Expand Up @@ -34,6 +34,7 @@ include 'lookup'
include 'oces-ssl'
include 'part-api'
include 'hibernate-common'
include 'hibernate-common-test'

rootProject.name = 'stamdata'
rootProject.children.each {project ->
Expand Down
36 changes: 36 additions & 0 deletions subprojects/hibernate-common-test/hibernate-common-test.gradle
@@ -0,0 +1,36 @@
// The contents of this file are subject to the Mozilla Public
// License Version 1.1 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of
// the License at http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS
// IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
// implied. See the License for the specific language governing
// rights and limitations under the License.
//
// Contributor(s): Contributors are attributed in the source code
// where applicable.
//
// The Original Code is "Stamdata".
//
// The Initial Developer of the Original Code is Trifork Public A/S.
//
// Portions created for the Original Code are Copyright 2011,
// Lægemiddelstyrelsen. All Rights Reserved.
//
// Portions created for the FMKi Project are Copyright 2011,
// National Board of e-Health (NSI). All Rights Reserved.

dependencies {

compile project(':shared')

// Guice

compile libs.guice_servlet

// Hibernate

compile libs.hibernate_entitymanager
compile libs.commons_lang
}
Expand Up @@ -5,7 +5,7 @@
import com.trifork.configuration.ConfigurationLoader;

public class ConfigurationHelper {
public static Configuration getConfiguration() {
return new ConfigurationLoader("default,unittest", "replication", "/").loadConfiguration();
public static Configuration getConfiguration(String module) {
return new ConfigurationLoader("default,unittest", module, "/").loadConfiguration();
}
}
Expand Up @@ -33,9 +33,9 @@ public class DatabaseHelper {

private SessionFactory sessionFactory;

public DatabaseHelper(Class<?>... entities) throws Exception {
public DatabaseHelper(String module, Class<?>... entities) throws Exception {

org.apache.commons.configuration.Configuration props = ConfigurationHelper.getConfiguration();
org.apache.commons.configuration.Configuration props = ConfigurationHelper.getConfiguration(module);

Configuration config = new Configuration();

Expand Down
Expand Up @@ -26,6 +26,7 @@
import static com.trifork.stamdata.util.DateUtils.yyyy_MM_dd;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -83,7 +84,7 @@ public void canEstablishData() throws Exception {
// LatestIkraft should be written to the db
// Actually, it should be written to db - discussed with Jan. So we do it now.
Calendar latestIkraft = CPRImporter.getLatestIkraft(con);
// assertNull(latestIkraft);
assertNotNull(latestIkraft);
con.close();
}

Expand Down
1 change: 1 addition & 0 deletions subprojects/lookup/lookup.gradle
Expand Up @@ -30,6 +30,7 @@ dependencies {
compile project(':oces-ssl')
compile project(':part-api')
compile project(':hibernate-common')
testCompile project(':hibernate-common-test')

// SERVLETS

Expand Down
Expand Up @@ -3,7 +3,6 @@
import static org.slf4j.LoggerFactory.getLogger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down
@@ -0,0 +1,23 @@
package com.trifork.stamdata.lookup.dao;

import java.util.Date;

import com.trifork.stamdata.views.cpr.Person;

public class CurrentPersonData {

private final Person person;

public CurrentPersonData(Person person) {
this.person = person;
}

public Date getValidFrom() {
return person.validFrom;
}

public String getCprNumber() {
return person.cpr;
}

}
@@ -0,0 +1,32 @@
package com.trifork.stamdata.lookup.dao;

import java.util.Date;

import javax.inject.Inject;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.trifork.stamdata.views.cpr.Person;

public class PersonDao {

private final Session session;

@Inject
public PersonDao(Session session) {
this.session = session;
}

public CurrentPersonData get(String cpr) {
Person person = (Person) session
.createCriteria(Person.class)
.add(Restrictions.eq("cpr", cpr))
.add(Restrictions.le("validFrom", new Date()))
.addOrder(Order.desc("validFrom"))
.setMaxResults(1)
.uniqueResult();
return new CurrentPersonData(person);
}
}
@@ -0,0 +1,42 @@
package com.trifork.stamdata.lookup.personpart;

import oio.sagdok.person._1_0.AttributListeType;
import oio.sagdok.person._1_0.CprBorgerType;
import oio.sagdok.person._1_0.PersonType;
import oio.sagdok.person._1_0.RegisterOplysningType;
import oio.sagdok.person._1_0.RegistreringType;

import com.trifork.stamdata.lookup.dao.CurrentPersonData;

public class PersonPartConverter {

public PersonType convert(CurrentPersonData person) {
PersonType result = new PersonType();
result.getRegistrering().add(createRegistreringType(person));
return result;
}

private RegistreringType createRegistreringType(CurrentPersonData person) {
RegistreringType result = new RegistreringType();
result.setAttributListe(createAttributListeType(person));
return result;
}

private AttributListeType createAttributListeType(CurrentPersonData person) {
AttributListeType result = new AttributListeType();
result.getRegisterOplysning().add(createRegisterOplysningType(person));
return result;
}

private RegisterOplysningType createRegisterOplysningType(CurrentPersonData person) {
RegisterOplysningType result = new RegisterOplysningType();
result.setCprBorger(createCprBorgerType(person));
return result;
}

private CprBorgerType createCprBorgerType(CurrentPersonData person) {
CprBorgerType result = new CprBorgerType();
result.setPersonCivilRegistrationIdentifier(person.getCprNumber());
return result;
}
}

This file was deleted.

@@ -0,0 +1,34 @@
package com.trifork.stamdata.lookup.rest;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.xml.bind.JAXBElement;

import oio.sagdok.person._1_0.PersonType;

import com.trifork.stamdata.lookup.dao.CurrentPersonData;
import com.trifork.stamdata.lookup.dao.PersonDao;
import com.trifork.stamdata.lookup.personpart.PersonPartConverter;

@Path("person/{cpr}")
public class PersonResource {
private final PersonDao personDao;
private final PersonPartConverter personPartConverter;

@Inject
public PersonResource(PersonDao personDao, PersonPartConverter personPartConverter) {
this.personDao = personDao;
this.personPartConverter = personPartConverter;
}

@GET
@Produces("text/xml")
public JAXBElement<PersonType> getPerson(@PathParam("cpr") String cpr) {
CurrentPersonData person = personDao.get(cpr);
PersonType personPart = personPartConverter.convert(person);
return new oio.sagdok.person._1_0.ObjectFactory().createPerson(personPart);
}
}
Expand Up @@ -7,7 +7,7 @@ public class RestModule extends JerseyServletModule {

@Override
protected void configureServlets() {
bind(HelloWorldResource.class);
bind(PersonResource.class);

serve("/*").with(GuiceContainer.class);

Expand Down
@@ -0,0 +1,92 @@
package com.trifork.stamdata.lookup.dao;

import static org.junit.Assert.*;

import java.util.Calendar;
import java.util.Date;

import org.hibernate.classic.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.trifork.stamdata.replication.DatabaseHelper;
import com.trifork.stamdata.views.cpr.Person;

public class PersonDaoTest {
private static DatabaseHelper db;
private Session session;
private PersonDao dao;

@BeforeClass
public static void beforeClass() throws Exception {
db = new DatabaseHelper("lookup", Person.class);
Session session = db.openSession();
session.createQuery("delete from Person").executeUpdate();
session.close();
}

@Before
public void before() {
session = db.openSession();
session.getTransaction().begin();
dao = new PersonDao(session);
}

@After
public void after() {
session.getTransaction().rollback();
session.close();
}

@Test
public void givesCurrentPersonDataWithContentsFromUnderlyingView() {
session.save(person(at(2005, Calendar.JANUARY, 5)));

CurrentPersonData person = dao.get("1020304050");
assertEquals("1020304050", person.getCprNumber());
assertEquals(at(2005, Calendar.JANUARY, 5), person.getValidFrom());
}

@Test
public void getsNewestPersonRecordIfNoRecordIsInTheFuture() {
session.save(person(at(2005, Calendar.JANUARY, 5)));
session.save(person(at(2010, Calendar.FEBRUARY, 10)));

CurrentPersonData person = dao.get("1020304050");
assertEquals(at(2010, Calendar.FEBRUARY, 10), person.getValidFrom());
}

@Test
public void getsCurrentPersonRecordIfRecordsInTheFuture() {
session.save(person(at(2005, Calendar.JANUARY, 5)));
session.save(person(at(2010, Calendar.FEBRUARY, 10)));
session.save(person(at(2110, Calendar.FEBRUARY, 10)));

CurrentPersonData person = dao.get("1020304050");
assertEquals(at(2010, Calendar.FEBRUARY, 10), person.getValidFrom());
}

private Person person(Date validFrom) {
Person result = new Person();
result.cpr = "1020304050";
result.koen = "M";
result.foedselsdato = at(1975, Calendar.MAY, 12);
result.modifiedBy = "AHJ";
result.createdBy = "AHJ";
result.validFrom = validFrom;
result.modifiedDate = validFrom;
result.createdDate = at(2005, Calendar.JANUARY, 5);
return result;
}

private Date at(int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
return calendar.getTime();
}
}
@@ -0,0 +1,30 @@
package com.trifork.stamdata.lookup.personpart;

import static org.junit.Assert.*;
import oio.sagdok.person._1_0.PersonType;

import org.junit.Before;
import org.junit.Test;

import com.trifork.stamdata.lookup.dao.CurrentPersonData;
import com.trifork.stamdata.views.cpr.Person;


public class PersonPartConverterTest {
private PersonPartConverter converter;

@Before
public void before() {
converter = new PersonPartConverter();
}

@Test
public void fillsOutCprNumber() {
Person person = new Person();
person.cpr = "1020304050";
CurrentPersonData currentPerson = new CurrentPersonData(person);

PersonType personType = converter.convert(currentPerson);
assertEquals("1020304050", personType.getRegistrering().get(0).getAttributListe().getRegisterOplysning().get(0).getCprBorger().getPersonCivilRegistrationIdentifier());
}
}

0 comments on commit 68dc604

Please sign in to comment.