private IEnumerable<VersionModel> GetVersions(ApplicationData application)
        {
            if (application == null)
            {
                throw new SettingsNotFoundException(Constants.ERROR_APPLICATION_UNKNOWN);
            }

            var data = GetVersionData(application);

            List<VersionModel> versions = new List<VersionModel>();

            foreach (var item in data)
            {
                VersionModel version = new VersionModel();
                version.Created = item.Created;
                version.Version = item.Version;

                versions.Add(version);
            }

            return versions;
        }
        private IEnumerable<VersionData> GetVersionData(ApplicationData application)
        {
            var data = (from ver in Store.Context.Versions
                        where ver.ApplicationId == application.Id
                        select ver);

            return data;
        }
        private IEnumerable<DirectoryData> GetDirectoriesData(ApplicationData application, string directoryName)
        {
            if (application == null)
            {
                throw new SettingsStoreException(Constants.ERROR_APPLICATION_NO_NAME);
            }

            if (string.IsNullOrWhiteSpace(directoryName))
                directoryName = null;

            var data = (from dir in Store.Context.Directories
                        where dir.Access.FirstOrDefault(acc => acc.ApiKeyId == Auth.CurrentIdentity.Id) != null
                        && dir.ApplicationId == application.Id
                        && (dir.Name == directoryName || true == (directoryName == null))
                        select dir);

            return data;
        }
        private IEnumerable<DirectoryModel> GetDirectories(ApplicationData application, string directoryName, bool includeSettings = false)
        {
            if (application == null)
            {
                throw new SettingsStoreException(Constants.ERROR_APPLICATION_NO_NAME);
            }

            string applicationName = application.Name;

            var data = GetDirectoriesData(application, directoryName);

            List<DirectoryModel> directories = new List<DirectoryModel>();

            foreach (var item in data)
            {
                DirectoryModel model = new DirectoryModel();

                model.AllowCreate = Auth.AllowCreateSetting(applicationName, item.Name);
                model.AllowDelete = Auth.AllowDeleteSetting(applicationName, item.Name);
                model.AllowWrite = Auth.AllowriteSetting(applicationName, item.Name);

                if (item.Description != null)
                    model.Description = item.Description.Trim();

                model.Name = item.Name.Trim();
                directories.Add(model);
            }

            return directories;
        }
 private bool DirectoryExists(ApplicationData application, string directoryName)
 {
     return Store.Context.Directories.Where(dir => dir.ApplicationId == application.Id && dir.Name == directoryName).SingleOrDefault() != null;
 }
        public ApplicationModel CreateApplication(string applicationName, string applicationDescription, string directoryName, string directoryDescription)
        {
            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 = Store.Context.Applications.FirstOrDefault(app => app.Name == applicationName);

            if (application != null)
            {
                throw new SettingsStoreException(Constants.ERROR_APPLICATION_ALREADY_EXISTS);
            }

            if (!NameValidator.ValidateName(applicationName))
            {
                throw new SettingsNotFoundException(Constants.ERROR_APPLICATION_NAME_INVALID);
            }

            application = new ApplicationData();
            DirectoryData cust_directory = null;
            DirectoryData def_directory = null;

            using (TransactionScope scope = new TransactionScope())
            {
                application.Name = applicationName;

                if (string.IsNullOrWhiteSpace(applicationDescription))
                {
                    applicationDescription = Constants.DEAULT_APPLICATION_DESCRIPTION;
                }

                //Create application
                application.Description = applicationDescription.Trim().Replace("  ", " ");
                application.Created = DateTime.UtcNow;

                Store.Context.Applications.Add(application);
                Store.Context.SaveChanges();

                //Create version 1
                VersionData version = new VersionData { Version = 1, Created = DateTime.UtcNow, ApplicationId = application.Id };
                Store.Context.Versions.Add(version);
                Store.Context.SaveChanges();

                //Create application default directory
                def_directory = new DirectoryData();
                def_directory.Name = Constants.DEAULT_DIRECTORY_NAME;
                def_directory.Description = Constants.DEAULT_DIRECTORY_DESCRIPTION;
                def_directory.ApplicationId = application.Id;
                def_directory.Created = DateTime.UtcNow;
                Store.Context.Directories.Add(def_directory);

                //Create custom first directory, if provided.
                if (!string.IsNullOrWhiteSpace(directoryName))
                {
                    cust_directory = new DirectoryData();
                    cust_directory.Name = directoryName.Trim();
                    if (directoryDescription != null)
                        cust_directory.Description = directoryDescription.Trim();
                    cust_directory.ApplicationId = application.Id;
                    cust_directory.Created = DateTime.UtcNow;
                    Store.Context.Directories.Add(cust_directory);
                }

                Store.Context.SaveChanges();

                //Create default api key for applicaiton, a trigger maintains access for the master apikey to the application directories.
                ApiKeyData apiKey = new ApiKeyData { ApiKey = ApiKeyGenerator.Create(), Name = application.Name, Application = application, Active = true, AdminKey = true, Created = DateTime.UtcNow };

                Store.Context.ApiKeys.Add(apiKey);
                Store.Context.SaveChanges();

                //Set access right to default directory.
                DirectoryAccessData access = new DirectoryAccessData();
                access.AllowWrite = true;
                access.AllowDelete = true;
                access.AllowCreate = true;
                access.ApiKey = apiKey;
                access.Directory = def_directory;
                Store.Context.Access.Add(access);

                //Set access right to custom directory.
                if (cust_directory != null)
                {
                    access = new DirectoryAccessData();
                    access.AllowWrite = true;
                    access.AllowDelete = true;
                    access.AllowCreate = true;
                    access.ApiKey = apiKey;
                    access.Directory = cust_directory;
                    Store.Context.Access.Add(access);
                }

                Store.Save();
                scope.Complete();
            }

            Auth.Invalidate();

            Store.Context.Entry<ApplicationData>(application).Reload();

            //reload the enities the reflect the master key access created by the trigger.
            if (cust_directory != null)
                Store.Context.Entry<DirectoryData>(cust_directory).Collection("Access").Load();

            if (def_directory != null)
                Store.Context.Entry<DirectoryData>(def_directory).Collection("Access").Load();

            return GetApplication(applicationName);
        }