/*
   * (non-Javadoc)
   * @see
   * au.org.aurin.wif.svc.ManualDemandScenarioService#updateManualDemandScenario
   * (au.org.aurin.wif.model.allocation.ManualDemandScenario, java.lang.String)
   */
  public void updateDemandOutcome(final DemandOutcome demandOutcome, final String projectId)
      throws WifInvalidInputException, WifInvalidConfigException, ParsingException {
    LOGGER.info(
        "updating ManualDemandScenario: {}, with id: {}",
        demandOutcome.getLabel(),
        demandOutcome.getId());
    try {
      if (demandOutcome.getProjectId().equals(projectId)) {
        demandOutcome.setRevision(
            demandOutcomeDao.findDemandOutcomeById(demandOutcome.getId()).getRevision());
        demandOutcomeDao.updateDemandOutcome(demandOutcome);
      } else {
        LOGGER.error(
            "illegal argument, the ManualDemandScenario supplied doesn't belong to project: "
                + projectId);
        throw new WifInvalidInputException(
            "illegal argument, the ManualDemandScenario supplied doesn't belong to project: "
                + projectId);
      }
    } catch (final IllegalArgumentException e) {

      LOGGER.error("illegal argument, the ManualDemandScenario supplied is invalid ");
      throw new WifInvalidInputException(
          "illegal argument, the ManualDemandScenario supplied is invalid ", e);
    }
  }
  public DemandOutcome createDemandOutcome(
      final List<AreaRequirement> outcome, final String projectId)
      throws WifInvalidInputException, WifInvalidConfigException {
    final DemandOutcome newDemandOutcome = new DemandOutcome();
    newDemandOutcome.setLabel("automated-manualdemand");
    LOGGER.debug("creating a new ManualDemandScenario={}", newDemandOutcome.getLabel());
    final WifProject project = projectService.getProject(projectId);
    newDemandOutcome.setProjectId(project.getId());
    for (final AreaRequirement areaRequirement : outcome) {
      // final ManualAreaRequirement manualAreaRequirement = new
      // ManualAreaRequirement();
      // manualAreaRequirement.setAllocationLU(areaRequirement.getAllocationLU());
      // manualAreaRequirement.setRequiredArea(areaRequirement.getRequiredArea());
      // manualAreaRequirement.setProjection(areaRequirement.getProjection());

      areaRequirement.setProjectionLabel(areaRequirement.getProjection().getLabel());

      newDemandOutcome.addAreaRequirenment(areaRequirement);
      LOGGER.info(
          "recreating manually required area for land use  {} in projection "
              + areaRequirement.getProjection().getLabel()
              + " is : {}",
          areaRequirement.getAllocationLU().getLabel(),
          areaRequirement.getRequiredArea());
    }

    final DemandOutcome savedDemandOutcome =
        demandOutcomeDao.persistDemandOutcome(newDemandOutcome);
    LOGGER.debug("returning the ManualDemandScenario with id={}", savedDemandOutcome.getId());
    return savedDemandOutcome;
  }
  /*
   * (non-Javadoc)
   * @see au.org.aurin.wif.svc.suitability.ManualDemandScenarioService#
   * createManualDemandScenario
   * (au.org.aurin.wif.model.suitability.ManualDemandScenario, java.lang.String)
   */
  public DemandOutcome createDemandOutcomeNew(
      final DemandOutcome demandOutcome, final String projectId)
      throws WifInvalidInputException, WifInvalidConfigException, ParsingException,
          IncompleteDemandOutcomeException {
    if (demandOutcome == null) {
      LOGGER.error("createManualDemandScenario failed: ManualDemandScenario is null or invalid");
      throw new WifInvalidInputException(
          "createManualDemandScenario failed: ManualDemandScenario is null or invalid");
    }
    final WifProject project = projectService.getProject(projectId);
    // final ManualDemandConfig manualdemandConfig = manualdemandConfigService
    // .getManualDemandConfig(projectId);
    final DemandConfig demandConfig = demandConfigService.getDemandConfig(projectId);
    project.setDemandConfig(demandConfig);
    // project.setManualDemandConfig(manualdemandConfig);
    try {
      // ManualDemandScenario = ManualDemandScenarioParser.parse(
      // ManualDemandScenario, manualdemandConfig, project);
    } catch (final Exception e) {
      LOGGER.error("Parsing new scenario failed", e);
      throw new IncompleteDemandOutcomeException("Parsing new scenario failed", e);
    }
    LOGGER.debug("persisting the DemandOutcome={}", demandOutcome.getLabel());
    demandOutcome.setProjectId(projectId);
    final DemandOutcome savedDemandOutcome = demandOutcomeDao.persistDemandOutcome(demandOutcome);
    LOGGER.debug("returning the ManualDemandScenario with id={}", savedDemandOutcome.getId());
    project.getDemandOutcomesMap().put(savedDemandOutcome.getId(), savedDemandOutcome.getLabel());
    wifProjectDao.updateProject(project);

    return savedDemandOutcome;
  }
 /*
  * (non-Javadoc)
  * @see
  * au.org.aurin.wif.svc.ManualDemandScenarioService#deleteManualDemandScenario
  * (java.lang.String, java.lang.String)
  */
 public void deleteDemandOutcome(final String id, final String projectId)
     throws WifInvalidInputException, WifInvalidConfigException, ParsingException {
   LOGGER.info("deleting the ManualDemandScenario with ID={}", id);
   final DemandOutcome demandOutcome = demandOutcomeDao.findDemandOutcomeById(id);
   if (demandOutcome.getProjectId().equals(projectId)) {
     // Deleting associated area requirements
     final List<AreaRequirement> requirements =
         areaRequirementDao.getAreaRequirements(demandOutcome.getId());
     for (final AreaRequirement areaRequirement : requirements) {
       areaRequirementDao.deleteAreaRequirement(areaRequirement);
     }
     demandOutcomeDao.deleteDemandOutcome(demandOutcome);
     final WifProject project = projectService.getProject(projectId);
     project.getDemandOutcomesMap().remove(id);
     wifProjectDao.updateProject(project);
   } else {
     LOGGER.error(
         "illegal argument, the ManualDemandScenario supplied doesn't belong to project: "
             + projectId);
     throw new WifInvalidInputException(
         "illegal argument, the ManualDemandScenario supplied doesn't belong to project: "
             + projectId);
   }
 }
  /**
   * Gets the suitability scenario.
   *
   * @param id the id
   * @return the suitability scenario
   * @throws WifInvalidInputException the wif invalid input exception
   * @throws WifInvalidConfigException the wif invalid config exception
   * @throws ParsingException the parsing exception
   */
  public DemandOutcome getDemandOutcome(final String id)
      throws WifInvalidInputException, WifInvalidConfigException, ParsingException {

    LOGGER.debug("getting the ManualDemandScenario with ID={}", id);
    try {
      final DemandOutcome demandOutcome = demandOutcomeDao.findDemandOutcomeById(id);
      if (demandOutcome == null) {
        LOGGER.error(
            "illegal argument, the ManualDemandScenario with the ID "
                + id
                + " supplied was not found ");
        throw new InvalidEntityIdException(
            "illegal argument, the ManualDemandScenario with the ID "
                + id
                + " supplied was not found ");
      }
      // final String projectId = demandOutcome.getProjectId();
      // final WifProject project = projectService.getProject(projectId);
      // final ManualDemandConfig manualdemandConfig = manualdemandConfigService
      // .getManualDemandConfig(projectId);
      // project.setManualDemandConfig(manualdemandConfig);
      // ManualDemandScenario = ManualDemandScenarioParser.parse(
      // ManualDemandScenario, manualdemandConfig, project);
      // ManualDemandScenario.setManualDemandConfig(manualdemandConfig);
      // ManualDemandScenario = ManualDemandScenarioParser
      // .parseAreaRequirements(ManualDemandScenario);

      // demandOutcome.setWifProject(project);
      return demandOutcome;

    } catch (final IllegalArgumentException e) {

      LOGGER.error(
          "illegal argument, the ID "
              + id
              + " supplied doesn't identify a valid ManualDemandScenario ");
      throw new WifInvalidInputException(
          "illegal argument, the ID "
              + id
              + " supplied doesn't identify a valid ManualDemandScenario ",
          e);
    }
  }
  /*
   * (non-Javadoc)
   * @see
   * au.org.aurin.wif.svc.ManualDemandScenarioService#getManualDemandScenarios
   * (java.lang.String)
   */
  public List<DemandOutcome> getDemandOutcomes(final String projectID)
      throws WifInvalidInputException {
    LOGGER.info("getting all ManualDemandScenarios for projectID: {} ", projectID);

    return demandOutcomeDao.getDemandOutcomes(projectID);
  }