/*
 * 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.program;

import org.hisp.dhis.android.core.common.GenericHandler;
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.ObjectStyle;
import org.hisp.dhis.android.core.common.ObjectStyleHandler;
import org.hisp.dhis.android.core.common.ObjectStyleModel;
import org.hisp.dhis.android.core.common.ObjectStyleModelBuilder;
import org.hisp.dhis.android.core.data.database.DatabaseAdapter;

public class ProgramStageHandler extends IdentifiableHandlerImpl<ProgramStage, ProgramStageModel> {
    private final ProgramStageSectionHandler programStageSectionHandler;
    private final ProgramStageDataElementHandler programStageDataElementHandler;
    private final GenericHandler<ObjectStyle, ObjectStyleModel> styleHandler;

    ProgramStageHandler(IdentifiableObjectStore<ProgramStageModel> programStageStore,
                               ProgramStageSectionHandler programStageSectionHandler,
                               ProgramStageDataElementHandler programStageDataElementHandler,
                               GenericHandler<ObjectStyle, ObjectStyleModel> styleHandler) {
        super(programStageStore);
        this.programStageSectionHandler = programStageSectionHandler;
        this.programStageDataElementHandler = programStageDataElementHandler;
        this.styleHandler = styleHandler;
    }

    @Override
    protected void afterObjectHandled(ProgramStage programStage, HandleAction action) {
        programStageDataElementHandler.handleProgramStageDataElements(
                programStage.programStageDataElements());
        programStageSectionHandler.handleProgramStageSection(programStage.uid(),
                programStage.programStageSections());
        styleHandler.handle(programStage.style(),
                new ObjectStyleModelBuilder(programStage.uid(), ProgramStageModel.TABLE));
    }

    public static ProgramStageHandler create(DatabaseAdapter databaseAdapter) {
        return new ProgramStageHandler(
                ProgramStageStore.create(databaseAdapter),
                ProgramStageSectionHandler.create(databaseAdapter),
                ProgramStageDataElementHandler.create(databaseAdapter),
                ObjectStyleHandler.create(databaseAdapter));
    }
}

@@ -2070,24 +2070,140 @@
 delBuilder;%0A
+import org.hisp.dhis.android.core.common.OrphanCleaner;%0Aimport org.hisp.dhis.android.core.common.OrphanCleanerImpl;%0A
 import org.h
@@ -2587,16 +2587,214 @@
 Handler;
+%0A    private final OrphanCleaner%3CProgramStage, ProgramStageDataElement%3E programStageDataElementCleaner;%0A    private final OrphanCleaner%3CProgramStage, ProgramStageSection%3E programStageSectionCleaner;
 %0A%0A    Pr
@@ -2889,39 +2889,32 @@
                 
-    
-   
 ProgramStageSect
@@ -2944,39 +2944,32 @@
 SectionHandler,%0A
-       
                 
@@ -3063,23 +3063,16 @@
         
-   
-    
 GenericH
@@ -3121,16 +3121,226 @@
 eHandler
+,%0A                        OrphanCleaner%3CProgramStage, ProgramStageDataElement%3E programStageDataElementCleaner,%0A                        OrphanCleaner%3CProgramStage, ProgramStageSection%3E programStageSectionCleaner
 ) %7B%0A    
@@ -3555,24 +3555,172 @@
 yleHandler;%0A
+        this.programStageDataElementCleaner = programStageDataElementCleaner;%0A        this.programStageSectionCleaner = programStageSectionCleaner;%0A
     %7D%0A%0A    @
@@ -4217,24 +4217,295 @@
 el.TABLE));%0A
+        if (action == HandleAction.Update) %7B%0A            programStageDataElementCleaner.deleteOrphan(programStage, programStage.programStageDataElements());%0A            programStageSectionCleaner.deleteOrphan(programStage, programStage.programStageSections());%0A        %7D%0A
     %7D%0A%0A    p
@@ -4852,32 +4852,434 @@
 eHandler.create(
+databaseAdapter),%0A                new OrphanCleanerImpl%3CProgramStage, ProgramStageDataElement%3E(ProgramStageDataElementModel.TABLE,%0A                        ProgramStageDataElementModel.Columns.PROGRAM_STAGE, databaseAdapter),%0A                new OrphanCleanerImpl%3CProgramStage, ProgramStageSection%3E(ProgramStageSectionModel.TABLE,%0A                        ProgramStageSectionModel.Columns.PROGRAM_STAGE, 
 databaseAdapter)
@@ -5288,9 +5288,8 @@
 %0A    %7D%0A%7D
-%0A

clean orphans