Пример #1
0
        private void okbutton_Click(object sender, RoutedEventArgs e)
        {
            csvtxtOptions                    = new OpenDataFileOptions();
            csvtxtOptions.HasHeader          = HeadersCheckbox.IsChecked == true ? true:false;
            csvtxtOptions.IsBasketData       = BasketDataCheckbox.IsChecked == true ? true : false;
            csvtxtOptions.FieldSeparatorChar = GetSepChar();
            csvtxtOptions.DecimalPointChar   = GetDeciChar();

            if (csvtxtOptions.FieldSeparatorChar != '\0') //close dialog if a non empty character is entered.
            {
                this.Close();
            }
        }
        public DataSource Open(string filename, string sheetname, bool removeSpacesSPSS = false, IOpenDataFileOptions odfo = null)
        {
            if (sheetname == null || sheetname.Trim().Length == 0) //29Apr2015 just to make sure sheetname should have valid chars and not spaces.
            {
                sheetname = string.Empty;
            }
            //int i = 10, j = 0;
            //if (i > 0) i = i / j;
            //filename = filename.ToLower();
            string datasetname   = "Dataset" + SessionDatasetCounter;
            string fileExtension = Path.GetExtension(filename).ToLower();

            if (fileExtension.Equals(".xls") || fileExtension.Equals(".xlsx"))
            {
                datasetname = GetDatasetName(filename, sheetname);
            }
            else
            {
                datasetname = GetDatasetName(filename);
            }
            //28May2018
            //Since now we read rdata files and load multiple data.frames sometimes dataframe
            //names are like Dataset2 or Dataset10 as they were saved-as from BSky grid earlier.
            //Now if rdata file loads a data that we Dataset10 at the time of creating RData file and then
            // we open several other files of other types(except rdata or xlsx) then at some point Dataset10
            //will be assigned to some file. But Dataset10 is already loaded from RData. Here the exception
            //occurs because _datasourcenames hold the old key and we try to add new key with same name. Duplicate
            //key cant be added.
            //Earlier we use to save the filename to make sure keys are different but now RData gives us memory dataframe
            // and we do not save the filename anymore so the uniqu key check we run on _datasources shows that key is
            // not found but when we try to add it to _datasourcenames it already contains the key.
            // basically we now check key in both _datasources  as well as _datasourcenames.
            // if _datasourcenames has 'Dataset10' as key and as a value(same to same) that means its memory dataframe
            // so the key that we generate above using SessionDatasetCounter should be incremented to get new name
            // like Dataset11 or Dataset12 until unique dataset name is not found. and then we follow the exisitng
            //  logic of adding new name to _datasources  as well as _datasourcenames

            ////StringBuilder DSName = new StringBuilder();
            ////for (; SessionDatasetCounter < 1000;)
            ////{
            ////    DSName.Clear();
            ////    DSName.Append("Dataset" + SessionDatasetCounter);//(GetDatasetName(filename));//
            ////    if (_datasourcenames.Keys.Contains(DSName.ToString()))
            ////    {
            ////        //Folowing code may not be needed but can be use to differentiate between memory and file datasets
            ////        // memory datasets have same string for key-values pair
            ////        string val = _datasourcenames[DSName.ToString()];
            ////        if (val.Equals(DSName.ToString()))
            ////        {

            ////        }

            ////        //This is what we need to do to make current dataset name (DatasetNN) unique
            ////        //from the memory loaded datasets
            ////        SessionDatasetCounter++;
            ////    }
            ////    else
            ////    {
            ////        datasetname = DSName.ToString();
            ////        break;
            ////    }
            ////}



            bool       keyfound = false, replaceDS = false;
            DataSource uids = null;

            if (_datasources.Keys.Contains(filename + sheetname)) //Check if filename is same and dataset is already loaded in the grid
            {
                //25Oct2016
                uids = _datasources[filename + sheetname];
                if (uids.RowCount == null || uids.RowCount == 0 || uids.Variables == null || uids.Variables.Count == 0) //if the dataset is NULL dataset
                {
                    keyfound    = true;
                    replaceDS   = true;
                    datasetname = uids.Name; //Dataset1, Dataset2 etc..
                }
                else
                {
                    return(_datasources[filename + sheetname]);
                }
            }

            DataSource loadedDS        = null;//already loaded DataSource whose dataset name is same as the new one
            bool       datasetkeyfound = _datasourcenames.Keys.Contains(datasetname);

            if (!keyfound && datasetkeyfound)// different file but same dataset name (as already loaded in UI)
            {
                //find the existing DataSource that is to be returned (for NO) and overwritten (for YES)
                loadedDS = FindDataSourceFromDatasetname(datasetname);

                //warn user and get user's choice to overwrite already loaded data.frame or not
                string msg1 = "Dataset with the same name is already loaded in the datagrid.\n";
                string msg2 = "Do you want to replace it with the new one?";

                MessageBoxResult mbr = MessageBox.Show(msg1 + msg2, "Overwrite exisiting dataset?", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (mbr == MessageBoxResult.Yes)
                {
                    replaceDS = true;//Overwrite DataSource if user selects YES
                }
                else
                {
                    return(loadedDS);// User selects NO, return this DataSource
                }
            }
            //25Oct2016 string datasetname = "Dataset" + SessionDatasetCounter;//(_datasources.Keys.Count + 1);//can also be filename without path and extention
            //AnalyticsData data = new AnalyticsData();
            //data.Result = datasrc;
            //data.AnalysisType = datasrc.CommandString;//21Oct2013
            UAReturn datasrc = _analyticService.DataSourceLoad(datasetname, filename, sheetname, removeSpacesSPSS, replaceDS, odfo);

            if (datasrc == null)
            {
                logService.WriteToLogLevel("Could not open: " + filename, LogLevelEnum.Error);
                return(null);
            }
            else if (datasrc != null && datasrc.Datasource == null)
            {
                if (datasrc.Error != null && datasrc.Error.Length > 0)
                {
                    logService.WriteToLogLevel("Could not open: " + filename + ".\n" + datasrc.Error, LogLevelEnum.Error);
                }
                else
                {
                    logService.WriteToLogLevel("Could not open: " + filename + ".\nInvalid format OR issue related to R.Net server.", LogLevelEnum.Error);
                    datasrc.Error = "Could not open: " + filename + ".\nInvalid format OR issue related to R.Net server.";
                }
                //string[,] errwarnmsg=OutputHelper.GetMetaData(1, "normal");//08Jun2013
                ////AnalyticsData data = new AnalyticsData();
                ////data.Result = datasrc;
                ////if (!data.Result.Success)
                ////{
                ////    OutputWindowContainer owc = (LifetimeService.Instance.Container.Resolve<IOutputWindowContainer>()) as OutputWindowContainer;//new line
                ////    IOutputWindow _outputWindow = owc.ActiveOutputWindow;//To get active ouput window to populate analysis.AD.
                ////    _outputWindow.Show();
                ////    OutputHelper.Reset();
                ////    _outputWindow.AddAnalyis(data);
                ////}
                //SendToOutput(datasrc);
                DataSource dsnull = new DataSource()
                {
                    Message = datasrc.Error
                };

                //fix for emptydataset.sav. This dataset does not have any rows but has colnames.
                //So when we open it, it is added to uadatasets (say "Dataset2") because read.spss() opens it.
                //But in the UI grid logic it fails to load because of no rows. And so SessionDatasetCounter is not incremented.
                // So for next dataset the same name will be used ( say "Dataset2")
                //Now, if we try to open another dataset (with Dataset2) it causes some issue and end up
                //showing "no rows" message. And now we need to restart the BlueSky app, else we can't open any other dataset
                //EASY FIX is, increment SessionDatasetCounter if "no rows" condition appears. "Dataset3" will be used for
                //another dataset and so everything should be fine on R and C# side.
                //But uadataset will contain an entry for emtydataset, i.e. "Dataset2"
                //LITTLE COMPLICATED FIX is when you see no row(or detect that while opening the dataset in
                // R and stop  it there), you
                // should clean uadataset(for error:'Dataset with the same name already on the global list')
                // and also clean some C# side objects(the dictionary) those map to this empty dataset.
                if (datasrc != null && datasrc.Error != null && datasrc.Error.Equals("No rows in Dataset"))
                {
                    SessionDatasetCounter++;
                }

                return(dsnull);
            }


            /////14Jun2015 ADD alogic here to check for duplicate keys before moving further


            //datasrc.CommandString = "Open Dataset";//21Oct2013
            DataSource ds = datasrc.Datasource.ToClientDataSource();

            if (ds != null)   //03Dec2012
            {
                if (keyfound) //25Oct2016 dataset already existed but was set to null, somehow
                {
                    //remove its old key/val and then add new(outside 'if' )
                    _datasources.Remove(ds.FileName + ds.SheetName);///key filename
                    //21OCt2019 moved in 'if' below _datasourcenames.Remove(datasetname /*+ ds.SheetName*/);//5Mar2014
                }
                else
                {
                    if (loadedDS != null)
                    {
                        _datasources.Remove(loadedDS.FileName + loadedDS.SheetName);///key filename
                    }
                }

                if (datasetkeyfound)
                {
                    _datasourcenames.Remove(datasetname);
                }

                //Add new keys
                if (!_datasources.ContainsKey(ds.FileName + ds.SheetName))             //for avoiding crash
                {
                    _datasources.Add(ds.FileName + ds.SheetName, ds);                  ///key filename
                }
                if (!_datasourcenames.ContainsKey(datasetname))                        //for avoiding crash
                {
                    _datasourcenames.Add(datasetname /*+ ds.SheetName*/, ds.FileName); //5Mar2014
                }
            }

            ///incrementing dataset counter //// 14Feb2013
            //16OCt2019 if(!keyfound) SessionDatasetCounter++;
            SendToOutput(datasrc);
            return(ds);
        }
        //25Oct2016 Added 'replace' parameter (to overwrite a dataset with new values)
        public UAReturn DataSourceLoad(string datasetName, string fileName, string sheetname, bool removeSpacesSPSS = false, bool replace = false, IOpenDataFileOptions odfo = null)
        {
            UAReturn r;

            ServerDataSource dataSource = new ServerDataSource(_userSession.DefaultDispatcher, fileName, datasetName, sheetname, removeSpacesSPSS, replace, odfo);

            r = _userSession.DefaultDispatcher.DataSourceLoad(dataSource, sheetname, removeSpacesSPSS); //,odfo
            //08Jun2013
            //if ( r.Success)
            //{
            //    return r;
            //}

            //return null;
            return(r); //08Jun2013
        }
        public ServerDataSource(CommandDispatcher dispatcher, string fileName, string datasetname, string sheetname = "", bool replace = false, IOpenDataFileOptions odfo = null)
        {
            fileName              = fileName != null ? fileName : string.Empty;           //to avoid crash
            this.Dispatcher       = dispatcher;
            this.FileNameWithPath = fileName;                                             //.ToLower();
            this.FileName         = System.IO.Path.GetFileName(FileNameWithPath);         //filename
            this.SheetName        = sheetname;                                            //29Apr2015
            this.Name             = datasetname;                                          //dataset name assigned by application to the opened dataset(.sav) file
            this.Extension        = Path.GetExtension(fileName).Replace('.', ' ').Trim(); //fileName.Substring(fileName.LastIndexOf(".")+1);
            this.Replace          = replace;                                              //25Oct2016

            //16Nov2017  extra options for opening CSV/TXT/DAT flat files.
            if (odfo == null)
            {
            }
            else
            {
                this.DecimalCharacter = odfo.DecimalPointChar.ToString();
                this.FieldSeparator   = odfo.FieldSeparatorChar.ToString();
                this.HasHeader        = odfo.HasHeader;
                this.IsBasketData     = odfo.IsBasketData;
            }
        }
Пример #5
0
 private void cancelbutton_Click(object sender, RoutedEventArgs e)
 {
     csvtxtOptions = null;
     this.Close();
 }