public static ProjectDataSet BuildProjectDataSetForCreate(DataTable table, out ProjectDataSet projectDataSet, out ProjectDataSet.ProjectRow projectRow,out bool iscreate) { var inputProjectRow = table.Rows[0]; Console.WriteLine("Starting import of project for {0}", inputProjectRow[_mapping.ProjectMap["PROJ_NAME"]].ToString()); projectDataSet = new ProjectDataSet(); iscreate = false; projectRow = projectDataSet.Project.NewProjectRow(); Guid projectGuid = Guid.NewGuid(); projectRow.PROJ_UID = projectGuid; projectRow.PROJ_NAME = inputProjectRow[_mapping.ProjectMap["PROJ_NAME"]].ToString(); //projectRow.ENTERPRISE_PROJECT_TYPE_NAME = inputProjectRow[_mapping.ProjectMap["ENTERPRISE_PROJECT_TYPE_NAME"]].ToString(); // set the project start date to min of start dates for all tasks provided mapping exists for task start with key = TASK_START_DATE if (ProjectHasTasks(table) && _mapping.TaskMap.ContainsKey("TASK_START_DATE") && table.Columns.Contains(_mapping.TaskMap["TASK_START_DATE"].ToString())) { DateTime minDate = table.AsEnumerable().Min(t => t.Field<DateTime>(_mapping.TaskMap["TASK_START_DATE"])); projectRow.PROJ_INFO_START_DATE = minDate; } projectDataSet.Project.AddProjectRow(projectRow); if (!Repository.CheckIfProjectExists(inputProjectRow[_mapping.ProjectMap["PROJ_NAME"]].ToString())) { Console.WriteLine("Starting create project for {0}", inputProjectRow[_mapping.ProjectMap["PROJ_NAME"]].ToString()); BuildProjectCustomFields(projectDataSet, inputProjectRow, projectGuid); projectGuid = new Repository().CreateProject(projectDataSet); //create with minimal initiation data including EPT Type iscreate = true; } else { Console.WriteLine("Project already exists for {0}, Starting an update", inputProjectRow[_mapping.ProjectMap["PROJ_NAME"]].ToString()); projectGuid = Repository.GetProjectList().Project .First(t => t.PROJ_NAME.Trim().ToUpper() == inputProjectRow[_mapping.ProjectMap["PROJ_NAME"]].ToString().Trim().ToUpper()).PROJ_UID; } projectDataSet = Repository.ReadProject(projectGuid); projectRow = projectDataSet.Project.Rows[0] as SvcProject.ProjectDataSet.ProjectRow; BuildResources(table, projectDataSet, projectGuid); // build team new Repository().UpdateProjectTeam(projectDataSet); projectDataSet = Repository.ReadProject(projectGuid); projectRow = projectDataSet.Project.Rows[0] as SvcProject.ProjectDataSet.ProjectRow; return projectDataSet; }
void bgw_DoWork(object sender, DoWorkEventArgs e) { try { XLDataSource source = new XLDataSource(); Repository repository = new Repository(); List<string> successfulProjects = new List<string>(); List<string> failedProjects = new List<string>(); TextBox.CheckForIllegalCrossThreadCalls = false; DataSet ds = source.ReadData(txtFileName.Text); List<string> errors; if (ds.Tables.Count > 0 && !DataSetBuilder.Validate(ds.Tables[0], out errors)) { txtError.AppendText("Project Import aborted due to validation error" + Environment.NewLine); foreach (string error in errors) { txtError.AppendText(error + Environment.NewLine); } return; } //For every project in the input file for (int i = 0; i < ds.Tables.Count; i++) //some number (total) { //System.Threading.Thread.Sleep(100); int percents = ((i) * 100) / ds.Tables.Count; bgw.ReportProgress(percents, new ProjectStatus() { ProjectName = ds.Tables[i].Rows[0][DataSetBuilder.Mapping.ProjectMap["PROJ_NAME"]].ToString(), Status = "Start", SuccessCount = successfulProjects.Count, FailedCount = failedProjects.Count }); //2 arguments: //1. procenteges (from 0 t0 100) - i do a calcumation //2. some current value! ProjectDataSet projectDataSet; ProjectDataSet.ProjectRow row = null; try { DataTable table = ds.Tables[i]; bool iscreate, hasPlan; DataSetBuilder.BuildProjectDataSetForCreate(table, out projectDataSet, out row, out iscreate, out hasPlan); DataSetBuilder.BuildProjectDataSetForUpdate(table, projectDataSet, row, iscreate, hasPlan); successfulProjects.Add(row.PROJ_NAME); percents = ((i + 1) * 100) / ds.Tables.Count; bgw.ReportProgress(percents, new ProjectStatus() { ProjectName = ds.Tables[i].Rows[0][DataSetBuilder.Mapping.ProjectMap["PROJ_NAME"]].ToString(), Status = "Complete", SuccessCount = successfulProjects.Count, FailedCount = failedProjects.Count }); } catch (Exception ex) { if (row != null) { txtError.AppendText(string.Format("An error occured in import of project {0} .Skipping Project import. Failure reason = {1}", row.PROJ_NAME, ex.Message)); failedProjects.Add(row.PROJ_NAME); percents = ((i + 1) * 100) / ds.Tables.Count; bgw.ReportProgress(percents, new ProjectStatus() { ProjectName = ds.Tables[i].Rows[0][DataSetBuilder.Mapping.ProjectMap["PROJ_NAME"]].ToString(), Status = "Fail", SuccessCount = successfulProjects.Count, FailedCount = failedProjects.Count }); } else { txtError.AppendText(string.Format("An error occured.Skipping Project import. Failure reason = {0}", ex.Message)); failedProjects.Add(""); percents = ((i + 1) * 100) / ds.Tables.Count; bgw.ReportProgress(percents, new ProjectStatus() { ProjectName = ds.Tables[i].Rows[0][DataSetBuilder.Mapping.ProjectMap["PROJ_NAME"]].ToString(), Status = "Fail", SuccessCount = successfulProjects.Count, FailedCount = failedProjects.Count }); } continue; } } } catch (Exception ex) { txtError.AppendText(string.Format("An error occured.Skipping Project import. Failure reason = {0}", ex.Message)); } }