/*
 * Copyright (c) 2015 Memorial Sloan-Kettering Cancer Center.
 *
 * This library 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. The software and documentation provided hereunder
 * is on an "as is" basis, and Memorial Sloan-Kettering Cancer Center has no
 * obligations to provide maintenance, support, updates, enhancements or
 * modifications. In no event shall Memorial Sloan-Kettering Cancer Center be
 * liable to any party for direct, indirect, special, incidental or
 * consequential damages, including lost profits, arising out of the use of this
 * software and its documentation, even if Memorial Sloan-Kettering Cancer
 * Center has been advised of the possibility of such damage.
 */

/*
 * This file is part of cBioPortal.
 *
 * cBioPortal 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.
 *
 * 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.cbioportal.genome_nexus.service.internal;

import org.cbioportal.genome_nexus.model.VariantAnnotation;
import org.cbioportal.genome_nexus.service.AnnotationEnricher;
import org.cbioportal.genome_nexus.service.EnrichmentService;

import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Benjamin Gross
 */
@Service
public class VEPEnrichmentService implements EnrichmentService
{
    private Map<String, AnnotationEnricher> enrichers;

    @Override
    public void enrichAnnotation(VariantAnnotation variantAnnotation)
    {
        // modify JSON returned by VEP
        if (enrichers != null)
        {
            for (AnnotationEnricher enricher: enrichers.values())
            {
                enricher.enrich(variantAnnotation);
            }
        }
    }

    @Override
    public void registerEnricher(String id, AnnotationEnricher enricher)
    {
        // initiate enricher list if not initiated yet
        if (enrichers == null)
        {
            // linked hash map ensures that enrichers are executed by the same order they are registered
            enrichers = new LinkedHashMap<>();
        }

        enrichers.put(id, enricher);
    }

    @Override
    public void unregisterEnricher(String id)
    {
        if (enrichers != null)
        {
            enrichers.put(id, null);
        }
    }
}

@@ -1546,16 +1546,101 @@
 ernal;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -1861,24 +1861,51 @@
 e.Service;%0A%0A
+import java.util.Iterator;%0A
 import java.
@@ -1945,16 +1945,44 @@
 til.Map;
+%0Aimport java.util.Map.Entry;
 %0A%0A/**%0A *
@@ -2084,11 +2084,93 @@
 vice
-%0A
+ 
 %7B%0A
+    private static final Log LOG = LogFactory.getLog(VEPEnrichmentService.class);%0A
     
@@ -2304,20 +2304,16 @@
 otation)
-%0A   
  %7B%0A     
@@ -2368,32 +2368,34 @@
 richers != null)
+ %7B
 %0A        %7B%0A     
@@ -2391,27 +2391,159 @@
     
-%7B%0A            for (
+    Iterator%3CEntry%3CString, AnnotationEnricher%3E%3E it = enrichers.entrySet().iterator();%0A            while (it.hasNext()) %7B%0A                Entry%3CString, 
 Anno
@@ -2548,32 +2548,33 @@
 notationEnricher
+%3E
  enricher: enric
@@ -2566,17 +2566,71 @@
 enricher
-:
+ = it.next();%0A                try %7B%0A                   
  enriche
@@ -2634,85 +2634,252 @@
 cher
-s.v
+.getV
 alue
-s
 ()
-)%0A            %7B%0A                enricher.enrich(variantAnnotation);
+.enrich(variantAnnotation);%0A                %7D catch (Exception e) %7B%0A                    LOG.warn(%22Failed to enrich with %22 + enricher.getKey() + %22: %22 + variantAnnotation.getVariant() + %22 %22 + e.getLocalizedMessage());%0A                %7D
 %0A   

Fix #350 mutation assessor enrichment failure