Example #1
0
        //temporary solution: norman :FinishUpload2
        public List <Error> FinishUpload(TaskManager taskManager)
        {
            DataStructureManager dsm = new DataStructureManager();
            DatasetManager       dm  = new DatasetManager();

            try
            {
                List <Error>   temp        = new List <Error>();
                DatasetVersion workingCopy = new DatasetVersion();
                //datatuple list
                List <DataTuple> rows            = new List <DataTuple>();
                Dataset          ds              = null;
                bool             inputWasAltered = false;

                if (TaskManager.Bus.ContainsKey(TaskManager.DATASET_ID) && TaskManager.Bus.ContainsKey(TaskManager.DATASTRUCTURE_ID))
                {
                    long id    = Convert.ToInt32(TaskManager.Bus[TaskManager.DATASET_ID]);
                    long iddsd = Convert.ToInt32(TaskManager.Bus[TaskManager.DATASTRUCTURE_ID]);

                    ds = dm.GetDataset(id);
                    // Javad: Please check if the dataset does exists!!

                    //GetValues from the previus version
                    // Status
                    DatasetVersion latestVersion = dm.GetDatasetLatestVersion(ds);
                    string         status        = DatasetStateInfo.NotValid.ToString();
                    if (latestVersion.StateInfo != null)
                    {
                        status = latestVersion.StateInfo.State;
                    }


                    #region Progress Informations

                    if (TaskManager.Bus.ContainsKey(TaskManager.CURRENTPACKAGESIZE))
                    {
                        TaskManager.Bus[TaskManager.CURRENTPACKAGESIZE] = 0;
                    }
                    else
                    {
                        TaskManager.Bus.Add(TaskManager.CURRENTPACKAGESIZE, 0);
                    }

                    if (TaskManager.Bus.ContainsKey(TaskManager.CURRENTPACKAGE))
                    {
                        TaskManager.Bus[TaskManager.CURRENTPACKAGE] = 0;
                    }
                    else
                    {
                        TaskManager.Bus.Add(TaskManager.CURRENTPACKAGE, 0);
                    }

                    #endregion

                    #region structured data

                    if (TaskManager.Bus.ContainsKey(TaskManager.DATASTRUCTURE_TYPE) && TaskManager.Bus[TaskManager.DATASTRUCTURE_TYPE].Equals(DataStructureType.Structured))
                    {
                        string           title            = "";
                        long             datasetid        = ds.Id;
                        XmlDatasetHelper xmlDatasetHelper = new XmlDatasetHelper();
                        title = xmlDatasetHelper.GetInformation(ds.Id, NameAttributeValues.title);
                        try
                        {
                            //Stopwatch fullTime = Stopwatch.StartNew();

                            //Stopwatch loadDT = Stopwatch.StartNew();
                            List <long> datatupleFromDatabaseIds = dm.GetDatasetVersionEffectiveTupleIds(dm.GetDatasetLatestVersion(ds.Id));
                            //loadDT.Stop();
                            //Debug.WriteLine("Load DT From Db Time " + loadDT.Elapsed.TotalSeconds.ToString());

                            StructuredDataStructure sds = dsm.StructuredDataStructureRepo.Get(iddsd);
                            dsm.StructuredDataStructureRepo.LoadIfNot(sds.Variables);

                            #region excel reader

                            if (TaskManager.Bus[TaskManager.EXTENTION].ToString().Equals(".xlsm"))
                            {
                                int packageSize = 10000;

                                TaskManager.Bus[TaskManager.CURRENTPACKAGESIZE] = packageSize;

                                int counter = 0;

                                ExcelReader reader = new ExcelReader();

                                //schleife
                                dm.CheckOutDatasetIfNot(ds.Id, GetUsernameOrDefault()); // there are cases, the dataset does not get checked out!!
                                if (!dm.IsDatasetCheckedOutFor(ds.Id, GetUsernameOrDefault()))
                                {
                                    throw new Exception(string.Format("Not able to checkout dataset '{0}' for  user '{1}'!", ds.Id, GetUsernameOrDefault()));
                                }

                                workingCopy = dm.GetDatasetWorkingCopy(ds.Id);

                                //set StateInfo of the previus version
                                if (workingCopy.StateInfo == null)
                                {
                                    workingCopy.StateInfo = new Vaiona.Entities.Common.EntityStateInfo()
                                    {
                                        State = status
                                    };
                                }
                                else
                                {
                                    workingCopy.StateInfo.State = status;
                                }


                                do
                                {
                                    //Stopwatch packageTime = Stopwatch.StartNew();

                                    counter++;
                                    TaskManager.Bus[TaskManager.CURRENTPACKAGE] = counter;

                                    // open file
                                    Stream = reader.Open(TaskManager.Bus[TaskManager.FILEPATH].ToString());
                                    Stopwatch upload = Stopwatch.StartNew();
                                    rows = reader.ReadFile(Stream, TaskManager.Bus[TaskManager.FILENAME].ToString(), sds, (int)id, packageSize);
                                    upload.Stop();
                                    Debug.WriteLine("ReadFile: " + counter + "  Time " + upload.Elapsed.TotalSeconds.ToString());

                                    if (reader.ErrorMessages.Count > 0)
                                    {
                                        //model.ErrorList = reader.errorMessages;
                                    }
                                    else
                                    {
                                        //XXX Add packagesize to excel read function
                                        if (TaskManager.Bus.ContainsKey(TaskManager.DATASET_STATUS))
                                        {
                                            if (TaskManager.Bus[TaskManager.DATASET_STATUS].ToString().Equals("new"))
                                            {
                                                upload = Stopwatch.StartNew();
                                                dm.EditDatasetVersion(workingCopy, rows, null, null);
                                                upload.Stop();
                                                Debug.WriteLine("EditDatasetVersion: " + counter + "  Time " + upload.Elapsed.TotalSeconds.ToString());
                                                //Debug.WriteLine("----");
                                            }
                                            if (TaskManager.Bus[TaskManager.DATASET_STATUS].ToString().Equals("edit"))
                                            {
                                                if (rows.Count() > 0)
                                                {
                                                    //Stopwatch split = Stopwatch.StartNew();
                                                    Dictionary <string, List <DataTuple> > splittedDatatuples = new Dictionary <string, List <DataTuple> >();
                                                    splittedDatatuples = uploadWizardHelper.GetSplitDatatuples(rows, (List <long>)TaskManager.Bus[TaskManager.PRIMARY_KEYS], workingCopy, ref datatupleFromDatabaseIds);
                                                    //split.Stop();
                                                    //Debug.WriteLine("Split : " + counter + "  Time " + split.Elapsed.TotalSeconds.ToString());

                                                    //Stopwatch upload = Stopwatch.StartNew();
                                                    dm.EditDatasetVersion(workingCopy, splittedDatatuples["new"], splittedDatatuples["edit"], null);
                                                    //    upload.Stop();
                                                    //    Debug.WriteLine("Upload : " + counter + "  Time " + upload.Elapsed.TotalSeconds.ToString());
                                                    //    Debug.WriteLine("----");
                                                }
                                            }
                                        }
                                        else
                                        {
                                        }
                                    }

                                    Stream.Close();

                                    //packageTime.Stop();
                                    //Debug.WriteLine("Package : " + counter + " packageTime Time " + packageTime.Elapsed.TotalSeconds.ToString());
                                } while (rows.Count() > 0);

                                //fullTime.Stop();
                                //Debug.WriteLine("FullTime " + fullTime.Elapsed.TotalSeconds.ToString());
                            }

                            #endregion

                            #region ascii reader


                            if (TaskManager.Bus[TaskManager.EXTENTION].ToString().Equals(".csv") ||
                                TaskManager.Bus[TaskManager.EXTENTION].ToString().Equals(".txt"))
                            {
                                // open file
                                AsciiReader reader = new AsciiReader();
                                //Stream = reader.Open(TaskManager.Bus[TaskManager.FILEPATH].ToString());

                                //DatasetManager dm = new DatasetManager();
                                //Dataset ds = dm.GetDataset(id);

                                Stopwatch totalTime = Stopwatch.StartNew();

                                if (dm.IsDatasetCheckedOutFor(ds.Id, GetUsernameOrDefault()) || dm.CheckOutDataset(ds.Id, GetUsernameOrDefault()))
                                {
                                    workingCopy = dm.GetDatasetWorkingCopy(ds.Id);
                                    int packageSize = 100000;
                                    TaskManager.Bus[TaskManager.CURRENTPACKAGESIZE] = packageSize;
                                    //schleife
                                    int counter = 0;

                                    //set StateInfo of the previus version
                                    if (workingCopy.StateInfo == null)
                                    {
                                        workingCopy.StateInfo = new Vaiona.Entities.Common.EntityStateInfo()
                                        {
                                            State = status
                                        };
                                    }
                                    else
                                    {
                                        workingCopy.StateInfo.State = status;
                                    }

                                    do
                                    {
                                        counter++;
                                        inputWasAltered = false;
                                        TaskManager.Bus[TaskManager.CURRENTPACKAGE] = counter;

                                        Stream = reader.Open(TaskManager.Bus[TaskManager.FILEPATH].ToString());
                                        rows   = reader.ReadFile(Stream, TaskManager.Bus[TaskManager.FILENAME].ToString(), (AsciiFileReaderInfo)TaskManager.Bus[TaskManager.FILE_READER_INFO], sds, id, packageSize);
                                        Stream.Close();

                                        if (reader.ErrorMessages.Count > 0)
                                        {
                                            foreach (var err in reader.ErrorMessages)
                                            {
                                                temp.Add(new Error(ErrorType.Dataset, err.GetMessage()));
                                            }
                                            //return temp;
                                        }
                                        //model.Validated = true;
                                        Stopwatch dbTimer = Stopwatch.StartNew();

                                        if (TaskManager.Bus.ContainsKey(TaskManager.DATASET_STATUS))
                                        {
                                            if (TaskManager.Bus[TaskManager.DATASET_STATUS].ToString().Equals("new"))
                                            {
                                                dm.EditDatasetVersion(workingCopy, rows, null, null);
                                            }

                                            if (TaskManager.Bus[TaskManager.DATASET_STATUS].ToString().Equals("edit"))
                                            {
                                                if (rows.Count() > 0)
                                                {
                                                    //Dictionary<string, List<DataTuple>> splittedDatatuples = new Dictionary<string, List<AbstractTuple>>();
                                                    var splittedDatatuples = uploadWizardHelper.GetSplitDatatuples(rows, (List <long>)TaskManager.Bus[TaskManager.PRIMARY_KEYS], workingCopy, ref datatupleFromDatabaseIds);
                                                    dm.EditDatasetVersion(workingCopy, splittedDatatuples["new"], splittedDatatuples["edit"], null);
                                                    inputWasAltered = true;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (rows.Count() > 0)
                                            {
                                                Dictionary <string, List <DataTuple> > splittedDatatuples = new Dictionary <string, List <DataTuple> >();
                                                splittedDatatuples = uploadWizardHelper.GetSplitDatatuples(rows, (List <long>)TaskManager.Bus[TaskManager.PRIMARY_KEYS], workingCopy, ref datatupleFromDatabaseIds);
                                                dm.EditDatasetVersion(workingCopy, splittedDatatuples["new"], splittedDatatuples["edit"], null);
                                                inputWasAltered = true;
                                            }
                                        }

                                        dbTimer.Stop();
                                        Debug.WriteLine(" db time" + dbTimer.Elapsed.TotalSeconds.ToString());
                                    } while (rows.Count() > 0 || inputWasAltered == true);



                                    totalTime.Stop();
                                    Debug.WriteLine(" Total Time " + totalTime.Elapsed.TotalSeconds.ToString());
                                }

                                //Stream.Close();
                            }

                            #endregion

                            #region contentdescriptors

                            //remove all contentdescriptors from the old version
                            //generatedTXT
                            if (workingCopy.ContentDescriptors.Any(c => c.Name.Equals("generatedTXT")))
                            {
                                ContentDescriptor tmp =
                                    workingCopy.ContentDescriptors.Where(c => c.Name.Equals("generatedTXT"))
                                    .FirstOrDefault();
                                dm.DeleteContentDescriptor(tmp);
                            }

                            //generatedCSV
                            if (workingCopy.ContentDescriptors.Any(c => c.Name.Equals("generatedCSV")))
                            {
                                ContentDescriptor tmp =
                                    workingCopy.ContentDescriptors.Where(c => c.Name.Equals("generatedCSV"))
                                    .FirstOrDefault();
                                dm.DeleteContentDescriptor(tmp);
                            }
                            //generated
                            if (workingCopy.ContentDescriptors.Any(c => c.Name.Equals("generated")))
                            {
                                ContentDescriptor tmp =
                                    workingCopy.ContentDescriptors.Where(c => c.Name.Equals("generated"))
                                    .FirstOrDefault();
                                dm.DeleteContentDescriptor(tmp);
                            }


                            #endregion


                            // ToDo: Get Comment from ui and users
                            MoveAndSaveOriginalFileInContentDiscriptor(workingCopy);
                            dm.CheckInDataset(ds.Id, "upload data from upload wizard", GetUsernameOrDefault());


                            //send email
                            var es = new EmailService();
                            es.Send(MessageHelper.GetUpdateDatasetHeader(),
                                    MessageHelper.GetUpdateDatasetMessage(datasetid, title, GetUsernameOrDefault()),
                                    ConfigurationManager.AppSettings["SystemEmail"]
                                    );
                        }
                        catch (Exception e)
                        {
                            temp.Add(new Error(ErrorType.Other, "Can not upload. : " + e.Message));
                            var es = new EmailService();
                            es.Send(MessageHelper.GetErrorHeader(),
                                    "Can not upload. : " + e.Message,
                                    ConfigurationManager.AppSettings["SystemEmail"]
                                    );
                        }
                        finally
                        {
                        }
                    }

                    #endregion

                    #region unstructured data

                    if (TaskManager.Bus.ContainsKey(TaskManager.DATASTRUCTURE_TYPE) && TaskManager.Bus[TaskManager.DATASTRUCTURE_TYPE].Equals(DataStructureType.Unstructured))
                    {
                        // checkout the dataset, apply the changes, and check it in.
                        if (dm.IsDatasetCheckedOutFor(ds.Id, GetUsernameOrDefault()) || dm.CheckOutDataset(ds.Id, GetUsernameOrDefault()))
                        {
                            try
                            {
                                workingCopy = dm.GetDatasetWorkingCopy(ds.Id);



                                using (var unitOfWork = this.GetUnitOfWork())
                                {
                                    workingCopy = unitOfWork.GetReadOnlyRepository <DatasetVersion>().Get(workingCopy.Id);

                                    //set StateInfo of the previus version
                                    if (workingCopy.StateInfo == null)
                                    {
                                        workingCopy.StateInfo = new Vaiona.Entities.Common.EntityStateInfo()
                                        {
                                            State = status
                                        };
                                    }
                                    else
                                    {
                                        workingCopy.StateInfo.State = status;
                                    }

                                    unitOfWork.GetReadOnlyRepository <DatasetVersion>().Load(workingCopy.ContentDescriptors);

                                    SaveFileInContentDiscriptor(workingCopy);
                                }
                                dm.EditDatasetVersion(workingCopy, null, null, null);

                                // ToDo: Get Comment from ui and users
                                dm.CheckInDataset(ds.Id, "upload unstructured data", GetUsernameOrDefault(), ViewCreationBehavior.None);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                    }

                    #endregion
                }
                else
                {
                    temp.Add(new Error(ErrorType.Dataset, "Dataset is not selected."));
                }

                if (temp.Count <= 0)
                {
                    dm.CheckInDataset(ds.Id, "checked in but no update on data tuples", GetUsernameOrDefault(), ViewCreationBehavior.None);
                }
                else
                {
                    dm.UndoCheckoutDataset(ds.Id, GetUsernameOrDefault());
                }
                return(temp);
            }
            finally
            {
                dm.Dispose();
                dsm.Dispose();
            }
        }
        public bool deleteFile(string path)
        {
            path = Server.UrlDecode(path);
            {
                using (EntityPermissionManager entityPermissionManager = new EntityPermissionManager())
                    using (DatasetManager datasetManager = new DatasetManager())
                    {
                        try
                        {
                            DatasetInfo datasetInfo = (DatasetInfo)Session["DatasetInfo"];
                            string      entityType  = (string)Session["EntityType"];

                            DatasetVersion workingCopy = new DatasetVersion();
                            string         status      = DatasetStateInfo.NotValid.ToString();
                            string[]       temp        = path.Split('\\');
                            long           datasetID   = datasetInfo.DatasetId;
                            status = datasetManager.GetDatasetLatestVersion(datasetID).StateInfo.State;
                            bool access = entityPermissionManager.HasEffectiveRight(HttpContext.User.Identity.Name, typeof(Dataset), datasetID, RightType.Delete);

                            if (access && (datasetManager.IsDatasetCheckedOutFor(datasetID, HttpContext.User.Identity.Name) || datasetManager.CheckOutDataset(datasetID, HttpContext.User.Identity.Name)))
                            {
                                try
                                {
                                    workingCopy = datasetManager.GetDatasetWorkingCopy(datasetID);

                                    using (var unitOfWork = this.GetUnitOfWork())
                                    {
                                        workingCopy = unitOfWork.GetReadOnlyRepository <DatasetVersion>().Get(workingCopy.Id);

                                        //set StateInfo of the previus version
                                        if (workingCopy.StateInfo == null)
                                        {
                                            workingCopy.StateInfo = new Vaiona.Entities.Common.EntityStateInfo()
                                            {
                                                State = status
                                            };
                                        }
                                        else
                                        {
                                            workingCopy.StateInfo.State = status;
                                        }

                                        unitOfWork.GetReadOnlyRepository <DatasetVersion>().Load(workingCopy.ContentDescriptors);

                                        ContentDescriptor contentDescriptor = workingCopy.ContentDescriptors.Where(cd => cd.URI.Equals(path)).FirstOrDefault();
                                        datasetManager.DeleteContentDescriptor(contentDescriptor);
                                    }

                                    //set modification
                                    workingCopy.ModificationInfo = new EntityAuditInfo()
                                    {
                                        Performer  = HttpContext.User?.Identity?.Name,
                                        Comment    = "File",
                                        ActionType = AuditActionType.Delete
                                    };

                                    // set system key values
                                    int v = 1;
                                    if (workingCopy.Dataset.Versions != null && workingCopy.Dataset.Versions.Count > 1)
                                    {
                                        v = workingCopy.Dataset.Versions.Count();
                                    }
                                    workingCopy.Metadata = setSystemValuesToMetadata(v, workingCopy.Dataset.MetadataStructure.Id, workingCopy.Metadata);

                                    datasetManager.EditDatasetVersion(workingCopy, null, null, null);

                                    // ToDo: Get Comment from ui and users
                                    datasetManager.CheckInDataset(datasetID, temp.Last(), HttpContext.User.Identity.Name, ViewCreationBehavior.None);
                                    Session["DatasetInfo"] = datasetInfo;
                                    Session["EntityType"]  = entityType;
                                    return(true);
                                }
                                catch
                                {
                                    datasetManager.CheckInDataset(datasetID, "Failed to delete File " + temp.Last(), HttpContext.User.Identity.Name, ViewCreationBehavior.None);
                                    Session["DatasetInfo"] = datasetInfo;
                                    Session["EntityType"]  = entityType;
                                    return(false);
                                }
                            }
                            Session["DatasetInfo"] = datasetInfo;
                            Session["EntityType"]  = entityType;
                            return(false);
                        }
                        catch
                        {
                            return(false);
                        }
                    }
            }
        }
Example #3
0
        //temporary solution: norman :FinishUpload2
        public async Task <List <Error> > FinishUpload()
        {
            DataStructureManager dsm       = new DataStructureManager();
            DatasetManager       dm        = new DatasetManager();
            IOUtility            iOUtility = new IOUtility();
            List <Error>         temp      = new List <Error>();
            long   id                  = 0;
            string title               = "";
            int    numberOfRows        = 0;
            int    numberOfSkippedRows = 0;

            try
            {
                DatasetVersion workingCopy = new DatasetVersion();
                //datatuple list
                List <DataTuple> rows = new List <DataTuple>();
                //Dataset ds = null;
                bool inputWasAltered = false;

                if (Bus.ContainsKey(TaskManager.DATASET_ID) && Bus.ContainsKey(TaskManager.DATASTRUCTURE_ID))
                {
                    id = Convert.ToInt32(Bus[TaskManager.DATASET_ID]);
                    long iddsd = Convert.ToInt32(Bus[TaskManager.DATASTRUCTURE_ID]);

                    //GetValues from the previus version
                    // Status
                    DatasetVersion latestVersion = dm.GetDatasetLatestVersion(id);
                    title = latestVersion.Title;
                    string status = DatasetStateInfo.NotValid.ToString();
                    if (latestVersion.StateInfo != null)
                    {
                        status = latestVersion.StateInfo.State;
                    }

                    #region Progress Informations

                    if (Bus.ContainsKey(TaskManager.CURRENTPACKAGESIZE))
                    {
                        Bus[TaskManager.CURRENTPACKAGESIZE] = 0;
                    }
                    else
                    {
                        Bus.Add(TaskManager.CURRENTPACKAGESIZE, 0);
                    }

                    if (Bus.ContainsKey(TaskManager.CURRENTPACKAGE))
                    {
                        Bus[TaskManager.CURRENTPACKAGE] = 0;
                    }
                    else
                    {
                        Bus.Add(TaskManager.CURRENTPACKAGE, 0);
                    }

                    #endregion Progress Informations

                    #region structured data

                    if (Bus.ContainsKey(TaskManager.DATASTRUCTURE_TYPE) && Bus[TaskManager.DATASTRUCTURE_TYPE].Equals(DataStructureType.Structured))
                    {
                        long             datasetid        = id;
                        XmlDatasetHelper xmlDatasetHelper = new XmlDatasetHelper();



                        try
                        {
                            // load all data tuple ids from the latest version
                            List <long> datatupleFromDatabaseIds = dm.GetDatasetVersionEffectiveTupleIds(dm.GetDatasetLatestVersion(id));

                            // load structured data structure
                            StructuredDataStructure sds = dsm.StructuredDataStructureRepo.Get(iddsd);
                            dsm.StructuredDataStructureRepo.LoadIfNot(sds.Variables);

                            #region excel reader

                            if (Bus[TaskManager.EXTENTION].ToString().Equals(".xlsm") ||
                                iOUtility.IsSupportedExcelFile(Bus[TaskManager.EXTENTION].ToString()))
                            {
                                int packageSize = 100000;

                                Bus[TaskManager.CURRENTPACKAGESIZE] = packageSize;

                                int counter = 0;

                                //schleife
                                dm.CheckOutDatasetIfNot(id, User.Name); // there are cases, the dataset does not get checked out!!
                                if (!dm.IsDatasetCheckedOutFor(id, User.Name))
                                {
                                    throw new Exception(string.Format("Not able to checkout dataset '{0}' for  user '{1}'!", id, User.Name));
                                }

                                workingCopy = dm.GetDatasetWorkingCopy(id);

                                //set StateInfo of the previus version
                                if (workingCopy.StateInfo == null)
                                {
                                    workingCopy.StateInfo = new Vaiona.Entities.Common.EntityStateInfo()
                                    {
                                        State = status
                                    };
                                }
                                else
                                {
                                    workingCopy.StateInfo.State = status;
                                }

                                ExcelReader         reader = null;
                                ExcelFileReaderInfo excelFileReaderInfo = null;

                                if (iOUtility.IsSupportedExcelFile(Bus[TaskManager.EXTENTION].ToString()))
                                {
                                    excelFileReaderInfo = (ExcelFileReaderInfo)Bus[TaskManager.FILE_READER_INFO];
                                }

                                reader = new ExcelReader(sds, excelFileReaderInfo);

                                do
                                {
                                    counter++;
                                    Bus[TaskManager.CURRENTPACKAGE] = counter;

                                    //open stream
                                    Stream = reader.Open(Bus[TaskManager.FILEPATH].ToString());
                                    rows   = new List <DataTuple>();

                                    if (iOUtility.IsSupportedExcelFile(Bus[TaskManager.EXTENTION].ToString()))
                                    {
                                        if (reader.Position < excelFileReaderInfo.DataEndRow)
                                        {
                                            rows = reader.ReadFile(Stream, Bus[TaskManager.FILENAME].ToString(), (int)id, packageSize);
                                        }
                                    }
                                    else
                                    {
                                        rows = reader.ReadTemplateFile(Stream, Bus[TaskManager.FILENAME].ToString(), (int)id, packageSize);
                                    }

                                    //Debug.WriteLine("ReadFile: " + counter + "  Time " + upload.Elapsed.TotalSeconds.ToString());

                                    if (reader.ErrorMessages.Count > 0)
                                    {
                                        //model.ErrorList = reader.errorMessages;
                                    }
                                    else
                                    {
                                        //XXX Add packagesize to excel read function
                                        if (Bus.ContainsKey(TaskManager.DATASET_STATUS))
                                        {
                                            if (Bus[TaskManager.DATASET_STATUS].ToString().Equals("new") || ((UploadMethod)Bus[TaskManager.UPLOAD_METHOD]).Equals(UploadMethod.Append))
                                            {
                                                dm.EditDatasetVersion(workingCopy, rows, null, null);
                                                //Debug.WriteLine("EditDatasetVersion: " + counter + "  Time " + upload.Elapsed.TotalSeconds.ToString());
                                                //Debug.WriteLine("----");
                                            }
                                            else
                                            if (Bus[TaskManager.DATASET_STATUS].ToString().Equals("edit"))
                                            {
                                                if (rows.Count() > 0)
                                                {
                                                    Dictionary <string, List <DataTuple> > splittedDatatuples = new Dictionary <string, List <DataTuple> >();
                                                    splittedDatatuples = uploadWizardHelper.GetSplitDatatuples(rows, (List <long>)Bus[TaskManager.PRIMARY_KEYS], workingCopy, ref datatupleFromDatabaseIds);

                                                    dm.EditDatasetVersion(workingCopy, splittedDatatuples["new"], splittedDatatuples["edit"], null);
                                                }
                                            }
                                        }
                                        else
                                        {
                                        }
                                    }
                                    Stream?.Close();

                                    //count rows
                                    numberOfRows += rows.Count();
                                } while (rows.Count() > 0 && rows.Count() <= packageSize);

                                numberOfSkippedRows = reader.NumberOSkippedfRows;
                            }

                            #endregion excel reader

                            #region ascii reader

                            if (iOUtility.IsSupportedAsciiFile(Bus[TaskManager.EXTENTION].ToString()))
                            {
                                // open file
                                AsciiReader reader = new AsciiReader(sds, (AsciiFileReaderInfo)Bus[TaskManager.FILE_READER_INFO]);

                                if (dm.IsDatasetCheckedOutFor(id, User.Name) || dm.CheckOutDataset(id, User.Name))
                                {
                                    workingCopy = dm.GetDatasetWorkingCopy(id);

                                    //set packagsize for one loop
                                    int packageSize = 100000;
                                    Bus[TaskManager.CURRENTPACKAGESIZE] = packageSize;

                                    //schleife
                                    int counter = 0;

                                    //set StateInfo of the previus version
                                    if (workingCopy.StateInfo == null)
                                    {
                                        workingCopy.StateInfo = new Vaiona.Entities.Common.EntityStateInfo()
                                        {
                                            State = status
                                        };
                                    }
                                    else
                                    {
                                        workingCopy.StateInfo.State = status;
                                    }

                                    do
                                    {
                                        counter++;
                                        inputWasAltered = false;
                                        Bus[TaskManager.CURRENTPACKAGE] = counter;

                                        Stream = reader.Open(Bus[TaskManager.FILEPATH].ToString());
                                        rows   = reader.ReadFile(Stream, Bus[TaskManager.FILENAME].ToString(), id, packageSize);
                                        Stream.Close();

                                        if (reader.ErrorMessages.Count > 0)
                                        {
                                            foreach (var err in reader.ErrorMessages)
                                            {
                                                temp.Add(new Error(ErrorType.Dataset, err.GetMessage()));
                                            }
                                            //return temp;
                                        }

                                        if (Bus.ContainsKey(TaskManager.DATASET_STATUS)) //check wheter there is a dataset status in the upload wizard bus
                                        {
                                            // based the dataset status and/ or the upload method
                                            if (Bus[TaskManager.DATASET_STATUS].ToString().Equals("new") || ((UploadMethod)Bus[TaskManager.UPLOAD_METHOD]).Equals(UploadMethod.Append))
                                            {
                                                dm.EditDatasetVersion(workingCopy, rows, null, null); // add all datatuples to the datasetversion
                                            }
                                            else
                                            if (Bus[TaskManager.DATASET_STATUS].ToString().Equals("edit")) // datatuples allready exist
                                            {
                                                if (rows.Count() > 0)
                                                {
                                                    //split the incoming datatuples to (new|edit) based on the primary keys
                                                    var splittedDatatuples = uploadWizardHelper.GetSplitDatatuples(rows, (List <long>)Bus[TaskManager.PRIMARY_KEYS], workingCopy, ref datatupleFromDatabaseIds);
                                                    dm.EditDatasetVersion(workingCopy, splittedDatatuples["new"], splittedDatatuples["edit"], null);
                                                    inputWasAltered = true;
                                                }
                                            }
                                        }
                                        else // if there is no dataset status in the bus, use dataset status edit
                                        {
                                            if (rows.Count() > 0)
                                            {
                                                //split the incoming datatuples to (new|edit) based on the primary keys
                                                Dictionary <string, List <DataTuple> > splittedDatatuples = new Dictionary <string, List <DataTuple> >();
                                                splittedDatatuples = uploadWizardHelper.GetSplitDatatuples(rows, (List <long>)Bus[TaskManager.PRIMARY_KEYS], workingCopy, ref datatupleFromDatabaseIds);
                                                dm.EditDatasetVersion(workingCopy, splittedDatatuples["new"], splittedDatatuples["edit"], null);
                                                inputWasAltered = true;
                                            }
                                        }

                                        //count rows
                                        numberOfRows += rows.Count();
                                    } while ((rows.Count() > 0 && rows.Count() <= packageSize) || inputWasAltered == true);

                                    numberOfSkippedRows = reader.NumberOSkippedfRows;
                                }

                                //Stream.Close();
                            }

                            #endregion ascii reader

                            #region contentdescriptors

                            //remove all contentdescriptors from the old version
                            //generatedTXT
                            if (workingCopy.ContentDescriptors.Any(c => c.Name.Equals("generatedTXT")))
                            {
                                ContentDescriptor tmp =
                                    workingCopy.ContentDescriptors.Where(c => c.Name.Equals("generatedTXT"))
                                    .FirstOrDefault();
                                dm.DeleteContentDescriptor(tmp);
                            }

                            //generatedCSV
                            if (workingCopy.ContentDescriptors.Any(c => c.Name.Equals("generatedCSV")))
                            {
                                ContentDescriptor tmp =
                                    workingCopy.ContentDescriptors.Where(c => c.Name.Equals("generatedCSV"))
                                    .FirstOrDefault();
                                dm.DeleteContentDescriptor(tmp);
                            }
                            //generated
                            if (workingCopy.ContentDescriptors.Any(c => c.Name.Equals("generated")))
                            {
                                ContentDescriptor tmp =
                                    workingCopy.ContentDescriptors.Where(c => c.Name.Equals("generated"))
                                    .FirstOrDefault();
                                dm.DeleteContentDescriptor(tmp);
                            }

                            #endregion contentdescriptors

                            #region set System value into metadata

                            if (Bus.ContainsKey(TaskManager.DATASET_STATUS))
                            {
                                bool newdataset = Bus[TaskManager.DATASET_STATUS].ToString().Equals("new");
                                int  v          = 1;
                                if (workingCopy.Dataset.Versions != null && workingCopy.Dataset.Versions.Count > 1)
                                {
                                    v = workingCopy.Dataset.Versions.Count();
                                }

                                //set modification
                                workingCopy.ModificationInfo = new EntityAuditInfo()
                                {
                                    Performer  = User.Name,
                                    Comment    = "Data",
                                    ActionType = newdataset ? AuditActionType.Create : AuditActionType.Edit
                                };

                                setSystemValuesToMetadata(id, v, workingCopy.Dataset.MetadataStructure.Id, workingCopy.Metadata, newdataset);
                                dm.EditDatasetVersion(workingCopy, null, null, null);
                            }

                            #endregion set System value into metadata

                            // ToDo: Get Comment from ui and users
                            MoveAndSaveOriginalFileInContentDiscriptor(workingCopy);
                            dm.CheckInDataset(id, numberOfRows + " rows", User.Name);

                            //send email
                            var es = new EmailService();
                            es.Send(MessageHelper.GetUpdateDatasetHeader(datasetid),
                                    MessageHelper.GetUpdateDatasetMessage(datasetid, title, User.DisplayName),
                                    ConfigurationManager.AppSettings["SystemEmail"]
                                    );
                        }
                        catch (Exception e)
                        {
                            temp.Add(new Error(ErrorType.Other, "Can not upload. : " + e.Message));
                            var es = new EmailService();
                            es.Send(MessageHelper.GetErrorHeader(),
                                    "Can not upload. : " + e.Message,
                                    ConfigurationManager.AppSettings["SystemEmail"]
                                    );
                        }
                        finally
                        {
                        }
                    }

                    #endregion structured data

                    #region unstructured data

                    if (Bus.ContainsKey(TaskManager.DATASTRUCTURE_TYPE) && Bus[TaskManager.DATASTRUCTURE_TYPE].Equals(DataStructureType.Unstructured))
                    {
                        // checkout the dataset, apply the changes, and check it in.
                        if (dm.IsDatasetCheckedOutFor(id, User.Name) || dm.CheckOutDataset(id, User.Name))
                        {
                            try
                            {
                                workingCopy = dm.GetDatasetWorkingCopy(id);

                                using (var unitOfWork = this.GetUnitOfWork())
                                {
                                    workingCopy.VersionNo += 1;

                                    //set StateInfo of the previus version
                                    if (workingCopy.StateInfo == null)
                                    {
                                        workingCopy.StateInfo = new Vaiona.Entities.Common.EntityStateInfo()
                                        {
                                            State = status
                                        };
                                    }
                                    else
                                    {
                                        workingCopy.StateInfo.State = status;
                                    }

                                    unitOfWork.GetReadOnlyRepository <DatasetVersion>().Load(workingCopy.ContentDescriptors);

                                    SaveFileInContentDiscriptor(workingCopy);
                                }

                                if (Bus.ContainsKey(TaskManager.DATASET_STATUS))
                                {
                                    bool newdataset = Bus[TaskManager.DATASET_STATUS].ToString().Equals("new");
                                    int  v          = 1;
                                    if (workingCopy.Dataset.Versions != null && workingCopy.Dataset.Versions.Count > 1)
                                    {
                                        v = workingCopy.Dataset.Versions.Count();
                                    }

                                    //set modification
                                    workingCopy.ModificationInfo = new EntityAuditInfo()
                                    {
                                        Performer  = User.Name,
                                        Comment    = "File",
                                        ActionType = AuditActionType.Create
                                    };

                                    setSystemValuesToMetadata(id, v, workingCopy.Dataset.MetadataStructure.Id, workingCopy.Metadata, newdataset);

                                    dm.EditDatasetVersion(workingCopy, null, null, null);
                                }

                                //filename
                                string filename = "";
                                if (Bus.ContainsKey(TaskManager.FILENAME))
                                {
                                    filename = Bus[TaskManager.FILENAME]?.ToString();
                                }

                                // ToDo: Get Comment from ui and users
                                dm.CheckInDataset(id, filename, User.Name, ViewCreationBehavior.None);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                    }

                    #endregion unstructured data
                }
                else
                {
                    temp.Add(new Error(ErrorType.Dataset, "Dataset is not selected."));
                }

                if (temp.Count <= 0)
                {
                    dm.CheckInDataset(id, "no update on data tuples", User.Name, ViewCreationBehavior.None);
                }
                else
                {
                    dm.UndoCheckoutDataset(id, User.Name);
                }
            }
            catch (Exception ex)
            {
                temp.Add(new Error(ErrorType.Dataset, ex.Message));

                dm.CheckInDataset(id, "no update on data tuples", User.Name, ViewCreationBehavior.None);
            }
            finally
            {
                if (RunningASync)
                {
                    var user = User;

                    if (temp.Any())
                    {
                        var es = new EmailService();
                        es.Send(MessageHelper.GetPushApiUploadFailHeader(id, title),
                                MessageHelper.GetPushApiUploadFailMessage(id, user.Name, temp.Select(e => e.ToString()).ToArray()),
                                new List <string> {
                            user.Email
                        }, null, new List <string> {
                            ConfigurationManager.AppSettings["SystemEmail"]
                        });
                    }
                    else
                    {
                        var es = new EmailService();
                        es.Send(MessageHelper.GetASyncFinishUploadHeader(id, title),
                                MessageHelper.GetASyncFinishUploadMessage(id, title, numberOfRows, numberOfSkippedRows),
                                new List <string> {
                            user.Email
                        }, null, new List <string> {
                            ConfigurationManager.AppSettings["SystemEmail"]
                        });
                    }
                }

                dm.Dispose();
                dsm.Dispose();
            }

            return(temp);
        }