private void createEnvironment(EnvironmentRegistry environmentRegistry, String code) {
    Environment environment = new Environment();
    environment.setCode(code);
    environment.setLongName(code);

    environmentRegistry.persist(environment);

    if (log.isDebugEnabled()) {
      log.debug("Successfully created Environment: " + environment.getCode());
    }
  }
 private void addDeploymentToEnvironment(
     EnvironmentRegistry environmentRegistry, Environment environment, Deployment deployment) {
   environment.attachDeployment(deployment);
   environmentRegistry.update(environment);
   if (log.isDebugEnabled()) {
     log.debug(
         "Successfully added deployment: "
             + deployment.getLocation()
             + "@"
             + deployment.getDeployedComponent().getCode()
             + " to Environment: "
             + environment.getCode());
   }
 }
  private void createContextForEvent(String eventXml) {
    if (log.isDebugEnabled()) {
      log.debug("Starting to create context for event: " + eventXml);
    }
    XmlMuncher xmlDocument = new XmlMuncher(eventXml);

    String deploymentLocation = xmlDocument.readValueFromXml("/*/:context/:deploymentLocation");
    String deployedComponentCode =
        xmlDocument.readValueFromXml("/*/:context/:deployedComponentCode");

    if (log.isDebugEnabled()) {
      log.debug("Create if missing - softwareComponent: " + deployedComponentCode);
    }
    SoftwareComponent softwareComponent =
        softwareComponentRegistry.findByCode(deployedComponentCode);
    if (softwareComponent == null) {
      createSoftwareComponent(deployedComponentCode);
      softwareComponent = softwareComponentRegistry.findByCode(deployedComponentCode);
      if (log.isDebugEnabled()) {
        log.debug(
            "Successfully created and persisted SoftwareComponent: " + softwareComponent.getCode());
      }
    } else {
      if (log.isDebugEnabled()) {
        log.debug("SoftwareComponent: " + deployedComponentCode + " already exists.");
      }
    }

    if (log.isDebugEnabled()) {
      log.debug(
          "Create if missing - deployment: " + deployedComponentCode + " @ " + deploymentLocation);
    }
    Deployment deployment =
        deploymentRegistry.findByComponentCodeAndLocation(
            deployedComponentCode, deploymentLocation);
    if (deployment == null) {
      createDeployment(softwareComponent, deploymentLocation);
      deployment =
          deploymentRegistry.findByComponentAndLocation(softwareComponent, deploymentLocation);
      if (log.isDebugEnabled()) {
        log.debug(
            "Successfully created and persisted Deployment: "
                + deployment.getLocation()
                + "@"
                + deployment.getDeployedComponent().getCode());
      }
    } else {
      if (log.isDebugEnabled()) {
        log.debug(
            "Deployment: "
                + deployedComponentCode
                + " @ "
                + deploymentLocation
                + " already exists.");
      }
    }

    if (log.isDebugEnabled()) {
      log.debug("Create if missing - environment: " + autoDeployEnvironmentName);
    }
    EnvironmentRegistry environmentRegistry =
        EventLoggerServiceContainer.getServiceContainer().getDAO(EnvironmentRegistry.class);
    Environment environment = environmentRegistry.findByCode(autoDeployEnvironmentName);
    if (environment == null) {
      if (log.isDebugEnabled()) {
        log.debug("Environment " + autoDeployEnvironmentName + " missing, creating it....");
      }
      createEnvironment(environmentRegistry, autoDeployEnvironmentName);
      environment = environmentRegistry.findByCode(autoDeployEnvironmentName);
    } else {
      if (log.isDebugEnabled()) {
        log.debug("Environment: " + autoDeployEnvironmentName + " already exists.");
      }
    }

    if (!environment.hasAttachedDeployment(deployment)) {
      if (log.isDebugEnabled()) {
        log.debug(
            "Attaching Deployment: "
                + deployedComponentCode
                + " @ "
                + deploymentLocation
                + " to Environment: "
                + autoDeployEnvironmentName);
      }
      addDeploymentToEnvironment(environmentRegistry, environment, deployment);
    }

    if (log.isDebugEnabled()) {
      log.debug("Successfully created context for event: " + eventXml);
    }
  }