use of eu.etaxonomy.cdm.model.location.NamedAreaLevel in project cdmlib by cybertaxonomy.
the class MarkupImportBase method handleDistributionLocality.
private String handleDistributionLocality(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
Map<String, Attribute> attributes = getAttributes(parentEvent);
String classValue = getAndRemoveRequiredAttributeValue(parentEvent, attributes, CLASS);
String statusValue = getAndRemoveAttributeValue(attributes, STATUS);
String frequencyValue = getAndRemoveAttributeValue(attributes, FREQUENCY);
Taxon taxon = state.getCurrentTaxon();
// TODO which ref to take?
Reference sourceReference = state.getConfig().getSourceReference();
String text = "";
while (reader.hasNext()) {
XMLEvent next = readNoWhitespace(reader);
if (isMyEndingElement(next, parentEvent)) {
if (StringUtils.isNotBlank(text)) {
String label = CdmUtils.removeTrailingDots(normalize(text));
TaxonDescription description = getExtractedMarkupMarkedDescription(state, taxon, sourceReference);
NamedAreaLevel level = makeNamedAreaLevel(state, classValue, next);
// status
PresenceAbsenceTerm status = null;
if (isNotBlank(statusValue)) {
try {
status = state.getTransformer().getPresenceTermByKey(statusValue);
if (status == null) {
UUID uuid = state.getTransformer().getPresenceTermUuid(statusValue);
if (uuid != null) {
status = this.getPresenceAbsenceTerm(state, uuid, statusValue, statusValue, statusValue, false, null);
}
}
if (status == null) {
// TODO
String message = "The presence/absence status '%s' could not be transformed to an CDM status";
fireWarningEvent(String.format(message, statusValue), next, 4);
}
} catch (UndefinedTransformerMethodException e) {
throw new RuntimeException(e);
}
} else {
status = PresenceAbsenceTerm.PRESENT();
}
// frequency
if (isNotBlank(frequencyValue)) {
if (frequencyValue.equalsIgnoreCase("absent") && PresenceAbsenceTerm.PRESENT().equals(status)) {
// to be on the safe side that not real status has been defined yet.
status = PresenceAbsenceTerm.ABSENT();
} else {
String message = "The frequency attribute is currently not yet available in CDM";
fireWarningEvent(message, parentEvent, 6);
}
}
NamedArea higherArea = null;
List<NamedArea> areas = new ArrayList<>();
String patSingleArea = "([^,\\(]{3,})";
String patSeparator = "(,|\\sand\\s)";
String hierarchiePattern = String.format("%s\\((%s(%s%s)*)\\)", patSingleArea, patSingleArea, patSeparator, patSingleArea);
Pattern patHierarchie = Pattern.compile(hierarchiePattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = patHierarchie.matcher(label);
if (matcher.matches()) {
String higherAreaStr = matcher.group(1).trim();
higherArea = makeArea(state, higherAreaStr, level);
String[] innerAreas = matcher.group(2).split(patSeparator);
for (String innerArea : innerAreas) {
if (isNotBlank(innerArea)) {
NamedArea singleArea = makeArea(state, innerArea.trim(), level);
areas.add(singleArea);
NamedArea partOf = singleArea.getPartOf();
// if (partOf == null){
// singleArea.setPartOf(higherArea);
// }
}
}
} else {
NamedArea singleArea = makeArea(state, label, level);
areas.add(singleArea);
}
for (NamedArea area : areas) {
// create distribution
Distribution distribution = Distribution.NewInstance(area, status);
distribution.addPrimaryTaxonomicSource(sourceReference);
description.addElement(distribution);
}
} else {
String message = "Empty distribution locality";
fireWarningEvent(message, next, 4);
}
return text;
} else if (isStartingElement(next, COORDINATES)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isEndingElement(next, COORDINATES)) {
// TODO
popUnimplemented(next.asEndElement());
} else if (next.isCharacters()) {
text += next.asCharacters().getData();
} else {
handleUnexpectedElement(next);
}
}
throw new IllegalStateException("<DistributionLocality> has no closing tag");
}
use of eu.etaxonomy.cdm.model.location.NamedAreaLevel in project cdmlib by cybertaxonomy.
the class MarkupImportBase method makeNamedAreaLevel.
/**
* @param state
* @param levelString
* @param next
* @return
*/
protected NamedAreaLevel makeNamedAreaLevel(MarkupImportState state, String levelString, XMLEvent next) {
NamedAreaLevel level;
try {
level = state.getTransformer().getNamedAreaLevelByKey(levelString);
if (level == null) {
UUID levelUuid = state.getTransformer().getNamedAreaLevelUuid(levelString);
if (levelUuid == null) {
String message = "Unknown distribution locality class (named area level): %s. Create new level instead.";
message = String.format(message, levelString);
fireWarningEvent(message, next, 6);
}
level = getNamedAreaLevel(state, levelUuid, levelString, levelString, levelString, null);
}
} catch (UndefinedTransformerMethodException e) {
throw new RuntimeException(e);
}
return level;
}
use of eu.etaxonomy.cdm.model.location.NamedAreaLevel in project cdmlib by cybertaxonomy.
the class MarkupSpecimenImport method handleLocality.
private void handleLocality(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, DerivedUnitFacade facade) throws XMLStreamException {
String classValue = getClassOnlyAttribute(parentEvent);
boolean isLocality = false;
NamedAreaLevel areaLevel = null;
if ("locality".equalsIgnoreCase(classValue) || state.getConfig().isIgnoreLocalityClass()) {
isLocality = true;
} else {
areaLevel = makeNamedAreaLevel(state, classValue, parentEvent);
}
String text = "";
// elements
while (reader.hasNext()) {
XMLEvent next = readNoWhitespace(reader);
if (isMyEndingElement(next, parentEvent)) {
if (StringUtils.isNotBlank(text)) {
text = normalize(text);
text = removeTrailingPunctuation(text);
if (isLocality) {
facade.setLocality(text, getDefaultLanguage(state));
} else {
text = CdmUtils.removeTrailingDots(text);
NamedArea area = makeArea(state, text, areaLevel);
facade.addCollectingArea(area);
}
}
// TODO
return;
} else if (isStartingElement(next, ALTITUDE)) {
handleNotYetImplementedElement(next);
// homotypicalGroup = handleNom(state, reader, next, taxon,
// homotypicalGroup);
} else if (isStartingElement(next, COORDINATES)) {
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, ANNOTATION)) {
handleNotYetImplementedElement(next);
} else if (next.isCharacters()) {
text += next.asCharacters().getData();
} else {
handleUnexpectedElement(next);
}
}
throw new IllegalStateException("<SpecimenType> has no closing tag");
}
use of eu.etaxonomy.cdm.model.location.NamedAreaLevel in project cdmlib by cybertaxonomy.
the class SpecimenCdmExcelImport method handleAreas.
private void handleAreas(DerivedUnitFacade facade, SpecimenRow row, SpecimenCdmExcelImportState state) {
List<PostfixTerm> areas = row.getLeveledAreas();
for (PostfixTerm lArea : areas) {
String description = lArea.term;
String abbrev = lArea.term;
NamedAreaType type = null;
String key = lArea.postfix + "_" + lArea.term;
UUID areaUuid = state.getArea(key);
NamedAreaLevel level = state.getPostfixLevel(lArea.postfix);
TermMatchMode matchMode = state.getConfig().getAreaMatchMode();
NamedArea area = getNamedArea(state, areaUuid, lArea.term, description, abbrev, type, level, null, matchMode);
facade.addCollectingArea(area);
if (areaUuid == null) {
state.putArea(key, area.getUuid());
}
}
}
use of eu.etaxonomy.cdm.model.location.NamedAreaLevel in project cdmlib by cybertaxonomy.
the class NamedAreaLevelExcelImport method firstPass.
@Override
protected void firstPass(SpecimenCdmExcelImportState state) {
NamedAreaLevellRow row = state.getNamedAreaLevelRow();
// level
UUID uuid = row.getUuid();
String label = row.getAbbreviation();
String text = row.getDescription();
String labelAbbrev = row.getAbbreviation();
NamedAreaLevel level = getNamedAreaLevel(state, uuid, label, text, labelAbbrev, null);
if (StringUtils.isNotBlank(row.getPostfix())) {
state.putPostfixLevel(row.getPostfix(), level);
}
// save
getTermService().save(level);
return;
}
Aggregations