public void CopyVersion(string applicationName, int newVersionNumber, int copyFromVersionNumber)
        {
            if (!Auth.AllowCreateVersion(applicationName))
            {
                throw new SettingsAuthorizationException(AuthorizationScope.Version, AuthorizationLevel.Create, applicationName, Auth.CurrentIdentity.Id);
            }

            var application = GetApplicationsData(applicationName).SingleOrDefault();

            if (application == null)
            {
                throw new SettingsNotFoundException(Constants.ERROR_APPLICATION_UNKNOWN);
            }

            var copyVersion = GetVersionData(application).SingleOrDefault(v => v.Version == copyFromVersionNumber);
            var newVersion  = GetVersionData(application).SingleOrDefault(v => v.Version == newVersionNumber);

            if (newVersion != null)
            {
                throw new SettingsDuplicateException(Constants.ERROR_VERION_ALREADY_EXISTS);
            }

            if (copyVersion == null)
            {
                throw new SettingsNotFoundException(Constants.ERROR_VERION_UNKNOWN);
            }

            using (TransactionScope scope = TransactionScopeFactory.CreateReaduncommited())
            {
                newVersion = new VersionData();
                newVersion.ApplicationId = application.Id;
                newVersion.Created       = DateTime.UtcNow;
                newVersion.Version       = newVersionNumber;
                application.Versions.Add(newVersion);
                Store.Save();

                foreach (var item in copyVersion.Settings)
                {
                    SettingData setting = SettingData.Copy(item);
                    setting.VersionId = newVersion.Id;
                    Store.Context.Settings.Add(setting);
                }

                Store.Save();
                scope.Complete();
            }
        }
        public void CopyApplication(string applicationName, string newApplicationName, string newApplicationDescription, int version)
        {
            if (!Auth.AllowCreateApplication(applicationName))
            {
                throw new SettingsAuthorizationException(AuthorizationScope.Application, AuthorizationLevel.Create, applicationName, Auth.CurrentIdentity.Id);
            }

            if (string.IsNullOrWhiteSpace(applicationName))
            {
                throw new SettingsStoreException(Constants.ERROR_APPLICATION_NO_NAME);
            }

            var application = GetApplicationsData(applicationName).SingleOrDefault();

            if (application == null)
            {
                throw new SettingsNotFoundException(Constants.ERROR_APPLICATION_UNKNOWN);
            }

            using (TransactionScope scope = new TransactionScope())
            {
                CreateApplication(newApplicationName, newApplicationDescription);

                var newApplication = GetApplicationsData(newApplicationName).Single();

                List <DirectoryData> newDirectories = new List <DirectoryData>();
                List <VersionData>   copyVersions   = new List <VersionData>();
                List <SettingData>   newSettings    = new List <SettingData>();

                foreach (var item in application.Directories)
                {
                    DirectoryData data = new DirectoryData
                    {
                        ApplicationId = newApplication.Id,
                        Name          = item.Name,
                        Description   = item.Description
                    };

                    newApplication.Directories.Add(data);
                    newDirectories.Add(data);
                }
                if (version == 0)
                {
                    copyVersions.AddRange((from v in application.Versions
                                           orderby v.Version descending
                                           select v));
                }
                else
                {
                    copyVersions.Add((from v in application.Versions
                                      orderby v.Version ascending
                                      select v).First());
                }

                foreach (var item in copyVersions)
                {
                    VersionData newVersion = new VersionData();
                    newVersion.Version       = item.Version;
                    newVersion.ApplicationId = newApplication.Id;
                    newVersion.Created       = DateTime.UtcNow;

                    Store.Save();

                    foreach (var setting in item.Settings)
                    {
                        SettingData newSetting = SettingData.Copy(setting);
                        setting.VersionId   = newVersion.Id;
                        setting.DirectoryId = newDirectories.Single(d => d.Name == setting.Directory.Name).Id;
                        newSettings.Add(newSetting);
                    }
                }

                Store.Context.Settings.AddRange(newSettings);
                Store.Save();
                scope.Complete();
            }
        }