Example #1
0
        public ApplicationInformation Save(ApplicationInformation data, UserApplication editor, Abc.Services.Contracts.Application application)
        {
            Contract.Requires <ArgumentNullException>(null != data);
            Contract.Requires <ArgumentNullException>(null != editor);
            Contract.Requires <ArgumentNullException>(null != editor.Application);
            Contract.Requires <ArgumentNullException>(null != editor.User);
            Contract.Requires <ArgumentNullException>(null != application);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != data.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.Application.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.User.Identifier);
            Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != application.Identifier);

            Contract.Ensures(Contract.Result <ApplicationInformation>() != null);

            using (new PerformanceMonitor())
            {
                var source        = new DomainSource();
                var editorData    = source.GetUserById(application.Identifier, editor.User.Identifier);
                var serverDetails = ((IConvert <Details>)data).Convert();
                if (data.IsNew)
                {
                    if (this.PermitApplicationCreation(application, editor.User))
                    {
                        if (data.IsNew)
                        {
                            serverDetails.IsValid = true;
                            app.Create(serverDetails);

                            if (userApplicationCache.ContainsKey(editor.User.Identifier))
                            {
                                userApplicationCache.Remove(editor.User.Identifier);
                            }
                        }
                    }
                    else
                    {
                        Logging.Log("User tried to create an application without authorization.");
                    }
                }
                else
                {
                    if (editorData.RoleValue == (int)RoleType.Manager)
                    {
                        app.Update(serverDetails);

                        if (userApplicationCache.ContainsKey(editor.User.Identifier))
                        {
                            userApplicationCache.Remove(editor.User.Identifier);
                        }
                    }
                    else
                    {
                        Logging.Log("User tried to update an application without authorization.");
                    }
                }

                var appInfoData = ((IConvert <ApplicationInfoData>)data).Convert();
                appInfoData.Load(editorData);

                var table = new AzureTable <ApplicationInfoData>(ServerConfiguration.Default, new ApplicationInfoValidator());

                var existing = this.Get(appInfoData);

                if (null != existing)
                {
                    existing.Environment = appInfoData.Environment;
                    existing.Name        = appInfoData.Name;
                    existing.Description = appInfoData.Description;
                    existing.Deleted     = appInfoData.Deleted;
                    existing.Active      = appInfoData.Active;
                    appInfoData          = existing;
                }
                else
                {
                    appInfoData.CreatedBy = editor.User.Identifier;
                    appInfoData.Owner     = editor.User.Identifier;
                    appInfoData.CreatedOn = DateTime.UtcNow;
                }

                appInfoData.LastUpdatedBy = editor.User.Identifier;
                appInfoData.LastUpdatedOn = DateTime.UtcNow;

                table.AddOrUpdateEntity(appInfoData);

                if (data.IsNew && editorData.RoleValue == (int)RoleType.Client)
                {
                    var save = new Abc.Services.Contracts.Application()
                    {
                        Identifier = serverDetails.ApplicationId,
                    };

                    var ua = new UserApplication()
                    {
                        Application = save,
                        User        = editor.User,
                        Deleted     = false,
                        Active      = true,
                    };

                    this.Save(ua, editor);
                }

                return(data);
            }
        }