/*
   * (non-Javadoc)
   * @see sif3.infra.rest.env.ClientEnvironmentManager#reloadSessionFromFromWorkstore(sif3.common.model.EnvironmentKey)
   */
  public EnvironmentType reloadSessionFromFromWorkstore(EnvironmentKey environmentKey)
      throws IllegalArgumentException, PersistenceException {
    setSIF3Session(envOps.loadSession(environmentKey));
    if (getSIF3Session() != null) // session exists and was loaded successfully
    {
      // Extract the service info and store it in an easy accessible way in the session.
      EnvironmentType environment =
          envOps.loadEnvironmentFromString(getSIF3Session().getEnvironmentXML());
      SIFSessionUtils.loadServiceInfoForSession(getSIF3Session(), environment);
      populateInfrastructureURLs(environment);

      // XML no longer needed.
      getSIF3Session().setEnvironmentXML(null);

      return environment;
    } else {
      return null; // error already logged.
    }
  }
  /*
   * (non-Javadoc)
   * @see sif3.infra.rest.env.ClientEnvironmentManager#removeEnvironment(sif3.common.model.EnvironmentKey)
   */
  public boolean removeEnvironment(EnvironmentKey environmentKey) {
    if (existsSIF3SessionInSessionStore()) {
      // check if the session is for the given environment key.
      if ((environmentKey == null)
          || (!environmentKey.equals(
              sif3Session))) // They are NOT the same. That is odd! should not happen
      {
        logger.error(
            "Attempted to remove a environment for an active session with the environment key: "
                + getSIF3Session().getEnvironmentName()
                + ". Deletion request is for environment key: "
                + environmentKey.getEnvironmentName()
                + ". Inconsistent data! No action taken.");
        return false;
      }
    } else {
      // Session is not in session store. => Attempt to load it and if that is successful we remove
      // it.
      setSIF3Session(envOps.loadSession(environmentKey));
    }

    // If we get here we either have a valid session in the store for which the environment must
    // removed or there is no
    // environment in the workstore for the given environmentKey.
    if (existsSIF3SessionInSessionStore()) {
      if (envOps.removeEnvFromWorkstoreBySessionToken(getSIF3Session().getSessionToken())) {
        // remove session from session store as well.
        disconnect();
        return true;

      } else // that is odd! Could not remove session in session store.
      {
        return false;
      }
    } else {
      return true; // no such session/environment => no action taken.
    }
  }
  /*
   * (non-Javadoc)
   * @see sif3.infra.rest.env.ClientEnvironmentManager#createOrUpdateEnvironment(sif3.infra.common.model.EnvironmentType)
   */
  public EnvironmentType createOrUpdateEnvironment(
      EnvironmentType inputEnvironment, TokenInfo tokenInfo) {
    setSIF3Session(envOps.createOrUpdateSession(inputEnvironment, tokenInfo));
    if (getSIF3Session() != null) // creation was successful
    {
      // Extract the service info and store it in an easy accessible way in the session.
      SIFSessionUtils.loadServiceInfoForSession(getSIF3Session(), inputEnvironment);
      populateInfrastructureURLs(inputEnvironment);

      // XML no longer needed.
      getSIF3Session().setEnvironmentXML(null);

      return inputEnvironment;
    } else {
      return null; // error already logged.
    }
  }
 /*
  * (non-Javadoc)
  * @see sif3.infra.rest.env.ClientEnvironmentManager#loadTemplateEnvironmentData()
  */
 public EnvironmentType loadTemplateEnvironmentData() {
   ProviderEnvironment envInfo = getProviderEnvrionment();
   return envOps.loadTemplateEnvironmentData(
       envInfo.getTemplateXMLFileName(), envInfo.getAdapterType(), envInfo.getEnvironmentType());
 }
 /* (non-Javadoc)
  * @see sif3.infra.common.interfaces.EnvironmentManager#updateSessionSecurityInfo(java.lang.String, java.lang.String, java.util.Date)
  */
 @Override
 public boolean updateSessionSecurityInfo(
     String sessionToken, String securityToken, Date securityExpiryDate) {
   return envOps.updateSessionSecurityInfo(sessionToken, securityToken, securityExpiryDate);
 }
 /* (non-Javadoc)
  * @see sif3.infra.common.interfaces.EnvironmentManager#getServiceProperties()
  */
 @Override
 public AdvancedProperties getServiceProperties() {
   return envOps.getServiceProperties();
 }
 /*
  * (non-Javadoc)
  * @see sif3.infra.rest.env.ClientEnvironmentManager#getEnvironmentInfo
  */
 @Override
 public EnvironmentInfo getEnvironmentInfo() {
   return envOps.getEnvironmentInfo();
 }