use of eu.etaxonomy.cdm.model.location.NamedArea 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.location.NamedArea in project cdmlib by cybertaxonomy.
the class CsvTaxExportRedlist method handleDiscriptionData.
private void handleDiscriptionData(CsvTaxRecordRedlist record, Taxon taxon) {
Set<TaxonDescription> descriptions = taxon.getDescriptions();
ArrayList<String> distributions = new ArrayList<String>();
for (TaxonDescription description : descriptions) {
for (DescriptionElementBase el : description.getElements()) {
if (el.isInstanceOf(Distribution.class)) {
Distribution distribution = CdmBase.deproxy(el, Distribution.class);
NamedArea area = distribution.getArea();
distributions.add(area.getTitleCache());
}
}
}
record.setCountryCode(distributions);
}
use of eu.etaxonomy.cdm.model.location.NamedArea in project cdmlib by cybertaxonomy.
the class MarkupFeatureImport method makeVernacular.
private List<DescriptionElementBase> makeVernacular(MarkupImportState state, String subheading, String commonNameString) throws XMLStreamException {
List<DescriptionElementBase> result = new ArrayList<>();
Reference sourceReference = state.getConfig().getSourceReference();
String[] splits = commonNameString.split(",");
for (String split : splits) {
split = split.trim();
if (!split.matches(".*\\(.*\\)\\.?")) {
fireWarningEvent("Common name string '" + split + "' does not match given pattern", state.getReader().peek(), 4);
}
String name = split.replaceAll("\\(.*\\)", "").replace(".", "").trim();
String languageStr = split.replaceFirst(".*\\(", "").replaceAll("\\)\\.?", "").trim();
Language language = null;
if (StringUtils.isNotBlank(languageStr)) {
language = makeLanguageByLangStr(state, languageStr);
}
DescriptionElementBase commonName;
if (name != null && name.length() < 255) {
NamedArea area = null;
commonName = CommonTaxonName.NewInstance(name, language, area);
commonName.addPrimaryTaxonomicSource(sourceReference);
} else {
if (language == null) {
language = getDefaultLanguage(state);
}
commonName = TextData.NewInstance(Feature.COMMON_NAME(), name, language, null);
commonName.addPrimaryTaxonomicSource(sourceReference);
String warning = "Vernacular feature is >255 size. Therefore it is handled as TextData, not CommonTaxonName: " + name;
fireWarningEvent(warning, state.getReader().peek(), 1);
}
result.add(commonName);
}
return result;
}
use of eu.etaxonomy.cdm.model.location.NamedArea 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.NamedArea in project cdmlib by cybertaxonomy.
the class MarkupImportBase method makeArea.
/**
* @param state
* @param areaName
* @param level
* @return
*/
protected NamedArea makeArea(MarkupImportState state, String areaName, NamedAreaLevel level) {
// TODO FM vocabulary
TermVocabulary<NamedArea> voc = null;
NamedAreaType areaType = null;
NamedArea area = null;
try {
area = state.getTransformer().getNamedAreaByKey(areaName);
} catch (UndefinedTransformerMethodException e) {
throw new RuntimeException(e);
}
if (area == null) {
boolean isNewInState = false;
UUID uuid = state.getAreaUuid(areaName);
if (uuid == null) {
isNewInState = true;
try {
uuid = state.getTransformer().getNamedAreaUuid(areaName);
if (uuid == null) {
uuid = UUID.randomUUID();
state.putAreaUuid(areaName, uuid);
}
} catch (UndefinedTransformerMethodException e) {
throw new RuntimeException(e);
}
}
CdmImportBase.TermMatchMode matchMode = CdmImportBase.TermMatchMode.UUID_LABEL;
area = getNamedArea(state, uuid, areaName, areaName, areaName, areaType, level, voc, matchMode);
if (isNewInState) {
state.putAreaUuid(areaName, area.getUuid());
// TODO just for testing -> make generic and move to better place
String geoServiceLayer = "vmap0_as_bnd_political_boundary_a";
String layerFieldName = "nam";
if ("Bangka".equals(areaName)) {
String areaValue = "PULAU BANGKA#SUMATERA SELATAN";
GeoServiceArea geoServiceArea = new GeoServiceArea();
geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
this.editGeoService.setMapping(area, geoServiceArea);
// save(area, state);
}
if ("Luzon".equals(areaName)) {
GeoServiceArea geoServiceArea = new GeoServiceArea();
List<String> list = Arrays.asList("HERMANA MAYOR ISLAND#CENTRAL LUZON", "HERMANA MENOR ISLAND#CENTRAL LUZON", "CENTRAL LUZON");
for (String areaValue : list) {
geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
}
this.editGeoService.setMapping(area, geoServiceArea);
// save(area, state);
}
if ("Mindanao".equals(areaName)) {
GeoServiceArea geoServiceArea = new GeoServiceArea();
List<String> list = Arrays.asList("NORTHERN MINDANAO", "SOUTHERN MINDANAO", "WESTERN MINDANAO");
// TODO to be continued
for (String areaValue : list) {
geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
}
this.editGeoService.setMapping(area, geoServiceArea);
// save(area, state);
}
if ("Palawan".equals(areaName)) {
GeoServiceArea geoServiceArea = new GeoServiceArea();
List<String> list = Arrays.asList("PALAWAN#SOUTHERN TAGALOG");
for (String areaValue : list) {
geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
}
this.editGeoService.setMapping(area, geoServiceArea);
// save(area, state);
}
}
}
return area;
}
Aggregations