use of org.openforis.collect.model.TaxonTree.Node in project collect by openforis.
the class CollectSpeciesListService method loadSpeciesListData.
@Override
public Object loadSpeciesListData(Survey survey, String taxonomyName, String attribute, String speciesCode) {
CollectTaxonomy taxonomy = speciesManager.loadTaxonomyByName((CollectSurvey) survey, taxonomyName);
TaxonTree taxonTree = speciesManager.loadTaxonTree(taxonomy);
Taxon taxon = taxonTree.findTaxonByCode(speciesCode);
if (taxon == null) {
return null;
}
Node taxonNode = taxonTree.getNodeBySystemId(taxon.getSystemId());
if (FAMILY_ATTRIBUTE.equals(attribute) || FAMILY_NAME_ATTRIBUTE.equals(attribute) || FAMILY_SCIENTIFIC_NAME_ATTRIBUTE.equals(attribute)) {
// family name
Node familyNode = taxonNode.getAncestor(TaxonRank.FAMILY);
return familyNode == null ? null : familyNode.getTaxon().getScientificName();
} else if (GENUS_ATTRIBUTE.equals(attribute)) {
Node genusNode = taxonNode.getAncestor(TaxonRank.GENUS);
return genusNode == null ? null : genusNode.getTaxon().getScientificName();
} else if (SCIENTIFIC_NAME_ATTRIBUTE.equals(attribute)) {
return taxon.getScientificName();
} else if (CODE_ATTRIBUTE.equals(attribute)) {
return taxon.getCode();
} else if (Languages.exists(Standard.ISO_639_3, attribute)) {
TaxonVernacularName vernacularName = taxonNode.getVernacularName(attribute);
return vernacularName == null ? null : vernacularName.getVernacularName();
} else {
// info (extra) attribute
TaxonomyDefinition taxonDefinition = survey.getReferenceDataSchema().getTaxonomyDefinition(taxonomyName);
if (taxonDefinition == null) {
throw new IllegalArgumentException("No reference data schema found for taxonomy: " + taxonomyName);
} else {
int attributeIndex = taxonDefinition.getAttributeNames().indexOf(attribute);
if (attributeIndex < 0) {
throw new IllegalArgumentException("Unsupported attribute: " + attribute);
} else {
return taxon.getInfoAttribute(attributeIndex);
}
}
}
}
use of org.openforis.collect.model.TaxonTree.Node in project collect by openforis.
the class SpeciesBackupImportTask method findParentTaxon.
private Taxon findParentTaxon(SpeciesBackupLine line) throws ParsingException {
Node parentNode = line.getParentId() == null ? null : taxonTree.getNodeBySystemId(line.getParentId());
Taxon parent = parentNode == null ? null : parentNode.getTaxon();
return parent;
}
use of org.openforis.collect.model.TaxonTree.Node in project collect by openforis.
the class SpeciesManager method loadTaxonTree.
public TaxonTree loadTaxonTree(CollectTaxonomy taxonomy) {
TaxonTree tree = taxonTreeByTaxonomyIdCache.get(taxonomy.getId());
if (tree == null) {
String taxonomyName = taxonomy.getName();
TaxonomyDefinition taxonDefinition = taxonomy.getSurvey().getReferenceDataSchema().getTaxonomyDefinition(taxonomyName);
List<Taxon> taxons = taxonDao.loadTaxonsForTreeBuilding(taxonomy);
tree = new TaxonTree(taxonDefinition);
Map<Integer, Taxon> idToTaxon = new HashMap<Integer, Taxon>();
for (Taxon taxon : taxons) {
Integer systemId = taxon.getSystemId();
Integer parentId = taxon.getParentId();
Taxon parent = parentId == null ? null : idToTaxon.get(parentId);
Node newNode = tree.addNode(parent, taxon);
List<TaxonVernacularName> vernacularNames = taxonVernacularNameDao.findByTaxon(systemId);
tree.addVernacularNames(newNode, vernacularNames);
idToTaxon.put(systemId, taxon);
}
taxonTreeByTaxonomyIdCache.put(taxonomy.getId(), tree);
}
return tree;
}
use of org.openforis.collect.model.TaxonTree.Node in project collect by openforis.
the class SpeciesImportProcess method createTaxon.
protected Taxon createTaxon(SpeciesLine line, TaxonRank rank, Taxon parent, String normalizedScientificName) throws ParsingException {
boolean mostSpecificRank = line.getRank() == rank;
Taxon taxon = taxonTree.findTaxonByScientificName(normalizedScientificName);
boolean newTaxon = taxon == null;
if (newTaxon) {
taxon = new Taxon();
taxon.setTaxonRank(rank);
taxon.setScientificName(normalizedScientificName);
Node node = taxonTree.addNode(parent, taxon);
node.addMetadata(LINE_NUMBER_TREE_NODE_METADATA, line.getLineNumber());
node.addMetadata(RAW_SCIENTIFIC_NAME_TREE_NODE_METADATA, line.getRawScientificName());
} else if (mostSpecificRank) {
checkDuplicateScientificName(line, parent, normalizedScientificName);
}
if (mostSpecificRank) {
String code = line.getCode();
Integer taxonId = line.getTaxonId();
checkDuplicates(line, code, taxonId);
taxon.setCode(code);
taxon.setTaxonId(taxonId);
TaxonomyDefinition taxonDefinition = survey.getReferenceDataSchema().getTaxonomyDefinition(taxonomyName);
List<Attribute> taxonAttributes = taxonDefinition.getAttributes();
for (Attribute attribute : taxonAttributes) {
String value = line.getInfoAttribute(attribute.getName());
taxon.addInfoAttribute(value);
}
taxonTree.updateNodeInfo(taxon);
processVernacularNames(line, taxon);
}
return taxon;
}
use of org.openforis.collect.model.TaxonTree.Node in project collect by openforis.
the class SpeciesImportProcess method checkDuplicates.
protected void checkDuplicates(SpeciesLine line, String code, Integer taxonId) throws ParsingException {
Node foundNode = null;
foundNode = taxonTree.getNodeByTaxonId(taxonId);
if (foundNode != null) {
throwDuplicateRowParsingException(line, SpeciesFileColumn.NO, foundNode);
}
foundNode = taxonTree.getNodeByCode(code);
if (foundNode != null) {
throwDuplicateRowParsingException(line, SpeciesFileColumn.CODE, foundNode);
}
}
Aggregations