org.phenotips.vocabulary.internal.DefaultVocabularyManager.java Source code

Java tutorial

Introduction

Here is the source code for org.phenotips.vocabulary.internal.DefaultVocabularyManager.java

Source

/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/
 */
package org.phenotips.vocabulary.internal;

import org.phenotips.vocabulary.Vocabulary;
import org.phenotips.vocabulary.VocabularyManager;
import org.phenotips.vocabulary.VocabularyTerm;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;

/**
 * Default implementation of the {@link VocabularyManager} component, which uses all the {@link Vocabulary vocabularies}
 * registered in the component manager.
 *
 * @version $Id: c5d0dbb32ef69c78651c7e819715dba42fe35baa $
 * @since 1.2M4 (under different names since 1.0M8)
 */
@Component
@Singleton
public class DefaultVocabularyManager implements VocabularyManager, Initializable {
    /** The currently available vocabularies. */
    @Inject
    private Map<String, Vocabulary> vocabularies;

    /** The available vocabularies, including keys for each of their aliases. */
    private Map<String, Vocabulary> aliasVocabularies;

    @Override
    public void initialize() throws InitializationException {
        this.aliasVocabularies = new HashMap<>();
        for (Vocabulary vocabulary : this.vocabularies.values()) {
            for (String alias : vocabulary.getAliases()) {
                this.aliasVocabularies.put(alias, vocabulary);
            }
        }
    }

    @Override
    public VocabularyTerm resolveTerm(String termId) {
        Vocabulary vocabulary = getVocabularyForTerm(termId);
        if (vocabulary != null) {
            return vocabulary.getTerm(termId);
        }
        return null;
    }

    @Override
    public Vocabulary getVocabulary(String vocabularyId) {
        return this.aliasVocabularies.get(vocabularyId);
    }

    @Override
    public List<String> getAvailableVocabularies() {
        return new ArrayList<>(this.vocabularies.keySet());
    }

    /**
     * Finds the owner vocabulary given a term identifier. The vocabulary is identified by the term ID prefix, for
     * example {@code HP} in {@code HP:0002066}.
     *
     * @param termId the term identifier to process
     * @return the owner vocabulary, or {@code null} if the term doesn't belong to a known vocabulary
     */
    private Vocabulary getVocabularyForTerm(String termId) {
        String vocabularyId = StringUtils.substringBefore(termId, ":");
        if (StringUtils.isNotBlank(vocabularyId)) {
            return this.aliasVocabularies.get(vocabularyId);
        }
        return null;
    }
}