partial void Insertentry_status_update_time(entry_status_update_time instance);
 partial void Deleteentry_status_update_time(entry_status_update_time instance);
Exemple #3
0
        //-----------------------------------------------------------
        public int StartProcess()
        {
            int result = 1;
            if (core == null)
            {
                throw new Exception("Empty core object");
            }
            //PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, Properties.Settings.Default.PTR_Convert_RAIFF_PAZRConnectionString);
            try
            {
              PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Start processing");
              PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Reading dynamic configuration");
              #region Variables from xml configuration
              String url = ConfigXmlReader.XmlGetUIConfigParam(core.ProcessConfigurationXml, "url");
              String apiKey = ConfigXmlReader.XmlGetUIConfigParam(core.ProcessConfigurationXml, "api-key");
              String secretApiKey = PasswordEncoder.PtrDecryptText(ConfigXmlReader.XmlGetUIConfigParam(core.ProcessConfigurationXml, "secret-api-key"));
              EntriesDownloadMode entriesDownloadMode = (EntriesDownloadMode)Convert.ToInt32(ConfigXmlReader.XmlGetUIConfigParam(core.ProcessConfigurationXml, "get-entries-mode").Substring(0, 1));
              EntriesDownloadDate entriesDownloadDate =
                ConfigXmlReader.XmlGetUIConfigParam(core.ProcessConfigurationXml, "get-entries-mode:automatic-date").Substring(0, 3).ToUpper() == "YES" ?
                EntriesDownloadDate.AUTOMATIC : EntriesDownloadDate.MANUAL;
              String startDateString = ConfigXmlReader.XmlGetUIConfigParam(core.ProcessConfigurationXml, "get-entries-mode:automatic-date:start-date");
              DateTime startDate = DateTime.MinValue;
              if(entriesDownloadDate == EntriesDownloadDate.MANUAL)
              {
                if(!DateTime.TryParseExact(startDateString, "yyyy-MM-dd", null, DateTimeStyles.None, out startDate))
                  {
                  PTR3Core.LOG.Log(MsgStatusEnum.MS_Warning, "Could not parse the date provided in the configuration. Taking all entries.");
                  entriesDownloadMode = EntriesDownloadMode.ALL;
                  }
              }
              String connectionString = ConverterUtils.GetDBConvertersConnectionString(core);
              PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Url to connect to: {0}", url);
              PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Api key: {0}", apiKey);
              if(entriesDownloadMode == EntriesDownloadMode.SELECTED && entriesDownloadDate== EntriesDownloadDate.AUTOMATIC)
              {
                using(RaiffeisenPazrDataClassesDataContext dc = new RaiffeisenPazrDataClassesDataContext(connectionString)) //"Data Source=172.17.25.59;Initial Catalog=PTR_Convert_RAIFF_PAZR;User ID=sa;Password=R%T*2011;Connection Timeout=3600;Asynchronous Processing=true;MultipleActiveResultSets=true;Persist Security Info=True;"))
                {
                  startDate = (from dinfo in dc.entry_status_update_times
                               orderby dinfo.last_verification_status_update_date descending
                               select dinfo.last_verification_status_update_date).FirstOrDefault();
                  if(startDate == DateTime.MinValue)
                  {
                    PTR3Core.LOG.Log(MsgStatusEnum.MS_Warning, "No record indicating last update time in database. Taking all entries.");
                    entriesDownloadMode = EntriesDownloadMode.ALL;
                  }
                }
              }

              if(entriesDownloadMode == EntriesDownloadMode.ALL)
              {
                PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Downloading all entries");
              }
              else
              {
                PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Downloading selected entries from {0}.", startDate.ToString("yyyy-MM-dd HH:mm:ss.fff"));
              }
              PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Secret api key decrypted succesfully");
              #endregion
              XNamespace serv = "http://leader.raiffeisen.pl/api";
              //url = "http://localhost:65301/LeaderTestWebApp/Test/";
              ActiveForm form = new ActiveForm(serv, apiKey, secretApiKey, url);

              List<Tuple<String, String>> messageParameters = new List<Tuple<String, String>>();
              //Format daty: "2014-02-05T10:22:32.112+01:00"
              //2014-02-12T12:28:15.497+01:00
              //2014-02-12T12:27:46.954+01:00
              //2014-02-12T12:14:33.019+01:00
              //2014-02-12T12:14:33.019+01:00
              //2014-02-12T12:14:02.352+01:00
              //2014-02-12T12:13:13.693+01:00
              //2014-02-12T12:12:51.861+01:00

              messageParameters.Add(new Tuple<string, string>("lastVerificationStatusUpdateThreshold", startDate.ToString("yyyy-MM-ddTHH:mm:ss.ff+01:00")));//startDate.AddHours(-1).ToString("yyyy-MM-ddTHH:mm:ss.fff+01:00")));//
              PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Downloading entries changed from {0}", startDate.ToString("yyyy-MM-ddTHH:mm:ss.fff+01:00"));
              ActiveForm.ActiveFormAnswer answer = form.CallActiveFormsMethod("getEntriesForPaymentVerification",messageParameters);
              XElement entryData = answer.ResponseXml;
              PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Received message {0}", answer.AnswerCode.ToString());
              if(answer.AnswerCode == ActiveForm.AnswerCodes.OK)
              {
                List<leader_entry> entryList = (from entry in entryData.Descendants(serv + "entry")
                  select new leader_entry()
                  {
                    entryId = Int32.Parse(entry.Descendants(serv + "entryId").First().Value.Trim()),
                    transactionId = entry.Descendants(serv + "transactionId").First().Value.Trim(),
                    firstName = entry.Descendants(serv + "firstName").First().Value.Trim(),
                    lastName = entry.Descendants(serv + "lastName").First().Value.Trim(),
                    street = entry.Descendants(serv+"registrationAddress").Descendants(serv + "street").First().Value.Trim(),
                    houseNumber = entry.Descendants(serv+"registrationAddress").Descendants(serv + "houseNumber").First().Value.Trim(),
                    apartmentNumber = entry.Descendants(serv+"registrationAddress").Descendants(serv + "apartmentNumber").First().Value.Trim(),
                    postCode = entry.Descendants(serv+"registrationAddress").Descendants(serv + "postCode").First().Value.Trim(),
                    city = entry.Descendants(serv+"registrationAddress").Descendants(serv + "city").First().Value.Trim(),
                    streetMail = entry.Descendants(serv + "mailAddress").Descendants(serv + "street").First().Value.Trim(),
                    houseNumberMail = entry.Descendants(serv + "mailAddress").Descendants(serv + "houseNumber").First().Value.Trim(),
                    apartmentNumberMail = entry.Descendants(serv + "mailAddress").Descendants(serv + "apartmentNumber").First().Value.Trim(),
                    postCodeMail = entry.Descendants(serv + "mailAddress").Descendants(serv + "postCode").First().Value.Trim(),
                    cityMail = entry.Descendants(serv + "mailAddress").Descendants(serv + "city").First().Value.Trim(),
                    createDate = Convert.ToDateTime(entry.Descendants(serv+"creationTime").First().Value.Trim())
                  }).ToList();
                PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Retreived {0} entries.", entryList.Count);
                String lastVerificationStatusDateString = String.Empty;
                try
                {
                  lastVerificationStatusDateString = entryData.Descendants(serv + "lastVerificationStatusUpdateTime").First().Value;
                }
                catch(Exception)
                {
                }
                DateTime lastVerificationStatusDate;
                if(!String.IsNullOrEmpty(lastVerificationStatusDateString))
                {
                  Boolean success = DateTime.TryParse(lastVerificationStatusDateString, out lastVerificationStatusDate);
                  if(success)
                  {
                    using(RaiffeisenPazrDataClassesDataContext dc = new RaiffeisenPazrDataClassesDataContext(connectionString))
                    {
                      entry_status_update_time time = new entry_status_update_time() { last_verification_status_update_date = lastVerificationStatusDate, date_received = DateTime.Now };
                      dc.entry_status_update_times.InsertOnSubmit(time);
                      dc.SubmitChanges();
                    }
                  }
                }

                //using(RaiffeisenPazrDataClassesDataContext dc = new RaiffeisenPazrDataClassesDataContext(connectionString)
                //{
                //  entry_status_update_time est = new entry_status_update_time() { date_received = DateTime.Now, last_verification_status_update_date = entry
                //}
                PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "{0} entries downloaded", entryList.Count);
                TimeSpan expirationPeriod = new TimeSpan(3,0,0,0);
                DateTime now = DateTime.Now;
                foreach (leader_entry entry in entryList)
                {
                  // Na razie zakładam prostą obsługę - jeżeli przyjdzie wniosek o takim samym transaction Id - robię update na bazie
                  //
                  // jeśli transaction_id pojawia się po raz pierwszy, robię instert
                  try
                  {
                    using(RaiffeisenPazrDataClassesDataContext dc = new RaiffeisenPazrDataClassesDataContext(connectionString))
                    {
                      leader_entry entryFromBase = (from le in dc.leader_entries
                                                  where le.transactionId == entry.transactionId
                                                  select le).FirstOrDefault();
                      if (entryFromBase != null)
                      {
                        //Wniosek istniał w bazie - update
                        PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Entry {0} allready exists in database. Updating fields.", entry.entryId);
                        CopyEntryFields(entry, entryFromBase);

                      }
                      else
                      {
                        PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Entry {0} does not exist in database. Creating new entry.", entry.entryId);
                        entryFromBase = new leader_entry();
                        CopyEntryFields(entry, entryFromBase);
                        entryFromBase.transactionId = entry.transactionId;
                        entryFromBase.dateReceived = DateTime.Now; //Data rzeczywistego pobrania wniosku
                        dc.leader_entries.InsertOnSubmit(entryFromBase);
                      }
                      dc.SubmitChanges();
                    }
                  }
                  catch (Exception ex)
                  {
                    throw ex;
                  }
                }
                //Przedawnianie wniosków - dokładnie omówić z PL - wydaje mi się że moje podejście jest dobre
                PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Staring the expiration process");
                using(RaiffeisenPazrDataClassesDataContext dc = new RaiffeisenPazrDataClassesDataContext(connectionString))
                  {
                  var allExpiredEntries = (from entry in dc.leader_entries
                                   join itn in dc.itn_transactions on entry.transactionId equals itn.orderID into itng
                                   from itng_ in itng.DefaultIfEmpty()
                                           where entry.status == 0 &&
                                           ((itng_ == null && now - entry.createDate  > expirationPeriod) || (itng_ != null && itng_.transDate - entry.createDate > expirationPeriod))
                                   select entry).ToList();

                  if(allExpiredEntries.Count == 0)
                  {
                    PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "No entry has recently expired.");
                  }
                  else if(allExpiredEntries.Count == 1)
                  {
                    PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "1 entry has recently expired.");
                  }
                  else
                  {
                    PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "{0} entries have recently expired.", allExpiredEntries.Count);
                  }

                  foreach(var entry in allExpiredEntries)
                  {
                    PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Setting the expired status for entry {0}", entry.entryId);
                    entry.status = 100;
                  }
                  dc.SubmitChanges();
                  }
              }
              else
              {
                PTR3Core.LOG.Log(MsgStatusEnum.MS_Error, "Could not get valid response from Leader. Error: {0}",answer.AnswerCode.ToString());
                PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Full response xml:", answer.ResponseXml.ToString());
              }
              PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Save dynamic configuration");
              PTR3Core.LOG.Log(MsgStatusEnum.MS_Info, "Finish processing");

            }
            catch (Exception ex)
            {
                PTR3Core.LOG.Log(MsgStatusEnum.MS_Error, ex, "");
                result = -1;
            }
            return result;
        }