use of eu.etaxonomy.cdm.model.common.IntextReference in project cdmlib by cybertaxonomy.
the class MarkupFeatureImport method makeFeatureString.
/**
* @param state
* @param reader
* @param feature
* @param taxonDescription
* @param lastDescriptionElement
* @param distributionList
* @param next
* @return
* @throws XMLStreamException
* @throws
*/
private DescriptionElementBase makeFeatureString(MarkupImportState state, XMLEventReader reader, Feature feature, TaxonDescription taxonDescription, DescriptionElementBase lastDescriptionElement, XMLEvent next, Boolean isFreetext) throws XMLStreamException {
// for specimen only
if (feature.equals(Feature.SPECIMEN()) || feature.equals(Feature.MATERIALS_EXAMINED()) || feature.getUuid().equals(MarkupTransformer.uuidWoodSpecimens)) {
List<DescriptionElementBase> specimens = specimenImport.handleMaterialsExamined(state, reader, next, feature, taxonDescription);
for (DescriptionElementBase specimen : specimens) {
if (specimen.getInDescription() == null) {
taxonDescription.addElement(specimen);
}
lastDescriptionElement = specimen;
}
state.setCurrentCollector(null);
return lastDescriptionElement;
} else if (feature.equals(Feature.COMMON_NAME()) && (isFreetext == null || !isFreetext)) {
List<DescriptionElementBase> commonNames = makeCommonNameString(state, reader, next);
// NOTE: we do also have the old version makeVernacular, which was called from "others" below
for (DescriptionElementBase commonName : commonNames) {
taxonDescription.addElement(commonName);
lastDescriptionElement = commonName;
}
return lastDescriptionElement;
} else {
// others
Map<String, SubheadingResult> subheadingMap = handleString(state, reader, next, feature);
for (String subheading : subheadingMap.keySet()) {
Feature subheadingFeature = feature;
if (StringUtils.isNotBlank(subheading) && subheadingMap.size() > 1) {
subheadingFeature = makeFeature(subheading, state, next, null);
}
if (feature.equals(Feature.COMMON_NAME()) && (isFreetext == null || !isFreetext)) {
// NOTE: see above
// List<DescriptionElementBase> commonNames = makeVernacular(state, subheading, subheadingMap.get(subheading));
// for (DescriptionElementBase commonName : commonNames){
// taxonDescription.addElement(commonName);
// lastDescriptionElement = commonName;
// }
} else {
TextData textData = TextData.NewInstance(subheadingFeature);
SubheadingResult subHeadingResult = subheadingMap.get(subheading);
LanguageString languageString = textData.putText(getDefaultLanguage(state), subHeadingResult.text);
if (isNotEmptyCollection(subHeadingResult.references.getReferences())) {
for (LabeledReference reference : subHeadingResult.references.getReferences()) {
textData.addPrimaryTaxonomicSource(reference.ref, reference.detail);
}
textData.addImportSource(null, null, state.getConfig().getSourceReference(), null);
} else {
textData.addPrimaryTaxonomicSource(state.getConfig().getSourceReference());
}
// intext references
for (IntextReference intext : subHeadingResult.inlineReferences) {
languageString.addIntextReference(intext);
}
taxonDescription.addElement(textData);
lastDescriptionElement = textData;
// TODO how to handle figures when these data are split in
// subheadings
}
}
return lastDescriptionElement;
}
}
use of eu.etaxonomy.cdm.model.common.IntextReference in project cdmlib by cybertaxonomy.
the class MarkupImportBase method handleString.
/**
* Handle < string > .
* @param state
* @param reader
* @param parentEvent
* @param feature only needed for distributionLocalities
* @return
* @throws XMLStreamException
*/
protected Map<String, SubheadingResult> handleString(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, Feature feature) throws XMLStreamException {
// attributes
String classValue = getClassOnlyAttribute(parentEvent, false);
if (StringUtils.isNotBlank(classValue)) {
String message = "class attribute for <string> not yet implemented";
fireWarningEvent(message, parentEvent, 2);
}
boolean isHabitat = false;
// subheadings
Map<String, SubheadingResult> subHeadingMap = new HashMap<>();
String currentSubheading = null;
boolean isTextMode = true;
String text = "";
StringReferences currentReferences = null;
List<IntextReference> inlineReferences = new ArrayList<>();
boolean lastWasReference = false;
while (reader.hasNext()) {
XMLEvent next = readNoWhitespace(reader);
if (isMyEndingElement(next, parentEvent)) {
putCurrentSubheading(subHeadingMap, currentSubheading, text, currentReferences, inlineReferences);
return subHeadingMap;
}
// check if last event was reference
if (lastWasReference && !isStartingElement(next, BR) && !isEndingElement(next, BR) && !isStartingElement(next, SUB_HEADING)) {
for (LabeledReference labeledRef : currentReferences.content) {
if (labeledRef.ref != null) {
IntextReference intext = IntextReference.NewInstance(labeledRef.ref, null, 0, 0);
inlineReferences.add(intext);
text += intext.toInlineString(labeledRef.label);
} else {
text += labeledRef.label;
}
}
lastWasReference = false;
}
if (isStartingElement(next, BR)) {
text += "<br/>";
isTextMode = false;
} else if (isEndingElement(next, BR)) {
isTextMode = true;
} else if (isHtml(next)) {
text += getXmlTag(next);
} else if (isStartingElement(next, SUB_HEADING)) {
text = putCurrentSubheading(subHeadingMap, currentSubheading, text, currentReferences, inlineReferences);
currentReferences = null;
inlineReferences = new ArrayList<>();
lastWasReference = false;
// TODO footnotes
currentSubheading = getCData(state, reader, next).trim();
} else if (isStartingElement(next, DISTRIBUTION_LOCALITY)) {
if (feature != null && !feature.equals(Feature.DISTRIBUTION())) {
String message = "Distribution locality only allowed for feature of type 'distribution'";
fireWarningEvent(message, next, 4);
}
text += handleDistributionLocality(state, reader, next);
} else if (next.isCharacters()) {
if (!isTextMode) {
String message = "String is not in text mode";
fireWarningEvent(message, next, 6);
} else {
text += next.asCharacters().getData();
}
} else if (isStartingElement(next, HEADING)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, VERNACULAR_NAMES)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, QUOTE)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, DEDICATION)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, TAXONTYPE)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, FULL_NAME)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, REFERENCES)) {
if (currentReferences != null) {
fireWarningEvent("References do already exist", next, 2);
}
currentReferences = handleStringReferences(state, reader, next);
lastWasReference = true;
} else if (isStartingElement(next, REFERENCE)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, GATHERING)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, ANNOTATION)) {
// TODO //TODO test handleSimpleAnnotation
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, HABITAT)) {
text += featureImport.handleHabitat(state, reader, next);
isHabitat = true;
} else if (isStartingElement(next, FIGURE_REF)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, FIGURE)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, FOOTNOTE_REF)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, FOOTNOTE)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, WRITER)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, DATES)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, TO_KEY)) {
handleNotYetImplementedElement(next);
} else {
handleUnexpectedElement(next);
}
}
throw new IllegalStateException("<String> has no closing tag");
}
use of eu.etaxonomy.cdm.model.common.IntextReference in project cdmlib by cybertaxonomy.
the class DeduplicationHelper method reallocateSingleItem.
private void reallocateSingleItem(CdmBase cdmBase1, CdmBase cdmBase2, ReferenceHolder refHolder, Set<ICdmBase> cloneSet) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
List<CdmBase> referencingObjects = genericDao.getCdmBasesByFieldAndClass(refHolder.otherClass, refHolder.propertyName, cdmBase2, null);
for (CdmBase referencingObject : referencingObjects) {
if (!cloneSet.contains(referencingObject)) {
String className = refHolder.otherClass.getSimpleName();
String propertyName = refHolder.propertyName;
String hql = " UPDATE " + className + " c " + " SET c." + propertyName + " = :newValue " + " WHERE c.id = :id ";
Query query = session.createQuery(hql);
query.setEntity("newValue", cdmBase1);
query.setInteger("id", referencingObject.getId());
int rowCount = query.executeUpdate();
if (logger.isDebugEnabled()) {
logger.debug("Rows affected: " + rowCount);
}
session.refresh(referencingObject);
if (refHolder.otherClass == IntextReference.class) {
IntextReference intextRef = CdmBase.deproxy(referencingObject, IntextReference.class);
IIntextReferencable refEnt = intextRef.getReferencedEntity();
if (refEnt != null) {
String newText = refEnt.getText() == null ? null : refEnt.getText().replace(cdmBase2.getUuid().toString(), cdmBase1.getUuid().toString());
refEnt.setText(newText);
session.saveOrUpdate(refEnt);
}
// TODO
/*
* UPDATE LanguageString
* SET text = Replace(text, cdmBase2.getUuuid(), cdmBase1.getUuid)
* WHERE id IN (SELECT * FROM IntextReference
*
*/
System.out.println("IntextReference found");
}
}
}
session.flush();
}
use of eu.etaxonomy.cdm.model.common.IntextReference in project cdmlib by cybertaxonomy.
the class CdmGenericDaoImplTest method testReallocateIntextReferenceForNameAndAnnotation.
@Test
public void testReallocateIntextReferenceForNameAndAnnotation() throws MergeException {
UUID uuidPinusAlba = UUID.fromString("52743cec-b893-4e8b-b06c-91f9b9ba8fee");
UUID uuidAbiesAlba = UUID.fromString("6ed56b43-7cca-4c3b-bb90-7576da81c072");
// CREATE DATA
TaxonName pinusAlba = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
pinusAlba.setTitleCache("BotanicalName1", true);
pinusAlba.setUuid(uuidPinusAlba);
TaxonName abiesAlba = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
abiesAlba.setTitleCache("Abies alba", true);
abiesAlba.setUuid(uuidAbiesAlba);
Taxon taxon = Taxon.NewInstance(null, null);
Annotation annotation = Annotation.NewDefaultLanguageInstance("My annotation on Abies alba and its habit.");
taxon.addAnnotation(annotation);
IntextReference intextRefName = annotation.addIntextReference(abiesAlba, "My annotation on ", "Abies alba", " and its habit.");
String uuidIntextRefName = intextRefName.getUuid().toString();
Assert.assertEquals("My annotation on <cdm:name cdmId='" + uuidAbiesAlba + "' intextId='" + uuidIntextRefName + "'>Abies alba</cdm:name> and its habit.", annotation.getText());
// SAVE AND COMMIT
taxonDao.save(taxon);
nameDao.save(abiesAlba);
nameDao.save(pinusAlba);
commitAndStartNewTransaction(null);
// MERGE
DefaultMergeStrategy strategy = DefaultMergeStrategy.NewInstance(TaxonName.class);
abiesAlba = nameDao.findByUuid(uuidAbiesAlba);
pinusAlba = nameDao.findByUuid(uuidPinusAlba);
cdmGenericDao.merge(pinusAlba, abiesAlba, strategy);
taxon = (Taxon) taxonDao.findByUuid(taxon.getUuid());
annotation = taxon.getAnnotations().iterator().next();
Assert.assertEquals("My annotation on <cdm:name cdmId='" + uuidPinusAlba + "' intextId='" + uuidIntextRefName + "'>Abies alba</cdm:name> and its habit.", annotation.getText());
}
use of eu.etaxonomy.cdm.model.common.IntextReference in project cdmlib by cybertaxonomy.
the class Cdm2CdmImportBase method handlePersistedIntextReference.
protected IntextReference handlePersistedIntextReference(IntextReference intextReference, Cdm2CdmImportState state) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
IntextReference result = handlePersisted((VersionableEntity) intextReference, state);
result.setReferencedEntity(detache(result.getReferencedEntity(), false, state));
Method targetMethod = IntextReference.class.getDeclaredMethod("setTarget", IIntextReferenceTarget.class);
targetMethod.setAccessible(true);
targetMethod.invoke(result, detache(result.getTarget(), false, state));
return result;
}
Aggregations