class EmrHelperServiceImpl implements EmrHelperService {

    EmrClusterPreCreateDto emrPreCreateClusterStepsImpl(EmrClusterAlternateKeyDto emrClusterAlternateKeyDto, EmrClusterCreateRequest request) throws Exception
    {
        // Get the EMR cluster definition and ensure it exists.
        EmrClusterDefinitionEntity emrClusterDefinitionEntity = emrClusterDefinitionDaoHelper.getEmrClusterDefinitionEntity(
            new EmrClusterDefinitionKey(emrClusterAlternateKeyDto.getNamespace(), emrClusterAlternateKeyDto.getEmrClusterDefinitionName()));

        // Get EMR cluster definition XML stored in the database.
        String originalClusterDefinitionXml = emrClusterDefinitionEntity.getConfiguration();

        // Unmarshal definition xml into JAXB object.
        EmrClusterDefinition emrClusterDefinition = xmlHelper.unmarshallXmlToObject(EmrClusterDefinition.class, originalClusterDefinitionXml);

        // Perform override if override is set.
        overrideEmrClusterDefinition(emrClusterDefinition, request.getEmrClusterDefinitionOverride());

        // Get account ID.
        String accountId = emrClusterDefinition.getAccountId();

        // Get AWS parameters.
        EmrParamsDto emrParamsDto = emrHelper.getEmrParamsDtoByAccountId(accountId);

        // Replace all S3 managed location variables in EMR cluster definition XML.
        String overriddenEmrClusterDefinitionXml = xmlHelper.objectToXml(emrClusterDefinition);
        String replacedEmrClusterDefinitionXml = overriddenEmrClusterDefinitionXml
            .replaceAll(getS3ManagedReplaceString(), emrHelper.getS3StagingLocation(emrParamsDto.getTrustingAccountStagingBucketName()));
        emrClusterDefinition = xmlHelper.unmarshallXmlToObject(EmrClusterDefinition.class, replacedEmrClusterDefinitionXml);

        // Perform the EMR cluster definition configuration validation.
        emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(emrClusterDefinition);

        // Check permissions.
        namespaceIamRoleAuthorizationHelper.checkPermissions(emrClusterDefinitionEntity.getNamespace(), emrClusterDefinition.getServiceIamRole(),
            emrClusterDefinition.getEc2NodeIamProfileName());

        // Create and return EMR cluster pre-create DTO.
        return new EmrClusterPreCreateDto(emrClusterDefinition, emrParamsDto);
    }

}