/*
 * Copyright (c) 2017, University of Oslo
 *
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.hisp.dhis.android.core.dataset;

import org.hisp.dhis.android.core.common.ObjectWithUid;
import org.hisp.dhis.android.core.common.ObjectWithoutUidStore;
import org.hisp.dhis.android.core.data.database.DatabaseAdapter;
import org.hisp.dhis.android.core.indicator.DataSetIndicatorLinkModel;
import org.hisp.dhis.android.core.indicator.DataSetIndicatorLinkModelBuilder;
import org.hisp.dhis.android.core.indicator.DataSetIndicatorLinkStore;
import org.hisp.dhis.android.core.organisationunit.OrganisationUnit;

import java.util.List;
import java.util.Set;

class DataSetParentLinkManager {
    private final ObjectWithoutUidStore<DataSetDataElementLinkModel> dataSetDataElementStore;
    private final ObjectWithoutUidStore<DataSetOrganisationUnitLinkModel> dataSetOrganisationUnitStore;
    private final ObjectWithoutUidStore<DataSetIndicatorLinkModel> dataSetIndicatorStore;

    DataSetParentLinkManager(
            ObjectWithoutUidStore<DataSetDataElementLinkModel> dataSetDataElementStore,
            ObjectWithoutUidStore<DataSetOrganisationUnitLinkModel> dataSetOrganisationUnitStore,
            ObjectWithoutUidStore<DataSetIndicatorLinkModel> dataSetIndicatorStore) {
        this.dataSetDataElementStore = dataSetDataElementStore;
        this.dataSetOrganisationUnitStore = dataSetOrganisationUnitStore;
        this.dataSetIndicatorStore = dataSetIndicatorStore;
    }

    static DataSetParentLinkManager create(DatabaseAdapter databaseAdapter) {
        return new DataSetParentLinkManager(
                DataSetDataElementLinkStore.create(databaseAdapter),
                DataSetOrganisationUnitLinkStore.create(databaseAdapter),
                DataSetIndicatorLinkStore.create(databaseAdapter));
    }

    void saveDataSetDataElementAndIndicatorLinks(List<DataSet> dataSets) {
        for (DataSet dataSet : dataSets) {
            saveDataSetDataElementLink(dataSet);
            saveDataSetIndicatorLink(dataSet);
        }
    }

    private void saveDataSetDataElementLink(DataSet dataSet) {
        List<DataSetElement> dataSetElements = dataSet.dataSetElements();
        if (dataSetElements != null) {
            DataSetDataElementLinkModelBuilder builder = new DataSetDataElementLinkModelBuilder(dataSet);
            for (DataSetElement dataSetDataElement : dataSetElements) {
                dataSetDataElementStore.updateOrInsertWhere(builder.buildModel(dataSetDataElement));
            }
        }
    }

    private void saveDataSetIndicatorLink(DataSet dataSet) {
        List<ObjectWithUid> indicators = dataSet.indicators();
        if (indicators != null) {
            DataSetIndicatorLinkModelBuilder builder = new DataSetIndicatorLinkModelBuilder(dataSet);
            for (ObjectWithUid indicator : indicators) {
                dataSetIndicatorStore.updateOrInsertWhere(builder.buildModel(indicator));
            }
        }
    }

    void saveDataSetOrganisationUnitLinks(List<OrganisationUnit> organisationUnits, Set<String> dataSetUids) {
        if (organisationUnits != null) {
            for (OrganisationUnit organisationUnit : organisationUnits) {
                saveDataSetOrganisationUnitLink(organisationUnit, dataSetUids);
            }
        }
    }

    private void saveDataSetOrganisationUnitLink(OrganisationUnit organisationUnit, Set<String> dataSetUids) {
        List<DataSet> orgUnitDataSets = organisationUnit.dataSets();
        if (orgUnitDataSets != null) {
            DataSetOrganisationUnitLinkModelBuilder builder =
                    new DataSetOrganisationUnitLinkModelBuilder(organisationUnit);
            for (DataSet dataSet : orgUnitDataSets) {
                if (dataSetUids.contains(dataSet.uid())) {
                    dataSetOrganisationUnitStore.updateOrInsertWhere(builder.buildModel(dataSet));
                }
            }
        }
    }
}
@@ -1998,77 +1998,8 @@
 ore;
-%0Aimport org.hisp.dhis.android.core.organisationunit.OrganisationUnit;
 %0A%0Aim
@@ -2022,30 +2022,8 @@
 ist;
-%0Aimport java.util.Set;
 %0A%0Acl
@@ -2151,112 +2151,8 @@
 re;%0A
-    private final ObjectWithoutUidStore%3CDataSetOrganisationUnitLinkModel%3E dataSetOrganisationUnitStore;%0A
     
@@ -2360,106 +2360,8 @@
 re,%0A
-            ObjectWithoutUidStore%3CDataSetOrganisationUnitLinkModel%3E dataSetOrganisationUnitStore,%0A
     
@@ -2510,82 +2510,8 @@
 re;%0A
-        this.dataSetOrganisationUnitStore = dataSetOrganisationUnitStore;%0A
     
@@ -2769,82 +2769,8 @@
 r),%0A
-                DataSetOrganisationUnitLinkStore.create(databaseAdapter),%0A
     
@@ -3870,24 +3870,24 @@
 dicators) %7B%0A
+
             
@@ -3997,968 +3997,6 @@
    %7D
-%0A%0A    void saveDataSetOrganisationUnitLinks(List%3COrganisationUnit%3E organisationUnits, Set%3CString%3E dataSetUids) %7B%0A        if (organisationUnits != null) %7B%0A            for (OrganisationUnit organisationUnit : organisationUnits) %7B%0A                saveDataSetOrganisationUnitLink(organisationUnit, dataSetUids);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void saveDataSetOrganisationUnitLink(OrganisationUnit organisationUnit, Set%3CString%3E dataSetUids) %7B%0A        List%3CDataSet%3E orgUnitDataSets = organisationUnit.dataSets();%0A        if (orgUnitDataSets != null) %7B%0A            DataSetOrganisationUnitLinkModelBuilder builder =%0A                    new DataSetOrganisationUnitLinkModelBuilder(organisationUnit);%0A            for (DataSet dataSet : orgUnitDataSets) %7B%0A                if (dataSetUids.contains(dataSet.uid())) %7B%0A                    dataSetOrganisationUnitStore.updateOrInsertWhere(builder.buildModel(dataSet));%0A                %7D%0A            %7D%0A        %7D%0A    %7D
 %0A%7D

remove saveDataSetOrgUnitLinks method from DataSetParentLinkManager