Search in sources :

Example 1 with IntextReference

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;
    }
}
Also used : IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) TextData(eu.etaxonomy.cdm.model.description.TextData) ArrayList(java.util.ArrayList) List(java.util.List) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) Map(java.util.Map) Feature(eu.etaxonomy.cdm.model.description.Feature) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase)

Example 2 with IntextReference

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");
}
Also used : IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 3 with IntextReference

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();
}
Also used : IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) Query(org.hibernate.Query) ICdmBase(eu.etaxonomy.cdm.model.common.ICdmBase) CdmBase(eu.etaxonomy.cdm.model.common.CdmBase) IIntextReferencable(eu.etaxonomy.cdm.model.common.IIntextReferencable)

Example 4 with IntextReference

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());
}
Also used : DefaultMergeStrategy(eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy) IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) CommonTaxonName(eu.etaxonomy.cdm.model.description.CommonTaxonName) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) UUID(java.util.UUID) Annotation(eu.etaxonomy.cdm.model.common.Annotation) Test(org.junit.Test) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)

Example 5 with IntextReference

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;
}
Also used : IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) Method(java.lang.reflect.Method)

Aggregations

IntextReference (eu.etaxonomy.cdm.model.common.IntextReference)7 LanguageString (eu.etaxonomy.cdm.model.common.LanguageString)3 TextData (eu.etaxonomy.cdm.model.description.TextData)2 Reference (eu.etaxonomy.cdm.model.reference.Reference)2 Taxon (eu.etaxonomy.cdm.model.taxon.Taxon)2 DefaultMergeStrategy (eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy)2 CdmTransactionalIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 Test (org.junit.Test)2 Annotation (eu.etaxonomy.cdm.model.common.Annotation)1 CdmBase (eu.etaxonomy.cdm.model.common.CdmBase)1 ICdmBase (eu.etaxonomy.cdm.model.common.ICdmBase)1 IIntextReferencable (eu.etaxonomy.cdm.model.common.IIntextReferencable)1 Language (eu.etaxonomy.cdm.model.common.Language)1 CommonTaxonName (eu.etaxonomy.cdm.model.description.CommonTaxonName)1 DescriptionElementBase (eu.etaxonomy.cdm.model.description.DescriptionElementBase)1 Feature (eu.etaxonomy.cdm.model.description.Feature)1 TaxonDescription (eu.etaxonomy.cdm.model.description.TaxonDescription)1 TaxonName (eu.etaxonomy.cdm.model.name.TaxonName)1