/*
 * 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.HandleAction;
import org.hisp.dhis.android.core.common.IdentifiableHandlerImpl;
import org.hisp.dhis.android.core.common.IdentifiableObjectStore;
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 java.util.List;

public class SectionHandler extends IdentifiableHandlerImpl<Section, SectionModel> {
    private final ObjectWithoutUidStore<SectionDataElementLinkModel> sectionDataElementLinkStore;

    SectionHandler(IdentifiableObjectStore<SectionModel> sectionStore,
                   ObjectWithoutUidStore<SectionDataElementLinkModel> sectionDataElementLinkStore) {
        super(sectionStore);
        this.sectionDataElementLinkStore = sectionDataElementLinkStore;
    }

    public static SectionHandler create(DatabaseAdapter databaseAdapter) {
        return new SectionHandler(
                SectionStore.create(databaseAdapter),
                SectionDataElementLinkStore.create(databaseAdapter));
    }

    @Override
    protected void afterObjectHandled(Section section, HandleAction action) {
        saveSectionDataElementLink(section);
    }

    private void saveSectionDataElementLink(Section section) {
        List<ObjectWithUid> dataElements = section.dataElements();
        if (dataElements != null) {
            SectionDataElementLinkModelBuilder builder =
                    new SectionDataElementLinkModelBuilder(section);
            for (ObjectWithUid dataElement : dataElements) {
                sectionDataElementLinkStore.updateOrInsertWhere(builder.buildModel(dataElement));
            }
        }
    }
}
@@ -1818,29 +1818,95 @@
 .common.
-ObjectWithUid
+LinkModelHandler;%0Aimport org.hisp.dhis.android.core.common.LinkModelHandlerImpl
 ;%0Aimport
@@ -1950,27 +1950,19 @@
 jectWith
-outUidStore
+Uid
 ;%0Aimport
@@ -2025,32 +2025,8 @@
 r;%0A%0A
-import java.util.List;%0A%0A
 publ
@@ -2106,16 +2106,17 @@
 odel%3E %7B%0A
+%0A
     priv
@@ -2125,38 +2125,48 @@
 e final 
-ObjectWithoutUidStore%3C
+LinkModelHandler%3CObjectWithUid, 
 SectionD
@@ -2208,21 +2208,23 @@
 mentLink
-Store
+Handler
 ;%0A%0A    S
@@ -2312,30 +2312,40 @@
     
-ObjectWithoutUidStore%3C
+LinkModelHandler%3CObjectWithUid, 
 Sect
@@ -2391,21 +2391,23 @@
 mentLink
-Store
+Handler
 ) %7B%0A    
@@ -2431,16 +2431,17 @@
 Store);%0A
+%0A
         
@@ -2467,21 +2467,23 @@
 mentLink
-Store
+Handler
  = secti
@@ -2499,21 +2499,23 @@
 mentLink
-Store
+Handler
 ;%0A    %7D%0A
@@ -2687,32 +2687,125 @@
                 
+new LinkModelHandlerImpl%3CObjectWithUid,%0A                        SectionDataElementLinkModel%3E(
 SectionDataEleme
@@ -2840,16 +2840,17 @@
 dapter))
+)
 ;%0A    %7D%0A
@@ -2951,20 +2951,16 @@
        s
-aveS
 ectionDa
@@ -2976,155 +2976,41 @@
 Link
-(section);%0A    %7D%0A%0A    private void saveSectionDataElementLink(Section section) %7B%0A        List%3CObjectWithUid%3E dataElements = section.dataElements();
+Handler.handleMany(section.uid(),
 %0A   
@@ -3018,56 +3018,25 @@
     
-if (dataElements != null) %7B%0A            S
+        s
 ection
-D
+.d
 ataE
@@ -3045,38 +3045,13 @@
 ment
-LinkModelBuilder builder =%0A   
+s(),%0A
     
@@ -3058,25 +3058,24 @@
             
- 
 new SectionD
@@ -3113,192 +3113,10 @@
 ion)
+)
 ;
-%0A            for (ObjectWithUid dataElement : dataElements) %7B%0A                sectionDataElementLinkStore.updateOrInsertWhere(builder.buildModel(dataElement));%0A            %7D%0A        %7D
 %0A   

Modify sections handling to use new LinkModelHanlder