/*
 * 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<DataElementUids> dataSetElements = dataSet.dataSetElements();
        if (dataSetElements != null) {
            DataSetDataElementLinkModelBuilder builder = new DataSetDataElementLinkModelBuilder(dataSet);
            for (DataElementUids 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();
        assert orgUnitDataSets != null;
        DataSetOrganisationUnitLinkModelBuilder builder = new DataSetOrganisationUnitLinkModelBuilder(organisationUnit);
        for (DataSet dataSet : orgUnitDataSets) {
            if (dataSetUids.contains(dataSet.uid())) {
                dataSetOrganisationUnitStore.updateOrInsertWhere(builder.buildModel(dataSet));
            }
        }
    }
}
@@ -4967,15 +4967,12 @@
     
-assert 
+if (
 orgU
@@ -4990,18 +4990,24 @@
  != null
-;%0A
+) %7B%0A    
         
@@ -5047,32 +5047,52 @@
 uilder builder =
+%0A                   
  new DataSetOrga
@@ -5139,32 +5139,36 @@
 nUnit);%0A        
+    
 for (DataSet dat
@@ -5197,32 +5197,36 @@
 ) %7B%0A            
+  
+  
 if (dataSetUids.
@@ -5248,24 +5248,28 @@
 t.uid())) %7B%0A
+    
             
@@ -5347,24 +5347,42 @@
 (dataSet));%0A
+                %7D%0A
             

fix DataSetParentLinkManager