use of eu.etaxonomy.cdm.model.name.INonViralName in project cdmlib by cybertaxonomy.
the class CsvDemoExport method handleSynonyms.
/**
* This method concatenates several synonyms in a list.
*
* @param record
* @param taxon
*/
private void handleSynonyms(CsvDemoRecord record, Taxon taxon) {
Set<Synonym> synonyms = taxon.getSynonyms();
ArrayList<String> synonymLabels = new ArrayList<>();
for (Synonym synonym : synonyms) {
SynonymType type = synonym.getType();
if (type == null) {
// should not happen
type = SynonymType.SYNONYM_OF();
}
INonViralName name = synonym.getName();
synonymLabels.add(name.getTitleCache());
}
record.setSynonyms(synonymLabels);
}
use of eu.etaxonomy.cdm.model.name.INonViralName in project cdmlib by cybertaxonomy.
the class CsvTaxExportRedlist method doInvoke.
/**
* Retrieves data from a CDM DB and serializes them CDM to CSV.
* Starts with root taxa and traverses the classification to retrieve
* children taxa, synonyms, relationships, descriptive data, red list
* status (features).
* Taxa that are not part of the classification are not found.
*
* @param exImpConfig
* @param dbname
* @param filename
*/
@Override
protected void doInvoke(CsvTaxExportStateRedlist state) {
CsvTaxExportConfiguratorRedlist config = state.getConfig();
TransactionStatus txStatus = startTransaction(true);
List<NamedArea> selectedAreas = config.getNamedAreas();
Set<TaxonNode> taxonNodes = assembleTaxonNodeSet(config);
PrintWriter writer = null;
ByteArrayOutputStream byteArrayOutputStream;
try {
byteArrayOutputStream = config.getByteArrayOutputStream();
writer = new PrintWriter(byteArrayOutputStream);
// geographical Filter
List<TaxonNode> filteredNodes = handleGeographicalFilter(state, selectedAreas, taxonNodes);
// sorting List
Collections.sort(filteredNodes, new Comparator<TaxonNode>() {
@Override
public int compare(TaxonNode tn1, TaxonNode tn2) {
Taxon taxon1 = tn1.getTaxon();
Taxon taxon2 = tn2.getTaxon();
if (taxon1 != null && taxon2 != null) {
return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());
} else {
return 0;
}
}
});
for (TaxonNode node : filteredNodes) {
Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
CsvTaxRecordRedlist record = assembleRecord(state);
INonViralName name = taxon.getName();
Classification classification = node.getClassification();
config.setClassificationTitleCache(classification.getTitleCache());
if (!this.recordExists(taxon)) {
handleTaxonBase(record, taxon, name, taxon, classification, null, false, false, config);
record.write(writer);
this.addExistingRecord(taxon);
}
// misapplied names
handleMisapplication(taxon, writer, classification, record, config);
writer.flush();
}
} catch (ClassCastException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
this.clearExistingRecordIds();
}
commitTransaction(txStatus);
return;
}
use of eu.etaxonomy.cdm.model.name.INonViralName in project cdmlib by cybertaxonomy.
the class CsvNameExport method createNewRecord.
private HashMap<String, String> createNewRecord(TaxonNode childNode, CsvNameExportState state) {
HashMap<String, String> nameRecord = new HashMap<>();
nameRecord.put("classification", childNode.getClassification().getTitleCache());
if (!childNode.getTaxon().getName().getRank().isLower(Rank.GENUS())) {
return null;
}
TaxonNode familyNode = getHigherNode(childNode, Rank.FAMILY());
Taxon taxon;
String nameString;
IBotanicalName name;
if (familyNode == null) {
nameRecord.put("familyTaxon", null);
nameRecord.put("familyName", null);
nameRecord.put("descriptionsFam", null);
} else {
familyNode = CdmBase.deproxy(familyNode);
familyNode.getTaxon().setProtectedTitleCache(true);
nameRecord.put("familyTaxon", familyNode.getTaxon().getTitleCache());
if (familyMap.get(familyNode.getTaxon().getUuid()) != null) {
nameRecord.putAll(familyMap.get(familyNode.getTaxon().getUuid()));
} else {
taxon = (Taxon) getTaxonService().load(familyNode.getTaxon().getUuid());
taxon = CdmBase.deproxy(taxon);
name = CdmBase.deproxy(taxon.getName());
nameRecord.put("familyName", name.getNameCache());
extractDescriptions(nameRecord, taxon, Feature.INTRODUCTION(), "descriptionsFam", state);
familyMap.put(familyNode.getTaxon().getUuid(), nameRecord);
}
}
TaxonNode genusNode = getHigherNode(childNode, Rank.GENUS());
if (genusNode != null) {
genusNode = CdmBase.deproxy(genusNode);
genusNode.getTaxon().setProtectedTitleCache(true);
nameRecord.put("genusTaxon", genusNode.getTaxon().getTitleCache());
if (genusMap.get(genusNode.getTaxon().getUuid()) != null) {
nameRecord.putAll(genusMap.get(genusNode.getTaxon().getUuid()));
} else {
taxon = (Taxon) getTaxonService().load(genusNode.getTaxon().getUuid());
taxon = CdmBase.deproxy(taxon);
name = CdmBase.deproxy(taxon.getName());
if (name.getNameCache() != null) {
nameRecord.put("genusName", name.getNameCache());
} else {
nameRecord.put("genusName", name.getGenusOrUninomial());
}
extractDescriptions(nameRecord, taxon, getNotesFeature(state), "descriptionsGen", state);
genusMap.put(genusNode.getTaxon().getUuid(), nameRecord);
}
} else {
nameRecord.put("genusTaxon", null);
nameRecord.put("genusName", null);
nameRecord.put("descriptionsGen", null);
}
// taxon = (Taxon) getTaxonService().load(childNode.getTaxon().getUuid());
taxon = CdmBase.deproxy(childNode.getTaxon());
// if (taxon.isPublish()){
INonViralName nonViralName = taxon.getName();
nameString = createTaggedNameString(nonViralName, false);
nameRecord.put("childTaxon", taxon.getTitleCache());
if (taxon.getSec() != null) {
nameRecord.put("secRef", taxon.getSec().getTitleCache());
} else {
nameRecord.put("secRef", null);
}
getTaxonRelations(nameRecord, taxon);
name = CdmBase.deproxy(taxon.getName());
nameRecord.put("childName", nameString);
nameRecord.put("nameId", String.valueOf(name.getId()));
nameRecord.put("nameCache", name.getNameCache());
nameRecord.put("titleName", name.getTitleCache());
if (name.getNomenclaturalReference() != null) {
nameRecord.put("NomRefTitleCache", name.getNomenclaturalReference().getTitleCache());
} else {
nameRecord.put("NomRefTitleCache", null);
}
nameRecord.put("fullName", name.getNameCache());
nameRecord.put("fullTitleCache", name.getFullTitleCache());
Set<TypeDesignationBase> typeDesSet = name.getTypeDesignations();
Iterator<TypeDesignationBase> it = typeDesSet.iterator();
String typeNameString = NOT_DESIGNATED;
String statusString = null;
if (it.hasNext()) {
TypeDesignationBase<?> typeDes = CdmBase.deproxy(it.next());
if (typeDes instanceof NameTypeDesignation) {
NameTypeDesignation nameTypeDes = CdmBase.deproxy(typeDes, NameTypeDesignation.class);
IBotanicalName typeName = CdmBase.deproxy(nameTypeDes.getTypeName());
if (typeName != null) {
typeNameString = "<i>" + typeName.getNameCache() + "</i> " + typeName.getAuthorshipCache();
if (nameTypeDes.getTypeStatus() != null) {
NameTypeDesignationStatus status = CdmBase.deproxy(nameTypeDes.getTypeStatus());
statusString = status.getTitleCache();
}
}
}
}
nameRecord.put("typeName", typeNameString);
StringBuffer homotypicalSynonyms = new StringBuffer();
TreeMap<HomotypicalGroup, List<Synonym>> heterotypicSynonymsList = new TreeMap<>(new HomotypicalGroupComparator());
List<Synonym> homotypicSynonymsList = new ArrayList<>();
StringBuffer heterotypicalSynonyms = new StringBuffer();
List<Synonym> homotypicSynonyms;
HomotypicalGroup group;
IBotanicalName synonymName;
String doubtfulTitleCache;
for (Synonym synonym : taxon.getSynonyms()) {
synonymName = CdmBase.deproxy(synonym.getName());
group = CdmBase.deproxy(synonymName.getHomotypicalGroup());
synonymName.generateFullTitle();
if (synonym.isDoubtful()) {
if (!synonymName.getFullTitleCache().startsWith("?")) {
doubtfulTitleCache = "?" + synonymName.getFullTitleCache();
synonymName = synonymName.clone();
synonymName.setFullTitleCache(doubtfulTitleCache, true);
}
}
if (!group.equals(name.getHomotypicalGroup())) {
if (heterotypicSynonymsList.containsKey(group)) {
heterotypicSynonymsList.get(group).add(synonym);
} else {
homotypicSynonyms = new ArrayList<>();
homotypicSynonyms.add(synonym);
heterotypicSynonymsList.put(group, homotypicSynonyms);
homotypicSynonyms = null;
}
} else {
synonymName.generateFullTitle();
homotypicSynonymsList.add(synonym);
}
}
String synonymString;
boolean first = true;
for (List<Synonym> list : heterotypicSynonymsList.values()) {
Collections.sort(list, new HomotypicGroupTaxonComparator(null));
first = true;
for (TaxonBase<?> synonym : list) {
NomenclaturalStatus status = null;
if (!synonym.getName().getStatus().isEmpty()) {
status = CdmBase.deproxy(synonym.getName().getStatus().iterator().next());
if (status.getType().isInvalid()) {
heterotypicalSynonyms.append(" <invalid> ");
synonymName = CdmBase.deproxy(synonym.getName());
synonymString = createSynonymNameString(synonymName, state.getConfig().isInvalidNamesQuoted());
heterotypicalSynonyms.append(synonymString);
continue;
}
}
if (first) {
heterotypicalSynonyms.append(" <heterotypic> ");
} else {
heterotypicalSynonyms.append(" <homonym> ");
}
first = false;
synonymName = CdmBase.deproxy(synonym.getName());
synonymString = createSynonymNameString(synonymName, false);
heterotypicalSynonyms.append(synonymString);
}
}
first = true;
Collections.sort(homotypicSynonymsList, new HomotypicGroupTaxonComparator(null));
NomenclaturalStatus status = null;
for (TaxonBase<?> synonym : homotypicSynonymsList) {
if (!synonym.getName().getStatus().isEmpty()) {
status = CdmBase.deproxy(synonym.getName().getStatus().iterator().next());
if (status.getType().isInvalid()) {
homotypicalSynonyms.append(" <invalid> ");
synonymName = CdmBase.deproxy(synonym.getName());
synonymString = createSynonymNameString(synonymName, true);
homotypicalSynonyms.append(synonymString);
continue;
} else if (!first) {
homotypicalSynonyms.append(" <homonym> ");
}
} else if (!first) {
homotypicalSynonyms.append(" <homonym> ");
}
first = false;
synonymName = CdmBase.deproxy(synonym.getName());
synonymString = createSynonymNameString(synonymName, false);
homotypicalSynonyms.append(synonymString);
}
nameRecord.put("synonyms_homotypic", homotypicalSynonyms.toString());
nameRecord.put("synonyms_heterotypic", heterotypicalSynonyms.toString());
nameRecord.put("status", statusString);
Set<NameRelationship> nameRelations = name.getNameRelations();
INonViralName relatedName = null;
String nameRelType = null;
String relNameString = null;
if (nameRelations.size() > 0) {
NameRelationship nameRel = nameRelations.iterator().next();
IBotanicalName fromName = CdmBase.deproxy(nameRel.getFromName());
if (fromName.equals(taxon.getName())) {
relatedName = nameRel.getToName();
} else {
relatedName = nameRel.getFromName();
}
nameRel = CdmBase.deproxy(nameRel);
nameRelType = nameRel.getType().getTitleCache();
relNameString = createTaggedNameString(relatedName, getStatus(relatedName));
}
nameRecord.put("relatedName", relNameString);
nameRecord.put("nameRelType", nameRelType);
extractDescriptions(nameRecord, taxon, Feature.DISTRIBUTION(), "descriptions", state);
return nameRecord;
}
use of eu.etaxonomy.cdm.model.name.INonViralName in project cdmlib by cybertaxonomy.
the class MarkupNomenclatureImport method handleNom.
/**
* Creates the name defined by a nom tag. Adds it to the given homotypical
* group (if not null).
*
* @param state
* @param reader
* @param parentEvent
* @param homotypicalGroup
* @return
* @throws XMLStreamException
*/
private INonViralName handleNom(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, HomotypicalGroup homotypicalGroup) throws XMLStreamException {
boolean isSynonym = false;
boolean isNameType = state.isNameType();
// attributes
Map<String, Attribute> attributes = getAttributes(parentEvent);
boolean isMisidentification = getAndRemoveBooleanAttributeValue(parentEvent, attributes, "misidentification", false);
String classValue = getAndRemoveRequiredAttributeValue(parentEvent, attributes, "class");
checkNoAttributes(attributes, parentEvent);
INonViralName name;
TaxonRelationship misappliedRelation = null;
if (isMisidentification) {
if (isNameType || ACCEPTED.equalsIgnoreCase(classValue)) {
fireWarningEvent("Misidentification only defined for synonyms", parentEvent, 4);
}
name = createNameByCode(state, null);
Taxon acc = state.getCurrentTaxon();
Taxon misapplied = Taxon.NewInstance(name, null);
misappliedRelation = acc.addMisappliedName(misapplied, null, null);
} else if (!isNameType && ACCEPTED.equalsIgnoreCase(classValue)) {
isSynonym = false;
name = createName(state, homotypicalGroup, isSynonym);
} else if (!isNameType && SYNONYM.equalsIgnoreCase(classValue)) {
isSynonym = true;
name = createName(state, homotypicalGroup, isSynonym);
} else if (isNameType && NAME_TYPE.equalsIgnoreCase(classValue)) {
// TODO do we need to define the rank here?
name = createNameByCode(state, null);
} else {
fireUnexpectedAttributeValue(parentEvent, CLASS, classValue);
name = createNameByCode(state, null);
}
Map<String, String> nameMap = new HashMap<>();
String text = "";
boolean nameFilled = false;
state.setNameStatus(null);
while (reader.hasNext()) {
XMLEvent next = readNoWhitespace(reader);
if (isMyEndingElement(next, parentEvent)) {
// fill the name with all data gathered, if not yet done before
if (nameFilled == false) {
fillName(state, nameMap, name, misappliedRelation, next);
}
handleNomText(state, parentEvent, text, isNameType);
state.getDeduplicationHelper().replaceAuthorNamesAndNomRef(name);
handleNameStatus(state, name, next);
state.setNameStatus(null);
return name;
} else if (isEndingElement(next, ANNOTATION)) {
// NOT YET IMPLEMENTED //TODO test
// handleSimpleAnnotation
popUnimplemented(next.asEndElement());
} else if (isStartingElement(next, FULL_NAME)) {
handleFullName(state, reader, name, next);
} else if (isStartingElement(next, NUM)) {
handleNomNum(state, reader, next);
} else if (isStartingElement(next, NAME)) {
handleName(state, reader, next, nameMap);
} else if (isStartingElement(next, CITATION)) {
// we need to fill the name here to have nomenclatural author available for the following citations
fillName(state, nameMap, name, misappliedRelation, next);
nameFilled = true;
handleCitation(state, reader, next, name, misappliedRelation);
} else if (next.isCharacters()) {
text += next.asCharacters().getData();
} else if (isStartingElement(next, HOMONYM)) {
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, NOTES)) {
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, NOMENCLATURAL_NOTES)) {
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, ANNOTATION)) {
handleNotYetImplementedElement(next);
} else {
handleUnexpectedElement(next);
}
}
throw new IllegalStateException("Nom has no closing tag");
}
use of eu.etaxonomy.cdm.model.name.INonViralName in project cdmlib by cybertaxonomy.
the class MarkupSpecimenImport method handleGathering.
private void handleGathering(MarkupImportState state, XMLEventReader readerOrig, XMLEvent parentEvent, DerivedUnitFacade facade) throws XMLStreamException {
checkNoAttributes(parentEvent);
boolean hasCollector = false;
boolean hasFieldNum = false;
LookAheadEventReader reader = new LookAheadEventReader(parentEvent.asStartElement(), readerOrig);
// elements
while (reader.hasNext()) {
XMLEvent next = readNoWhitespace(reader);
if (isMyEndingElement(next, parentEvent)) {
if (!hasCollector) {
if (state.getCurrentCollector() == null) {
checkMandatoryElement(hasCollector, parentEvent.asStartElement(), COLLECTOR);
} else {
facade.setCollector(state.getCurrentCollector());
}
}
checkMandatoryElement(hasFieldNum, parentEvent.asStartElement(), FIELD_NUM);
return;
} else if (isStartingElement(next, COLLECTOR)) {
hasCollector = true;
String collectorStr = getCData(state, reader, next);
TeamOrPersonBase<?> collector = createCollector(state, collectorStr);
facade.setCollector(collector);
state.setCurrentCollector(collector);
} else if (isStartingElement(next, ALTERNATIVE_COLLECTOR)) {
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, FIELD_NUM)) {
hasFieldNum = true;
String fieldNumStr = getCData(state, reader, next);
facade.setFieldNumber(fieldNumStr);
} else if (isStartingElement(next, ALTERNATIVE_FIELD_NUM)) {
handleAlternativeFieldNumber(state, reader, next, facade.innerFieldUnit());
} else if (isStartingElement(next, COLLECTION_TYPE_STATUS)) {
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, COLLECTION_AND_TYPE)) {
handleGatheringCollectionAndType(state, reader, next, facade);
} else if (isStartingElement(next, ALTERNATIVE_COLLECTION_TYPE_STATUS)) {
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, SUB_GATHERING)) {
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, COLLECTION)) {
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, LOCALITY)) {
handleLocality(state, reader, next, facade);
} else if (isStartingElement(next, FULL_NAME)) {
// can be any
Rank defaultRank = Rank.SPECIES();
INonViralName nvn = createNameByCode(state, defaultRank);
handleFullName(state, reader, nvn, next);
TaxonName name = TaxonName.castAndDeproxy(nvn);
DeterminationEvent.NewInstance(name, facade.innerDerivedUnit() != null ? facade.innerDerivedUnit() : facade.innerFieldUnit());
} else if (isStartingElement(next, DATES)) {
TimePeriod timePeriod = handleDates(state, reader, next);
facade.setGatheringPeriod(timePeriod);
} else if (isStartingElement(next, GATHERING_NOTES)) {
handleAmbigousManually(state, reader, next.asStartElement());
} else if (isStartingElement(next, NOTES)) {
handleNotYetImplementedElement(next);
} else if (next.isCharacters()) {
String text = next.asCharacters().getData().trim();
if (isPunctuation(text)) {
// do nothing
} else if (state.isSpecimenType() && charIsSimpleType(text)) {
// do nothing
} else if ((text.equals("=") || text.equals("(")) && reader.nextIsStart(ALTERNATIVE_FIELD_NUM)) {
// do nothing
} else if ((text.equals(").") || text.equals(")")) && reader.previousWasEnd(ALTERNATIVE_FIELD_NUM)) {
// do nothing
} else if (charIsOpeningOrClosingBracket(text)) {
// for now we don't do anything, however in future brackets may have semantics
} else {
// TODO
String message = "Unrecognized text: %s";
fireWarningEvent(String.format(message, text), next, 6);
}
} else {
handleUnexpectedElement(next);
}
}
throw new IllegalStateException("Collection has no closing tag.");
}
Aggregations