/*
 * 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$
 * @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;

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

    @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.vocabularies.get(vocabularyId);
    }

    @Override
    public List<String> getAvailableVocabularies() {
        return new ArrayList<String>(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.vocabularies.get(vocabularyId);
        }
        return null;
    }
}

@@ -1787,16 +1787,153 @@
 aries;%0A%0A
+    /** The available vocabularies, including keys for each of their aliases. */%0A    private Map%3CString, Vocabulary%3E aliasVocabularies;%0A%0A
     @Ove
@@ -2016,35 +2016,18 @@
     
-Map%3CString, Vocabulary%3E new
+this.alias
 Voca
@@ -2034,19 +2034,16 @@
 bularies
-Map
  = new H
@@ -2214,19 +2214,26 @@
         
-new
+this.alias
 Vocabula
@@ -2236,19 +2236,16 @@
 bularies
-Map
 .put(ali
@@ -2289,56 +2289,8 @@
   %7D%0A
-        this.vocabularies = newVocabulariesMap;%0A
     
@@ -2635,33 +2635,38 @@
     return this.
-v
+aliasV
 ocabularies.get(
@@ -2751,16 +2751,20 @@
 laries()
+%0A   
  %7B%0A     
@@ -3381,25 +3381,30 @@
 return this.
-v
+aliasV
 ocabularies.

Add a REST-full API for accessing vocabularies Expose each vocabulary only once, instead of listing them under each of their aliases