Search in sources :

Example 6 with MergeException

use of eu.etaxonomy.cdm.strategy.merge.MergeException in project cdmlib by cybertaxonomy.

the class AgentServiceImplTest method testConvertTeam2Person.

@Test
@DataSets({ @DataSet(loadStrategy = CleanSweepInsertLoadStrategy.class, value = "/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"), @DataSet(value = "/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml") })
public void testConvertTeam2Person() {
    String fullAuthor = "Original author";
    String nomTitle = "Abrev. aut.";
    Team team = Team.NewTitledInstance(fullAuthor, nomTitle);
    Annotation annotation = Annotation.NewDefaultLanguageInstance("Meine annotation");
    team.addAnnotation(annotation);
    team.setContact(getContact());
    TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
    name.setCombinationAuthorship(team);
    service.save(team);
    nameSerivce.save(name);
    UpdateResult result = null;
    Person person = null;
    try {
        result = service.convertTeam2Person(team);
        person = (Person) result.getCdmEntity();
    } catch (IllegalArgumentException e) {
        Assert.fail("No IllegalArgumentException should be thrown");
    } catch (MergeException e) {
        Assert.fail("No Merge exception should be thrown");
    }
    Assert.assertNotNull(person);
    Assert.assertEquals("Title cache must be equal", fullAuthor, person.getTitleCache());
    Assert.assertEquals("Nom. title must be equal", nomTitle, person.getNomenclaturalTitleCache());
    Assert.assertEquals("Annotations should be moved", 1, person.getAnnotations().size());
    Assert.assertNotNull("Contact must be copied too", person.getContact());
    Assert.assertEquals("person must be combination author now", person, name.getCombinationAuthorship());
}
Also used : MergeException(eu.etaxonomy.cdm.strategy.merge.MergeException) Team(eu.etaxonomy.cdm.model.agent.Team) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) Person(eu.etaxonomy.cdm.model.agent.Person) Annotation(eu.etaxonomy.cdm.model.common.Annotation) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test) DataSets(org.unitils.dbunit.annotation.DataSets)

Example 7 with MergeException

use of eu.etaxonomy.cdm.strategy.merge.MergeException in project cdmlib by cybertaxonomy.

the class AgentServiceImplTest method testConvertTeam2PersonWithMember.

@Test
@DataSets({ @DataSet(loadStrategy = CleanSweepInsertLoadStrategy.class, value = "/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"), @DataSet(value = "/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml") })
public void testConvertTeam2PersonWithMember() {
    String fullAuthor = "Original author";
    String nomTitle = "Abrev. aut.";
    Team team = Team.NewTitledInstance(fullAuthor, nomTitle);
    Annotation annotation = Annotation.NewDefaultLanguageInstance("Meine annotation");
    team.addAnnotation(annotation);
    Annotation annotation2 = Annotation.NewDefaultLanguageInstance("Meine annotation2");
    team.addAnnotation(annotation2);
    team.setContact(getContact());
    TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
    name.setCombinationAuthorship(team);
    Person member = Person.NewTitledInstance("Member person");
    member.setNomenclaturalTitle("Memb. pers.");
    Annotation annotation3 = Annotation.NewDefaultLanguageInstance("Meine annotation3");
    member.addAnnotation(annotation3);
    team.addTeamMember(member);
    service.save(team);
    nameSerivce.save(name);
    Person person = null;
    UpdateResult result = null;
    try {
        result = service.convertTeam2Person(team);
        person = (Person) result.getCdmEntity();
    } catch (IllegalArgumentException e) {
        Assert.fail("No IllegalArgumentException should be thrown");
    } catch (MergeException e) {
        Assert.fail("No Merge exception should be thrown");
    }
    Assert.assertNotNull(person);
    Assert.assertEquals("Convert result and 'member' must be equal'", member, person);
    Assert.assertEquals("Title cache must be equal", "Member person", person.getTitleCache());
    Assert.assertEquals("Nom. title must be equal", "Memb. pers.", person.getNomenclaturalTitleCache());
    // FIXME should annotations be taken only from member ??
    // Assert.assertEquals("Annotations should be moved", 1, person.getAnnotations().size());
    String annotationText = person.getAnnotations().iterator().next().getText();
    // Assert.assertEquals("The only annotation should be annotation 3", annotation3.getText(), annotationText);
    // FIXME currently merge mode is still MERGE for user defined fields
    // Assert.assertNull("Contact must not be copied", person.getContact());
    Assert.assertEquals("person must be combination author now", person, name.getCombinationAuthorship());
}
Also used : MergeException(eu.etaxonomy.cdm.strategy.merge.MergeException) Team(eu.etaxonomy.cdm.model.agent.Team) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) Person(eu.etaxonomy.cdm.model.agent.Person) Annotation(eu.etaxonomy.cdm.model.common.Annotation) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test) DataSets(org.unitils.dbunit.annotation.DataSets)

Example 8 with MergeException

use of eu.etaxonomy.cdm.strategy.merge.MergeException in project cdmlib by cybertaxonomy.

the class CdmGenericDaoImplTest method testReallocatePersonTeam.

@Test
public void testReallocatePersonTeam() throws MergeException {
    TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
    name1.setTitleCache("BotanicalName1", true);
    IBook book1 = ReferenceFactory.newBook();
    Team team1 = Team.NewInstance();
    Team team2 = Team.NewInstance();
    team1.setTitleCache("team1", true);
    team2.setTitleCache("team2", true);
    Person person1 = Person.NewTitledInstance("person1");
    Person person2 = Person.NewTitledInstance("person2");
    team1.setNomenclaturalTitleCache("T.1", true);
    String street1 = "Strasse1";
    person1.setContact(Contact.NewInstance(street1, "12345", "Berlin", Country.ARGENTINAARGENTINEREPUBLIC(), "pobox", "Region", "a@b.de", "f12345", "+49-30-123456", URI.create("www.abc.de"), Point.NewInstance(2.4, 3.2, ReferenceSystem.WGS84(), 3)));
    team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
    String street3 = "Street3";
    team2.addAddress(street3, null, null, null, null, null, Point.NewInstance(1.1, 2.2, null, 4));
    String emailAddress1 = "Email1";
    team1.addEmailAddress(emailAddress1);
    // FIXME
    // team2.addTeamMember(person1);
    team2.addTeamMember(person2);
    String emailAddress2 = "Email2";
    team2.addEmailAddress(emailAddress2);
    Credit credit1 = Credit.NewInstance(team2, "credit1");
    book1.addCredit(credit1);
    agentDao.save(team1);
    agentDao.save(team2);
    agentDao.save(person1);
    agentDao.save(person2);
    cdmGenericDao.save((Reference) book1);
    // starting condition
    name1.setCombinationAuthorship(person1);
    Assert.assertEquals("Name1 should have person1 as combination author", person1, name1.getCombinationAuthorship());
    DefaultMergeStrategy strategy = DefaultMergeStrategy.NewInstance(TeamOrPersonBase.class);
    // strategy.setOnlyReallocateLinks(true);
    FieldUnit fieldUnit1 = FieldUnit.NewInstance();
    fieldUnit1.setPrimaryCollector(person1);
    cdmGenericDao.save(fieldUnit1);
    try {
        cdmGenericDao.merge(team2, person1, strategy);
        Assert.fail("We expect exception because fieldunit.primaryCollector is of type person");
    } catch (MergeException e) {
        if (!e.getMessage().contains("Object can not be merged into new object as it is referenced in a way that does not allow merging")) {
            Assert.fail("The exception should be the one thrown by DeduplicationHelper.reallocateByHolder(...)");
        }
        // clean up for next test
        fieldUnit1.setPrimaryCollector(null);
    } catch (Exception e) {
        Assert.fail("Unhandled exception during merge");
    }
    Assert.assertEquals("Name1 should still have person1 as combination author", person1, name1.getCombinationAuthorship());
    // test collections
    team1.addTeamMember(person1);
    try {
        cdmGenericDao.merge(team2, person1, strategy);
        Assert.fail("We expect exception because fieldunit.primaryCollector is of type person");
    } catch (MergeException e) {
        if (!e.getMessage().contains("Object can not be merged into new object as it is referenced in a way that does not allow merging")) {
            Assert.fail("The exception should be the one thrown by DeduplicationHelper.reallocateByHolder(...)");
        }
        // clean up for next test
        team1.removeTeamMember(person1);
    } catch (Exception e) {
        Assert.fail("Unhandled exception during merge");
    }
    Assert.assertEquals("Name1 should still have person1 as combination author", person1, name1.getCombinationAuthorship());
    // test successful merge
    cdmGenericDao.save(name1);
    cdmGenericDao.merge(team2, person1, strategy);
    Assert.assertEquals("Name1 should have team2 as combination author now", team2, name1.getCombinationAuthorship());
}
Also used : DefaultMergeStrategy(eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy) FieldUnit(eu.etaxonomy.cdm.model.occurrence.FieldUnit) Credit(eu.etaxonomy.cdm.model.common.Credit) MergeException(eu.etaxonomy.cdm.strategy.merge.MergeException) IBook(eu.etaxonomy.cdm.model.reference.IBook) CommonTaxonName(eu.etaxonomy.cdm.model.description.CommonTaxonName) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) Team(eu.etaxonomy.cdm.model.agent.Team) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) Person(eu.etaxonomy.cdm.model.agent.Person) FileNotFoundException(java.io.FileNotFoundException) MergeException(eu.etaxonomy.cdm.strategy.merge.MergeException) MatchException(eu.etaxonomy.cdm.strategy.match.MatchException) Test(org.junit.Test) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)

Example 9 with MergeException

use of eu.etaxonomy.cdm.strategy.merge.MergeException in project cdmlib by cybertaxonomy.

the class CdmGenericDaoImplTest method testDelete.

// ***************** TESTS **************************************************
@Test
@DataSets({ @DataSet(loadStrategy = CleanSweepInsertLoadStrategy.class, value = "/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"), @DataSet("/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml") })
public void testDelete() {
    Reference ref1 = ReferenceFactory.newBook();
    Reference ref2 = ReferenceFactory.newBook();
    Annotation annotation = Annotation.NewInstance("Anno1", null);
    ref1.addAnnotation(annotation);
    cdmGenericDao.saveOrUpdate(ref1);
    cdmGenericDao.saveOrUpdate(ref2);
    List<Reference> list = cdmGenericDao.list(Reference.class, 10, 0, null, null);
    try {
        cdmGenericDao.merge(ref2, ref1, null);
    } catch (MergeException e) {
        Assert.fail();
    }
    commitAndStartNewTransaction(null);
    list = cdmGenericDao.list(Reference.class, 10, 0, null, null);
    Assert.assertEquals(1, list.size());
}
Also used : MergeException(eu.etaxonomy.cdm.strategy.merge.MergeException) Reference(eu.etaxonomy.cdm.model.reference.Reference) IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) Annotation(eu.etaxonomy.cdm.model.common.Annotation) Test(org.junit.Test) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) DataSets(org.unitils.dbunit.annotation.DataSets)

Example 10 with MergeException

use of eu.etaxonomy.cdm.strategy.merge.MergeException in project cdmlib by cybertaxonomy.

the class Contact method merge.

// ************************ MERGE /MATCH ***************************/
public void merge(Contact contact2) throws MergeException {
    if (contact2 != null) {
        mergeList(this.getEmailAddresses(), contact2.getEmailAddresses());
        mergeList(this.getFaxNumbers(), contact2.getFaxNumbers());
        mergeList(this.getPhoneNumbers(), contact2.getPhoneNumbers());
        mergeList(this.getUrls(), contact2.getUrls());
        for (Address address : contact2.getAddresses()) {
            try {
                if (this.addresses == null) {
                    this.addresses = new HashSet<>();
                }
                this.addresses.add(address.clone());
            } catch (CloneNotSupportedException e) {
                throw new MergeException("Address must implement Cloneable");
            }
        }
    }
}
Also used : MergeException(eu.etaxonomy.cdm.strategy.merge.MergeException)

Aggregations

MergeException (eu.etaxonomy.cdm.strategy.merge.MergeException)14 Team (eu.etaxonomy.cdm.model.agent.Team)5 TaxonName (eu.etaxonomy.cdm.model.name.TaxonName)5 CdmTransactionalIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)5 Test (org.junit.Test)5 Person (eu.etaxonomy.cdm.model.agent.Person)4 Annotation (eu.etaxonomy.cdm.model.common.Annotation)4 HashSet (java.util.HashSet)4 DataSets (org.unitils.dbunit.annotation.DataSets)4 ICdmBase (eu.etaxonomy.cdm.model.common.ICdmBase)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 UnhandledException (org.apache.commons.lang.UnhandledException)3 MappingException (org.hibernate.MappingException)3 IntextReference (eu.etaxonomy.cdm.model.common.IntextReference)2 MatchException (eu.etaxonomy.cdm.strategy.match.MatchException)2 ConvertMergeStrategy (eu.etaxonomy.cdm.strategy.merge.ConvertMergeStrategy)2 Field (java.lang.reflect.Field)2 CollectionMetadata (org.hibernate.metadata.CollectionMetadata)2 AnyType (org.hibernate.type.AnyType)2 CollectionType (org.hibernate.type.CollectionType)2