private bool SaveProductAttributes(int vendorID, artikelEigenschap artikelEigenschapData)
        {
            var alliantieName     = VendorSettingsHelper.GetAlliantieName(vendorID);
            var retailPartnerCode = VendorSettingsHelper.GetRetailPartnerCode(vendorID);
            var sequenceNumberArtikelEigenschap = CommunicatorHelper.GetSequenceNumber(vendorID);

            artikelEigenschapData.header.berichtDatumTijd  = DateTime.Now;
            artikelEigenschapData.header.berichtNaam       = "artikelEigenschap";
            artikelEigenschapData.header.retailPartnerCode = retailPartnerCode;
            artikelEigenschapData.header.bestandsNaam      = string.Format("{0}{1}artikelEigenschap.xml", sequenceNumberArtikelEigenschap, alliantieName);

            var messageIDArtAttr = MessageHelper.InsertMessage(MessageHelper.WehkampMessageType.ProductAttribute, artikelEigenschapData.header.bestandsNaam, vendorID);

            try
            {
                artikelEigenschapData.SaveToFile(string.Format(Path.Combine(ConfigurationHelper.ProductAttributesRootFolder, vendorID.ToString(CultureInfo.InvariantCulture), artikelEigenschapData.header.bestandsNaam)));
                MessageHelper.UpdateMessageStatus(messageIDArtAttr, WehkampMessageStatus.Success);
            }
            catch (Exception ex)
            {
                log.Fatal("Error while saving artikelEigenschap file", ex);
                MessageHelper.UpdateMessageStatus(messageIDArtAttr, WehkampMessageStatus.Error);

                return(false);
            }

            return(true);
        }
예제 #2
0
        private void SaveStockReturn(int vendorID, retourAanvraag stockReturnRequest)
        {
            var alliantieName     = VendorSettingsHelper.GetAlliantieName(vendorID);
            var retailPartnerCode = VendorSettingsHelper.GetRetailPartnerCode(vendorID);
            var sequenceNumber    = CommunicatorHelper.GetSequenceNumber(vendorID);

            stockReturnRequest.header.berichtDatumTijd  = DateTime.Now;
            stockReturnRequest.header.berichtNaam       = "retourAanvraag";
            stockReturnRequest.header.retailPartnerCode = retailPartnerCode;
            stockReturnRequest.header.bestandsNaam      = string.Format("{0}{1}retourAanvraag.xml", sequenceNumber, alliantieName);

            var messageID = MessageHelper.InsertMessage(MessageHelper.WehkampMessageType.StockReturnRequest, stockReturnRequest.header.bestandsNaam, vendorID);

            try
            {
                stockReturnRequest.SaveToFile(string.Format(Path.Combine(ConfigurationHelper.StockReturnRequestRootFolder, vendorID.ToString(CultureInfo.InvariantCulture), stockReturnRequest.header.bestandsNaam)));
                MessageHelper.UpdateMessageStatus(messageID, WehkampMessageStatus.Success);
            }
            catch (Exception ex)
            {
                log.Fatal("Error while saving retourAanvraag file", ex);
                MessageHelper.UpdateMessageStatus(messageID, WehkampMessageStatus.Error);
            }
        }
예제 #3
0
        private void SavePriceChanges(int vendorID, prijsAanpassing priceChange)
        {
            var alliantieName     = VendorSettingsHelper.GetAlliantieName(vendorID);
            var retailPartnerCode = VendorSettingsHelper.GetRetailPartnerCode(vendorID);
            var sequenceNumber    = CommunicatorHelper.GetSequenceNumber(vendorID);

            priceChange.header.berichtDatumTijd  = DateTime.Now;
            priceChange.header.berichtNaam       = "prijsAanpassing";
            priceChange.header.retailPartnerCode = retailPartnerCode;
            priceChange.header.bestandsNaam      = string.Format("{0}{1}prijsAanpassing.xml", sequenceNumber, alliantieName);

            var messageIDPriceChange = MessageHelper.InsertMessage(MessageHelper.WehkampMessageType.ProductPriceUpdate, priceChange.header.bestandsNaam, vendorID);

            try
            {
                priceChange.SaveToFile(string.Format(Path.Combine(ConfigurationHelper.ProductPricesRootFolder, vendorID.ToString(CultureInfo.InvariantCulture), priceChange.header.bestandsNaam)));
                MessageHelper.UpdateMessageStatus(messageIDPriceChange, WehkampMessageStatus.Success);
            }
            catch (Exception ex)
            {
                log.Fatal("Error while saving Price Change file", ex);
                MessageHelper.UpdateMessageStatus(messageIDPriceChange, WehkampMessageStatus.Error);
            }
        }
예제 #4
0
        private void SaveProductInformation(int vendorID, artikelInformatie artInfo, PetaPoco.Database db = null)
        {
            var alliantieName     = VendorSettingsHelper.GetAlliantieName(vendorID);
            var retailPartnerCode = VendorSettingsHelper.GetRetailPartnerCode(vendorID);
            var sequenceNumberArtikelInformatie = CommunicatorHelper.GetSequenceNumber(vendorID);

            artInfo.header.berichtDatumTijd  = DateTime.Now;
            artInfo.header.berichtNaam       = "artikelInformatie";
            artInfo.header.retailPartnerCode = retailPartnerCode;
            artInfo.header.bestandsNaam      = string.Format("{0}{1}artikelInformatie.xml", sequenceNumberArtikelInformatie, alliantieName);

            var messageIDArtInfo = MessageHelper.InsertMessage(MessageHelper.WehkampMessageType.ProductInformation, artInfo.header.bestandsNaam, vendorID, db);

            try
            {
                artInfo.SaveToFile(string.Format(Path.Combine(ConfigurationHelper.ProductInformationRootFolder, vendorID.ToString(CultureInfo.InvariantCulture), artInfo.header.bestandsNaam)));
                MessageHelper.UpdateMessageStatus(messageIDArtInfo, WehkampMessageStatus.Success, db);
            }
            catch (Exception ex)
            {
                log.Fatal("Error while saving artikelInfo file", ex);
                MessageHelper.UpdateMessageStatus(messageIDArtInfo, WehkampMessageStatus.Error);
            }
        }
예제 #5
0
        private void SaveShipmentInformation(int vendorID, aankomst shipment, PetaPoco.Database db = null)
        {
            var alliantieName          = VendorSettingsHelper.GetAlliantieName(vendorID);
            var retailPartnerCode      = VendorSettingsHelper.GetRetailPartnerCode(vendorID);
            var sequenceNumberShipment = CommunicatorHelper.GetSequenceNumber(vendorID);

            shipment.header.berichtDatumTijd  = DateTime.Now;
            shipment.header.berichtNaam       = "aankomst";
            shipment.header.retailPartnerCode = retailPartnerCode;
            shipment.header.bestandsNaam      = string.Format("{0}{1}aankomst.xml", sequenceNumberShipment, alliantieName);

            var messageIDShipment = MessageHelper.InsertMessage(MessageHelper.WehkampMessageType.ShipmentNotification, shipment.header.bestandsNaam, vendorID, db);

            try
            {
                shipment.SaveToFile(Path.Combine(ConfigurationHelper.ShipmentNotificationRootFolder, vendorID.ToString(CultureInfo.InvariantCulture), shipment.header.bestandsNaam));
                MessageHelper.UpdateMessageStatus(messageIDShipment, WehkampMessageStatus.Success, db);
            }
            catch (Exception ex)
            {
                log.Fatal("Error while saving aankomst file", ex);
                MessageHelper.UpdateMessageStatus(messageIDShipment, WehkampMessageStatus.Error);
            }
        }
예제 #6
0
        private void DownloadMessageFiles(int vendorID)
        {
            var remoteFileList = new ArrayList();

            _downloadedFiles = new Dictionary <string, decimal>();

            var ftpClient = SftpHelper.CreateClient(VendorSettingsHelper.GetSFTPSetting(vendorID), CommunicatorHelper.GetWehkampPrivateKeyFilename(vendorID), "D1r@ct379");

            if (ftpClient == null)
            {
                log.AuditCritical("SFTP failed to connect");
                return;
            }

            remoteFileList.Clear();
            var allianceDirectoryName = string.Format("TO_{0}", VendorSettingsHelper.GetAlliantieName(vendorID).ToUpperInvariant());

            try
            {
                ftpClient.ListDirectory(allianceDirectoryName, remoteFileList);

                foreach (TElSftpFileInfo fileinfo in remoteFileList)
                {
                    //log.Info(string.Format("Checking remote item '{0}'", fileinfo.Name));

                    if (fileinfo.Attributes.FileType != TSBSftpFileType.ftFile && (fileinfo.Attributes.FileType != TSBSftpFileType.ftUnknown && !fileinfo.Name.Contains(".xml")))
                    {
                        //log.Info(string.Format("Not a file. Skip item '{0}'", fileinfo.Name));
                        continue;
                    }
                    var message = MessageHelper.GetMessageByFilename(fileinfo.Name);

                    // if this is a second attempt, only try again if the validation has failed or there was an error downloading
                    if (message != null && message.Status != WehkampMessageStatus.ErrorDownload &&
                        message.Status != WehkampMessageStatus.ValidationFailed)
                    {
                        //log.Info(string.Format("Skip downloading file '{0}'", fileinfo.Name));
                        continue;
                    }
                    try
                    {
                        //log.Info(string.Format("Start downloading file '{0}'", fileinfo.Name));
                        ftpClient.DownloadFile(Path.Combine(allianceDirectoryName, fileinfo.Name).ToUnixPath(), Path.Combine(ConfigurationHelper.IncomingFilesRootFolder, vendorID.ToString(CultureInfo.InvariantCulture), fileinfo.Name));

                        ftpClient.RemoveFile(Path.Combine(allianceDirectoryName, fileinfo.Name).ToUnixPath());
                    }
                    catch (Exception e)
                    {
                        log.AuditError(string.Format("Cannot access {0} over SFTP: {1}\n{2}", allianceDirectoryName, e.Message, e.StackTrace));

                        if (message != null)
                        {
                            MessageHelper.UpdateMessageStatus(message.MessageID, WehkampMessageStatus.ErrorDownload);
                        }
                    }
                    finally
                    {
                        if (message != null)
                        {
                            MessageHelper.UpdateMessageAttempt(message.MessageID);

                            if (message.Attempts + 1 >= 10)
                            {
                                MessageHelper.UpdateMessageStatus(message.MessageID, WehkampMessageStatus.MaxRetryExceeded);
                            }
                        }
                    }

                    if (message == null || message.Attempts + 1 < 10)
                    {
                        _downloadedFiles.Add(fileinfo.Name, message != null ? message.MessageID : -1);
                    }
                }
            }
            catch (Exception e)
            {
                log.AuditError(string.Format("Cannot access {0} over SFTP: {1}\n{2}", allianceDirectoryName, e.Message, e.StackTrace));
            }

            CreateMessagesFromDownloadedFiles(vendorID);

            ftpClient.Dispose();
        }
예제 #7
0
        private void UploadMessageFiles(int vendorID)
        {
            var pluginLocation = GetPluginLocation();

            var typesXsdPath = Path.Combine(pluginLocation, "XSD", "wdpTypes.xsd");

            // first move all files to the outgoing directory
            var directories = MessageHelper.GetMessageFolders("To", vendorID);

            foreach (var directory in directories)
            {
                foreach (var file in Directory.EnumerateFiles(directory))
                {
                    var filename = Path.GetFileName(file);

                    if (filename == null)
                    {
                        continue;
                    }

                    var wkm = MessageHelper.GetMessageByFilename(filename);
                    if (wkm != null && (wkm.MessageType == MessageHelper.WehkampMessageType.ProductAttribute || wkm.MessageType == MessageHelper.WehkampMessageType.ShipmentNotification) &&
                        wkm.LastModified.HasValue &&
                        wkm.LastModified.Value.AddMinutes(20) > DateTime.Now.ToUniversalTime())
                    {
                        continue;
                    }

                    //Only export files with the status Success
                    if (wkm == null || wkm.Status != WehkampMessageStatus.Success)
                    {
                        continue;
                    }

                    File.Move(Path.Combine(directory, filename), Path.Combine(ConfigurationHelper.OutgoingFilesRootFolder, vendorID.ToString(CultureInfo.InvariantCulture), filename));
                }
            }
            var ftpClient = SftpHelper.CreateClient(VendorSettingsHelper.GetSFTPSetting(vendorID), CommunicatorHelper.GetWehkampPrivateKeyFilename(vendorID), "D1r@ct379");

            if (ftpClient == null)
            {
                log.AuditCritical("SFTP failed to connect");
                return;
            }

            // then we upload all of them
            var remoteFileList = new ArrayList();

            if (ConfigurationHelper.ListFTPFilesCheck)
            {
                ftpClient.ListDirectory("TO_WK", remoteFileList);
            }

            var remoteFiles = (TElSftpFileInfo[])remoteFileList.ToArray(typeof(TElSftpFileInfo));

            foreach (var file in Directory.EnumerateFiles(Path.Combine(ConfigurationHelper.OutgoingFilesRootFolder, vendorID.ToString(CultureInfo.InvariantCulture))))
            {
                var filename = Path.GetFileName(file);
                var message  = MessageHelper.GetMessageByFilename(filename);

                if (message == null || (message.Status != WehkampMessageStatus.ErrorUpload && message.Status != WehkampMessageStatus.Success))
                {
                    continue;
                }

                if (message.Status == WehkampMessageStatus.Success)
                {
                    MessageHelper.ResetMessageAttempt(message.MessageID);
                }

                var error = false;
                MessageHelper.UpdateMessageAttempt(message.MessageID);
                if (!ConfigurationHelper.ListFTPFilesCheck || remoteFiles.All(c => c.Name != file))
                {
                    try
                    {
                        var path = Path.Combine(ConfigurationHelper.OutgoingFilesRootFolder, message.VendorID.ToString(CultureInfo.InvariantCulture), filename);
                        var doc  = XDocument.Load(path);

                        if (doc.Root == null)
                        {
                            continue;
                        }

                        if (_downloadedFiles.ContainsKey(filename))
                        {
                            _downloadedFiles[filename] = message.MessageID;
                        }
                        else
                        {
                            _downloadedFiles.Add(filename, message.MessageID);
                        }

                        MessageHelper.UpdateMessageRecieved(message.MessageID, DateTime.Now.ToUniversalTime());

                        var xsdPath = Path.Combine(pluginLocation, "XSD", string.Format("{0}.xsd", doc.Root.Name.LocalName));

                        if (!File.Exists(xsdPath) || !File.Exists(typesXsdPath))
                        {
                            throw new FileNotFoundException(String.Format("Cannot find Xsd validation files\n{0}\n{1}", xsdPath, typesXsdPath));
                        }

                        var schemas = new XmlSchemaSet();

                        schemas.Add(null, XElement.Load(xsdPath).CreateReader());
                        schemas.Add(null, XElement.Load(typesXsdPath).CreateReader());

                        schemas.Compile();

                        doc.Validate(schemas, null);

                        ftpClient.UploadFile(path, Path.Combine("TO_WK", filename).ToUnixPath(), TSBFileTransferMode.ftmOverwrite);

                        File.Move(Path.Combine(ConfigurationHelper.OutgoingFilesRootFolder, message.VendorID.ToString(CultureInfo.InvariantCulture), filename), Path.Combine(ConfigurationHelper.ArchivedRootFolder, message.VendorID.ToString(CultureInfo.InvariantCulture), filename));

                        MessageHelper.UpdateMessageStatus(message.MessageID, WehkampMessageStatus.Archived);
                        MessageHelper.UpdateMessagePath(message.MessageID, ConfigurationHelper.ArchivedRootFolder);
                        MessageHelper.UpdateMessageSent(message.MessageID, DateTime.Now.ToUniversalTime());
                    }
                    catch (Exception e)
                    {
                        if (e is XmlSchemaValidationException)
                        {
                            MessageHelper.UpdateMessageStatus(_downloadedFiles[filename], WehkampMessageStatus.ValidationFailed);
                            log.AuditError(string.Format("XML validation failed for file {0} : {1}\n{2}", filename, e.Message, e.StackTrace));
                        }
                        else
                        {
                            MessageHelper.UpdateMessageStatus(message.MessageID, WehkampMessageStatus.ErrorUpload);
                            log.AuditError(string.Format("Cannot upload file {0}", filename), e);
                        }
                        MessageHelper.UpdateMessagePath(message.MessageID, ConfigurationHelper.OutgoingFilesRootFolder);
                        error = true;
                    }
                }
                else
                {
                    MessageHelper.UpdateMessageStatus(message.MessageID, WehkampMessageStatus.ErrorUpload);
                    MessageHelper.UpdateMessagePath(message.MessageID, ConfigurationHelper.OutgoingFilesRootFolder);
                    log.AuditError(string.Format("Cannot upload file {0}, File already exists on remote server.", filename));
                    error = true;
                }

                if (error && message.Attempts + 1 > 10)
                {
                    MessageHelper.UpdateMessageStatus(message.MessageID, WehkampMessageStatus.MaxRetryExceeded);
                    File.Move(Path.Combine(ConfigurationHelper.OutgoingFilesRootFolder, filename), Path.Combine(ConfigurationHelper.FailedFilesRootFolder, filename));
                    MessageHelper.UpdateMessagePath(message.MessageID, ConfigurationHelper.FailedFilesRootFolder);
                }
            }
            ftpClient.Dispose();
        }
        private bool ProcessProductImages(Database db, List <ProductMedia> productMedias, int vendorID)
        {
            if (!productMedias.Any())
            {
                return(true);
            }

            var attributeid = db.FirstOrDefault <int>("SELECT AttributeID FROM ProductAttributeMetaData WHERE AttributeCode = @0", "WehkampProductNumber");

            if (attributeid == 0)
            {
                throw new ConfigurationErrorsException("Attribute \"WehkampProductNumber\" is missing from the ProductAttributeMetadata table");
            }

            var wehkampPids = new Dictionary <int, string>();

            if (productMedias.Count <= 500)
            {
                var productMediaArray = string.Join(",", productMedias.Select(c => c.ProductID).ToArray());
                wehkampPids = db.Dictionary <int, string>(string.Format("SELECT ProductID, Value FROM ProductAttributeValue WHERE AttributeID = {1} AND ProductID IN ({0})", productMediaArray, attributeid));
            }
            else
            {
                for (var i = 0; i < productMedias.Count; i += 500)
                {
                    var productMediaArray = string.Join(",", productMedias.Skip(i).Take(Math.Min(500, productMedias.Count - i)).Select(c => c.ProductID).ToArray());
                    var temp = db.Dictionary <int, string>(string.Format("SELECT ProductID, Value FROM ProductAttributeValue WHERE AttributeID = {1} AND ProductID IN ({0})", productMediaArray, attributeid));

                    foreach (var pm in temp)
                    {
                        if (!wehkampPids.ContainsKey(pm.Key))
                        {
                            wehkampPids.Add(pm.Key, pm.Value);
                        }
                    }
                }
            }

            var ftpClient = SftpHelper.CreateClient(VendorSettingsHelper.GetSFTPSetting(vendorID), CommunicatorHelper.GetWehkampPrivateKeyFilename(vendorID), "D1r@ct379");

            if (ftpClient == null)
            {
                log.AuditCritical("SFTP failed to connect");
                return(false);
            }


            string manid, path, newFilename;
            int    index;

            foreach (var media in productMedias)
            {
                if (!wehkampPids.ContainsKey(media.ProductID))
                {
                    continue;
                }

                manid = media.ManufacturerID.Split(' ')[0];

                if (string.IsNullOrEmpty(manid))
                {
                    continue;
                }

                index = media.ImagePath.LastIndexOf(manid, StringComparison.InvariantCultureIgnoreCase);

                if (index < 0)
                {
                    continue;
                }

                path = Path.Combine(ConfigurationHelper.FTPMediaDirectory, media.ImagePath);

                if (!File.Exists(path))
                {
                    log.Error(string.Format("Could not find image file {0}", path));
                    continue;
                }

                newFilename = string.Format("{0}_{1}", wehkampPids[media.ProductID], media.ImagePath.Substring(index));

                try
                {
                    //Only upload files ending on:
                    //1. _b.png - Back
                    //2. _f.png - Front
                    //3. _h.png - Hoover

                    //if (newFilename.ToLowerInvariant().EndsWith("_b.png") || newFilename.ToLowerInvariant().EndsWith("_f.png") || newFilename.ToLowerInvariant().EndsWith("_h.png"))
                    if (newFilename.ToLowerInvariant().EndsWith("_b.png") || newFilename.ToLowerInvariant().EndsWith("_f.png") || newFilename.ToLowerInvariant().EndsWith("_h.png"))
                    {
                        ftpClient.UploadFile(path, Path.Combine("TO_WK", "Pictures", newFilename).ToUnixPath(), TSBFileTransferMode.ftmOverwrite);
                    }
                }
                catch (Exception e)
                {
                    log.Error(string.Format("Cannot upload file : {0}", media.ImagePath), e);
                    return(false);
                }
            }

            return(true);
        }