예제 #1
0
        public override DbImportResult Import(bool canCreateProperties = false)
        {
            DbImportResult = new DbImportResult();

            if (MetaData.ImportType != CommonUtils.ImportType.Cji5)
            {
                DbImportResult.ErrorMessages.Add(IMPORT_TYPE_NOT_COMPATIBLE);
                return DbImportResult;
            }

            CanCreateProperties = false;

            IList<Cji5DataAdapter> importData = new List<Cji5DataAdapter>();

            string connString = BuildConnectionString(MetaData.FullFileName);

            using (var excelConn = new OleDbConnection(connString))
            {
                try
                {
                    using (var cmd = new OleDbCommand())
                    {
                        cmd.CommandTimeout = 600;
                        cmd.Connection = excelConn;

                        excelConn.Open();

                        DataTable dbSchema = excelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        if (dbSchema == null || dbSchema.Rows.Count < 1)
                        {
                            throw new Exception("Error: Could not determine the name of the first worksheet.");
                        }
                        string firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
                        cmd.CommandText = string.Format(@"SELECT * FROM [{0}] where [Ref Document Number] IS NOT NULL", firstSheetName);

                        if (!WorkSheetCheckColumnNamesAreValid<PurchaseColumn>(GetColumnHeadersFromDataSet(cmd, (int)PurchaseColumn.ValueInObjCrcy)))
                        {
                            DbImportResult.ErrorMessages.Add(ExcelWorkSheetColumnsNotValidMessage());
                            return DbImportResult;
                        }

                        int rowNumber = 1;
                        using (OleDbDataReader dr = cmd.ExecuteReader())
                        {
                            while (dr != null && dr.Read())
                            {
                                rowNumber++;
                                try
                                {
                                    var adapter = new Cji5DataAdapter(dr,rowNumber);
                                    if (adapter.IsValid())
                                    {
                                        if (ContainsDuplicates(importData, adapter))
                                        {
                                            string message = String.Format(
                                                "Duplicate Po Number '{0}', Project Definition '{1}', Reference Item '{2}', Reference Doc type '{3}' and WBS Element '{4}' . Will SKIPP this row.",
                                                 adapter.PoNumber, adapter.ProjectDefinition, adapter.ReferenceItem, adapter.ReferenceDocType, adapter.WbsElement);
                                            DbImportResult.ErrorMessages.Add(string.Format("FinancialDataAdapter row {0} - {1}", rowNumber, message));
                                        }
                                        else
                                        {
                                            importData.Add(adapter);
                                        }
                                    }
                                    else
                                    {
                                        foreach (string message in adapter.ErrorMessages)
                                        {
                                            RaiseMessage(CommonUtils.MessageType.Error, message);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    DbImportResult.ErrorMessages.Add(string.Format("FinancialDataAdapter row {0} - {1}", rowNumber, ex.Message));
                                }
                            }
                            excelConn.Close();
                        }
                    }

                    if (MetaData.ImportType == CommonUtils.ImportType.Cji5)
                    {
                        InsertData(importData);
                    }
                    else
                    {
                    }

                    DbImportResult.ImportedCount = mSavedResults.Count;
                    return DbImportResult;
                }
                catch (OleDbException ex)
                {
                    DbImportResult.ErrorMessages.Add(ex.ToString());
                    return DbImportResult;
                }

                finally
                {
                    if (excelConn.State == ConnectionState.Open)
                    {
                        excelConn.Close();
                    }
                }
            }
        }
예제 #2
0
 private bool ContainsDuplicates(IList<Cji5DataAdapter> importData, Cji5DataAdapter adapter)
 {
     return false; //Check for duplicates removed temporaray as per https://jira.issgroup.com.au/browse/BODCMS-1479
     //if (importData.Any(x => x.PoNumber == adapter.PoNumber && x.ProjectDefinition == adapter.ProjectDefinition &&
     //    x.ReferenceItem == adapter.ReferenceItem && x.ReferenceDocType == adapter.ReferenceDocType && x.WbsElement == adapter.WbsElement))
     //{
     //    return true;
     //}
     //return false;
 }