//private BaseDataVersionStrategy GetVersioningStrategy()
        //{
        //    var entityType = typeof (TEntity);

        //    var attributes = entityType.GetCustomAttributes(typeof(BaseDataImportAttribute), true) as BaseDataImportAttribute[];

        //    if (attributes == null || !attributes.Any())
        //        return null; // or throw exception

        //    return attributes[0].CreateVersionStrategy<TEntity>();
        //}

        /// <summary>
        /// Loads the data.
        /// </summary>
        public virtual void LoadData()
        {
            try
            {
                ProvideFeedback(string.Format("Loading {0}", Inflector.Pluralize(Inflector.Titleize2(typeof(TEntity).Name))));

                string tableName = typeof(TEntity).EntityTableName();

                using (var session = this.SessionProvider.SessionFactory.OpenStatelessSession())
                {
                    session.CreateSQLQuery(EnableDisableNonClusteredIndexes(tableName, true)) //ALL
                    .ExecuteUpdate();
                }

                using (var session = this.SessionProvider.SessionFactory.OpenStatelessSession())
                {
                    EntityStrategy.CurrentSession = session;
                    using (var reader = DataProvider[Reader.Name])
                    {
                        if (!reader.AlreadyImported())
                        {
                            using (var bulkImporter = new BulkInsert <TEntity, TKey>(session.Connection))
                            {
                                bulkImporter.ConnectionRequested += (o, e) =>
                                {
                                    e.Data = session.Connection as SqlConnection;
                                };
                                bulkImporter.Prepare();
                                bulkImporter.BatchSize = BatchSize;;
                                while (reader.Read())
                                {
                                    var temp = EntityStrategy.LoadFromReader(reader);
                                    bulkImporter.Insert(temp);
                                }
                            }
                        }
                    }
                }

                Task.Factory.StartNew(() =>
                {
                    using (var session = this.SessionProvider.SessionFactory.OpenStatelessSession())
                    {
                        session.CreateSQLQuery(EnableDisableNonClusteredIndexes(tableName)) //ALL
                        .SetTimeout(600)
                        .ExecuteUpdate();
                    }
                }, TaskCreationOptions.LongRunning);
            }
            catch (Exception ex)
            {
                Logger.Write(ex, $"Error importing data for entity type {typeof(TEntity).Name} using strategy {typeof(TStrategy).Name}");
            }
        }
        public override async void OnSave(bool enableNotificantions = false)
        {
            if (string.IsNullOrEmpty(Model.Name) || string.IsNullOrEmpty(Model.GroupPracticePacId))
            {
                MessageBox.Show("Orginization Name and Group Id fields are required", "Invalid Input");
                return;
            }

            int num;

            if (int.TryParse(NumberOfMembers, out num))
            {
                Model.NumberofGroupPracticeMembers = num;
            }
            else if (!string.IsNullOrEmpty(NumberOfMembers))
            {
                MessageBox.Show("Please enter a valid number for Group Practice Members", "Invalid Input");
                return;
            }

            Validate();
            Model.Validate();
            Model.Addresses.ForEach(a => a.Validate());

            if (HasErrors || Model.HasErrors || Model.Addresses.Any(a => a.HasErrors))
            {
                return;
            }

            try
            {
                using (var session = DataServiceProvider.SessionFactory.OpenStatelessSession())
                {
                    if (session.Query <MedicalPractice>().FirstOrDefault(x => x.Id != Model.Id && x.GroupPracticePacId == Model.GroupPracticePacId) != null)
                    {
                        MessageBox.Show("There is already a medical practice with the same Group Practice Pac Id. Group Practice Pac Id must be unique.", "Notification");
                        return;
                    }
                }

                Model.IsEdited = true;

                var       errorOccurred   = false;
                Exception errorException  = null;
                var       progressService = new ProgressService();

                progressService.SetProgress("Saving " + Inflector.Titleize2(typeof(MedicalPractice).Name), 0, false, true);

                // await Task.Delay(500);

                var operationComplete = await progressService.Execute(() => OnSave(Model),
                                                                      result =>
                {
                    if (!result.Status && result.Exception != null)
                    {
                        errorOccurred  = true;
                        errorException = result.Exception;
                    }
                    else
                    {
                        errorOccurred  = false;
                        errorException = null;
                    }
                },
                                                                      new CancellationToken());

                if (operationComplete)
                {
                    progressService.SetProgress("Completed", 100, true, false);

                    if (!errorOccurred || errorException == null)
                    {
                        EventAggregator.GetEvent <GenericNotificationEvent>()
                        .Publish(string.Format("MedicalPractice {0} has been saved successfully.", Model.Name));
                        EventAggregator.GetEvent <RequestLoadMappingTabEvent>().Publish(typeof(MedicalPracticeEditViewModel));
                        NavigateBack();
                    }
                    else
                    {
                        Logger.Write(errorException, "Error saving Medical Practice \"{0}\"", Model.Name);
                    }
                }
            }
            catch (Exception exc)
            {
                Logger.Write(exc, "Error saving Medical Practice \"{0}\"", Model.Name);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Reconciles this instance.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="MonahrqCoreException"></exception>
        protected override bool Reconcile()
        {
            var instalResult = true;

            Events.GetEvent <MessageUpdateEvent>()
            .Publish(new MessageUpdateEvent
            {
                Message = "Loading Open Source Wings"
            });

            try
            {
                //Check that wing is not already registered
                using (var session = Provider.SessionFactory.OpenSession())
                {
                    Infrastructure.Entities.Domain.Wings.Wing wing;

                    if (session.Query <Infrastructure.Entities.Domain.Wings.Wing>()
                        .Any <Infrastructure.Entities.Domain.Wings.Wing>(w => w.WingGUID == new Guid(DynamicTargetWingConstants.WING_MODULE_GUID)))
                    {
                        wing = session.Query <Infrastructure.Entities.Domain.Wings.Wing>()
                               .FirstOrDefault(w => w.WingGUID == new Guid(DynamicTargetWingConstants.WING_MODULE_GUID));
                    }
                    else
                    {
                        wing = this.FactoryWing();
                        session.Save(wing);
                    }

                    foreach (var wingTarget in DynamicTargetService.InstalledDynamicTargets.ToList())
                    {
                        Events.GetEvent <MessageUpdateEvent>()
                        .Publish(new MessageUpdateEvent
                        {
                            Message = "Loading " + Inflector.Titleize2(wingTarget.Name)
                        });

                        var result = DynamicTargetService.InstallDynamicTarget(wingTarget, wing, session);

                        switch (result.Status)
                        {
                        case OpenSourceInstallFlags.Error:
                            throw new MonahrqCoreException(result.Message);

                        case OpenSourceInstallFlags.AlreadyExists:
                            continue;

                        case OpenSourceInstallFlags.Success:
                            var target = result.Target;

                            if (target != null)
                            {
                                OnTargetAdded(session, target, wingTarget);
                            }
                            break;
                        }

                        //DynamicTargetService.ImportMeasures(target, wingTarget, session);
                        //DynamicTargetService.ImportReports(wingTarget, session);

                        //Target target;

                        //var contentTarget = session.Query<Target>()
                        //                           .FirstOrDefault(t => t.Name.ToLower() == wingTarget.Name.ToLower());

                        //using (var tx = session.BeginTransaction())
                        //{

                        //    if (contentTarget != null &&
                        //        session.Query<Target>().Any(w => w.Name.ToUpper() == contentTarget.Name.ToUpper() &&
                        //                                         w.Version.Number.ToUpper() == contentTarget.Version.Number.ToUpper()))
                        //    {
                        //        continue;
                        //    }

                        //    target = wingTarget.CreateTarget(wing);
                        //    target.LoadCustomElements(wingTarget);
                        //    target.LoadCustomScopes(wingTarget);

                        //    Thread.Sleep(1000);

                        //    session.SaveOrUpdate(wing);
                        //    session.Flush();

                        //    tx.Commit();
                        //}

                        //OnTargetAdded(session, target, wingTarget);
                        //DynamicTargetService.OnApplyDynamicTargetDatasetHints(target, wingTarget, session);

                        //if (target != null)
                        //{
                        //    DynamicTargetService.ReconcileMigrations(wingTarget);
                        //}

                        //instalResult = true;
                    }
                }
            }
            catch (Exception exc)
            {
                base.Logger.Write(exc, "Error reconciling dynamic module {0}", this.Description);
                instalResult = false;
            }

            return(instalResult);
        }