Project

General

Profile

Revision 6826dbcd

ID6826dbcdac1dc0674fdd1972c277361baaa6302e
Parent 83c98dbc
Child 0c3b20de

Added by Katja Luther about 3 years ago

change return type of changeSynonymToAcceptedTaxon to updateResult

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ITaxonService.java
116 116
     *          in the homotypic group. It is up to the implementing class to
117 117
     *          handle this situation via an exception or in another way.
118 118
     */
119
    public Taxon changeSynonymToAcceptedTaxon(Synonym synonym, Taxon acceptedTaxon, boolean deleteSynonym) throws HomotypicalGroupChangeException;
119
    public UpdateResult changeSynonymToAcceptedTaxon(Synonym synonym, Taxon acceptedTaxon, boolean deleteSynonym) throws HomotypicalGroupChangeException;
120 120

  
121 121
    /**
122 122
     * @param synonymUuid
......
950 950
            TaxonRelationshipType oldRelationshipType, SynonymType synonymType) throws DataChangeNoRollbackException;
951 951

  
952 952

  
953

  
953 954
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TaxonServiceImpl.java
223 223

  
224 224
    @Override
225 225
    @Transactional(readOnly = false)
226
    public Taxon changeSynonymToAcceptedTaxon(Synonym synonym, Taxon acceptedTaxon, boolean deleteSynonym) throws HomotypicalGroupChangeException{
227

  
226
    public UpdateResult changeSynonymToAcceptedTaxon(Synonym synonym, Taxon acceptedTaxon, boolean deleteSynonym) {
227
        UpdateResult result = new UpdateResult();
228 228
        TaxonNameBase<?,?> acceptedName = acceptedTaxon.getName();
229 229
        TaxonNameBase<?,?> synonymName = synonym.getName();
230 230
        HomotypicalGroup synonymHomotypicGroup = synonymName.getHomotypicalGroup();
......
232 232
        //check synonym is not homotypic
233 233
        if (acceptedName.getHomotypicalGroup().equals(synonymHomotypicGroup)){
234 234
            String message = "The accepted taxon and the synonym are part of the same homotypical group and therefore can not be both accepted.";
235
            throw new HomotypicalGroupChangeException(message);
235
            result.addException(new HomotypicalGroupChangeException(message));
236
            result.setAbort();
237
            return result;
236 238
        }
237 239

  
238 240
        Taxon newAcceptedTaxon = Taxon.NewInstance(synonymName, acceptedTaxon.getSec());
239 241
        dao.save(newAcceptedTaxon);
242
        result.setCdmEntity(newAcceptedTaxon);
240 243
        SynonymType relTypeForGroup = SynonymType.HOMOTYPIC_SYNONYM_OF();
241 244
        List<Synonym> heteroSynonyms = acceptedTaxon.getSynonymsInGroup(synonymHomotypicGroup);
242 245

  
......
249 252
            }
250 253
        }
251 254
        dao.saveOrUpdate(acceptedTaxon);
252

  
255
        result.addUpdatedObject(acceptedTaxon);
253 256
        if (deleteSynonym){
254 257

  
255 258
            try {
......
259 262
                this.deleteSynonym(synonym, config);
260 263

  
261 264
            } catch (Exception e) {
262
                logger.info("Can't delete old synonym from database");
265
                result.addException(e);
263 266
            }
264 267
        }
265 268

  
266
        return newAcceptedTaxon;
269
        return result;
267 270
    }
268 271

  
269 272
    @Override
......
271 274
    public UpdateResult changeSynonymToAcceptedTaxon(UUID synonymUuid,
272 275
            UUID acceptedTaxonUuid,
273 276
            UUID newParentNodeUuid,
274
            boolean deleteSynonym) throws HomotypicalGroupChangeException {
277
            boolean deleteSynonym)  {
275 278
        UpdateResult result = new UpdateResult();
276 279
        Synonym synonym = CdmBase.deproxy(dao.load(synonymUuid), Synonym.class);
277 280
        Taxon acceptedTaxon = CdmBase.deproxy(dao.load(acceptedTaxonUuid), Taxon.class);
278
        Taxon taxon =  changeSynonymToAcceptedTaxon(synonym, acceptedTaxon, deleteSynonym);
281
        result =  changeSynonymToAcceptedTaxon(synonym, acceptedTaxon, deleteSynonym);
282
        Taxon newTaxon = (Taxon)result.getCdmEntity();
279 283
        TaxonNode newParentNode = taxonNodeDao.load(newParentNodeUuid);
280
        TaxonNode newNode = newParentNode.addChildTaxon(taxon, null, null);
284
        TaxonNode newNode = newParentNode.addChildTaxon(newTaxon, null, null);
281 285
        taxonNodeDao.save(newNode);
282
        result.addUpdatedObject(taxon);
286
        result.addUpdatedObject(newTaxon);
283 287
        result.addUpdatedObject(acceptedTaxon);
284 288
        result.setCdmEntity(newNode);
285 289
        return result;
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TaxonServiceImplBusinessTest.java
116 116
		boolean copyCitationInfo = true;
117 117
		Taxon taxon = null;
118 118
		try {
119
			taxon = service.changeSynonymToAcceptedTaxon(s1, t1, deleteSynonym);
119
			taxon = (Taxon)service.changeSynonymToAcceptedTaxon(s1, t1, deleteSynonym).getCdmEntity();
120 120
			Assert.fail("Change must fail for synonym and taxon in same homotypical group");
121 121
		} catch (HomotypicalGroupChangeException e) {
122 122
			//OK
......
125 125
		Assert.assertEquals("Homotypical group of old accepted taxon should still contain exactly 2 names", 2, oldGroup.getTypifiedNames().size());
126 126
		Assert.assertTrue("Old accepted taxon should now have 2 synonyms", t1.getSynonyms().size() == 2);
127 127
		try {
128
			taxon = service.changeSynonymToAcceptedTaxon(s2, t1, deleteSynonym);
128
			taxon = (Taxon)service.changeSynonymToAcceptedTaxon(s2, t1, deleteSynonym).getCdmEntity();
129 129
		} catch (HomotypicalGroupChangeException e) {
130 130
			Assert.fail("Change must not throw exception for heterotypic synonym change");
131 131
		}
......
159 159
		//run
160 160
		Taxon newTaxon = null;
161 161
		try {
162
			newTaxon = service.changeSynonymToAcceptedTaxon(s1, t1, false);
162
			newTaxon = (Taxon)service.changeSynonymToAcceptedTaxon(s1, t1, false).getCdmEntity();
163 163
		} catch (HomotypicalGroupChangeException e1) {
164 164
			Assert.fail("Invocation of change method should not throw an exception");
165 165
		}
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TaxonServiceImplTest.java
36 36
import eu.etaxonomy.cdm.api.service.dto.IncludedTaxaDTO;
37 37
import eu.etaxonomy.cdm.api.service.exception.HomotypicalGroupChangeException;
38 38
import eu.etaxonomy.cdm.model.agent.Person;
39
import eu.etaxonomy.cdm.model.common.Annotation;
39 40
import eu.etaxonomy.cdm.model.common.CdmBase;
40 41
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
41 42
import eu.etaxonomy.cdm.model.common.Language;
......
230 231

  
231 232

  
232 233
        Taxon taxon = null;
234
        UpdateResult result = new UpdateResult();
233 235
        try {
234
            taxon = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true);
236
            result = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true);
235 237
        } catch (HomotypicalGroupChangeException e) {
236 238
            Assert.fail("Invocation of change method should not throw an exception");
237 239
        }
......
239 241
        //test flush (resave deleted object)
240 242
        TaxonBase<?> syn = service.find(uuidSyn);
241 243
        taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
242
        Taxon taxNew = (Taxon)service.find(taxon.getUuid());
244
        Taxon taxNew = (Taxon)service.find(result.getCdmEntity().getUuid());
243 245
        assertNull(syn);
244 246
        assertNotNull(taxWithSyn);
245 247
        assertNotNull(taxNew);
246 248

  
247
        Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, taxon.getSynonyms().size());
249
        Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, ((Taxon)result.getCdmEntity()).getSynonyms().size());
248 250
    }
249 251

  
250 252

  
......
260 262

  
261 263

  
262 264
        Taxon taxon = null;
265
        UpdateResult result = new UpdateResult();
263 266
        try {
264
            taxon = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true);
267
            result = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true);
265 268
            service.save(taxon);
266 269
        } catch (HomotypicalGroupChangeException e) {
267 270
            Assert.fail("Invocation of change method should not throw an exception");
......
272 275
        //test flush (resave deleted object)
273 276
        TaxonBase<?> syn = service.find(uuidSyn);
274 277
        taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
275
        Taxon taxNew = (Taxon)service.find(taxon.getUuid());
278
        Taxon taxNew = (Taxon)service.find(((Taxon)result.getCdmEntity()).getUuid());
276 279
        assertNull(syn);
277 280
        assertNotNull(taxWithSyn);
278 281
        assertNotNull(taxNew);
......
757 760
    }
758 761

  
759 762
    @Test
763
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
764

  
765
    public final void testDeleteSynonymWithAnnotations(){
766
        final String[]tableNames = {
767
//                "TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
768
//                "HomotypicalGroup","HomotypicalGroup_AUD"
769
        };
770

  
771

  
772
        UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
773
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
774

  
775
        Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
776
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
777
        taxon2.addSynonym(synonym1, SynonymType.HETEROTYPIC_SYNONYM_OF());
778

  
779
        Annotation annotation = Annotation.NewDefaultLanguageInstance("test");
780
        synonym1.addAnnotation(annotation);
781
        service.saveOrUpdate(synonym1);
782

  
783
        DeleteResult result = service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
784
        if (result.isError()){
785
            Assert.fail();
786
        }
787
        this.commitAndStartNewTransaction(tableNames);
788

  
789

  
790
    }
791

  
792

  
793
    @Test
760 794
    @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
761 795

  
762 796
    public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedName(){
......
1203 1237

  
1204 1238
    @Test
1205 1239
    @DataSet(value="BlankDataSet.xml")
1240
    public final void testDeleteTaxonWithAnnotations(){
1241

  
1242
        //create a small classification
1243

  
1244
        Taxon testTaxon = getTestTaxon();
1245

  
1246
        service.save(testTaxon).getUuid();
1247

  
1248
        Taxon speciesTaxon = (Taxon)service.find(SPECIES1_UUID);
1249
        Iterator<TaxonDescription> descriptionIterator = speciesTaxon.getDescriptions().iterator();
1250
        UUID descrUUID = null;
1251
        UUID descrElementUUID = null;
1252
        if (descriptionIterator.hasNext()){
1253
            TaxonDescription descr = descriptionIterator.next();
1254
            descrUUID = descr.getUuid();
1255
            descrElementUUID = descr.getElements().iterator().next().getUuid();
1256
        }
1257
        BotanicalName taxonName = (BotanicalName) nameService.find(SPECIES1_NAME_UUID);
1258
        assertNotNull(taxonName);
1259

  
1260
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1261
        config.setDeleteNameIfPossible(false);
1262
        Annotation annotation = Annotation.NewDefaultLanguageInstance("test");
1263
        speciesTaxon.addAnnotation(annotation);
1264

  
1265

  
1266
       // try {
1267

  
1268
        DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1269
        if (!result.isOk()){
1270
            Assert.fail();
1271
        }
1272
        commitAndStartNewTransaction(null);
1273

  
1274
        taxonName = (BotanicalName) nameService.find(SPECIES1_NAME_UUID);
1275
        Taxon taxon = (Taxon)service.find(SPECIES1_UUID);
1276

  
1277
        //descriptionService.find(descrUUID);
1278
        assertNull(descriptionService.find(descrUUID));
1279
        assertNull(descriptionService.getDescriptionElementByUuid(descrElementUUID));
1280
        //assertNull(synName);
1281
        assertNotNull(taxonName);
1282
        assertNull(taxon);
1283
        config.setDeleteNameIfPossible(true);
1284
        Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
1285
        service.save(newTaxon);
1286
        result = service.deleteTaxon(newTaxon.getUuid()
1287
                , config, null);
1288
        if (!result.isOk()){
1289
            Assert.fail();
1290
        }
1291

  
1292

  
1293
    }
1294

  
1295
    @Test
1296
    @DataSet(value="BlankDataSet.xml")
1206 1297
    public final void testDeleteTaxonUsedInTaxonRelation(){
1207 1298

  
1208 1299
        //create a small classification

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)