/*
   * (non-Javadoc)
   * @see com.telefonica.euro_iaas.paasmanager.manager.ProductManager#load(java .lang.String)
   */
  public ProductRelease load(String name, ClaudiaData data) throws EntityNotFoundException {
    log.info("Loading product release " + name);
    ProductRelease productRelease = null;
    String product, version;
    try {
      product = name.split("-")[0];
      version = name.split("-")[1];
    } catch (Exception ex) {
      String msg = "Entity with invalid name(" + name + "): use <productName>-<version> ";
      log.warn(msg);
      throw new EntityNotFoundException(ProductRelease.class, msg, name);
    }
    try {
      log.info("Loading from sdc " + product + " " + version);

      productRelease = productReleaseDao.load(name);

    } catch (EntityNotFoundException e) {
      // Go to SDC and search. After this, introduce product in database
      log.info("The product " + name + " is not in database");
      try {
        ProductRelease pRelease = productReleaseSdcDao.load(product, version, data);
        productRelease = create(pRelease);

      } catch (EntityNotFoundException e5) {
        String msg = "No product release NOT Found in SDC neither in PaasManager: " + name;
        log.warn(msg);
        throw new EntityNotFoundException(ProductRelease.class, msg, name);
      } catch (SdcException e6) {
        String msg = "SDC failure at loading ProductRelease " + name + " " + e6.getMessage();
        log.warn(msg);
        throw new EntityNotFoundException(ProductRelease.class, msg, name);
      } catch (InvalidEntityException e7) {
        String msg = "SDC failure at loading ProductRelease " + name + " " + e7.getMessage();
        log.warn(msg);
        throw new EntityNotFoundException(ProductRelease.class, msg, name);
      } catch (AlreadyExistsEntityException e8) {
        // if already exist, we should update metadatas or attributes
        String msg = "SDC failure at loading ProductRelease " + name + " " + e8.getMessage();
        log.warn(msg);
        throw new EntityNotFoundException(ProductRelease.class, msg, name);
      }
    }

    return productRelease;
  }
  public ProductRelease loadFromSDCAndCreate(String name, ClaudiaData data)
      throws EntityNotFoundException {
    log.info("Sync product release " + name);
    ProductRelease productRelease;
    String product = name.split("-")[0];
    String version = name.split("-")[1];

    try {
      log.info("Loading from sdc " + product + " " + version);
      ProductRelease pRelease = productReleaseSdcDao.load(product, version, data);
      try {

        productRelease = productReleaseDao.load(name);

        HashSet<Attribute> attributesCopy = new HashSet<Attribute>(productRelease.getAttributes());
        HashSet<Metadata> metatadasCopy = new HashSet<Metadata>(productRelease.getMetadatas());

        productReleaseDao.removeAttributesAndMetadatas(productRelease);
        // remove
        for (Attribute attribute : attributesCopy) {
          attributeDao.remove(attribute);
        }

        for (Metadata metadata : metatadasCopy) {
          metadataDao.remove(metadata);
        }

        // add new attributes and metadatas
        for (Attribute attribute : pRelease.getAttributes()) {
          Attribute newAttribute = attributeDao.create(attribute);
          productRelease.addAttribute(newAttribute);
        }

        for (Metadata metadata : pRelease.getMetadatas()) {
          Metadata newMetadata = metadataDao.create(metadata);
          productRelease.addMetadata(newMetadata);
        }
        productReleaseDao.update(productRelease);
      } catch (Exception ex) {
        log.info("Product don't exist in database: creates");
        productRelease = create(pRelease);
      }
    } catch (EntityNotFoundException e5) {
      String msg = "Product release NOT found in SDC: " + name;
      log.warn(msg);
      throw new EntityNotFoundException(ProductRelease.class, msg, name);
    } catch (SdcException e6) {
      String msg = "Failure at loading ProductRelease from SDC" + name + " " + e6.getMessage();
      log.warn(msg);
      throw new EntityNotFoundException(ProductRelease.class, msg, name);
    } catch (InvalidEntityException e7) {
      String msg = "Failure creating new ProductRelease " + name + " " + e7.getMessage();
      log.warn(msg);
      throw new EntityNotFoundException(ProductRelease.class, msg, name);
    } catch (AlreadyExistsEntityException e8) {
      String msg = "Failure creating ProductRelease " + name + " " + e8.getMessage();
      log.warn(msg);
      throw new EntityNotFoundException(ProductRelease.class, msg, name);
    }

    return productRelease;
  }