--- /dev/null
+/**
+* Copyright (C) 2021 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.api.service.description;
+
+/**
+ * @author a.mueller
+ * @since 07.10.2021
+ */
+public class AggregationException extends RuntimeException {
+
+ private static final long serialVersionUID = -883213782380432255L;
+
+ public AggregationException() {
+ super();
+ }
+
+ public AggregationException(String message, Throwable cause, boolean enableSuppression,
+ boolean writableStackTrace) {
+ super(message, cause, enableSuppression, writableStackTrace);
+ }
+
+ public AggregationException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public AggregationException(String message) {
+ super(message);
+ }
+
+ public AggregationException(Throwable cause) {
+ super(cause);
+ }
+
+}
try {
preAggregate(subMonitor);
} catch (Exception e) {
- result.addException(new RuntimeException("Unhandled error during pre-aggregation", e));
- result.setError();
- done();
- return result;
+ return handleException(e, "Unhandled error during pre-aggregation");
+ }
+
+ try {
+ verifyConfiguration(subMonitor);
+ } catch (Exception e) {
+ return handleException(e, "Unhandled error during configuration check");
}
subMonitor.worked(preAccumulateTicks);
try {
aggregate(taxonNodeIdList, aggregateMonitor);
} catch (Exception e) {
- result.addException(new RuntimeException("Unhandled error during aggregation: " + e.getMessage() , e));
- e.printStackTrace();
- result.setError();
- done();
- return result;
+ return handleException(e, "Unhandled error during aggregation");
}
double end = System.currentTimeMillis();
result.addException(new RuntimeException("Unhandled error during doInvoke", e));
return result;
}
+ }
+ private UpdateResult handleException(Exception e, String unhandledMessage) {
+ Exception ex;
+ if (e instanceof AggregationException){
+ ex = e;
+ }else{
+ ex = new RuntimeException(unhandledMessage + ": " + e.getMessage() , e);
+ e.printStackTrace();
+ }
+ result.addException(ex);
+ result.setError();
+ done();
+ return result;
}
protected void aggregate(List<Integer> taxonNodeIdList, IProgressMonitor subMonitor) throws JvmLimitsException {
protected abstract void preAggregate(IProgressMonitor monitor);
+ protected abstract void verifyConfiguration(IProgressMonitor monitor);
+
/**
* hook for initializing object when a new transaction starts
*/
// ******************* CONSTRUCTOR *********************************/
public DistributionAggregation() {}
+
@Override
protected String pluralDataType(){
return "distributions";
logger.info("Time elapsed for making super areas : " + (end2 - end1) / (1000) + "s");
}
+ @Override
+ protected void verifyConfiguration(IProgressMonitor monitor){
+ if (!AggregationSourceMode.list(AggregationMode.ToParent, AggregationType.Distribution)
+ .contains(getConfig().getToParentSourceMode())){
+ throw new AggregationException("Unsupported source mode for to-parent aggregation: " + getConfig().getToParentSourceMode());
+ }
+ if (!AggregationSourceMode.list(AggregationMode.WithinTaxon, AggregationType.Distribution)
+ .contains(getConfig().getWithinTaxonSourceMode())){
+ throw new AggregationException("Unsupported source mode for within-taxon aggregation: " + getConfig().getToParentSourceMode());
+ }
+ }
+
@Override
protected void initTransaction() {
}
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
import eu.etaxonomy.cdm.model.description.TaxonDescription;
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.reference.ICdmTarget;
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
import eu.etaxonomy.cdm.model.taxon.Taxon;
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
logger.info("Time elapsed for pre-accumulate() : " + (end1 - start) / (1000) + "s");
}
+ @Override
+ protected void verifyConfiguration(IProgressMonitor monitor){
+ if (!AggregationSourceMode.list(AggregationMode.ToParent, AggregationType.StructuredDescription)
+ .contains(getConfig().getToParentSourceMode())){
+ throw new AggregationException("Unsupported source mode for to-parent aggregation: " + getConfig().getToParentSourceMode());
+ }
+ if (!AggregationSourceMode.list(AggregationMode.WithinTaxon, AggregationType.StructuredDescription)
+ .contains(getConfig().getWithinTaxonSourceMode())){
+ throw new AggregationException("Unsupported source mode for within-taxon aggregation: " + getConfig().getWithinTaxonSourceMode());
+ }
+ }
private boolean hasCharacterData(DescriptionElementBase element) {
return hasCategoricalData(element) || hasQuantitativeData(element);
//remove remaining sources-to-be-removed
for (IdentifiableSource sourceToRemove : sourcesToRemove) {
targetDescription.removeSource(sourceToRemove);
- if (sourceToRemove.getCdmSource() != null){
- @SuppressWarnings("unchecked")
- T descriptionToDelete = ((T)sourceToRemove.getCdmSource());
- ((IDescribable<T>)descriptionToDelete.describedEntity()).removeDescription(descriptionToDelete);
- structuredResultHolder.descriptionsToDelete.add(descriptionToDelete);
+ ICdmTarget target = sourceToRemove.getCdmSource();
+ if (target != null){
+ if (target.isInstanceOf(DescriptionBase.class)){
+ @SuppressWarnings("unchecked")
+ T descriptionToDelete = ((T)sourceToRemove.getCdmSource());
+ ((IDescribable<T>)descriptionToDelete.describedEntity()).removeDescription(descriptionToDelete);
+ structuredResultHolder.descriptionsToDelete.add(descriptionToDelete);
+ }else if (target.isInstanceOf(Taxon.class)){
+ //nothing to do for now
+ } else {
+ throw new AggregationException("CdmLink target type not yet supported: " + target.getClass().getSimpleName());
+ }
}
}
}
}else if (newTarget instanceof Taxon){
//nothing to do for now (we do not support reuse of sources linking to different taxa yet)
}else{
- throw new IllegalStateException("Sources not linking to a description or a taxon instance currently not yet supported.");
+ throw new AggregationException("Sources not linking to a description or a taxon instance currently not yet supported.");
}
}else{
- throw new IllegalStateException("Sources not linking to another CdmBase instance currently not yet supported.");
+ throw new AggregationException("Sources not linking to another CdmBase instance currently not yet supported.");
}
}
}else if (newTarget instanceof Taxon){
return newTarget.equals(existingTarget);
}else{
- throw new IllegalStateException("Other classes then SpecimenDescription and TaxonDescription are not yet supported. But was: " + newTarget.getClass());
+ throw new AggregationException("Other classes then SpecimenDescription and TaxonDescription are not yet supported. But was: " + newTarget.getClass());
}
}
}
}else if (targetElement.isInstanceOf(QuantitativeData.class)){
mergeDescriptionElement((QuantitativeData)targetElement, (QuantitativeData)newElement);
}else{
- throw new IllegalArgumentException("Class not supported: " + targetElement.getClass().getName());
+ throw new AggregationException("Class not supported: " + targetElement.getClass().getName());
}
}
Taxon taxon = ((TaxonDescription) desc).getTaxon();
source.setCdmSource(taxon);
}else {
- throw new IllegalStateException("Description type not yet supported for aggregation source mode TAXON: " + desc.getClass().getSimpleName() );
+ throw new AggregationException("Description type not yet supported for aggregation source mode TAXON: " + desc.getClass().getSimpleName() );
}
break;
- case ALL: //not yet supported
- case ALL_SAMEVALUE: //makes no sense
case NONE:
source = null;
break;
+ case ALL: //not yet supported
+ throw new AggregationException("Source mode not yet supported: " + sourceMode);
+ case ALL_SAMEVALUE: //makes no sense
+ throw new AggregationException("Illegal source mode: " + sourceMode);
default:
- throw new IllegalStateException("Source mode not yet supported:" + sourceMode);
+ throw new AggregationException("Source mode not supported: " + sourceMode);
}
if (source != null){
descriptiveResultHolder.sources.add(source);
config.setWithinTaxonSourceMode(AggregationSourceMode.ALL);
config.setToParentSourceMode(AggregationSourceMode.DESCRIPTION);
result = engine.invoke(config, repository);
- verifyStatusNotOk(result);
+ verifyStatusError(result, "Unsupported source mode for within-taxon aggregation: ALL");
commitAndStartNewTransaction();
- config.setWithinTaxonSourceMode(AggregationSourceMode.ALL);
+ config.setWithinTaxonSourceMode(AggregationSourceMode.DESCRIPTION);
+ config.setToParentSourceMode(AggregationSourceMode.ALL);
+ result = engine.invoke(config, repository);
+ verifyStatusError(result, "Unsupported source mode for to-parent aggregation: ALL");
+ commitAndStartNewTransaction();
+
+ config.setWithinTaxonSourceMode(AggregationSourceMode.ALL_SAMEVALUE);
config.setToParentSourceMode(AggregationSourceMode.DESCRIPTION);
result = engine.invoke(config, repository);
- verifyStatusNotOk(result);
+ verifyStatusError(result, "Unsupported source mode for within-taxon aggregation: ALL_SAMEVALUE");
commitAndStartNewTransaction();
// removeSomeDataFromFirstAggregation();
}
}
- private void verifyStatusNotOk(UpdateResult result) {
- if (result.getStatus() == UpdateResult.Status.OK){
- Assert.fail("Aggregation should fail but did not " + result.toString());
- //TODO test for expected exception
+ private void verifyStatusError(UpdateResult result, String expectedMessage) {
+ if (result.getStatus() != UpdateResult.Status.ERROR){
+ Assert.fail("Aggregation should fail with status error " + result.toString());
+ }
+ if (result.getExceptions().isEmpty()){
+ Assert.fail("Failing aggregation include exception " + result.toString());
+ }
+ Exception e = result.getExceptions().iterator().next();
+ if (! (e instanceof AggregationException)){
+ Assert.fail("Exception should be of type AggregationException " + result.toString());
+ }else if (expectedMessage != null){
+ Assert.assertEquals(expectedMessage, e.getMessage());
}
}