use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class CharacterNodeDto method fromTermNode.
public static CharacterNodeDto fromTermNode(TermNode<Character> child, TermTreeDto treeDto) {
Assert.notNull(child, "Node should not be null");
CharacterNodeDto dto = new CharacterNodeDto(child.getTerm() != null ? CharacterDto.fromCharacter(child.getTerm()) : null, null, child.getParent() != null ? child.getParent().getIndex(child) : 0, treeDto, child.getUuid(), child.treeIndex(), child.getPath());
if (child.getParent() != null) {
dto.setParentUuid(child.getParent().getUuid());
}
List<TermNodeDto> children = new ArrayList();
for (Object o : child.getChildNodes()) {
if (o instanceof TermNode) {
TermNode childNode = (TermNode) o;
if (childNode != null) {
if (childNode.getTermType().equals(TermType.Character)) {
children.add(CharacterNodeDto.fromTermNode(childNode, treeDto));
} else {
children.add(TermNodeDto.fromNode(childNode, treeDto));
}
}
}
}
dto.setChildren(children);
dto.setOnlyApplicableIf(child.getOnlyApplicableIf());
if (!dto.getOnlyApplicableIf().isEmpty()) {
for (FeatureStateDto stateDto : dto.getOnlyApplicableIf()) {
if (!treeDto.getOnlyApplicable().containsKey(dto.getTerm().getUuid())) {
treeDto.getOnlyApplicable().put(dto.getTerm().getUuid(), new HashSet<>());
}
treeDto.getOnlyApplicable().get(dto.getTerm().getUuid()).add(stateDto);
}
}
dto.setInapplicableIf(child.getInapplicableIf());
if (!dto.getInapplicableIf().isEmpty()) {
for (FeatureStateDto stateDto : dto.getInapplicableIf()) {
if (!treeDto.getInapplicableMap().containsKey(dto.getTerm().getUuid())) {
treeDto.getInapplicableMap().put(dto.getTerm().getUuid(), new HashSet<>());
}
treeDto.getInapplicableMap().get(dto.getTerm().getUuid()).add(stateDto);
}
}
dto.setTermType(child.getTermType());
return dto;
}
use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class TermTreeDto method termTreeDtoListFrom.
public static List<TermTreeDto> termTreeDtoListFrom(List<Object[]> results) {
// list to ensure order
List<TermTreeDto> dtos = new ArrayList<>();
// map to handle multiple representations/media/vocRepresentation because of LEFT JOIN
Map<UUID, TermTreeDto> dtoMap = new HashMap<>(results.size());
for (Object[] elements : results) {
UUID uuid = (UUID) elements[0];
if (dtoMap.containsKey(uuid)) {
// multiple results for one term -> multiple (voc) representation/media
if (elements[1] != null) {
dtoMap.get(uuid).addRepresentation((Representation) elements[1]);
}
} else {
// term representation
Set<Representation> representations = new HashSet<>();
if (elements[1] instanceof Representation) {
representations = new HashSet<>(1);
representations.add((Representation) elements[1]);
}
TermTreeDto termTreeDto = new TermTreeDto(uuid, representations, (TermType) elements[2], (String) elements[5], (boolean) elements[6], (boolean) elements[7], (boolean) elements[8]);
termTreeDto.setUri((URI) elements[3]);
if (termTreeDto.getTermType().equals(TermType.Character)) {
termTreeDto.setRoot(CharacterNodeDto.fromTermNode((TermNode<Character>) elements[4], termTreeDto));
} else {
termTreeDto.setRoot(TermNodeDto.fromNode((TermNode) elements[4], termTreeDto));
}
dtoMap.put(uuid, termTreeDto);
dtos.add(termTreeDto);
}
}
return dtos;
}
use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class JaxbImport method saveData.
/**
* Saves data in DB
*/
private void saveData(JaxbImportState state, DataSet dataSet) {
JaxbImportConfigurator jaxbImpConfig = state.getConfig();
boolean success = true;
Collection<TaxonBase> taxonBases;
List<? extends AgentBase> agents;
List<DefinedTermBase> terms;
List<User> users;
List<Reference> references;
List<TaxonName> taxonomicNames;
List<DescriptionBase> descriptions;
List<TypeDesignationBase<?>> typeDesignations;
List<SpecimenOrObservationBase> occurrences;
List<TermTree> featureTrees;
List<TermNode> termNodes;
List<Media> media;
List<LanguageStringBase> languageData;
List<TermVocabulary<DefinedTermBase>> termVocabularies;
List<HomotypicalGroup> homotypicalGroups;
TransactionStatus txStatus = startTransaction();
try {
if (jaxbImpConfig.isDoUser() == true) {
/*
* this is a crucial call, otherwise the password will not be set correctly
* and the whole authentication will not work
*
* a bit preliminary, should be used only if the complete database is replaced
*/
authenticate(Configuration.adminLogin, Configuration.adminPassword);
logger.info("Users: " + (users = dataSet.getUsers()).size());
for (User user : users) {
List<User> usersList = getUserService().listByUsername(user.getUsername(), null, null, null, null, null, null);
if (usersList.isEmpty()) {
getUserService().save(user);
} else {
// User existingUser = usersList.get(0);
// user.setId(existingUser.getId());
// getUserService().merge(user);
// merging does not yet work because of #4102
}
}
}
} catch (Exception ex) {
logger.error("Error saving users");
ex.printStackTrace();
success = false;
}
if ((jaxbImpConfig.isDoTerms() == true) && (terms = dataSet.getTerms()).size() > 0) {
// txStatus = startTransaction();
success &= saveTerms(terms);
// commitTransaction(txStatus);
}
if ((jaxbImpConfig.isDoTermVocabularies() == true) && (termVocabularies = dataSet.getTermVocabularies()).size() > 0) {
// txStatus = startTransaction();
success &= saveTermVocabularies(termVocabularies);
}
// txStatus = startTransaction();
try {
if (jaxbImpConfig.isDoAuthors() == true) {
if ((agents = dataSet.getAgents()).size() > 0) {
logger.info("Agents: " + agents.size());
getAgentService().saveOrUpdate((Collection) agents);
}
}
} catch (Exception ex) {
logger.error("Error saving agents");
success = false;
}
// txStatus = startTransaction();
try {
if (jaxbImpConfig.getDoReferences() != IImportConfigurator.DO_REFERENCES.NONE) {
if ((references = dataSet.getReferences()).size() > 0) {
logger.info("References: " + references.size());
getReferenceService().saveOrUpdate(references);
logger.info("ready...");
}
}
} catch (Exception ex) {
ex.printStackTrace();
logger.error("Error saving references");
success = false;
}
// txStatus = startTransaction();
try {
if (jaxbImpConfig.isDoTaxonNames() == true) {
if ((taxonomicNames = dataSet.getTaxonomicNames()).size() > 0) {
logger.info("Taxonomic names: " + taxonomicNames.size());
getNameService().saveOrUpdate(taxonomicNames);
}
}
} catch (Exception ex) {
logger.error("Error saving taxon names");
success = false;
}
// txStatus = startTransaction();
try {
if (jaxbImpConfig.isDoHomotypicalGroups() == true) {
if ((homotypicalGroups = dataSet.getHomotypicalGroups()).size() > 0) {
logger.info("Homotypical groups: " + homotypicalGroups.size());
getNameService().saveAllHomotypicalGroups(homotypicalGroups);
}
}
} catch (Exception ex) {
logger.error("Error saving homotypical groups");
success = false;
}
// Need to get the taxa and the synonyms here.
try {
if (jaxbImpConfig.isDoTaxa() == true) {
if (dataSet.getTaxonBases().size() > 0) {
List taxa = dataSet.getTaxonBases();
dataSet.getTaxonBases();
logger.info("Taxon bases: " + taxa.size());
taxa.iterator();
getTaxonService().saveOrUpdate(taxa);
/*while (taxBases.hasNext()){
getTaxonService().save(taxBases.next());
}*/
// getTaxonService().saveTaxonAll(taxonBases);
}
}
} catch (Exception ex) {
logger.error("Error saving taxa");
ex.printStackTrace();
success = false;
}
// NomenclaturalStatus, TypeDesignations
try {
if (jaxbImpConfig.isDoTypeDesignations() == true) {
if ((typeDesignations = dataSet.getTypeDesignations()).size() > 0) {
logger.info("Type Designations: " + typeDesignations.size());
getNameService().saveTypeDesignationAll(typeDesignations);
}
}
} catch (Exception ex) {
logger.error("Error saving type designations");
success = false;
}
// txStatus = startTransaction();
try {
if (jaxbImpConfig.isDoOccurrence() == true) {
if ((occurrences = dataSet.getOccurrences()).size() > 0) {
logger.info("Occurrences: " + occurrences.size());
getOccurrenceService().saveOrUpdate(occurrences);
}
}
} catch (Exception ex) {
logger.error("Error saving occurrences");
success = false;
}
// txStatus = startTransaction();
try {
if (jaxbImpConfig.isDoFeatureData() == true) {
if ((featureTrees = dataSet.getTermTrees()).size() > 0) {
logger.info("Feature data: " + featureTrees.size());
getTermTreeService().saveOrUpdate(featureTrees);
}
}
} catch (Exception ex) {
logger.error("Error saving feature data");
success = false;
}
// txStatus = startTransaction();
try {
if (jaxbImpConfig.isDoMedia() == true) {
if ((media = dataSet.getMedia()).size() > 0) {
logger.info("Media: " + media.size());
getMediaService().saveOrUpdate(media);
}
}
} catch (Exception ex) {
logger.error("Error saving media");
success = false;
}
if (jaxbImpConfig.isDoClassificationData() == true) {
logger.info("# Classification");
Collection<TaxonNode> nodes = dataSet.getTaxonNodes();
Collection<Classification> classifications = dataSet.getClassifications();
getClassificationService().saveTaxonNodeAll(nodes);
for (Classification tree : classifications) {
getClassificationService().saveOrUpdate(tree);
}
}
commitTransaction(txStatus);
logger.info("All data saved");
if (!success) {
state.setUnsuccessfull();
}
return;
}
use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class TaxonXTreatmentExtractor method buildFeatureTree.
/**
*/
private void buildFeatureTree() {
logger.info("buildFeatureTree");
TermTree<?> proibiospheretree = importer.getTermTreeService().find(proIbioTreeUUID);
if (proibiospheretree == null) {
List<TermTree> trees = importer.getTermTreeService().list(TermTree.class, null, null, null, null);
if (trees.size() == 1) {
TermTree<Feature> ft = trees.get(0);
if (featuresMap == null) {
featuresMap = new HashMap<>();
}
for (Feature feature : ft.getDistinctTerms()) {
if (feature != null) {
featuresMap.put(feature.getTitleCache(), feature);
}
}
}
proibiospheretree = TermTree.NewFeatureInstance();
proibiospheretree.setUuid(proIbioTreeUUID);
}
TermNode root2 = proibiospheretree.getRoot();
if (root2 != null) {
int nbChildren = root2.getChildCount() - 1;
while (nbChildren > -1) {
try {
root2.removeChild(nbChildren);
} catch (Exception e) {
logger.warn("Can't remove child from FeatureTree " + e);
}
nbChildren--;
}
}
for (Feature feature : featuresMap.values()) {
root2.addChild(feature);
}
importer.getTermTreeService().saveOrUpdate(proibiospheretree);
}
use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class OwlExportUtil method createFeatureTreeResource.
static Resource createFeatureTreeResource(TermTree featureTree, ICdmRepository repo, StructureTreeOwlExportState state) {
String featureTreeResourceUri = getFeatureTreeResourceUri(featureTree, state);
// check if tree exists
if (state.getModel().containsResource(ResourceFactory.createResource(featureTreeResourceUri))) {
return state.getModel().createResource(featureTreeResourceUri);
}
Resource featureTreeResource = state.getModel().createResource(featureTreeResourceUri).addProperty(OwlUtil.propUuid, featureTree.getUuid().toString()).addProperty(OwlUtil.propLabel, featureTree.getTitleCache()).addProperty(OwlUtil.propIsA, OwlUtil.TREE).addProperty(OwlUtil.propType, featureTree.getTermType().getKey());
TermNode rootNode = featureTree.getRoot();
Resource resourceRootNode = OwlExportUtil.createNodeResource(rootNode, false, repo, state);
featureTreeResource.addProperty(OwlUtil.propHasRootNode, resourceRootNode);
addChildNode(rootNode, resourceRootNode, repo, state);
return featureTreeResource;
}
Aggregations