private static void SetErrorOnItem(PriceImportLog logResult, string error) { if (string.IsNullOrEmpty(logResult.Detail)) { logResult.Detail = Resource.Business.GetString("ErrorIn"); } logResult.Detail += error + ", "; logResult.Detail = logResult.Detail.TrimEnd(','); logResult.Status = PriceImportLogStatus.Error; }
private void CheckLogResultItem(PriceImportLog lr) { if ((string.IsNullOrEmpty(lr.CodGrundfos) || string.IsNullOrEmpty(lr.CodProvider)) && string.IsNullOrEmpty(lr.Provider)) { SetErrorOnItem(lr, Resource.Business.GetString("ObligatoryData")); } lr.Detail = ""; lr.Status = PriceImportLogStatus.Add; if (lr.CodGrundfos == string.Empty && lr.CodProvider == string.Empty) { SetErrorOnItem(lr, Resource.Business.GetString("CodeError")); } if (!string.IsNullOrEmpty(lr.CodGrundfos)) { lr.CodGrundfos = lr.CodGrundfos.PadLeft(8, '0'); } if (!string.IsNullOrEmpty(lr.Provider)) { lr.Provider = CheckLogResultField(lr.Provider, typeof(Provider), lr, Resource.Business.GetString("Provider")).ToString(); } else { SetErrorOnItem(lr, Resource.Business.GetString("Provider")); } if (lr.Model.Length > 64) { SetErrorOnItem(lr, Resource.Business.GetString("Text64Characters")); lr.Model = ""; } if (lr.Description.Length > 512) { SetErrorOnItem(lr, Resource.Business.GetString("Text512Characters")); lr.Description = ""; } if (!string.IsNullOrEmpty(lr.Frequency)) { lr.Frequency = CheckLogResultField(lr.Frequency, typeof(Frequency), lr, Resource.Business.GetString("Frecuency")).ToString(); } else if (lr.Status == PriceImportLogStatus.Add) { lr.Frequency = "50/60"; lr.ParsedFrequency = null; } if (lr.TP == 0) { lr.TP = null; } if (!string.IsNullOrEmpty(lr.TPCurrency)) { lr.TPCurrency = CheckLogResultField(lr.TPCurrency, typeof(Currency), lr, Resource.Business.GetString("TPCurrency")).ToString(); } else if (lr.Status == PriceImportLogStatus.Add) { lr.TPCurrency = Resource.Business.GetString("DefaultCurrency"); } if (lr.GRP == 0) { lr.GRP = null; } if (!string.IsNullOrEmpty(lr.GRPCurrency)) { lr.GRPCurrency = CheckLogResultField(lr.GRPCurrency, typeof(Currency), lr, Resource.Business.GetString("GRPCurrency")).ToString(); } else if (lr.Status == PriceImportLogStatus.Add) { lr.GRPCurrency = Resource.Business.GetString("DefaultCurrency"); } if (lr.PL == 0) { lr.PL = null; } if (!string.IsNullOrEmpty(lr.PLCurrency)) { lr.PLCurrency = CheckLogResultField(lr.PLCurrency, typeof(Currency), lr, Resource.Business.GetString("PLCurrency")).ToString(); } else if (lr.Status == PriceImportLogStatus.Add) { lr.PLCurrency = Resource.Business.GetString("DefaultCurrency"); } if (!string.IsNullOrEmpty(lr.Cat1.Trim())) { lr.Cat1 = CheckLogResultField(lr.Cat1, typeof(CategoryBase), lr, Resource.Business.GetString("CategoryType1")).ToString(); } if (!string.IsNullOrEmpty(lr.Cat2.Trim())) { lr.Cat2 = CheckLogResultField(lr.Cat2, typeof(CategoryBase), lr, Resource.Business.GetString("CategoryType3")).ToString(); } if (!string.IsNullOrEmpty(lr.Cat3.Trim())) { lr.Cat3 = CheckLogResultField(lr.Cat3, typeof(CategoryBase), lr, Resource.Business.GetString("CategoryType5")).ToString(); } }
private object CheckLogResultField(object o, Type t, PriceImportLog logResult, string property) { if (o == null) { return(string.Empty); } if (t == typeof(Provider)) { IList <Provider> lstProv = provlist.FindAll(delegate(Provider record) { if ((!string.IsNullOrEmpty(record.Name) && record.Name.ToLower() == o.ToString().ToLower()) || (!string.IsNullOrEmpty(record.Code) && record.Code.ToLower() == o.ToString().ToLower())) { return(true); } return(false); }); if (lstProv.Count == 1) { logResult.ParsedProvider = lstProv[0]; return(o); } else if (lstProv.Count > 1) { property += " no es único"; } } else if (t == typeof(Currency)) { if (currlist.Exists(delegate(Currency record) { if ((!string.IsNullOrEmpty(record.Description) && record.Description.ToLower() == o.ToString().ToLower())) { return(true); } return(false); })) { return(o); } property += "("; foreach (Currency currency in currlist) { property += currency.Description + ","; } property = property.TrimEnd(','); property += ")"; } else if (t == typeof(Frequency)) { if (o.ToString() == "50" || o.ToString() == "60" || o.ToString() == "50/60") { Frequency?pt = null; switch (o.ToString()) { case "50": pt = Frequency.Hz50; break; case "60": pt = Frequency.Hz60; break; case "50/60": pt = Frequency.All; break; } logResult.ParsedFrequency = pt; return(o); } property += "(50,60,50/60)"; } else if (t == typeof(CategoryBase)) { IList <CategoryBase> lstCat = catlist.FindAll(delegate(CategoryBase record) { if ((!string.IsNullOrEmpty(record.Description) && (record.Description.ToLower() == o.ToString().ToLower())) || (!string.IsNullOrEmpty(record.Name) && record.Name.ToLower() == o.ToString().ToLower())) { return(true); } return(false); }); if (lstCat.Count == 1) { logResult.ParsedCategories.Add(lstCat[0]); return(o); } else if (lstCat.Count > 0) { property += " no es única"; } } SetErrorOnItem(logResult, property); return(o); }
/// <summary> /// Create a new PriceImport object based on the input file. /// </summary> /// <param name="filename">File with the CSV to import</param> /// <param name="description">Description for the new import process</param> /// <param name="haveHeader">Indicates if the file has header line</param> /// <param name="separationChar">Indicates the separation character in the CSV file</param> /// <param name="path">Indicates the path of the CSV file</param> /// <returns></returns> public PriceImport Create(string newfilename, string description, bool haveHeader, char separationChar, string path, string originalfilename) { Utils.GetLogger().Debug(string.Format("[[Product Import]] Start {0}", description)); DelimitedClassBuilder cb = CreateClassBuilder(separationChar, haveHeader); FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass()); object[] items = engine.ReadFile(path + newfilename); if (items.Length <= 0) { throw new EmptyImportationFileException("No se encontraron registros para ingresar."); } Utils.GetLogger().Debug(string.Format("[[Product Import]] Starting to get database data {0}", description)); // TODO: We should only get the necessary fields for each object and not the whole object. provlist = ControllerManager.Provider.GetActives() as List <Provider>; catlist = ControllerManager.CategoryBase.GetAll() as List <CategoryBase>; currlist = ControllerManager.Currency.GetAll() as List <Currency>; // Get Needed PriceBase Data IQuery q = NHibernateSession.CreateQuery("select lower(trim(P.Code)), lower(trim(PB.ProviderCode)), PB.Provider.ID, PB.ID FROM PriceBase PB JOIN PB.Product P"); q.SetResultTransformer(new NHibernate.Transform.AliasToBeanConstructorResultTransformer(typeof(PriceBaseFound).GetConstructors()[0])); List <PriceBaseFound> lst = q.List <PriceBaseFound>() as List <PriceBaseFound>; // Clear the memory NHibernateSession.Flush(); NHibernateSession.Clear(); Utils.GetLogger().Info(string.Format("[[Product Import]] Ready {0}", description)); List <PriceImportLog> lstDuplicates = new List <PriceImportLog>(items.Length); // Start the transaction this.BeginTransaction(); // Create the PriceImport item PriceImport pi = new PriceImport(); pi.ImportStatus = ImportStatus.Invalid; pi.File = originalfilename; pi.Description = description; pi.DateImported = DateTime.Now; pi.HaveHeader = haveHeader; pi.SeparationChar = separationChar; Save(pi); bool error = false; bool atLeastOneValid = false; for (int i = 0; i < items.Length; i++) { string originalline = ""; // Create the item PriceImportLog lr = new PriceImportLog(); lr.CodGrundfos = GetValue("CodGrundfos", items[i]).ToString(); lr.CodProvider = GetValue("CodProv", items[i]).ToString(); lr.Model = GetValue("Modelo", items[i]).ToString(); lr.Description = GetValue("Descripcion", items[i]).ToString(); lr.Provider = GetValue("Proveedor", items[i]).ToString(); lr.Frequency = GetValue("Frecuencia", items[i]).ToString(); lr.TP = (decimal?)GetValue("TP", items[i]); lr.TPCurrency = GetValue("MonedaTP", items[i]).ToString(); lr.GRP = (decimal?)GetValue("GRP", items[i]); lr.GRPCurrency = GetValue("MonedaGRP", items[i]).ToString(); lr.PL = (decimal?)GetValue("PL", items[i]); lr.PLCurrency = GetValue("MonedaPL", items[i]).ToString(); lr.Cat1 = GetValue("Familia", items[i]).ToString(); lr.Cat2 = GetValue("Tipo", items[i]).ToString(); lr.Cat3 = GetValue("Linea", items[i]).ToString(); lr.OriginalLine = originalline; lr.FileIndex = i; // Check if at least the required fields are completed, otherwise ignore. if (string.IsNullOrEmpty(lr.Provider) || (string.IsNullOrEmpty(lr.CodGrundfos) && string.IsNullOrEmpty(lr.CodProvider))) { NHibernateSession.Evict(lr); error = true; continue; } // Check the whole item data CheckLogResultItem(lr); if (lr.CodGrundfos == string.Empty || lr.CodProvider == string.Empty) { if (lstDuplicates.Exists(delegate(PriceImportLog record) { if ((record.CodGrundfos != string.Empty && record.CodGrundfos == lr.CodGrundfos && record.Provider == lr.Provider) || (record.CodProvider != string.Empty && record.CodProvider == lr.CodProvider && record.Provider == lr.Provider)) { return(true); } return(false); })) { SetErrorOnItem(lr, Resource.Business.GetString("DuplicatedProduct")); } } lstDuplicates.Add(lr); if (lr.Status != PriceImportLogStatus.Error) { string codGrundFos = lr.CodGrundfos.Trim(); string codProvider = lr.CodProvider.Trim(); Provider prov = FindInMemory(provlist, lr.Provider); PriceBaseFound pb; if (!string.IsNullOrEmpty(codGrundFos)) { codGrundFos = codGrundFos.ToLower(); pb = lst.Find(delegate(PriceBaseFound record) { if (record.Code == codGrundFos && record.ProviderId == prov.ID) { return(true); } return(false); }); } else { codProvider = codProvider.ToLower(); pb = lst.Find(delegate(PriceBaseFound record) { if (record.CodeProvider == codProvider && record.ProviderId == prov.ID) { return(true); } return(false); }); } if (pb != null) { lr.Status = PriceImportLogStatus.Modify; lr.ParsedPriceBase = new PriceBase(pb.PriceBaseId); if (string.IsNullOrEmpty(GetValue("Frecuencia", items[i]).ToString())) { lr.Frequency = string.Empty; lr.ParsedFrequency = null; } if (string.IsNullOrEmpty(GetValue("MonedaTP", items[i]).ToString())) { lr.TPCurrency = string.Empty; } if (string.IsNullOrEmpty(GetValue("MonedaGRP", items[i]).ToString())) { lr.GRPCurrency = string.Empty; } if (string.IsNullOrEmpty(GetValue("MonedaPL", items[i]).ToString())) { lr.PLCurrency = string.Empty; } } } // Save the item lr.PriceImport = pi; NHibernateSession.Save(lr); if (lr.Status != PriceImportLogStatus.Error) { atLeastOneValid = true; } else { error = true; } } // Update the status of the PriceImport item if (error) { pi.ImportStatus = (atLeastOneValid) ? ImportStatus.VerifiedSomeInvalid : ImportStatus.Invalid; } else { pi.ImportStatus = ImportStatus.Verified; } // Commit Changes CommitChanges(); Utils.GetLogger().Info(string.Format("[[Product Import]] Finished with Identifier {0} and status {1}", pi.ID, pi.ImportStatus)); File.Move(@path + newfilename, @path + pi.ID + ".csv"); return(pi); }