Search in sources :

Example 11 with TermNode

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;
}
Also used : ArrayList(java.util.ArrayList) TermNode(eu.etaxonomy.cdm.model.term.TermNode)

Example 12 with TermNode

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Representation(eu.etaxonomy.cdm.model.term.Representation) TermNode(eu.etaxonomy.cdm.model.term.TermNode) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 13 with TermNode

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;
}
Also used : DescriptionBase(eu.etaxonomy.cdm.model.description.DescriptionBase) User(eu.etaxonomy.cdm.model.permission.User) TaxonNode(eu.etaxonomy.cdm.model.taxon.TaxonNode) LanguageStringBase(eu.etaxonomy.cdm.model.common.LanguageStringBase) TransactionStatus(org.springframework.transaction.TransactionStatus) TermNode(eu.etaxonomy.cdm.model.term.TermNode) TaxonBase(eu.etaxonomy.cdm.model.taxon.TaxonBase) DefinedTermBase(eu.etaxonomy.cdm.model.term.DefinedTermBase) Classification(eu.etaxonomy.cdm.model.taxon.Classification) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) List(java.util.List) TermTree(eu.etaxonomy.cdm.model.term.TermTree) Reference(eu.etaxonomy.cdm.model.reference.Reference) TypeDesignationBase(eu.etaxonomy.cdm.model.name.TypeDesignationBase) Media(eu.etaxonomy.cdm.model.media.Media) TermVocabulary(eu.etaxonomy.cdm.model.term.TermVocabulary) URISyntaxException(java.net.URISyntaxException) HomotypicalGroup(eu.etaxonomy.cdm.model.name.HomotypicalGroup) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase)

Example 14 with TermNode

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);
}
Also used : TermTree(eu.etaxonomy.cdm.model.term.TermTree) TermNode(eu.etaxonomy.cdm.model.term.TermNode) Feature(eu.etaxonomy.cdm.model.description.Feature) MalformedLSIDException(com.ibm.lsid.MalformedLSIDException) TransformerException(javax.xml.transform.TransformerException) UnknownCdmTypeException(eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException) IOException(java.io.IOException)

Example 15 with TermNode

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;
}
Also used : Resource(com.hp.hpl.jena.rdf.model.Resource) TermNode(eu.etaxonomy.cdm.model.term.TermNode)

Aggregations

TermNode (eu.etaxonomy.cdm.model.term.TermNode)25 ArrayList (java.util.ArrayList)12 Feature (eu.etaxonomy.cdm.model.description.Feature)7 TermTree (eu.etaxonomy.cdm.model.term.TermTree)7 UUID (java.util.UUID)7 DefinedTerm (eu.etaxonomy.cdm.model.term.DefinedTerm)5 Representation (eu.etaxonomy.cdm.model.term.Representation)5 TermVocabulary (eu.etaxonomy.cdm.model.term.TermVocabulary)5 TermDto (eu.etaxonomy.cdm.persistence.dto.TermDto)4 HashSet (java.util.HashSet)4 Resource (com.hp.hpl.jena.rdf.model.Resource)3 ReferencedObjectUndeletableException (eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException)3 Media (eu.etaxonomy.cdm.model.media.Media)3 DefinedTermBase (eu.etaxonomy.cdm.model.term.DefinedTermBase)3 CdmTransactionalIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)3 Test (org.junit.Test)3 DataChangeNoRollbackException (eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException)2 MeasurementUnit (eu.etaxonomy.cdm.model.description.MeasurementUnit)2 StatisticalMeasure (eu.etaxonomy.cdm.model.description.StatisticalMeasure)2 TaxonNode (eu.etaxonomy.cdm.model.taxon.TaxonNode)2