/// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static ApplicationRegister CreateApplicationRegister(string locale)
        {
            environmentType environmentTypeRequest;
            environmentType environmentTypeResponse;

            using (FileStream xmlStream = File.OpenRead("Data files\\" + locale.ToUpper() + "\\EnvironmentRequest.xml"))
            {
                environmentTypeRequest = SerialiserFactory.GetXmlSerialiser <environmentType>().Deserialise(xmlStream);
            }

            using (FileStream xmlStream = File.OpenRead("Data files\\" + locale.ToUpper() + "\\EnvironmentResponse.xml"))
            {
                environmentTypeResponse = SerialiserFactory.GetXmlSerialiser <environmentType>().Deserialise(xmlStream);
            }

            Environment         environmentRequest  = MapperFactory.CreateInstance <environmentType, Environment>(environmentTypeRequest);
            Environment         environmentResponse = MapperFactory.CreateInstance <environmentType, Environment>(environmentTypeResponse);
            EnvironmentRegister environmentRegister = CreateEnvironmentRegister(environmentRequest, environmentResponse);
            ApplicationRegister applicationRegister = new ApplicationRegister
            {
                ApplicationKey       = environmentRequest.ApplicationInfo.ApplicationKey,
                SharedSecret         = "SecretDem0",
                EnvironmentRegisters = new Collection <EnvironmentRegister> {
                    { environmentRegister }
                }
            };

            return(applicationRegister);
        }
        /// <summary>
        /// <see cref="SifService{UI, DB}.Create(UI, string, string)"/>
        /// </summary>
        public override Guid Create(environmentType item, string zoneId = null, string contextId = null)
        {
            EnvironmentRegister environmentRegister =
                (new EnvironmentRegisterService()).RetrieveByUniqueIdentifiers
                    (item.applicationInfo.applicationKey, item.instanceId, item.userToken, item.solutionId);

            if (environmentRegister == null)
            {
                string errorMessage = string.Format("Environment with application key of {0}, solution ID of {1}, instance ID of {2} and user token of {3} does NOT exist.",
                                                    item.applicationInfo.applicationKey, (item.solutionId == null ? "[null]" : item.solutionId), (item.instanceId == null ? "[null]" : item.instanceId), (item.userToken == null ? "[null]" : item.userToken));
                throw new AlreadyExistsException(errorMessage);
            }

            string sessionToken = AuthenticationUtils.GenerateSessionToken(item.applicationInfo.applicationKey, item.instanceId, item.userToken, item.solutionId);

            environmentType environmentType = RetrieveBySessionToken(sessionToken);

            if (environmentType != null)
            {
                string errorMessage = string.Format("A session token already exists for environment with application key of {0}, solution ID of {1}, instance ID of {2} and user token of {3}.",
                                                    item.applicationInfo.applicationKey, (item.solutionId == null ? "[null]" : item.solutionId), (item.instanceId == null ? "[null]" : item.instanceId), (item.userToken == null ? "[null]" : item.userToken));
                throw new AlreadyExistsException(errorMessage);
            }

            IDictionary <InfrastructureServiceNames, InfrastructureService> infrastructureServices = CopyInfrastructureServices(environmentRegister.InfrastructureServices);
            IDictionary <string, ProvisionedZone> provisionedZones = CopyProvisionedZones(environmentRegister.ProvisionedZones);
            Environment repoItem = MapperFactory.CreateInstance <environmentType, Environment>(item);

            if (environmentRegister.DefaultZone != null)
            {
                repoItem.DefaultZone = CopyDefaultZone(environmentRegister.DefaultZone);
            }

            if (infrastructureServices.Count > 0)
            {
                repoItem.InfrastructureServices = CopyInfrastructureServices(environmentRegister.InfrastructureServices);
            }

            if (provisionedZones.Count > 0)
            {
                repoItem.ProvisionedZones = CopyProvisionedZones(environmentRegister.ProvisionedZones);
            }

            repoItem.SessionToken = sessionToken;
            Guid environmentId = repository.Save(repoItem);

            if (repoItem.InfrastructureServices.Count > 0)
            {
                InfrastructureService infrastructureService = repoItem.InfrastructureServices[InfrastructureServiceNames.environment];

                if (infrastructureService != null)
                {
                    infrastructureService.Value = infrastructureService.Value + "/" + environmentId;
                    repository.Save(repoItem);
                }
            }

            return(environmentId);
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private static EnvironmentRegister CreateEnvironmentRegister(Environment environmentRequest, Environment environmentResponse)
        {
            EnvironmentRegister environmentRegister = new EnvironmentRegister
            {
                ApplicationKey         = environmentRequest.ApplicationInfo.ApplicationKey,
                InfrastructureServices = environmentResponse.InfrastructureServices,
                InstanceId             = environmentRequest.InstanceId,
                ProvisionedZones       = environmentResponse.ProvisionedZones,
                SolutionId             = environmentRequest.SolutionId,
                UserToken = environmentRequest.UserToken
            };

            return(environmentRegister);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static EnvironmentRegister CreateEnvironmentRegister(Environment environmentRequest, Environment environmentResponse)
        {
            EnvironmentRegister environmentRegister = new EnvironmentRegister
            {
                ApplicationKey = environmentRequest.ApplicationInfo.ApplicationKey,
                InfrastructureServices = environmentResponse.InfrastructureServices,
                InstanceId = environmentRequest.InstanceId,
                ProvisionedZones = environmentResponse.ProvisionedZones,
                SolutionId = environmentRequest.SolutionId,
                UserToken = environmentRequest.UserToken
            };

            return environmentRegister;
        }
        private static ApplicationRegister CreateApplicationRegister(string path)
        {
            environmentType environmentTypeRequest;
            environmentType environmentTypeResponse;

            string request  = @path + "\\EnvironmentRequest.xml";
            string response = @path + "\\EnvironmentResponse.xml";

            if (!File.Exists(request) || !File.Exists(response))
            {
                return(null);
            }

            Console.WriteLine("");
            Console.WriteLine("Processsing input from: " + path);
            Console.WriteLine("Request:  " + request);
            Console.WriteLine("Response: " + response);
            Console.WriteLine("");

            using (FileStream xmlStream = File.OpenRead(request))
            {
                environmentTypeRequest = SerialiserFactory.GetXmlSerialiser <environmentType>().Deserialise(xmlStream);
            }

            using (FileStream xmlStream = File.OpenRead(response))
            {
                environmentTypeResponse = SerialiserFactory.GetXmlSerialiser <environmentType>().Deserialise(xmlStream);
            }

            Environment         environmentRequest  = MapperFactory.CreateInstance <environmentType, Environment>(environmentTypeRequest);
            Environment         environmentResponse = MapperFactory.CreateInstance <environmentType, Environment>(environmentTypeResponse);
            EnvironmentRegister environmentRegister = CreateEnvironmentRegister(environmentRequest, environmentResponse);
            ApplicationRegister applicationRegister = new ApplicationRegister
            {
                ApplicationKey       = environmentRequest.ApplicationInfo.ApplicationKey,
                SharedSecret         = "SecretDem0",
                EnvironmentRegisters = new Collection <EnvironmentRegister> {
                    { environmentRegister }
                }
            };

            return(applicationRegister);
        }
        /// <summary>
        /// <see cref="SifService{UI, DB}.Create(UI, string, string)"/>
        /// </summary>
        public override Guid Create(environmentType item, string zoneId = null, string contextId = null)
        {
            var environmentRegisterService          = new EnvironmentRegisterService();
            EnvironmentRegister environmentRegister = environmentRegisterService.RetrieveByUniqueIdentifiers(
                item.applicationInfo.applicationKey,
                item.instanceId,
                item.userToken,
                item.solutionId);

            if (environmentRegister == null)
            {
                var errorMessage =
                    $"Environment with [applicationKey:{item.applicationInfo.applicationKey}|solutionId:{item.solutionId ?? "<null>"}|instanceId:{item.instanceId ?? "<null>"}|userToken:{item.userToken ?? "<null>"}] does NOT exist.";
                throw new AlreadyExistsException(errorMessage);
            }

            string sessionToken = AuthenticationUtils.GenerateSessionToken(
                item.applicationInfo.applicationKey,
                item.instanceId,
                item.userToken,
                item.solutionId);

            environmentType environmentType = RetrieveBySessionToken(sessionToken);

            if (environmentType != null)
            {
                var errorMessage =
                    $"A session token already exists for environment with [applicationKey:{item.applicationInfo.applicationKey}|solutionId:{item.solutionId ?? "<null>"}|instanceId:{item.instanceId ?? "<null>"}|userToken:{item.userToken ?? "<null>"}].";
                throw new AlreadyExistsException(errorMessage);
            }

            IDictionary <InfrastructureServiceNames, InfrastructureService> infrastructureServices =
                CopyInfrastructureServices(environmentRegister.InfrastructureServices);
            IDictionary <string, ProvisionedZone> provisionedZones =
                CopyProvisionedZones(environmentRegister.ProvisionedZones);
            Environment repoItem = MapperFactory.CreateInstance <environmentType, Environment>(item);

            if (environmentRegister.DefaultZone != null)
            {
                repoItem.DefaultZone = CopyDefaultZone(environmentRegister.DefaultZone);
            }

            if (infrastructureServices.Count > 0)
            {
                repoItem.InfrastructureServices =
                    CopyInfrastructureServices(environmentRegister.InfrastructureServices);
            }

            if (provisionedZones.Count > 0)
            {
                repoItem.ProvisionedZones = CopyProvisionedZones(environmentRegister.ProvisionedZones);
            }

            repoItem.SessionToken = sessionToken;
            Guid environmentId = repository.Save(repoItem);

            if (repoItem.InfrastructureServices != null && repoItem.InfrastructureServices.Count > 0)
            {
                InfrastructureService infrastructureService =
                    repoItem.InfrastructureServices[InfrastructureServiceNames.environment];

                if (infrastructureService != null)
                {
                    infrastructureService.Value = infrastructureService.Value + "/" + environmentId;
                    repository.Save(repoItem);
                }
            }

            return(environmentId);
        }