Exemplo n.º 1
0
        public void Execute(JobExecutionContext context)
        {
            string pluginName = context.JobDetail.FullName;

            try
            {
                using (var unit = GetUnitOfWork())
                {
                    DataMap = context.JobDetail.JobDataMap;
                    DateTime start = DateTime.Now;

                    Plugin plugin = null;
                    if (DataMap.Contains("Plugin"))
                    {
                        int pluginID = ((Plugin)DataMap.Get("Plugin")).PluginID;
                        plugin = unit.Scope.Repository <Plugin>().GetSingle(x => x.PluginID == pluginID);

                        plugin.NextRun  = null;
                        plugin.Duration = null;
                    }

                    ConnectorSchedule schedule = null;
                    if (DataMap.Contains("ConnectorSchedule"))
                    {
                        int connectorScheduleID = ((ConnectorSchedule)DataMap.Get("ConnectorSchedule")).ConnectorScheduleID;
                        schedule                         = unit.Scope.Repository <ConnectorSchedule>().GetSingle(x => x.ConnectorScheduleID == connectorScheduleID);
                        schedule.LastRun                 = start;
                        schedule.Duration                = null;
                        schedule.ScheduledNextRun        = null;
                        schedule.ConnectorScheduleStatus = (int)ConnectorScheduleStatus.Running;
                    }

                    unit.Save();

                    Running = true;

                    log.InfoFormat("Starting plugin: {0}", pluginName);

                    Concentrator.Objects.Web.Client.Login(Concentrator.Objects.Web.ConcentratorPrincipal.SystemPrincipal);

                    var _repoConnectors = unit.Scope.Repository <Connector>();

                    // filter connectors based on the optional attribute on Plugin level
                    var att = GetType().GetCustomAttributes(typeof(ConnectorSystemAttribute), true);

                    if (att.Length > 0)
                    {
                        var attributevalue = ((ConnectorSystemAttribute)att[0]).ConnectorSystem;
                        _connectors = _repoConnectors.GetAll(c => c.ConnectorSystemID.HasValue && c.ConnectorSystem.Name == attributevalue).ToList();

#if !DEBUG
                        _connectors = _connectors.Where(c => c.IsActive).ToList();
#endif
                    }
                    else
                    {
                        try
                        {
                            _connectors = _repoConnectors.GetAll().ToList();
#if !DEBUG
                            _connectors = _connectors.Where(c => c.IsActive).ToList();
#endif
                        }
                        catch (Exception e)
                        {
                            log.Debug(e.InnerException);
                        }
                    }

                    if (schedule == null)
                    {
                        var allConnectorSchedules = unit.Scope.Repository <ConnectorSchedule>().GetAll(x => x.PluginID == plugin.PluginID).ToList();
                        _connectors = _connectors.Except(allConnectorSchedules.Select(x => x.Connector));
                    }
                    else
                    {
                        _connectors = _connectors.Where(x => x.ConnectorID == schedule.ConnectorID);
                    }

                    _vendors = unit.Scope.Repository <Vendor>().GetAll().ToList();

                    Stopwatch watch = Stopwatch.StartNew();
                    log.AuditInfo(string.Format("Starting {0} at {1}", Name, DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss")), Name);
                    Process();
                    watch.Stop();
                    log.AuditComplete(string.Format("Finished {0} at {1}. The plugin took {2} to finish", Name, DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss"), watch.Elapsed.TotalMinutes.ToString()), Name);

                    //_connectorSchedules.Where(x => x.ConnectorScheduleStatus != (int)ConnectorScheduleStatus.WaitForNextRun).ToList().ForEach(x => x.ConnectorScheduleStatus = (int)ConnectorScheduleStatus.Disabled);
                    TimeSpan ts   = DateTime.Now.Subtract(start);
                    string   time = ts.ToString().Substring(0, 8);

                    if (plugin != null && schedule == null)
                    {
                        plugin.LastRun  = start;
                        plugin.Duration = time;
                        if (context.NextFireTimeUtc.HasValue)
                        {
                            plugin.NextRun = context.NextFireTimeUtc.Value.ToLocalTime();
                        }
                    }

                    if (schedule != null)
                    {
                        if (context.NextFireTimeUtc.HasValue)
                        {
                            schedule.ScheduledNextRun = context.NextFireTimeUtc.Value.ToLocalTime();
                        }

                        schedule.ConnectorScheduleStatus = (int)ConnectorScheduleStatus.WaitForNextRun;
                        schedule.Duration = time;
                    }

                    log.InfoFormat("Finished plugin: {0}, duration : {1}, next run : {2}", pluginName, time, context.NextFireTimeUtc.HasValue ? context.NextFireTimeUtc.Value.ToLocalTime() : DateTime.MinValue);
                    unit.Save();
                }
            }
            catch (Exception ex)
            {
                //log.FatalFormat("Error executing {0} plugin : {1}\n{2}", pluginName, ex.InnerException != null ? ex.InnerException.Message : ex.Message, ex.StackTrace);
                log.AuditFatal(string.Format("Error executing {0} plugin : {1}\n{2}", pluginName, ex.InnerException != null ? ex.InnerException.Message : ex.Message, ex.StackTrace), ex, pluginName);
            }
        }
Exemplo n.º 2
0
        public DataTable[] Process()
        {
            try
            {
                string path = xlsPath;

                OleDbConnection       con     = null;
                System.Data.DataTable exceldt = null;

                try
                {
                    if (path.Contains(".xlsx"))
                    {
                        con =
                            new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path +
                                                ";Extended Properties=\"Excel 12.0;HDR=YES;\"");
                    }

                    else if (path.Contains(".xls"))
                    {
                        con =
                            new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
                                                ";Extended Properties=Excel 8.0");
                    }

                    else
                    {
                        log.Error("Foutief bestand");
                        return(null);
                    }

                    con.Open();

                    using (exceldt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null))
                    {
                        if (exceldt == null)
                        {
                            log.AuditError("Fout verwerken file: Ongeldig tablad");
                        }

                        String[] excelSheets = new String[exceldt.Rows.Count];
                        log.AuditInfo(string.Format("Found {0} worksheets in excel", exceldt.Rows.Count));
                        int i = 0;

                        DataTable[] tables = new DataTable[exceldt.Rows.Count];
                        // Add the sheet name to the string array.
                        foreach (DataRow row in exceldt.Rows)
                        {
                            excelSheets[i] = row["TABLE_NAME"].ToString();

                            string           sheet = row["TABLE_NAME"].ToString();
                            OleDbDataAdapter da    = null;
                            DataTable        dt    = null;

                            da = new OleDbDataAdapter(string.Format("select * from [{0}]", sheet), con);
                            dt = new DataTable();
                            da.Fill(dt);

                            // exceldt.
                            tables[i] = dt;
                            i++;
                        }
                        return(tables);
                    }
                    if (con != null)
                    {
                        con.Close();
                        con.Dispose();
                    }

                    //File.(path, Path.Combine(ConfigurationManager.AppSettings["ExcelProcessedDirectory"], e.Name + "-" + Guid.NewGuid()));
                }
                catch (IOException ex)
                {
                    log.AuditFatal("Fout verwerken file: " + ex.Message);
                    //File.Move(e.FullPath, Path.Combine(ConfigurationManager.AppSettings["ExcelErrorDirectory"], e.Name + "-" + Guid.NewGuid()));
                }
                catch (Exception ex)
                {
                    log.AuditFatal("Fout verwerken file: " + ex.Message);

                    //if (File.Exists(e.FullPath))
                    //  File.Move(e.FullPath, Path.Combine(ConfigurationManager.AppSettings["ExcelErrorDirectory"], e.Name + "-" + Guid.NewGuid()));
                    //Logging.UserMail("Fout bij het verwerken van de excel order, de naam van het tabblad dient 'EDI_Order_V2' te zijn. Of u maakt gebruik van een verouderde versie, vraag uw accountmanager naar de nieuwe EDI Excel template", mailLog.Mailaddress, DocumentType.Excel);
                }
                finally
                {
                    if (con != null)
                    {
                        con.Close();
                        con.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                log.AuditFatal(ex.InnerException);
            }
            return(null);
        }