gt:         if (expectedStudyList != null && !expectedStudyList.isEmpty()) {
context: /*****************************************************************
   * This file is part of Managing Agricultural Research for Learning &
   * Outcomes Platform (MARLO).
   * MARLO is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * at your option) any later version.
   * MARLO is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   * You should have received a copy of the GNU General Public License
   * along with MARLO. If not, see <http://www.gnu.org/licenses/>.
   *****************************************************************/
  
  package org.cgiar.ccafs.marlo.action.projects;
  
  import org.cgiar.ccafs.marlo.action.BaseAction;
  import org.cgiar.ccafs.marlo.config.APConstants;
  import org.cgiar.ccafs.marlo.data.manager.AuditLogManager;
  import org.cgiar.ccafs.marlo.data.manager.CgiarCrossCuttingMarkerManager;
  import org.cgiar.ccafs.marlo.data.manager.CrpMilestoneManager;
  import org.cgiar.ccafs.marlo.data.manager.GlobalUnitManager;
  import org.cgiar.ccafs.marlo.data.manager.InstitutionManager;
  import org.cgiar.ccafs.marlo.data.manager.LocElementManager;
  import org.cgiar.ccafs.marlo.data.manager.PhaseManager;
  import org.cgiar.ccafs.marlo.data.manager.PolicyMilestoneManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectExpectedStudyManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectExpectedStudyPolicyManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectInnovationManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectInnovationSharedManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyCenterManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyCountryManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyCrossCuttingMarkerManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyCrpManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyGeographicScopeManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyInfoManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyInnovationManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyOwnerManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicyRegionManager;
  import org.cgiar.ccafs.marlo.data.manager.ProjectPolicySubIdoManager;
  import org.cgiar.ccafs.marlo.data.manager.RepIndGenderYouthFocusLevelManager;
  import org.cgiar.ccafs.marlo.data.manager.RepIndGeographicScopeManager;
  import org.cgiar.ccafs.marlo.data.manager.RepIndOrganizationTypeManager;
  import org.cgiar.ccafs.marlo.data.manager.RepIndPolicyInvestimentTypeManager;
  import org.cgiar.ccafs.marlo.data.manager.RepIndPolicyTypeManager;
  import org.cgiar.ccafs.marlo.data.manager.RepIndStageProcessManager;
  import org.cgiar.ccafs.marlo.data.manager.SrfIdoManager;
  import org.cgiar.ccafs.marlo.data.manager.SrfSubIdoManager;
  import org.cgiar.ccafs.marlo.data.model.CgiarCrossCuttingMarker;
  import org.cgiar.ccafs.marlo.data.model.CrpMilestone;
  import org.cgiar.ccafs.marlo.data.model.ExpectedStudyProject;
  import org.cgiar.ccafs.marlo.data.model.GlobalUnit;
  import org.cgiar.ccafs.marlo.data.model.Institution;
  import org.cgiar.ccafs.marlo.data.model.LocElement;
  import org.cgiar.ccafs.marlo.data.model.Phase;
  import org.cgiar.ccafs.marlo.data.model.PolicyMilestone;
  import org.cgiar.ccafs.marlo.data.model.Project;
  import org.cgiar.ccafs.marlo.data.model.ProjectExpectedStudy;
  import org.cgiar.ccafs.marlo.data.model.ProjectExpectedStudyPolicy;
  import org.cgiar.ccafs.marlo.data.model.ProjectInnovation;
  import org.cgiar.ccafs.marlo.data.model.ProjectInnovationShared;
  import org.cgiar.ccafs.marlo.data.model.ProjectMilestone;
  import org.cgiar.ccafs.marlo.data.model.ProjectOutcome;
  import org.cgiar.ccafs.marlo.data.model.ProjectPartner;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicy;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicyCenter;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicyCountry;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicyCrossCuttingMarker;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicyCrp;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicyGeographicScope;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicyInnovation;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicyOwner;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicyRegion;
  import org.cgiar.ccafs.marlo.data.model.ProjectPolicySubIdo;
  import org.cgiar.ccafs.marlo.data.model.RepIndGenderYouthFocusLevel;
  import org.cgiar.ccafs.marlo.data.model.RepIndGeographicScope;
  import org.cgiar.ccafs.marlo.data.model.RepIndOrganizationType;
  import org.cgiar.ccafs.marlo.data.model.RepIndPolicyInvestimentType;
  import org.cgiar.ccafs.marlo.data.model.RepIndPolicyType;
  import org.cgiar.ccafs.marlo.data.model.RepIndStageProcess;
  import org.cgiar.ccafs.marlo.data.model.SrfIdo;
  import org.cgiar.ccafs.marlo.data.model.SrfSubIdo;
  import org.cgiar.ccafs.marlo.security.Permission;
  import org.cgiar.ccafs.marlo.utils.APConfig;
  import org.cgiar.ccafs.marlo.utils.AutoSaveReader;
  import org.cgiar.ccafs.marlo.validation.projects.ProjectPolicyValidator;
  
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.List;
  import java.util.stream.Collectors;
  
  import javax.inject.Inject;
  
  import com.google.gson.Gson;
  import com.google.gson.GsonBuilder;
  import com.google.gson.JsonObject;
  import org.apache.commons.lang3.StringUtils;
  
  /**
   * @author Sebastian Amariles - CIAT/CCAFS
   */
  public class ProjectPolicyAction extends BaseAction {
  
    private static final long serialVersionUID = 597647662288518417L;
  
    // Managers
    private GlobalUnitManager globalUnitManager;
    private ProjectPolicyManager projectPolicyManager;
    private ProjectPolicyInfoManager projectPolicyInfoManager;
    private ProjectManager projectManager;
    private PhaseManager phaseManager;
    private RepIndGeographicScopeManager repIndGeographicScopeManager;
    private LocElementManager locElementManager;
    private RepIndGenderYouthFocusLevelManager focusLevelManager;
    private RepIndOrganizationTypeManager repIndOrganizationTypeManager;
    private RepIndPolicyInvestimentTypeManager repIndPolicyInvestimentTypeManager;
    private RepIndStageProcessManager repIndStageProcessManager;
    private RepIndPolicyTypeManager repIndPolicyTypeManager;
    private SrfSubIdoManager srfSubIdoManager;
    private AuditLogManager auditLogManager;
    private ProjectExpectedStudyManager projectExpectedStudyManager;
    private CgiarCrossCuttingMarkerManager cgiarCrossCuttingMarkerManager;
    private ProjectPolicyCountryManager projectPolicyCountryManager;
    private ProjectPolicyOwnerManager projectPolicyOwnerManager;
    private ProjectPolicyCrpManager projectPolicyCrpManager;
    private ProjectPolicySubIdoManager projectPolicySubIdoManager;
    private ProjectPolicyCrossCuttingMarkerManager projectPolicyCrossCuttingMarkerManager;
    private ProjectExpectedStudyPolicyManager projectExpectedStudyPolicyManager;
    private ProjectPolicyInnovationManager projectPolicyInnovationManager;
    private ProjectInnovationManager projectInnovationManager;
    private ProjectPolicyGeographicScopeManager projectPolicyGeographicScopeManager;
    private ProjectPolicyRegionManager projectPolicyRegionManager;
    private PolicyMilestoneManager policyMilestoneManager;
    private CrpMilestoneManager crpMilestoneManager;
    private ProjectPolicyValidator validator;
    private ProjectPolicyCenterManager projectPolicyCenterManager;
    private InstitutionManager institutionManager;
    private SrfIdoManager srfIdoManager;
    private ProjectInnovationSharedManager projectInnovationSharedManager;
  
    // Variables
    private GlobalUnit loggedCrp;
    private Project project;
    private long projectID;
    private long policyID;
    private long subIdoPrimaryId;
    private long srfSubIdoPrimary;
    private long milestonePrimaryId;
    private long crpMilestonePrimary;
    private ProjectPolicy policy;
    private ProjectPolicy policyDB;
    private List<RepIndGeographicScope> geographicScopes;
    private List<LocElement> regions;
    private List<RepIndOrganizationType> organizationTypes;
    private List<RepIndGenderYouthFocusLevel> focusLevels;
    private List<RepIndPolicyInvestimentType> policyInvestimentTypes;
    private List<RepIndStageProcess> stageProcesses;
    private List<RepIndPolicyType> policyTypes;
    private List<LocElement> countries;
    private List<SrfSubIdo> subIdos;
    private List<SrfSubIdo> principalSubIdo;
    private List<GlobalUnit> crps;
    private List<ProjectExpectedStudy> expectedStudyList;
    private List<CgiarCrossCuttingMarker> cgiarCrossCuttingMarkers;
    private List<ProjectInnovation> innovationList;
    private List<CrpMilestone> milestoneList;
    private List<SrfIdo> srfIdos;
  
    private String transaction;
    private List<Institution> centers;
    private HashMap<Long, String> idoList;
  
    @Inject
    public ProjectPolicyAction(APConfig config, GlobalUnitManager globalUnitManager,
      ProjectPolicyManager projectPolicyManager, ProjectPolicyInfoManager projectPolicyInfoManager,
      ProjectManager projectManager, PhaseManager phaseManager, RepIndGeographicScopeManager repIndGeographicScopeManager,
      LocElementManager locElementManager, RepIndGenderYouthFocusLevelManager focusLevelManager,
      RepIndOrganizationTypeManager repIndOrganizationTypeManager,
      RepIndPolicyInvestimentTypeManager repIndPolicyInvestimentTypeManager,
      RepIndStageProcessManager repIndStageProcessManager, RepIndPolicyTypeManager repIndPolicyTypeManager,
      SrfSubIdoManager srfSubIdoManager, AuditLogManager auditLogManager,
      ProjectExpectedStudyManager projectExpectedStudyManager,
      CgiarCrossCuttingMarkerManager cgiarCrossCuttingMarkerManager,
      ProjectPolicyCountryManager projectPolicyCountryManager, ProjectPolicyOwnerManager projectPolicyOwnerManager,
      ProjectPolicyCrpManager projectPolicyCrpManager, ProjectPolicySubIdoManager projectPolicySubIdoManager,
      ProjectPolicyCrossCuttingMarkerManager projectPolicyCrossCuttingMarkerManager, ProjectPolicyValidator validator,
      ProjectExpectedStudyPolicyManager projectExpectedStudyPolicyManager,
      ProjectInnovationManager projectInnovationManager, ProjectPolicyInnovationManager projectPolicyInnovationManager,
      ProjectPolicyGeographicScopeManager projectPolicyGeographicScopeManager,
      ProjectPolicyRegionManager projectPolicyRegionManager, PolicyMilestoneManager policyMilestoneManager,
      CrpMilestoneManager crpMilestoneManager, ProjectPolicyCenterManager projectPolicyCenterManager,
      InstitutionManager institutionManager, SrfIdoManager srfIdoManager,
      ProjectInnovationSharedManager projectInnovationSharedManager) {
      super(config);
      this.globalUnitManager = globalUnitManager;
      this.projectPolicyManager = projectPolicyManager;
      this.projectPolicyInfoManager = projectPolicyInfoManager;
      this.projectManager = projectManager;
      this.phaseManager = phaseManager;
      this.repIndGeographicScopeManager = repIndGeographicScopeManager;
      this.locElementManager = locElementManager;
      this.focusLevelManager = focusLevelManager;
      this.repIndOrganizationTypeManager = repIndOrganizationTypeManager;
      this.repIndPolicyInvestimentTypeManager = repIndPolicyInvestimentTypeManager;
      this.repIndStageProcessManager = repIndStageProcessManager;
      this.repIndPolicyTypeManager = repIndPolicyTypeManager;
      this.srfSubIdoManager = srfSubIdoManager;
      this.auditLogManager = auditLogManager;
      this.projectExpectedStudyManager = projectExpectedStudyManager;
      this.cgiarCrossCuttingMarkerManager = cgiarCrossCuttingMarkerManager;
      this.projectPolicyCountryManager = projectPolicyCountryManager;
      this.projectPolicyOwnerManager = projectPolicyOwnerManager;
      this.projectPolicyCrpManager = projectPolicyCrpManager;
      this.projectPolicySubIdoManager = projectPolicySubIdoManager;
      this.projectPolicyCrossCuttingMarkerManager = projectPolicyCrossCuttingMarkerManager;
      this.validator = validator;
      this.projectExpectedStudyPolicyManager = projectExpectedStudyPolicyManager;
      this.projectInnovationManager = projectInnovationManager;
      this.projectPolicyInnovationManager = projectPolicyInnovationManager;
      this.projectPolicyGeographicScopeManager = projectPolicyGeographicScopeManager;
      this.projectPolicyRegionManager = projectPolicyRegionManager;
      this.policyMilestoneManager = policyMilestoneManager;
      this.crpMilestoneManager = crpMilestoneManager;
      this.projectPolicyCenterManager = projectPolicyCenterManager;
      this.institutionManager = institutionManager;
      this.srfIdoManager = srfIdoManager;
      this.projectInnovationSharedManager = projectInnovationSharedManager;
    }
  
    /**
     * Delete all LocElements Records when Geographic Scope is Global or NULL
     * 
     * @param policy
     * @param phase
     */
    public void deleteLocElements(ProjectPolicy policy, Phase phase, boolean isCountry) {
      if (isCountry) {
        if (policy.getProjectPolicyCountries() != null && policy.getProjectPolicyCountries().size() > 0) {
  
          List<ProjectPolicyCountry> regionPrev = new ArrayList<>(policy.getProjectPolicyCountries().stream()
            .filter(nu -> nu.isActive() && nu.getPhase().getId().equals(phase.getId())).collect(Collectors.toList()));
  
          for (ProjectPolicyCountry policyRegion : regionPrev) {
  
            projectPolicyCountryManager.deleteProjectPolicyCountry(policyRegion.getId());
  
          }
        }
      } else {
        if (policy.getProjectPolicyRegions() != null && policy.getProjectPolicyRegions().size() > 0) {
  
          List<ProjectPolicyRegion> regionPrev = new ArrayList<>(policy.getProjectPolicyRegions().stream()
            .filter(nu -> nu.isActive() && nu.getPhase().getId().equals(phase.getId())).collect(Collectors.toList()));
  
          for (ProjectPolicyRegion policyRegion : regionPrev) {
  
            projectPolicyRegionManager.deleteProjectPolicyRegion(policyRegion.getId());
  
          }
  
        }
      }
    }
  
    /**
     * The name of the autosave file is constructed and the path is searched
     * 
     * @return Auto save file path
     */
    private Path getAutoSaveFilePath() {
      // get the class simple name
      String composedClassName = policy.getClass().getSimpleName();
      // get the action name and replace / for _
      String actionFile = this.getActionName().replace("/", "_");
      // concatenate name and add the .json extension
      String autoSaveFile = policy.getId() + "_" + composedClassName + "_" + this.getActualPhase().getName() + "_"
        + this.getActualPhase().getYear() + "_" + actionFile + ".json";
      return Paths.get(config.getAutoSaveFolder() + autoSaveFile);
    }
  
    public List<Institution> getCenters() {
      return centers;
    }
  
    public List<CgiarCrossCuttingMarker> getCgiarCrossCuttingMarkers() {
      return cgiarCrossCuttingMarkers;
    }
  
    public List<LocElement> getCountries() {
      return countries;
    }
  
    public long getCrpMilestonePrimary() {
      return crpMilestonePrimary;
    }
  
    public List<GlobalUnit> getCrps() {
      return crps;
    }
  
    public List<ProjectExpectedStudy> getExpectedStudyList() {
      return expectedStudyList;
    }
  
    public List<RepIndGenderYouthFocusLevel> getFocusLevels() {
      return focusLevels;
    }
  
    public List<RepIndGeographicScope> getGeographicScopes() {
      return geographicScopes;
    }
  
    public List<ProjectInnovation> getInnovationList() {
      return innovationList;
    }
  
    public GlobalUnit getLoggedCrp() {
      return loggedCrp;
    }
  
    public List<CrpMilestone> getMilestoneList() {
      return milestoneList;
    }
  
    public long getMilestonePrimaryId() {
      return milestonePrimaryId;
    }
  
    public List<RepIndOrganizationType> getOrganizationTypes() {
      return organizationTypes;
    }
  
    public ProjectPolicy getPolicy() {
      return policy;
    }
  
    /**
     * Get the information for the Cross Cutting marker in the form
     * 
     * @param markerID
     * @return
     */
    public ProjectPolicyCrossCuttingMarker getPolicyCrossCuttingMarker(long markerID) {
      ProjectPolicyCrossCuttingMarker crossCuttingMarker = new ProjectPolicyCrossCuttingMarker();
      if (this.isDraft()) {
        // Cgiar Cross Cutting Markers Autosave
        if (policy.getCrossCuttingMarkers() != null) {
          for (ProjectPolicyCrossCuttingMarker projectPolicyCrossCuttingMarker : policy.getCrossCuttingMarkers()) {
            if (projectPolicyCrossCuttingMarker.getCgiarCrossCuttingMarker().getId() == markerID) {
              crossCuttingMarker = projectPolicyCrossCuttingMarker;
            }
          }
        }
      } else {
        crossCuttingMarker = projectPolicyCrossCuttingMarkerManager.getPolicyCrossCountryMarkerId(policyID, markerID,
          this.getActualPhase().getId());
      }
      if (crossCuttingMarker != null) {
        return crossCuttingMarker;
      } else {
        return null;
      }
    }
  
    public long getPolicyID() {
      return policyID;
    }
  
    public List<RepIndPolicyInvestimentType> getPolicyInvestimentTypes() {
      return policyInvestimentTypes;
    }
  
    public List<RepIndPolicyType> getPolicyTypes() {
      return policyTypes;
    }
  
    public List<SrfSubIdo> getPrincipalSubIdo() {
      return principalSubIdo;
    }
  
    public Project getProject() {
      return project;
    }
  
    public long getProjectID() {
      return projectID;
    }
  
    public List<LocElement> getRegions() {
      return regions;
    }
  
    public List<SrfIdo> getSrfIdos() {
      return srfIdos;
    }
  
    public long getSrfSubIdoPrimary() {
      return srfSubIdoPrimary;
    }
  
    public List<RepIndStageProcess> getStageProcesses() {
      return stageProcesses;
    }
  
    public long getSubIdoPrimaryId() {
      return subIdoPrimaryId;
    }
  
    public List<SrfSubIdo> getSubIdos() {
      return subIdos;
    }
  
    public String getTransaction() {
      return transaction;
    }
  
    @Override
    public void prepare() throws Exception {
  
      loggedCrp = (GlobalUnit) this.getSession().get(APConstants.SESSION_CRP);
      loggedCrp = globalUnitManager.getGlobalUnitById(loggedCrp.getId());
  
      policyID = Integer.parseInt(StringUtils.trim(this.getRequest().getParameter(APConstants.POLICY_REQUEST_ID)));
  
      if (this.getRequest().getParameter(APConstants.TRANSACTION_ID) != null) {
  
        transaction = StringUtils.trim(this.getRequest().getParameter(APConstants.TRANSACTION_ID));
        ProjectPolicy history = (ProjectPolicy) auditLogManager.getHistory(transaction);
  
        if (history != null) {
          policy = history;
        } else {
          this.transaction = null;
  
          this.setTransaction("-1");
        }
        if (policy.getProjectPolicyInfo() == null) {
          policy.getProjectPolicyInfo(this.getActualPhase());
        }
        // load relations
        if (policy.getProjectPolicyInfo() != null) {
  
          // load Policy Investment Type
          if (policy.getProjectPolicyInfo().getRepIndPolicyInvestimentType() != null
            && policy.getProjectPolicyInfo().getRepIndPolicyInvestimentType().getId() != null) {
            policy.getProjectPolicyInfo()
              .setRepIndPolicyInvestimentType(repIndPolicyInvestimentTypeManager.getRepIndPolicyInvestimentTypeById(
                policy.getProjectPolicyInfo().getRepIndPolicyInvestimentType().getId()));
          }
  
          // load Level Of Maturity Type
          if (policy.getProjectPolicyInfo().getRepIndStageProcess() != null
            && policy.getProjectPolicyInfo().getRepIndStageProcess().getId() != null) {
            policy.getProjectPolicyInfo().setRepIndStageProcess(repIndStageProcessManager
              .getRepIndStageProcessById(policy.getProjectPolicyInfo().getRepIndStageProcess().getId()));
          }
  
        }
  
      } else {
        policy = projectPolicyManager.getProjectPolicyById(policyID);
  
      }
  
      if (policy != null) {
  
        projectID = policy.getProject().getId();
        project = projectManager.getProjectById(projectID);
  
        Phase phase = phaseManager.getPhaseById(this.getActualPhase().getId());
        project.getProjecInfoPhase(phase);
  
        Path path = this.getAutoSaveFilePath();
  
        if (path.toFile().exists() && this.getCurrentUser().isAutoSave()) {
  
          // Autosave File in
          BufferedReader reader = null;
          reader = new BufferedReader(new FileReader(path.toFile()));
          Gson gson = new GsonBuilder().create();
          JsonObject jReader = gson.fromJson(reader, JsonObject.class);
          reader.close();
  
          AutoSaveReader autoSaveReader = new AutoSaveReader();
  
          policy = (ProjectPolicy) autoSaveReader.readFromJson(jReader);
  
          // Policy Geographic Scope List AutoSave
          boolean haveRegions = false;
          boolean haveCountries = false;
  
          if (policy.getGeographicScopes() != null) {
            for (ProjectPolicyGeographicScope projectPolicyGeographicScope : policy.getGeographicScopes()) {
              projectPolicyGeographicScope.setRepIndGeographicScope(repIndGeographicScopeManager
                .getRepIndGeographicScopeById(projectPolicyGeographicScope.getRepIndGeographicScope().getId()));
  
              if (projectPolicyGeographicScope.getRepIndGeographicScope().getId() == 2) {
                haveRegions = true;
              }
  
              if (projectPolicyGeographicScope.getRepIndGeographicScope().getId() != 1
                && projectPolicyGeographicScope.getRepIndGeographicScope().getId() != 2) {
                haveCountries = true;
              }
  
            }
          }
  
          if (haveRegions) {
            // Load Regions
            if (policy.getRegions() != null) {
              for (ProjectPolicyRegion projectPolicyRegion : policy.getRegions()) {
                projectPolicyRegion
                  .setLocElement(locElementManager.getLocElementById(projectPolicyRegion.getLocElement().getId()));
              }
            }
          }
  
          if (haveCountries) {
            // Load Countries
            if (policy.getCountriesIdsText() != null) {
              String[] countriesText = policy.getCountriesIdsText().replace("[", "").replace("]", "").split(",");
              List<String> countries = new ArrayList<>();
              for (String value : Arrays.asList(countriesText)) {
                countries.add(value.trim());
              }
              policy.setCountriesIds(countries);
            }
          }
  
          // Policy Type ( Whose Policy is This ? ) List Autosave
          if (policy.getOwners() != null) {
            for (ProjectPolicyOwner projectPolicyOwner : policy.getOwners()) {
              projectPolicyOwner.setRepIndPolicyType(
                repIndPolicyTypeManager.getRepIndPolicyTypeById(projectPolicyOwner.getRepIndPolicyType().getId()));
            }
          }
  
          // Centers List Autosave
          if (policy.getCenters() != null) {
            for (ProjectPolicyCenter projectPolicyCenter : policy.getCenters()) {
              projectPolicyCenter
                .setInstitution(institutionManager.getInstitutionById(projectPolicyCenter.getInstitution().getId()));
            }
          }
  
          // Crp List Autosave
          if (policy.getCrps() != null) {
            for (ProjectPolicyCrp projectPolicyCrp : policy.getCrps()) {
              projectPolicyCrp
                .setGlobalUnit(globalUnitManager.getGlobalUnitById(projectPolicyCrp.getGlobalUnit().getId()));
            }
          }
  
  
          // SubIdos List Autosave
          if (policy.getSubIdos() != null) {
            for (ProjectPolicySubIdo projectPolicySubIdo : policy.getSubIdos()) {
              projectPolicySubIdo
                .setSrfSubIdo(srfSubIdoManager.getSrfSubIdoById(projectPolicySubIdo.getSrfSubIdo().getId()));
            }
          }
  
          // Innovations List Autosave
          if (policy.getInnovations() != null) {
            for (ProjectPolicyInnovation projectPolicyInnovation : policy.getInnovations()) {
              projectPolicyInnovation.setProjectInnovation(projectInnovationManager
                .getProjectInnovationById(projectPolicyInnovation.getProjectInnovation().getId()));
            }
          }
  
          // Milestones List Autosave
          if (policy.getMilestones() != null) {
            for (PolicyMilestone policyMilestone : policy.getMilestones()) {
              policyMilestone
                .setCrpMilestone(crpMilestoneManager.getCrpMilestoneById(policyMilestone.getCrpMilestone().getId()));
            }
          }
  
          // Evidences List Autosave
          if (policy.getEvidences() != null) {
            for (ProjectExpectedStudyPolicy projectPolicyEvidence : policy.getEvidences()) {
              projectPolicyEvidence.setProjectExpectedStudy(projectExpectedStudyManager
                .getProjectExpectedStudyById(projectPolicyEvidence.getProjectExpectedStudy().getId()));
            }
          }
  
          // Cgiar Cross Cutting Markers Autosave
          if (policy.getCrossCuttingMarkers() != null) {
            for (ProjectPolicyCrossCuttingMarker projectPolicyCrossCuttingMarker : policy.getCrossCuttingMarkers()) {
              projectPolicyCrossCuttingMarker.setCgiarCrossCuttingMarker(cgiarCrossCuttingMarkerManager
                .getCgiarCrossCuttingMarkerById(projectPolicyCrossCuttingMarker.getCgiarCrossCuttingMarker().getId()));
              if (projectPolicyCrossCuttingMarker.getRepIndGenderYouthFocusLevel() != null) {
                if (projectPolicyCrossCuttingMarker.getRepIndGenderYouthFocusLevel().getId() != -1) {
                  projectPolicyCrossCuttingMarker
                    .setRepIndGenderYouthFocusLevel(focusLevelManager.getRepIndGenderYouthFocusLevelById(
                      projectPolicyCrossCuttingMarker.getRepIndGenderYouthFocusLevel().getId()));
                }
              }
            }
          }
  
          this.setDraft(true);
  
        } else {
          this.setDraft(false);
  
          if (policy.getProjectPolicyInfo() == null) {
            policy.getProjectPolicyInfo(phase);
          }
  
          // Setup Geographic Scope
          if (policy.getProjectPolicyGeographicScopes() != null) {
            policy.setGeographicScopes(new ArrayList<>(policy.getProjectPolicyGeographicScopes().stream()
              .filter(o -> o.isActive() && o.getPhase().getId().equals(phase.getId())).collect(Collectors.toList())));
          }
  
          // Policy Countries List
          if (policy.getProjectPolicyCountries() == null) {
            policy.setCountries(new ArrayList<>());
  
          } else {
            List<ProjectPolicyCountry> geographics =
              projectPolicyCountryManager.getPolicyCountrybyPhase(policy.getId(), phase.getId());
  
            // Load Countries
            policy.setCountries(geographics.stream().filter(sc -> sc.getLocElement().getLocElementType().getId() == 2)
              .collect(Collectors.toList()));
  
          }
  
          if (policy.getProjectPolicyRegions() == null) {
            policy.setRegions(new ArrayList<>());
          } else {
            List<ProjectPolicyRegion> geographics =
              projectPolicyRegionManager.getPolicyRegionbyPhase(policy.getId(), phase.getId());
            // Insitutions List Autosave
            if (policy.getCenters() != null) {
              for (ProjectPolicyCenter projectPolicyCenter : policy.getCenters()) {
                projectPolicyCenter
                  .setInstitution(institutionManager.getInstitutionById(projectPolicyCenter.getInstitution().getId()));
              }
            }
  
            // Load Regions
            policy.setRegions(geographics.stream().filter(sc -> sc.getLocElement().getLocElementType().getId() == 1)
              .collect(Collectors.toList()));
          }
  
          // Policy Type ( Whose Policy is This ? ) List
          if (policy.getProjectPolicyOwners() != null) {
            policy.setOwners(new ArrayList<>(policy.getProjectPolicyOwners().stream()
              .filter(o -> o.isActive() && o.getPhase().getId().equals(phase.getId())).collect(Collectors.toList())));
          }
  
          // Crp List
          if (policy.getProjectPolicyCrps() != null) {
            policy.setCrps(new ArrayList<>(policy.getProjectPolicyCrps().stream()
              .filter(o -> o.isActive() && o.getPhase().getId().equals(phase.getId())).collect(Collectors.toList())));
          }
  
          // SubIdos List
          if (policy.getProjectPolicySubIdos() != null) {
            policy.setSubIdos(new ArrayList<>(policy.getProjectPolicySubIdos().stream()
              .filter(o -> o.isActive() && o.getPhase().getId().equals(phase.getId())).collect(Collectors.toList())));
  
            // Get the ID of the principal Sub IDO if exist
            if (policy.getSubIdos(phase) != null) {
              List<ProjectPolicySubIdo> projectPolicies = new ArrayList<ProjectPolicySubIdo>();
  
              projectPolicies = policy.getSubIdos(phase).stream()
                .filter(p -> p != null && p.isActive() && p.getPrimary() != null && p.getPrimary())
                .collect(Collectors.toList());
  
              if (projectPolicies != null && projectPolicies.size() > 0 && projectPolicies.get(0) != null) {
                subIdoPrimaryId = projectPolicies.get(0).getSrfSubIdo().getId(); //
                srfSubIdoPrimary = projectPolicies.get(0).getSrfSubIdo().getId(); //
              }
            }
  
          }
  
          // Innovations List
          if (policy.getProjectPolicyInnovations() != null) {
            policy.setInnovations(new ArrayList<>(policy.getProjectPolicyInnovations().stream()
              .filter(o -> o.isActive() && o.getPhase().getId().equals(phase.getId())).collect(Collectors.toList())));
          }
  
          // Policy Milestones
          if (policy.getPolicyMilestones() != null) {
            policy.setMilestones(new ArrayList<>(policy.getPolicyMilestones().stream()
              .filter(o -> o.getPhase().getId().equals(phase.getId())).collect(Collectors.toList())));
  
            // Get the ID of the principal Sub IDO if exist
            if (policy.getMilestones() != null) {
              List<PolicyMilestone> projectPolicies = new ArrayList<PolicyMilestone>();
  
              projectPolicies = policy
                .getMilestones().stream().filter(p -> p != null && p.isActive() && p.getPrimary() != null
                  && p.getPrimary() && p.getPhase() != null && p.getPhase().getId().equals(phase.getId()))
                .collect(Collectors.toList());
  
              if (projectPolicies != null && projectPolicies.size() > 0 && projectPolicies.get(0) != null) {
                milestonePrimaryId = projectPolicies.get(0).getCrpMilestone().getId(); //
                crpMilestonePrimary = projectPolicies.get(0).getCrpMilestone().getId(); //
              }
            }
          }
  
          // Evidence List
          if (policy.getProjectExpectedStudyPolicies() != null) {
            policy.setEvidences(new ArrayList<>(policy.getProjectExpectedStudyPolicies().stream()
              .filter(o -> o.isActive() && o.getPhase().getId().equals(phase.getId())).collect(Collectors.toList())));
          }
  
          // Cgiar Cross Cutting Markers List
          if (policy.getCrossCuttingMarkers() != null) {
            policy.setCrossCuttingMarkers(new ArrayList<>(policy.getProjectPolicyCrossCuttingMarkers().stream()
              .filter(o -> o.isActive() && o.getPhase().getId().equals(phase.getId())).collect(Collectors.toList())));
          }
  
        }
  
        if (!this.isDraft()) {
          if (policy.getCountries() != null) {
            for (ProjectPolicyCountry country : policy.getCountries()) {
              policy.getCountriesIds().add(country.getLocElement().getIsoAlpha2());
            }
          }
        }
  
        // Getting The lists
  
        // Geographic Scope List
        geographicScopes = repIndGeographicScopeManager.findAll();
  
        // Regions for Geographic Scope Regional Selection
        regions = locElementManager.findAll().stream()
          .filter(c -> c.getLocElementType().getId().intValue() == 1 && c.isActive() && c.getIsoNumeric() != null)
          .collect(Collectors.toList());
  
        // Countries for Geographic Scope National - Multinational Selection
        countries = locElementManager.findAll().stream()
          .filter(c -> c.getLocElementType().getId().intValue() == 2 && c.isActive()).collect(Collectors.toList());
  
        // Organization Type List
        organizationTypes = repIndOrganizationTypeManager.findAll();
  
        // Level of Maturity List
        stageProcesses = new ArrayList<>(repIndStageProcessManager.findAll().stream()
          .filter(p -> p.getRepIndStageStudy() != null).collect(Collectors.toList())); // TODO change version.
  
        // Policy/Investment Type List
        policyInvestimentTypes = repIndPolicyInvestimentTypeManager.findAll();
  
        // Whose Policy is This ? List
        policyTypes = repIndPolicyTypeManager.findAll();
  
        // SubIdos List
        subIdos = srfSubIdoManager.findAll();
  
        // Cross Cutting Values List
        focusLevels = focusLevelManager.findAll();
  
        // Cross Cutting Markers
        cgiarCrossCuttingMarkers = cgiarCrossCuttingMarkerManager.findAll();
  
        // institutions
        List<Institution> centersTemp = new ArrayList<Institution>();
        List<ProjectPartner> projectPartnerList = project.getProjectPartners().stream()
          .filter(c -> c != null && c.isActive() && c.getPhase().equals(this.getActualPhase()))
          .collect(Collectors.toList());
        for (ProjectPartner projectPartner : projectPartnerList) {
          if (projectPartner.getInstitution() != null && projectPartner.getInstitution().getId() != null) {
            Institution institution = institutionManager.getInstitutionById(projectPartner.getInstitution().getId());
            if (institution != null && (institution.isPPA(this.getActualPhase().getCrp().getId(), this.getActualPhase())
              || institution.getInstitutionType().getId().longValue() == APConstants.INSTITUTION_CGIAR_CENTER_TYPE)) {
              centersTemp.add(institution);
            }
          }
        }
        centers = centersTemp;
  
        Project projectL = projectManager.getProjectById(projectID);
  
        // Get the innovations List
        innovationList = new ArrayList<>();
  
        List<ProjectInnovation> innovations =
          projectL.getProjectInnovations().stream().filter(c -> c.isActive()).collect(Collectors.toList());
        for (ProjectInnovation projectInnovation : innovations) {
          if (projectInnovation.getProjectInnovationInfo(this.getActualPhase()) != null) {
            innovationList.add(projectInnovation);
          }
        }
  
        // Institutions List
        if (policy.getProjectPolicyCenters() != null) {
          policy.setCenters(new ArrayList<>(policy.getProjectPolicyCenters().stream()
            .filter(o -> o.isActive() && o.getPhase().getId().equals(phase.getId())).collect(Collectors.toList())));
        }
  
        /*
         * Get the milestone List
         */
        milestoneList = new ArrayList<>();
  
        // Get outcomes list
        List<ProjectOutcome> projectOutcomesList = new ArrayList<>();
        projectOutcomesList = projectL.getProjectOutcomes().stream()
          .filter(
            po -> po.isActive() && po.getPhase() != null && po.getPhase().getId().equals(this.getActualPhase().getId()))
          .collect(Collectors.toList());
  
        if (projectOutcomesList != null) {
  
          for (ProjectOutcome projectOutcome : projectOutcomesList) {
            projectOutcome.setMilestones(projectOutcome.getProjectMilestones().stream()
              .filter(
                m -> m != null && m.isActive() && m.getYear() != 0 && m.getYear() <= this.getActualPhase().getYear())
              .collect(Collectors.toList()));
  
            if (projectOutcome.getMilestones() != null) {
              for (ProjectMilestone projectMilestone : projectOutcome.getMilestones()) {
                if (projectMilestone.getCrpMilestone() != null && projectMilestone.getCrpMilestone().isActive()) {
                  milestoneList.add(projectMilestone.getCrpMilestone());
                }
              }
            }
  
            // Add 'N/A' to milestone list dropdown if there is not selected any milestone
            /*
             * CrpMilestone milestoneTemp = new CrpMilestone();
             * milestoneTemp.setTitle("Not Applicable"); if(policy.getMilestones()== null) {
             * milestoneList.add(milestoneTemp); }
             */
          }
        }
  
        /*
         * Update 4/25/2019 Adding Shared Project Innovation in the lists.
         */
        List<ProjectInnovationShared> innovationsShared = projectInnovationSharedManager.findAll().stream()
          .filter(p -> p != null && p.getId() != null && p.getProject() != null && p.getProject().getId() != null
            && p.getProject().equals(this.project) && p.isActive() && p.getPhase() != null && p.getPhase().getId() != null
            && p.getPhase().equals(this.getActualPhase()) && p.getProjectInnovation() != null
            && p.getProjectInnovation().getId() != null && p.getProjectInnovation().isActive()
            && p.getProjectInnovation().getProjectInnovationInfo(this.getActualPhase()) != null)
          .collect(Collectors.toList());
        /*
         * List<ProjectInnovationShared> innovationShareds = new
         * ArrayList<>(project.getProjectInnovationShareds().stream()
         * .filter(px -> px.isActive() && px.getPhase().getId() == this.getActualPhase().getId()
         * && px.getProjectInnovation().isActive()
         * && px.getProjectInnovation().getProjectInnovationInfo(this.getActualPhase()) != null)
         * .collect(Collectors.toList()));
         */
        if (innovationsShared != null && innovationsShared.size() > 0) {
          for (ProjectInnovationShared innovationShared : innovationsShared) {
            if (!innovationList.contains(innovationShared.getProjectInnovation())) {
              this.innovationList.add(innovationShared.getProjectInnovation());
            }
          }
        }
  
        List<ProjectExpectedStudy> allProjectStudies = new ArrayList<ProjectExpectedStudy>();
  
        // Load Studies
        List<ProjectExpectedStudy> studies = project.getProjectExpectedStudies().stream()
          .filter(c -> c.isActive() && c.getProjectExpectedStudyInfo(this.getActualPhase()) != null)
          .collect(Collectors.toList());
        if (studies != null && studies.size() > 0) {
          allProjectStudies.addAll(studies);
        }
  
        // Load Shared studies
        List<ExpectedStudyProject> expectedStudyProject = new ArrayList<>(project.getExpectedStudyProjects().stream()
          .filter(px -> px.isActive() && px.getPhase().getId().equals(this.getActualPhase().getId())
            && px.getProjectExpectedStudy().isActive()
            && px.getProjectExpectedStudy().getProjectExpectedStudyInfo(this.getActualPhase()) != null)
          .collect(Collectors.toList()));
        if (expectedStudyProject != null && expectedStudyProject.size() > 0) {
          for (ExpectedStudyProject expectedStudy : expectedStudyProject) {
            if (!allProjectStudies.contains(expectedStudy.getProjectExpectedStudy())) {
              allProjectStudies.add(expectedStudy.getProjectExpectedStudy());
            }
          }
        }
  
        if (allProjectStudies != null && allProjectStudies.size() > 0) {
          // Every study of the current cycle year will be editable
          expectedStudyList = new ArrayList<ProjectExpectedStudy>();
          expectedStudyList = allProjectStudies.stream()
            .filter(ex -> ex.isActive() && ex.getProjectExpectedStudyInfo(phase) != null
              && ex.getProjectExpectedStudyInfo().getStudyType() != null
              && ex.getProjectExpectedStudyInfo().getStudyType().getId().intValue() == 1 && ex.getProject() != null)
            .collect(Collectors.toList());
        }
  
        List<ProjectExpectedStudy> evidences = projectExpectedStudyManager.findAll().stream()
          .filter(s -> s != null && s.getProject() == null && s.getProjectExpectedStudyInfo(this.getActualPhase()) != null
            && s.getProjectExpectedStudyInfo().getPhase() != null
            && s.getProjectExpectedStudyInfo().getPhase().getId().equals(this.getActualPhase().getId()))
          .collect(Collectors.toList());
        if (evidences != null && !evidences.isEmpty()) {
  