/*
 * Copyright 2015-2017 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.opencb.opencga.storage.core.manager.clinical;

import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.datastore.core.QueryResult;
import org.opencb.opencga.catalog.db.api.DBIterator;
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.catalog.managers.CatalogManager;
import org.opencb.opencga.catalog.managers.ClinicalAnalysisManager;
import org.opencb.opencga.core.models.ClinicalAnalysis;
import org.opencb.opencga.core.models.clinical.Comment;
import org.opencb.opencga.core.models.clinical.Interpretation;
import org.opencb.opencga.core.models.clinical.ReportedVariant;
import org.opencb.opencga.storage.core.StorageEngineFactory;
import org.opencb.opencga.storage.core.clinical.ClinicalVariantEngine;
import org.opencb.opencga.storage.core.clinical.ClinicalVariantException;
import org.opencb.opencga.storage.core.clinical.ReportedVariantIterator;
import org.opencb.opencga.storage.core.exceptions.StorageEngineException;
import org.opencb.opencga.storage.core.manager.StorageManager;

import java.io.IOException;

public class ClinicalInterpretationManager extends StorageManager {

    private ClinicalVariantEngine clinicalVariantEngine;

    public ClinicalInterpretationManager(CatalogManager catalogManager, StorageEngineFactory storageEngineFactory) {
        super(catalogManager, storageEngineFactory);
    }

    @Override
    public void testConnection() throws StorageEngineException {
    }

    public QueryResult<ReportedVariant> index(String token) throws IOException, ClinicalVariantException {
        return null;
    }

    public QueryResult<ReportedVariant> index(String study, String token) throws IOException, ClinicalVariantException, CatalogException {
        ClinicalAnalysisManager clinicalAnalysisManager = catalogManager.getClinicalAnalysisManager();
        DBIterator<ClinicalAnalysis> iterator = clinicalAnalysisManager.iterator(study, new Query(), QueryOptions.empty(), token);
        return null;
    }

    public QueryResult<ReportedVariant> query(Query query, QueryOptions options, String token)
            throws IOException, ClinicalVariantException {

        return clinicalVariantEngine.query(query, options, "");
    }

    public QueryResult<Interpretation> interpretationQuery(Query query, QueryOptions options, String token)
            throws IOException, ClinicalVariantException {

        return clinicalVariantEngine.interpretationQuery(query, options, "");
    }

    public ReportedVariantIterator iterator(Query query, QueryOptions options, String toten) throws IOException, ClinicalVariantException {
        return clinicalVariantEngine.iterator(query, options, "");
    }

    public void addInterpretationComment(long interpretationId, Comment comment, String toten)
            throws IOException, ClinicalVariantException {
        clinicalVariantEngine.addInterpretationComment(interpretationId, comment, "");
    }

    public void addReportedVariantComment(long interpretationId, String variantId, Comment comment, String toten)
            throws IOException, ClinicalVariantException {
        clinicalVariantEngine.addReportedVariantComment(interpretationId, variantId, comment, "");
    }
}

@@ -1104,16 +1104,108 @@
 alysis;%0A
+import org.opencb.opencga.core.models.Project;%0Aimport org.opencb.opencga.core.models.Study;%0A
 import o
@@ -1248,24 +1248,24 @@
 al.Comment;%0A
-
 import org.o
@@ -1823,16 +1823,67 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Apublic
@@ -1941,24 +1941,85 @@
 eManager %7B%0A%0A
+    private ClinicalAnalysisManager clinicalAnalysisManager;%0A
     private 
@@ -2233,16 +2233,96 @@
 actory);
+%0A%0A        clinicalAnalysisManager = catalogManager.getClinicalAnalysisManager();
 %0A    %7D%0A%0A
@@ -2685,111 +2685,8 @@
 n %7B%0A
-        ClinicalAnalysisManager clinicalAnalysisManager = catalogManager.getClinicalAnalysisManager();%0A
     
@@ -2983,34 +2983,495 @@
 VariantException
- %7B
+, CatalogException %7B%0A%0A        QueryResult%3CProject%3E projectQueryResult = catalogManager.getProjectManager().get(new Query(), QueryOptions.empty(), token);%0A        List%3CString%3E studyIds = new ArrayList%3C%3E();%0A        for (Project project : projectQueryResult.getResult()) %7B%0A            for (Study study : project.getStudies()) %7B%0A                studyIds.add(String.valueOf(study.getId()));%0A            %7D%0A        %7D%0A%0A        for (String studyId : studyIds) %7B%0A%0A        %7D
 %0A%0A        return

check user permission