use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.
the class CdmImportBase method getSpecimenDescription.
/**
* Like {@link #getSpecimenDescription(SpecimenOrObservationBase, boolean, boolean)}
* Only matches a description if the given reference is a source of the description.<BR>
* If a new description is created the given reference will be added as a source.
*
* @see #getTaxonDescription(Taxon, boolean, boolean)
*/
public SpecimenDescription getSpecimenDescription(SpecimenOrObservationBase specimen, Reference ref, boolean isImageGallery, boolean createNewIfNotExists) {
SpecimenDescription result = null;
@SuppressWarnings("unchecked") Set<SpecimenDescription> descriptions = specimen.getDescriptions();
for (SpecimenDescription description : descriptions) {
if (description.isImageGallery() == isImageGallery) {
if (hasCorrespondingSource(ref, description)) {
result = description;
break;
}
}
}
if (result == null && createNewIfNotExists) {
result = SpecimenDescription.NewInstance(specimen);
result.setImageGallery(isImageGallery);
if (ref != null) {
result.addImportSource(null, null, ref, null);
}
}
return result;
}
use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.
the class MediaServiceImpl method delete.
@Override
@Transactional(readOnly = false)
public DeleteResult delete(UUID mediaUuid, MediaDeletionConfigurator config) {
DeleteResult result = new DeleteResult();
Media media = this.load(mediaUuid);
if (media == null) {
return result;
}
result = isDeletable(mediaUuid, config);
if (result.isOk()) {
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
for (CdmBase ref : references) {
IDescribable<?> updatedObject = null;
IService<ICdmBase> service = null;
if (ref instanceof TextData) {
TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class);
DescriptionBase<?> description = HibernateProxyHelper.deproxy(textData.getInDescription(), DescriptionBase.class);
IDescribable<?> objectToUpdate = null;
boolean deleteIsMatchingInstance = false;
if (description instanceof TaxonDescription) {
objectToUpdate = ((TaxonDescription) description).getTaxon();
deleteIsMatchingInstance = config.getDeleteFrom() instanceof Taxon;
service = (IService) taxonService;
} else if (description instanceof SpecimenDescription) {
objectToUpdate = ((SpecimenDescription) description).getDescribedSpecimenOrObservation();
deleteIsMatchingInstance = config.getDeleteFrom() instanceof SpecimenOrObservationBase;
service = (IService) specimenService;
} else if (description instanceof TaxonNameDescription) {
objectToUpdate = ((TaxonNameDescription) description).getTaxonName();
deleteIsMatchingInstance = config.getDeleteFrom() instanceof TaxonName;
service = (IService) nameService;
} else {
throw new RuntimeException("Unsupported DescriptionBase class");
}
if (objectToUpdate == null) {
continue;
} else if ((config.isDeleteFromDescription() && deleteIsMatchingInstance && config.getDeleteFrom().getId() == objectToUpdate.getId()) || config.isDeleteFromEveryWhere()) {
updatedObject = handleDeleteMedia(media, textData, description, (IDescribable) objectToUpdate);
} else {
// this should not be happen, because it is not deletable. see isDeletable
result.setAbort();
}
// } else if (ref instanceof MediaSpecimen && config.getDeleteFrom().getId() == ref.getId() && config.getDeleteFrom() instanceof MediaSpecimen){
// MediaSpecimen mediaSpecimen = HibernateProxyHelper.deproxy(ref, MediaSpecimen.class);
// mediaSpecimen.setMediaSpecimen(null);
// updatedObject = mediaSpecimen;
// service = (IService)specimenService;
} else if (ref instanceof MediaRepresentation) {
continue;
} else {
result.setAbort();
}
if (updatedObject != null) {
// service should always be != null if updatedObject != null
service.update(updatedObject);
result.addUpdatedObject((CdmBase) updatedObject);
}
}
if (result.isOk()) {
dao.delete(media);
result.addDeletedObject(media);
}
}
return result;
}
use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.
the class MediaServiceImpl method isDeletable.
@Override
public DeleteResult isDeletable(UUID mediaUuid, DeleteConfiguratorBase config) {
DeleteResult result = new DeleteResult();
Media media = this.load(mediaUuid);
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
MediaDeletionConfigurator mediaConfig = (MediaDeletionConfigurator) config;
CdmBase deleteFrom = mediaConfig.getDeleteFrom();
if (mediaConfig.isDeleteFromEveryWhere()) {
return result;
}
for (CdmBase ref : references) {
String message = null;
if (ref instanceof MediaRepresentation) {
continue;
} else if (ref instanceof TextData) {
TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class);
DescriptionBase<?> description = HibernateProxyHelper.deproxy(textData.getInDescription(), DescriptionBase.class);
if (description instanceof TaxonDescription) {
TaxonDescription desc = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
if (desc.getTaxon() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof Taxon && ((Taxon) deleteFrom).getId() == desc.getTaxon().getId()))) {
continue;
} else {
message = "The media can't be deleted from the database because it is referenced by a taxon. (" + desc.getTaxon().getTitleCache() + ")";
result.setAbort();
}
} else if (description instanceof SpecimenDescription) {
SpecimenDescription desc = HibernateProxyHelper.deproxy(description, SpecimenDescription.class);
if (desc.getDescribedSpecimenOrObservation() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof SpecimenOrObservationBase && ((SpecimenOrObservationBase) deleteFrom).getId() == desc.getDescribedSpecimenOrObservation().getId()))) {
continue;
} else {
message = "The media can't be deleted from the database because it is referenced by a specimen or observation. (" + desc.getDescribedSpecimenOrObservation().getTitleCache() + ")";
result.setAbort();
}
} else if (description instanceof TaxonNameDescription) {
TaxonNameDescription desc = HibernateProxyHelper.deproxy(description, TaxonNameDescription.class);
if (desc.getTaxonName() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof TaxonName && ((TaxonName) deleteFrom).getId() == desc.getTaxonName().getId()))) {
continue;
} else {
message = "The media can't be deleted from the database because it is referenced by a scientific name. (" + desc.getTaxonName().getTitleCache() + ")";
result.setAbort();
}
}
} else if (ref instanceof MediaSpecimen) {
message = "The media can't be deleted from the database because it is referenced by a mediaspecimen. (" + ((MediaSpecimen) ref).getTitleCache() + ")";
result.setAbort();
} else {
message = "The media can't be completely deleted because it is referenced by another " + ref.getUserFriendlyTypeName();
result.setAbort();
}
if (message != null) {
result.addException(new ReferencedObjectUndeletableException(message));
result.addRelatedObject(ref);
}
}
return result;
}
use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.
the class DescriptionServiceImpl method deleteDescription.
@Override
@Transactional(readOnly = false)
public DeleteResult deleteDescription(DescriptionBase<?> description) {
DeleteResult deleteResult = new DeleteResult();
if (description == null) {
return deleteResult;
}
// avoid lazy init exception
description = load(description.getId(), Arrays.asList("descriptiveDataSets"));
deleteResult = isDeletable(description.getUuid());
if (deleteResult.getRelatedObjects() != null && deleteResult.getRelatedObjects().size() == 1) {
Iterator<CdmBase> relObjects = deleteResult.getRelatedObjects().iterator();
CdmBase next = relObjects.next();
if (next instanceof CdmLinkSource) {
CdmLinkSource source = (CdmLinkSource) next;
ICdmTarget target = source.getTarget();
}
}
if (deleteResult.isOk()) {
CdmBase.deproxy(description);
if (description instanceof TaxonDescription) {
TaxonDescription taxDescription = (TaxonDescription) description;
Taxon tax = taxDescription.getTaxon();
if (tax != null) {
tax.removeDescription(taxDescription, true);
deleteResult.addUpdatedObject(tax);
}
} else if (description instanceof SpecimenDescription) {
SpecimenDescription specimenDescription = (SpecimenDescription) description;
SpecimenOrObservationBase<?> specimen = specimenDescription.getDescribedSpecimenOrObservation();
if (specimen != null) {
specimen.removeDescription(specimenDescription);
deleteResult.addUpdatedObject(specimen);
}
}
for (DescriptiveDataSet dataset : description.getDescriptiveDataSets()) {
dataset.removeDescription(description);
}
dao.delete(description);
deleteResult.addDeletedObject(description);
deleteResult.setCdmEntity(description);
} else {
logger.info(deleteResult.getExceptions().toString());
}
return deleteResult;
}
use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.
the class DerivedUnitFacadeTest method testGetFieldObjectImageGalleryBooleanPersisted.
@Test
@DataSet
// TODO generally works has id problems with following tests when running in suite
@Ignore
public void testGetFieldObjectImageGalleryBooleanPersisted() {
UUID specimenUUID = UUID.fromString("25383fc8-789b-4eff-92d3-a770d0622351");
DerivedUnit specimen = (DerivedUnit) service.load(specimenUUID);
assertNotNull("Specimen should exist (persisted)", specimen);
try {
DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
SpecimenDescription imageGallery = facade.getFieldObjectImageGallery(true);
assertNotNull("Image gallery should exist", imageGallery);
assertEquals("UUID should be equal to the persisted uuid", UUID.fromString("8cb772e9-1577-45c6-91ab-dbec1413c060"), imageGallery.getUuid());
assertEquals("The image gallery should be flagged as such", true, imageGallery.isImageGallery());
assertEquals("There should be one TextData in image gallery", 1, imageGallery.getElements().size());
List<Media> media = imageGallery.getElements().iterator().next().getMedia();
assertEquals("There should be 1 media", 1, media.size());
} catch (DerivedUnitFacadeNotSupportedException e) {
e.printStackTrace();
fail();
}
}
Aggregations