/**
   * It deletes the tier.
   *
   * @param claudiaData
   * @param tier
   */
  public void delete(ClaudiaData claudiaData, Tier tier)
      throws EntityNotFoundException, InvalidEntityException, InfrastructureException {

    try {
      tier = loadTierWithNetworks(tier.getName(), tier.getVdc(), tier.getEnviromentName());
      log.info(
          "Deleting tier "
              + tier.getName()
              + " from vdc "
              + tier.getVdc()
              + "  env  "
              + tier.getEnviromentName()
              + " "
              + tier.getNetworks());
    } catch (EntityNotFoundException e) {

      String mens =
          "It is not possible to delete the tier " + tier.getName() + " since it is not exist";
      log.error(mens);
      throw new com.telefonica.euro_iaas.commons.dao.EntityNotFoundException(
          Tier.class, mens, tier);
    }

    log.info("Deleting the networks " + tier.getNetworks());

    List<Network> netsAux = new ArrayList<Network>();
    for (Network netNet : tier.getNetworks()) {
      netsAux.add(netNet);
    }

    tier.setNetworks(null);
    tierDao.update(tier);

    for (Network net : netsAux) {

      if (isAvailableToBeDeleted(net)) {
        log.info("Deleting network " + net.getNetworkName());
        try {
          networkManager.delete(net);
        } catch (Exception e) {
          log.info("There is an error to delete the network");
        }
      }
    }
    log.info("Networks deleted");

    try {
      tierDao.remove(tier);
    } catch (Exception e) {
      String mens = "It is not possible to delete the tier since it is not exist " + e.getMessage();
      log.error(mens);
      throw new InvalidEntityException(tier, e);
    }
  }
 public Tier update(Tier tier) throws InvalidEntityException {
   log.info("Update tier " + tier.getName());
   try {
     Tier newTier = tierDao.update(tier);
     return tierDao.loadComplete(newTier);
   } catch (Exception e) {
     log.error(
         "It is not possible to update the tier " + tier.getName() + " : " + e.getMessage(), e);
     throw new InvalidEntityException(
         "It is not possible to update the tier " + tier.getName() + " : " + e.getMessage());
   }
 }
 public Tier load(String name, String vdc, String environmentName) throws EntityNotFoundException {
   try {
     return tierDao.load(name, vdc, environmentName);
   } catch (Exception e) {
     throw new EntityNotFoundException(Tier.class, "error", e.getMessage());
   }
 }
 public boolean exists(String name, String vdc, String environmentName) {
   try {
     tierDao.load(name, vdc, environmentName);
     return true;
   } catch (Exception e) {
     return false;
   }
 }
  public Tier loadTierWithNetworks(String tierName, String vdc, String environmentName)
      throws EntityNotFoundException {
    try {
      return tierDao.loadTierWithNetworks(tierName, vdc, environmentName);

    } catch (Exception e) {
      String message = "Tier " + tierName + " not found";
      throw new EntityNotFoundException(Tier.class, message, e.getMessage());
    }
  }
 @Override
 public Tier loadTierWithProductReleaseAndMetadata(
     String tierName, String environmentName, String vdc) throws EntityNotFoundException {
   try {
     return tierDao.loadTierWithProductReleaseAndMetadata(tierName, vdc, environmentName);
   } catch (Exception e) {
     String message = "Tier " + tierName + " not found";
     throw new EntityNotFoundException(Tier.class, message, e.getMessage());
   }
 }
  private boolean isAvailableToBeDeleted(Network net) {

    List<Tier> tiers = tierDao.findAllWithNetwork(net.getNetworkName());
    if (tiers.isEmpty()) {
      log.info("The network " + net + " can be deleted");
      return true;
    } else {
      log.info(
          "The network "
              + net.getNetworkName()
              + " cannot be deleted. The following tiers are using it");
      for (Tier tier : tiers) {
        log.info(tier.getName());
      }
      return false;
    }
  }
 @Override
 public Tier loadComplete(Tier tier) throws EntityNotFoundException {
   return tierDao.loadComplete(tier);
 }
  /**
   * It insert the tier in the database.
   *
   * @param tier
   * @param data
   * @return
   * @throws InvalidEntityException
   * @throws InfrastructureException
   */
  public Tier tierInsertBD(Tier tier, ClaudiaData data)
      throws InvalidEntityException, InfrastructureException {
    List<Network> networskout = new ArrayList();
    try {
      return load(tier.getName(), tier.getVdc(), tier.getEnviromentName());
    } catch (EntityNotFoundException e) {

      // tier.setVdc(data.getVdc());
      // tier.setEnviromentName(data.getService());

      List<ProductRelease> productReleases = new ArrayList();
      if (tier.getProductReleases() != null && tier.getProductReleases().size() != 0) {
        for (ProductRelease p : tier.getProductReleases()) {
          productReleases.add(p);
        }
      } else {

        log.warn("There is not any product release associated to the tier " + tier.getName());
      }

      for (Network p : tier.getNetworks()) {
        networskout.add(p);
      }

      try {
        tier.setProductReleases(null);
        tier.setNetworks(null);
        tier = tierDao.create(tier);
      } catch (Exception e2) {
        String errorMessage =
            "The Tier  " + tier.getName() + "  cannot be created " + e2.getMessage();
        log.error(errorMessage);
        // restore(data, tier);
        throw new InvalidEntityException(errorMessage);
      }

      if (productReleases.size() != 0) {
        for (ProductRelease product : productReleases) {

          try {
            ProductRelease templateProduct =
                productReleaseManager.load(product.getProduct() + "-" + product.getVersion(), data);
            log.info(
                "Adding product release "
                    + templateProduct.getProduct()
                    + "-"
                    + templateProduct.getVersion()
                    + " to tier "
                    + templateProduct.getName());

            mergeAttributes(product, templateProduct);
            mergeMetadatas(product, templateProduct);

            tier.addProductRelease(templateProduct);

            tier = update(tier);
          } catch (Exception e2) {
            String errorMessage =
                "The ProductRelease Object "
                    + product.getProduct()
                    + "-"
                    + product.getVersion()
                    + " is "
                    + "NOT present in Database";
            log.error(errorMessage);
            throw new InvalidEntityException(e2);
          }
        }
      }
    }

    for (Network net : networskout) {

      try {
        net = networkManager.load(net.getNetworkName(), net.getVdc(), net.getRegion());
        log.info("Adding network " + net.getNetworkName() + "-" + " to tier " + tier.getName());
        tier.addNetwork(net);
        update(tier);
      } catch (Exception e2) {
        String errorMessage =
            "The Network Object " + net.getNetworkName() + " is " + "NOT present in Database";
        log.error(errorMessage);
        throw new InvalidEntityException(e2);
      }
    }

    return tier;
  }
 public List<Tier> findByCriteria(TierSearchCriteria criteria) throws EntityNotFoundException {
   return tierDao.findByCriteria(criteria);
 }
  public List<Tier> findAll() {

    return tierDao.findAll();
  }