@ -55,6 +55,15 @@ namespace Gremlin.GremlinData.DBClasses
set = > _dataHasHeadings = value ;
}
public Encoding Encoding
{
get = > _encoding ;
set
{
_encoding = value ;
ResetParser ( ) ;
}
}
//Constructors
public GenericImporter ( string filepath )
@ -109,7 +118,6 @@ namespace Gremlin.GremlinData.DBClasses
_newParser . SetDelimiters ( Separators ) ;
_newParser . HasFieldsEnclosedInQuotes = true ;
_csvParser = _newParser ;
_ = _csvParser . ReadFields ( ) ; //Skip Heading
}
public bool ImportFile ( )
@ -170,7 +178,7 @@ namespace Gremlin.GremlinData.DBClasses
ImportProducts ( mappingTable ) ;
break ;
case "CustomDescription" :
ImportCustomDescription ( _csvParser , mappingTable ) ;
ImportCustomDescription s ( mappingTable ) ;
break ;
default :
return false ;
@ -186,12 +194,128 @@ namespace Gremlin.GremlinData.DBClasses
return ImportFile ( ) ;
}
private bool ImportCustomDescriptions ( Dictionary < string , int > mappingTable )
{
List < CustomDescription > CDsReadFromFile = new ( 2500 ) ;
Encoding = Encoding . GetEncoding ( "UTF-8" ) ; //Custom-Descriptions-CSV hat festes Encoding.
using ( _csvParser )
{
// Skip the row with the column names:
_csvParser . ReadLine ( ) ;
while ( ! _csvParser . EndOfData )
{
// Read current line fields, pointer moves to the next line.
string [ ] fields = _csvParser . ReadFields ( ) ;
CustomDescription ImportedCD = new ( )
{
ProductNumber = fields [ 0 ] ,
OptionNumber = fields [ 1 ] = = "" ? null : fields [ 1 ] ,
Heading = fields [ 3 ] ,
DescriptionText = fields [ 4 ] ,
CoverletterText = fields [ 5 ] ,
Notes = fields [ 6 ] ,
DataModificationByUser = "Importer" ,
DataStatus = Status . Active . ToString ( ) ,
DataValidUntil = FarInTheFuture ,
DataVersionComment = "Initial Importer by CD-ImporterFomCsv" ,
} ;
ImportedCD . Product = new ( ) ;
ImportedCD . Supplier = new ( ) ;
ImportedCD . Supplier . AccountName = fields [ 2 ] is "" or "RB" ? "Agilent Technologies" : fields [ 2 ] ;
ImportedCD . DataCreationDate = ImportedCD . DataValidFrom = ImportedCD . DataModificationDate = DateTime . Now ;
ImportedCD . DataVersionNumber = 1 ;
CDsReadFromFile . Add ( ImportedCD ) ;
}
//Eingelesenen Custum Desciptions in DB schreiben:
//Check for 3PPs that not yet in the db.
//Step 1a: Add 3PP-Products from CD-List to list
//Step 1b: Add list to db.
//Step 2: Add CDs to products.
using ( GremlinContext db = new ( ) )
{
//Step 1a
List < Product > thirdPartyProductsFromImportedCDs = new ( ) ;
foreach ( CustomDescription CD in CDsReadFromFile )
{
if ( CD . Supplier . AccountName ! = "Agilent Technologies" )
{
Product new3PPProduct = new ( ) ;
new3PPProduct . CustomDescription = CD ;
new3PPProduct . HasBreakPrices = false ;
new3PPProduct . ListPrice = 0 ;
new3PPProduct . ProductNumber = CD . ProductNumber ;
new3PPProduct . OptionNumber = CD . OptionNumber ;
new3PPProduct . ProductStatus = Status . Active . ToString ( ) ;
new3PPProduct . SapLongDescription = null ;
new3PPProduct . SapShortDescription = null ;
new3PPProduct . Weight = 0 ;
new3PPProduct . IntroductionDate = DateTime . Now . Date ;
new3PPProduct . BreakRangeFrom = 0 ;
new3PPProduct . BreakRangeTo = 0 ;
new3PPProduct . ProductLine = DbHelper . ResolveProductLine ( db , "3P" ) ;
new3PPProduct . CustomDescription . Supplier = DbHelper . ResolveAccountByName ( db , new3PPProduct . CustomDescription . Supplier . AccountName ) ;
_ = MetaDataSetter . ForImport ( new3PPProduct , "GenericImporter-Method" , "Created at import from Custom Descriptions." ) ;
thirdPartyProductsFromImportedCDs . Add ( new3PPProduct ) ;
}
} ;
//Step 1b
db . Products . AddRange ( thirdPartyProductsFromImportedCDs ) ; //Imports both the products and the associated custom descriptions!
_ = db . SaveChanges ( ) ;
// Produkt aus DB laden, damit der Datensatz vom Context verfolgt wird und EF Core nicht versucht, diesen standardmäßig neu anzulegen.
//Step 2
List < CustomDescription > importedCDsWithEFReferences = new ( 100000 ) ;
List < CustomDescription > CDsWithoutEFReferences = new ( 100 ) ; //nur zur Kontrolle, wird nicht verwendet.
List < Product > productsInDb = db . Products . ToList ( ) ;
Account agilent = db . Accounts . Where ( a = > a . AccountName = = "Agilent Technologies" ) . Single ( ) ;
foreach ( CustomDescription CD in CDsReadFromFile )
{
//Skip Desciptions, if it has been already imported above (as part from 3PP)
if ( thirdPartyProductsFromImportedCDs . Contains ( CD . Product ) ) continue ;
//Establish EF Reference. If no PN/Opt found, then skip this custom description.
//CD.Product = GetProduct(db, CD.ProductNumber, CD.OptionNumber); //ResolveXY-functions return null, if no match is found in db.
CD . Product = productsInDb . Find ( p = > p . ProductNumber = = CD . ProductNumber & & p . OptionNumber = = CD . OptionNumber ) ;
if ( CD . Product = = null )
{
CDsWithoutEFReferences . Add ( CD ) ;
continue ;
}
//Establish EF Reference: If no Supplier-Account found, then skip this custom description (shouldn't happen), else set Supplier to Agilent (3PP-Supplier have been processed before and their products should not be part of this list).
if ( CD . Supplier = = null )
{
CDsWithoutEFReferences . Add ( CD ) ;
continue ;
}
CD . Supplier = agilent ;
//prepare properties
_ = MetaDataSetter . ForImport ( CD , "GenericImporter-Method" , "Initial import by CSV Importer (Function GenericImporter.ImportCustomDescriptions)" ) ;
//add to final list of CDs, that will go into the db.
importedCDsWithEFReferences . Add ( CD ) ;
}
db . CustomDescriptions . AddRange ( importedCDsWithEFReferences ) ;
db . SaveChanges ( ) ;
//Bestätigung senden
MessageBox . Show ( $"Es wurden {importedCDsWithEFReferences.Count} eigene Beschreibungen erfolgreich der Datenbank hinzugefügt.{Environment.NewLine}Es wurden {thirdPartyProductsFromImportedCDs.Count} 3PP-Produkte neu angelegt." ) ;
}
return true ;
}
}
private bool ImportProducts ( Dictionary < string , int > mappingTable )
{
List < Product > ProductsReadFromFile = new ( ParseProductFile ( mappingTable ) ) ;
return InsertProducts ( ProductsReadFromFile ) ;
return true ;
}
private bool InsertProducts ( List < Product > products )
@ -363,6 +487,7 @@ namespace Gremlin.GremlinData.DBClasses
bool result ;
result = ImportAccounts ( mappingTable ) ;
ResetParser ( ) ;
_ = _csvParser . ReadFields ( ) ; //Skip Heading
result = result & & ImportContacts ( mappingTable ) ;
return result ;
}