Example #1
0
        //Loading filtered data
        private async void LoadData()
        {
            if (!ServerInteractionHelper.IsNetworkAvailable() && !ServerInteractionHelper.TryAccessDatabase())
            {
                AnalyticalInstruments        = new BindableCollection <tblMeasuringDevice>();
                SelectedAnalyticalInstrument = new tblMeasuringDevice();
                return;
            }

            try
            {
                await Task.Run(() =>
                {
                    using (var db = new ApirsRepository <tblMeasuringDevice>())
                    {
                        AnalyticalInstruments = new BindableCollection <tblMeasuringDevice>(db.GetModel().ToList());
                    }
                });

                this.allAnalyticalInstruments = AnalyticalInstruments;

                if (AnalyticalInstruments.Count == 0)
                {
                    SelectedAnalyticalInstrument = new tblMeasuringDevice();
                }
                else if (AnalyticalInstruments.Count > 1)
                {
                    SelectedAnalyticalInstrument = AnalyticalInstruments.First();
                }
                else
                {
                    SelectedAnalyticalInstrument = AnalyticalInstruments.First();
                }
            }
            catch (Exception e)
            {
                AnalyticalInstruments         = new BindableCollection <tblMeasuringDevice>();
                this.allAnalyticalInstruments = AnalyticalInstruments;
                SelectedAnalyticalInstrument  = new tblMeasuringDevice();
            }
        }
        /// <summary>
        /// Uploading a filestream to the server
        /// </summary>
        /// <param name="fileStreamByte"></param>
        /// <returns></returns>
        public static Guid UploadFile(string filePath)
        {
            if (filePath != "")
            {
                //Getting file information
                FileInfo   fi = new FileInfo(filePath);
                FileStream fs;

                try
                {
                    //Implementing a new filestream
                    fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
                }
                catch
                {
                    MessageBox.ShowError("File is being used by another process.");
                    return(new Guid());
                }

                //Transfering filestream into binary format
                BinaryReader rdr      = new BinaryReader(fs);
                byte[]       fileData = rdr.ReadBytes((int)fs.Length);

                //Closing filestream
                rdr.Close();
                fs.Close();

                try
                {
                    //Instantiate database
                    using (ApirsDatabase db = new ApirsDatabase())
                    {
                        //Retrieving file meta data
                        string fileName = fi.Name;
                        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
                        string extension = fi.Extension;
                        char   charac    = 'a';

                        //Changing File name based on the count of occurences
                        while (db.v_FileStore.Where(x => x.name == fileName).Count() > 0)
                        {
                            fileName = fileNameWithoutExtension + charac + extension;
                            charac++;
                        }

                        //Establishing a sql connection
                        using (SqlConnection SqlConn = new SqlConnection(db.Database.Connection.ConnectionString.ToString()))
                        {
                            SqlCommand spAddFile = new SqlCommand("dbo.spAddFile", SqlConn);
                            //Testing if a connection is established
                            //Normally: if (sv.IsNetworkAvailable() && sv.IsServerConnected("130.83.96.87"))
                            if (ServerInteractionHelper.IsNetworkAvailable())
                            {
                                //Preparing the stored procedure
                                spAddFile.CommandType = CommandType.StoredProcedure;

                                //Adding the parameters
                                spAddFile.Parameters.Add("@pName", SqlDbType.NVarChar, 255).Value = fileName;
                                spAddFile.Parameters.Add("@pFile_Stream", SqlDbType.Image, fileData.Length).Value = fileData;

                                //Opening the connection
                                SqlConn.Open();

                                Guid result = (Guid)spAddFile.ExecuteScalar();

                                SqlConn.Close();

                                db.SaveChanges();

                                return(result);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    MessageBox.ShowError(UserMessageValueConverter.ConvertBack(1));
                    return(new Guid());
                }
            }

            return(new Guid());
        }
        /// <summary>
        /// Attempts to register the user
        /// </summary>
        /// <param name="parameter">The SecureString passed in from the view for the users password</param>
        /// <returns></returns>
        public async void Delete()
        {
            if (SelectedPerson.persIdPk == 0)
            {
                ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("You have to be logged in.");
            }

            // If existing window is visible, delete the customer and all their orders.
            // In a real application, you should add warnings and allow the user to cancel the operation.
            if (((ShellViewModel)IoC.Get <IShell>()).ShowQuestion("Are you REALLY sure to delete your user profile?" + Environment.NewLine
                                                                  + "You won't be able to reconstruct your created projects, rock samples and measurements." + Environment.NewLine
                                                                  + "Please be sure, that you have exported all relevant data.") == MessageBoxViewResult.No)
            {
                return;
            }

            CommandHelper ch = new CommandHelper();

            await ch.RunCommand(() => IsUpdateRunning, async() =>
            {
                await Task.Delay(2000);

                try
                {
                    //Establishing a sql connection
                    using (SqlConnection SqlConn = new SqlConnection(this.apirsDatabase.Database.Connection.ConnectionString.ToString()))
                    {
                        //Testing if a connection is established
                        if (ServerInteractionHelper.IsNetworkAvailable() && ServerInteractionHelper.TryAccessDatabase())
                        {
                            //Triggering the delete user sp
                            SqlCommand spDeleteUser  = new SqlCommand("dbo.spDeleteUser", SqlConn);
                            spDeleteUser.CommandType = CommandType.StoredProcedure;
                            //Adding the parameters
                            spDeleteUser.Parameters.Add("@pLogin", SqlDbType.NVarChar, 50);
                            spDeleteUser.Parameters["@pLogin"].Value = SelectedPerson.persUserName;
                            spDeleteUser.Parameters.Add("@responseMessage", SqlDbType.NVarChar, 250).Direction = ParameterDirection.Output;
                            //Executing the stored procedure
                            SqlConn.Open();
                            spDeleteUser.ExecuteNonQuery();
                            var par = Convert.ToString(spDeleteUser.Parameters["@responseMessage"].Value);
                            SqlConn.Close();

                            switch (par)
                            {
                            case "1":
                                ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("You successfully deleted your profile.");
                                _events.PublishOnUIThreadAsync(new ChangeUserMessage(0, "Logged out"));
                                _events.PublishOnUIThreadAsync(new ChangeViewModelMessage("LoginView"));
                                break;

                            default:
                                ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("An unexpected error occured.");
                                break;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                catch (NullReferenceException ne)
                {
                    Console.WriteLine(ne.Message);
                }
                catch (Exception e)
                {
                    ((ShellViewModel)IoC.Get <IShell>()).ShowInformation(e.Message);
                }
            });
        }