private long populateParentRelease(CmsCI env, String nsPath) {
    long releaseId = 0;
    // if we got new release lets populate parent releaseid with latest design
    CmsRelease manifestRelease = null;
    List<CmsRelease> manifestReleases = rfcProcessor.getLatestRelease(nsPath, "open");
    if (manifestReleases.size() > 0) {
      manifestRelease = manifestReleases.get(0);
    } else {
      List<CmsRelease> closedManifestReleases = rfcProcessor.getLatestRelease(nsPath, "closed");
      if (closedManifestReleases.size() > 0) {
        manifestRelease = closedManifestReleases.get(0);
      }
    }

    if (manifestRelease != null) {
      releaseId = manifestRelease.getReleaseId();
      List<CmsRelease> designReleases = rfcProcessor.getLatestRelease(env.getNsPath(), "closed");
      if (designReleases.size() > 0) {
        CmsRelease designRelease = designReleases.get(0);
        manifestRelease.setParentReleaseId(designRelease.getReleaseId());
        rfcProcessor.updateRelease(manifestRelease);
      }
    }

    return releaseId;
  }
 private void processCloudDeletions(long envId, Set<Long> cloudsToRemove) {
   // looks like we need to delete some clouds
   // first lets see if we have any open releases
   CmsCI env = getEnv(envId);
   String manifestNsPath = env.getNsPath() + "/" + env.getCiName() + "/manifest";
   String bomNsPath = env.getNsPath() + "/" + env.getCiName() + "/bom";
   List<CmsRelease> manifestOpenReleases =
       rfcProcessor.getReleaseBy3(manifestNsPath, null, RELEASE_STATE_OPEN);
   if (manifestOpenReleases.size() > 0) {
     // throw exception on open release
     throw new TransistorException(
         CmsError.TRANSISTOR_OPEN_MANIFEST_RELEASE,
         "There is open release in this environment, you have to commit or discard before deleteing the clouds!");
   }
   List<CmsRelease> bomOpenReleases =
       rfcProcessor.getReleaseBy3(bomNsPath, null, RELEASE_STATE_OPEN);
   if (bomOpenReleases.size() > 0) {
     // throw exception on open release
     throw new TransistorException(
         CmsError.TRANSISTOR_OPEN_BOM_RELEASE,
         "There is open BOM release in this environment, you have to deploy or discard before deleteing the clouds!");
   }
   // if we still here lets check if there are any boms deployed to the clouds that user tries to
   // remove
   for (long cloidId : cloudsToRemove) {
     long deployedToRelsCount =
         cmProcessor.getCountToCIRelationsByNS(
             cloidId, BASE_DEPLOYED_TO, null, null, bomNsPath, true);
     if (deployedToRelsCount > 0) {
       // throw exception that there are active boms
       throw new TransistorException(
           CmsError.TRANSISTOR_BOM_INSTANCES_EXIST,
           "There are deployed instances in the cloud, please put the cloud in offline mode for every platform and deploy!");
     }
   }
   // if everything cool, let remove Consumes relations from platforms and env
   for (long cloidId : cloudsToRemove) {
     List<CmsCIRelation> platformRels =
         cmProcessor.getFromCIRelationsNaked(envId, MANIFEST_COMPOSED_OF, MANIFEST_PLATFORM);
     for (CmsCIRelation platRel : platformRels) {
       List<CmsCIRelation> platformCloudRels =
           cmProcessor.getFromToCIRelations(platRel.getToCiId(), BASE_CONSUMES, cloidId);
       for (CmsCIRelation platCloudRel : platformCloudRels) {
         cmProcessor.deleteRelation(platCloudRel.getCiRelationId());
       }
     }
     // now lets remove env consumes rel
     List<CmsCIRelation> envCloudRels =
         cmProcessor.getFromToCIRelations(envId, BASE_CONSUMES, cloidId);
     for (CmsCIRelation cloudRel : envCloudRels) {
       cmProcessor.deleteRelation(cloudRel.getCiRelationId());
     }
   }
 }
 private boolean hasOpenManifestRelease(String nsPath) {
   List<CmsRelease> manReleases = rfcProcessor.getLatestRelease(nsPath, null);
   if (manReleases.size() > 0) {
     if ("open".equals(manReleases.get(0).getReleaseState())) {
       return true;
     }
   }
   return false;
 }
  /**
   * @param manifestPlatformRfcs
   * @param userId
   */
  private void processPlatformRfcs(ManifestRfcContainer manifestPlatformRfcs, String userId) {

    long t1 = System.currentTimeMillis();
    /** *** Handle root RFC and relations ***** */
    CmsRfcCI rootRfc = null;
    if (manifestPlatformRfcs.getRootRfcRelTouple().getRfcCI() != null) {
      rootRfc =
          rfcProcessor.createAndfetchRfcCINoCheck(
              manifestPlatformRfcs.getRootRfcRelTouple().getRfcCI(), userId);
      if (rootRfc.getCiState() == null) {
        rootRfc.setCiState("default");
      }
    } else {
      rootRfc = manifestPlatformRfcs.getManifestPlatformRfc();
    }

    for (CmsRfcRelation toRfcRelation :
        manifestPlatformRfcs.getRootRfcRelTouple().getToRfcRelation()) {
      toRfcRelation.setToCiId(rootRfc.getCiId());
      rfcProcessor.createRfcRelationNoCheck(toRfcRelation, userId);
    }

    for (CmsRfcRelation fromRfcRelation :
        manifestPlatformRfcs.getRootRfcRelTouple().getFromRfcRelation()) {
      fromRfcRelation.setFromCiId(rootRfc.getCiId());
      rfcProcessor.createRfcRelationNoCheck(fromRfcRelation, userId);
    }

    /** Handle DependsOn and other pack relations ** */
    for (ManifestRfcRelationTriplet rfcRelTriplet : manifestPlatformRfcs.getRfcRelTripletList()) {

      CmsRfcRelation rfcRelation = rfcRelTriplet.getRfcRelation();
      if (rfcRelation.getRfcAction() == null) {
        rfcRelation.setRfcAction("add");
      }

      CmsRfcCI toRfcCI = rfcRelTriplet.getToRfcCI();
      if (toRfcCI != null) {
        if (toRfcCI.getRfcAction() == null) {
          toRfcCI.setRfcAction("add");
        }
        manifestRfcProcessor.setCiId(toRfcCI);
        if (toRfcCI.getRfcId() == 0 && toRfcCI.getCiId() == 0) {
          toRfcCI = rfcProcessor.createAndfetchRfcCINoCheck(toRfcCI, userId);
        }
        rfcRelation.setToCiId(toRfcCI.getCiId());
      }

      CmsRfcCI fromRfcCI = rfcRelTriplet.getFromRfcCI();
      if (fromRfcCI != null) {
        if (fromRfcCI.getRfcAction() == null) {
          fromRfcCI.setRfcAction("add");
        }
        manifestRfcProcessor.setCiId(fromRfcCI);
        if (fromRfcCI.getRfcId() == 0 && fromRfcCI.getCiId() == 0) {
          fromRfcCI = rfcProcessor.createAndfetchRfcCINoCheck(fromRfcCI, userId);
        }
        rfcRelation.setFromCiId(fromRfcCI.getCiId());
      }

      if ("manifest.Entrypoint".equals(rfcRelation.getRelationName())) {
        rfcRelation.setFromCiId(rootRfc.getCiId());
      }

      manifestRfcProcessor.setCiRelationId(rfcRelation);
      rfcProcessor.createRfcRelationNoCheck(rfcRelation, userId);
    }

    /** Handle Requires relations ** */
    for (ManifestRootRfcContainer rfcRelTouple : manifestPlatformRfcs.getRfcRelToupleList()) {
      CmsRfcCI newRfc;
      if (rfcRelTouple.getRfcCI().getRfcId() == 0) {
        newRfc = rfcProcessor.createAndfetchRfcCINoCheck(rfcRelTouple.getRfcCI(), userId);
      } else {
        newRfc = rfcRelTouple.getRfcCI();
      }
      for (CmsRfcRelation rfcRel : rfcRelTouple.getToRfcRelation()) {
        if (rfcRel.getRfcAction() == null) {
          rfcRel.setRfcAction("add");
        }

        if ("manifest.Requires".equals(rfcRel.getRelationName()) && rfcRel.getFromCiId() == 0) {
          rfcRel.setFromCiId(rootRfc.getCiId());
        }
        rfcRel.setToCiId(newRfc.getCiId());
        rfcProcessor.createRfcRelationNoCheck(rfcRel, userId);
      }
      for (CmsRfcRelation rfcRel : rfcRelTouple.getFromRfcRelation()) {
        if (rfcRel.getRfcAction() == null) {
          rfcRel.setRfcAction("add");
        }

        if (rfcRel.getToCiId() == 0) {
          rfcRel.setToCiId(rootRfc.getCiId());
        }
        rfcRel.setFromCiId(newRfc.getCiId());
        rfcProcessor.createRfcRelationNoCheck(rfcRel, userId);
      }
    }

    for (CmsRfcCI rfc : manifestPlatformRfcs.getRfcList()) {
      rfcProcessor.createRfcCINoCheck(rfc, userId);
    }

    for (CmsRfcRelation rfcRelation : manifestPlatformRfcs.getRfcRelationList()) {
      if (rfcRelation.getFromCiId() == 0) {
        if ("base.Consumes".equals(rfcRelation.getRelationName())) {
          rfcRelation.setFromCiId(rootRfc.getCiId());
        } else {
          rfcRelation.setFromCiId(rootRfc.getCiId());
        }
      }
      if (rfcRelation.getRfcAction() == null) {
        rfcRelation.setRfcAction("add");
      }

      rfcProcessor.createRfcRelationNoCheck(rfcRelation, userId);
    }

    for (Long delCiId : manifestPlatformRfcs.getDeleteCiIdList()) {
      cmRfcMrgProcessor.requestCiDeleteCascadeNoRelsRfcs(delCiId, userId, 0);
    }

    for (CmsRfcRelation delRelation : manifestPlatformRfcs.getRfcDeleteRelationList()) {
      cmRfcMrgProcessor.requestRelationDelete(delRelation.getCiRelationId(), userId);
    }
    long t2 = System.currentTimeMillis();
    logger.info(
        " processPlatformRfcs  "
            + manifestPlatformRfcs.getManifestPlatformRfc().getNsPath()
            + " completed in  "
            + (t2 - t1));
  }