package org.hisp.dhis.trackedentity.action.programstage;

/*
 * Copyright (c) 2004-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.
 */

import com.opensymphony.xwork2.Action;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementService;
import org.hisp.dhis.program.ProgramIndicator;
import org.hisp.dhis.program.ProgramIndicatorService;
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.program.ProgramStageSection;
import org.hisp.dhis.program.ProgramStageService;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * @author Chau Thu Tran
 * @version AddProgramStageSectionAction.java 11:29:40 AM Aug 22, 2012 $
 */
public class AddProgramStageSectionAction
    implements Action
{
    // -------------------------------------------------------------------------
    // Dependencies
    // -------------------------------------------------------------------------

    private ProgramStageService programStageService;

    public void setProgramStageService( ProgramStageService programStageService )
    {
        this.programStageService = programStageService;
    }

    private DataElementService dataElementService;

    public void setDataElementService( DataElementService dataElementService )
    {
        this.dataElementService = dataElementService;
    }

    @Autowired
    private ProgramIndicatorService programIndicatorService;

    // -------------------------------------------------------------------------
    // Input/Output
    // -------------------------------------------------------------------------

    private Integer programStageId;

    public Integer getProgramStageId()
    {
        return programStageId;
    }

    public void setProgramStageId( Integer programStageId )
    {
        this.programStageId = programStageId;
    }

    private String name;

    public void setName( String name )
    {
        this.name = name;
    }

    private List<Integer> dataElementIds;

    public void setDataElementIds( List<Integer> dataElementIds )
    {
        this.dataElementIds = dataElementIds;
    }

    private List<Integer> selectedIndicators = new ArrayList<>();

    public void setSelectedIndicators( List<Integer> selectedIndicators )
    {
        this.selectedIndicators = selectedIndicators;
    }

    // -------------------------------------------------------------------------
    // Action implementation
    // -------------------------------------------------------------------------

    @Override
    public String execute()
        throws Exception
    {
        ProgramStage programStage = programStageService.getProgramStage( programStageId );

        // ---------------------------------------------------------------------
        // Section
        // ---------------------------------------------------------------------

        List<DataElement> dataElements = new ArrayList<>();
        
        for ( Integer id : dataElementIds )
        {
            dataElements.add( dataElementService.getDataElement( id ) );
        }
        
        ProgramStageSection programStageSection = new ProgramStageSection( StringUtils.trimToNull( name ), 
            dataElements, programStage.getProgramStageSections().size() );
        programStageSection.setAutoFields();



        // ---------------------------------------------------------------------
        // Program indicators
        // ---------------------------------------------------------------------

        List<ProgramIndicator> programIndicators = new ArrayList<>();

        for ( Integer id : selectedIndicators )
        {
            ProgramIndicator indicator = programIndicatorService.getProgramIndicator( id );
            programIndicators.add( indicator );
        }

        programStageSection.setProgramIndicators( programIndicators );
        programStageSection.setProgramStage( programStage );

        // ---------------------------------------------------------------------
        // Update program stage
        // ---------------------------------------------------------------------

        Set<ProgramStageSection> sections = programStage.getProgramStageSections();
        sections.add( programStageSection );

        programStage.setProgramStageSections( sections );
        programStageService.updateProgramStage( programStage );

        return SUCCESS;
    }
}

@@ -2016,32 +2016,90 @@
 mStageSection;%0D%0A
+import org.hisp.dhis.program.ProgramStageSectionService;%0D%0A
 import org.hisp.
@@ -3136,24 +3136,110 @@
 Service;%0D%0A%0D%0A
+    @Autowired%0D%0A    private ProgramStageSectionService programStageSectionService;%0D%0A%0D%0A
     // -----
@@ -5787,32 +5787,116 @@
 programStage );%0D
+%0A        programStageSectionService.saveProgramStageSection( programStageSection );%0D
 %0A%0D%0A        // --

Fix bug cannot add new ProgramStageSection. (#1006)