Exemplo n.º 1
0
        public void CreateScanException(Exception ex, ScanExceptionType exType, string shortDesc, string fullPath)
        {
            using (var ctx = new FreeSpaceContext())
            {
                var instance = new ScanException
                {
                    CreatedStamp  = DateTime.Now,
                    ExceptionType = (int)exType,
                    Path          = fullPath,
                    ShortDesc     = shortDesc,
                    Source        = ex.Source,
                    HResult       = ex.HResult,
                    Message       = ex.Message,
                    StackTrace    = ex.StackTrace,
                    ExceptionName = ex.GetType().ToString()
                };

                var fnf = ex as FileNotFoundException;
                if (fnf != null)
                {
                    instance.InnerExceptionMessage = fnf.InnerException?.Message;
                    instance.ExceptionName         = fnf.GetType().ToString();
                }
                var dnf = ex as DirectoryNotFoundException;
                if (dnf != null)
                {
                    instance.InnerExceptionMessage = dnf.InnerException?.Message;
                    instance.ExceptionName         = dnf.GetType().ToString();
                }
                var ptl = ex as PathTooLongException;
                if (ptl != null)
                {
                    instance.InnerExceptionMessage = ptl.InnerException?.Message;
                    instance.ExceptionName         = ptl.GetType().ToString();
                }
                var ua = ex as UnauthorizedAccessException;
                if (ua != null)
                {
                    instance.InnerExceptionMessage = ua.InnerException?.Message;
                    instance.ExceptionName         = ua.GetType().ToString();
                }
                var ioe = ex as IOException;
                if (ioe != null)
                {
                    instance.InnerExceptionMessage = ioe.InnerException?.Message;
                    instance.ExceptionName         = ioe.GetType().ToString();
                }

                ctx.ScanExceptions.Add(instance);
                ctx.SaveChanges();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// EXAMPLE! NOT IN USE NOW
        /// </summary>
        /// <returns></returns>
        private static Action <TaskProcessOrganizer <string> .TaskItem> CreateScanActionORIGINAL()
        {
            const int TRIES = 10;

            return(new Action <TaskProcessOrganizer <string> .TaskItem>((o) =>
            {
                var fullFilePath = o.Item;

                Bitmap bmp = null;
                Bitmap bmpBarcode = null;
                BarcodeData data = null;

                string barcode = null;
                string siteCode = null;

                StateManager.Item item = null;

                try
                {
                    var info = new FileInfo(fullFilePath);

                    if (info.Exists && !info.IsReadOnly(TRIES))
                    {
                        // ".tif"
                        var ext = Path.GetExtension(info.FullName);

#if PROC_TIFF_AS_JPEG
                        if (ext == ".tif")
                        {
                            int c = 0;

                            foreach (var im in info.FullName.TiffGetAllImages())
                            {
                                var name = info.FullName.ChangeFilePath((n) => n.Replace(ext, c++ + ext));
                                im.Save(name);
                                im.DisposeSf();
                            }

                            return;
                        }
#endif
                        bmp = ((Bitmap)Bitmap.FromFile(info.FullName)).Crop2();

                        item = StateManager.Default.ProcessItem_Begin(!MainForm.ms_ImportCoversheet);
                        item.FullFileName = fullFilePath;
                        item.FileInfoList.Add(new FileInfo(fullFilePath)); // Scanned Image

                        if (!MainForm.ms_ImportCoversheet)
                        {
                            StateManager.VoucherItem vitem = (StateManager.VoucherItem)item;
                            FileInfo barcFilePath = null;
                            Rectangle rect = Rectangle.Empty;
                            CommonTools.ParseVoucherImage(ref bmp, ref bmpBarcode, out rect, ref barcode, BarcodeTypeEnum.BT_All);

                            vitem.Barcode = barcode;

                            if (vitem.HasBarcode)
                            {
                                string user = string.Concat("Country: ", Program.currentUser.CountryID, " User: "******"{0:dd-MM-yyyy hh:mm}", DateTime.Now);
                                        gr.DrawString(str, font, Brushes.Black, new PointF(10, 10));
                                        gr.DrawString(u.Cast <string>(), font, Brushes.Black, new PointF(10, 25));
                                    }
                                }, user);

                                var coverArea = StateSaver.Default.Get <Rectangle>(Strings.VOUCHERCOVERREGION);
                                if (!coverArea.IsEmpty)
                                {
                                    var size = StateSaver.Default.Get <int>(Strings.PIXELSIZE, 5);
                                    bmp.Pixellate(coverArea);
                                }

                                List <BarcodeConfig> barcodeLayouts = StateSaver.Default.Get <List <BarcodeConfig> >(Strings.LIST_OF_BARCODECONFIGS);

                                foreach (var cfg in barcodeLayouts)
                                {
                                    if (cfg.ParseBarcode(barcode, ref data))
                                    {
                                        break;
                                    }
                                }

                                if (data == null)
                                {
                                    throw new ApplicationException("Can not find barcode");
                                }

                                item.CountryID = data.CountryID;
                                vitem.RetailerID = data.RetailerID;
                                vitem.VoucherID = data.VoucherID;
                                vitem.Barcode = barcode;

                                var barcodePath = fullFilePath.ChangeFilePath((name) => name.Replace(".", "_barcode."));
                                Global.IgnoreList.Add(barcodePath);

                                bmpBarcode.DrawOnImage((gr, s) =>
                                {
                                    using (var font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular))
                                    {
                                        var str = Convert.ToString(s);
                                        gr.DrawString(str, font, Brushes.Red, new PointF(10, 10));
                                    }
                                }, barcode);

                                bmpBarcode.Save(barcodePath, bmp.RawFormat);

                                barcFilePath = new FileInfo(barcodePath); // Scanned Barcode
                                vitem.FileInfoList.Add(barcFilePath);     // Scanned Barcode Image
                            }
                        }
                        else
                        {
                            MainForm.ms_ImportCoversheet = false;
                            MainForm.Default.InvokeSf(() => MainForm.Default.cbCoversheet.Checked = false);
                        }

                        if (item.CountryID == 0)
                        {
                            item.CountryID = MainForm.ms_DefaultCountryId;
                        }

                        if (!ext.EqualNoCase(".tif"))
                        {
                            bmp.Save(fullFilePath, ImageFormat.Jpeg);
                        }

                        if (item.Thumbnail == null)
                        {
                            item.Thumbnail = bmp.GetThumbnailImage(45, 45, () => false, IntPtr.Zero);
                        }

                        var sec = new CertificateSecurity(X509FindType.FindBySerialNumber, Strings.CERTNUMBER, StoreLocation.LocalMachine);
                        if (sec.Loaded)
                        {
                            var signFilePath = new FileInfo(Path.ChangeExtension(fullFilePath, ".sgn"));
                            item.Signature = sec.SignData(bmp.ToArray());
                            File.WriteAllBytes(signFilePath.FullName, item.Signature);
                            item.FileInfoList.Add(signFilePath);
                        }

                        item.State = StateManager.eState.OK;
                        item.Message = "";
                    }
                }
                catch (Exception ex)
                {
                    if (item != null)
                    {
                        item.State = StateManager.eState.Err;
                        item.Message = ex.Message;
                    }
                    var scex = new ScanException(ex, data)
                    {
                        SiteCode = siteCode,
                        FilePath = fullFilePath
                    };

                    if (Error != null)
                    {
                        Error(null, new ThreadExceptionEventArgs(ex));
                    }
                }
                finally
                {
                    bmp.DisposeSf();
                    bmpBarcode.DisposeSf();

                    MainForm.Default.m_MainContext.Post(MainForm.Default.ShowItemScannedCallback, item);

                    try
                    {
                        StateManager.Default.ProcessItem_End(item);
                    }
                    catch (Exception ex0)
                    {
                        if (item != null)
                        {
                            item.State = StateManager.eState.Err;
                            item.Message = ex0.Message;
                        }

                        var scex = new ScanException(ex0, data)
                        {
                            SiteCode = siteCode,
                            FilePath = fullFilePath
                        };

                        if (Error != null)
                        {
                            Error(null, new ThreadExceptionEventArgs(scex));
                        }
                    }
                }
            }));
        }
Exemplo n.º 3
0
        public Action <TaskProcessOrganizer <string> .TaskItem> GetAction()
        {
            const int TRIES = 10;

            return(new Action <TaskProcessOrganizer <string> .TaskItem>((o) =>
            {
                var fullFilePath = o.Item;

                Bitmap bmp = null;
                Bitmap bmpBarcode = null;
                BarcodeData data = null;

                string siteCode = null;

                StateManager.Item item = StateManager.Default.ProcessItem_Begin(false);

                try
                {
                    var info = new FileInfo(fullFilePath);

                    if (info.Exists && !info.IsReadOnly(TRIES))
                    {
                        // ".tif"
                        var ext = Path.GetExtension(info.FullName);

                        bmp = ((Bitmap)Bitmap.FromFile(info.FullName)).Crop2();

                        item.FullFileName = fullFilePath;
                        item.FileInfoList.Add(new FileInfo(fullFilePath)); // Scanned Image

                        MainForm.ms_ImportCoversheet = false;
                        MainForm.Default.InvokeSf(() => MainForm.Default.cbCoversheet.Checked = false);

                        if (item.CountryID == 0)
                        {
                            item.CountryID = MainForm.ms_DefaultCountryId;
                        }

                        if (!ext.EqualNoCase(".tif"))
                        {
                            bmp.Save(fullFilePath, ImageFormat.Jpeg);
                        }

                        if (item.Thumbnail == null)
                        {
                            item.Thumbnail = bmp.GetThumbnailImage(45, 45, () => false, IntPtr.Zero);
                        }

                        item.State = StateManager.eState.OK;
                        item.Message = "";

                        StateManager.Default.CompleteItem(item);
                    }
                }
                catch (Exception ex)
                {
                    if (item != null)
                    {
                        item.State = StateManager.eState.Err;
                        item.Message = ex.Message;
                    }
                    var scex = new ScanException(ex, data)
                    {
                        SiteCode = "na",
                        FilePath = fullFilePath
                    };

                    DelegateHelper.FireError(this, ex);
                }
                finally
                {
                    bmp.DisposeSf();
                    bmpBarcode.DisposeSf();

                    DelegateHelper.PostItemScannedCallback(item);

                    try
                    {
                        StateManager.Default.AddNewItem(item);
                    }
                    catch (Exception ex0)
                    {
                        if (item != null)
                        {
                            item.State = StateManager.eState.Err;
                            item.Message = ex0.Message;
                        }
                        var scex = new ScanException(ex0, data)
                        {
                            SiteCode = siteCode,
                            FilePath = fullFilePath
                        };

                        DelegateHelper.FireError(this, scex);
                    }
                }
            }));
        }
        public Action <TaskProcessOrganizer <string> .TaskItem> GetAction()
        {
            return(new Action <TaskProcessOrganizer <string> .TaskItem>((o) =>
            {
                var fullFilePath = o.Item;

                Bitmap bmp = null;
                Bitmap bmpBarcode = null;
                BarcodeData data = null;

                string barcode = null;
                string siteCode = null;

                StateManager.VoucherItem item = (StateManager.VoucherItem)StateManager.Default.ProcessItem_Begin(true);

                try
                {
                    var info = new FileInfo(fullFilePath);

                    if (info.Exists && !info.IsReadOnly(TRIES))
                    {
                        // ".tif"
                        var ext = Path.GetExtension(info.FullName);

                        bmp = ((Bitmap)Bitmap.FromFile(info.FullName)).Crop2();
                        item.FullFileName = fullFilePath;
                        item.FileInfoList.Add(new FileInfo(fullFilePath)); // Scanned Image

                        StateManager.VoucherItem vitem = (StateManager.VoucherItem)item;
                        FileInfo barcFilePath = null;
                        Rectangle rect = Rectangle.Empty;
                        CommonTools.ParseVoucherImage(ref bmp, ref bmpBarcode, out rect, ref barcode, BarcodeTypeEnum.BT_All);
                        vitem.Barcode = barcode;

                        string user = string.Concat("Country: ", Program.currentUser.CountryID, " User: "******"{0:dd-MM-yyyy hh:mm}", DateTime.Now);
                                gr.DrawString(str, font, Brushes.Black, new PointF(10, 10));
                                gr.DrawString(u.Cast <string>(), font, Brushes.Black, new PointF(10, 25));
                            }
                        }, user);

                        var coverArea = StateSaver.Default.Get <Rectangle>(Strings.VOUCHERCOVERREGION);
                        if (!coverArea.IsEmpty)
                        {
                            var size = StateSaver.Default.Get <int>(Strings.PIXELSIZE, 5);
                            bmp.Pixellate(coverArea);
                        }

                        if (vitem.HasBarcode)
                        {
                            List <BarcodeConfig> barcodeLayouts = StateSaver.Default.Get <List <BarcodeConfig> >(Strings.LIST_OF_BARCODECONFIGS);

                            foreach (var cfg in barcodeLayouts)
                            {
                                if (cfg.ParseBarcodeSafe(barcode, ref data))
                                {
                                    break;
                                }
                            }

                            if (data == null)
                            {
                                throw new ApplicationException("Barcode invalid");
                            }

                            vitem.CountryID = data.CountryID;
                            vitem.RetailerID = data.RetailerID;
                            vitem.VoucherID = data.VoucherID;
                            vitem.Barcode = barcode;

                            var barcodePath = fullFilePath.ChangeFilePath((name) => name.Replace(".", "_barcode."));
                            Global.IgnoreList.Add(barcodePath);

                            bmpBarcode.DrawOnImage((gr, s) =>
                            {
                                using (var font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular))
                                {
                                    var str = Convert.ToString(s);
                                    gr.DrawString(str, font, Brushes.Red, new PointF(10, 10));
                                }
                            }, barcode);

                            bmpBarcode.Save(barcodePath, bmp.RawFormat);

                            barcFilePath = new FileInfo(barcodePath); // Scanned Barcode
                            vitem.FileInfoList.Add(barcFilePath);     // Scanned Barcode Image
                        }

                        item = StateManager.Default.AddTransferFileItem(item);

                        if (item.CountryID == 0)
                        {
                            item.CountryID = MainForm.ms_DefaultCountryId;
                        }

                        if (!ext.EqualNoCase(".tif"))
                        {
                            bmp.Save(fullFilePath, ImageFormat.Jpeg);
                        }

                        if (item.Thumbnail == null)
                        {
                            item.Thumbnail = bmp.GetThumbnailImage(45, 45, () => false, IntPtr.Zero);
                        }

                        var sec = new CertificateSecurity(X509FindType.FindBySerialNumber, Strings.CERTNUMBER, StoreLocation.LocalMachine);
                        if (sec.Loaded)
                        {
                            var signFilePath = new FileInfo(Path.ChangeExtension(fullFilePath, ".sgn"));
                            item.Signature = sec.SignData(bmp.ToArray());
                            File.WriteAllBytes(signFilePath.FullName, item.Signature);
                            item.FileInfoList.Add(signFilePath);
                            Global.IgnoreList.Add(signFilePath.FullName);
                        }

                        item.State = StateManager.eState.OK;
                        item.Message = "";
                        StateManager.Default.CompleteItem(item);
                    }
                }
                catch (Exception ex)
                {
                    item.State = StateManager.eState.Err;
                    item.Message = ex.Message;

                    var scex = new ScanException(ex, data)
                    {
                        SiteCode = siteCode,
                        FilePath = fullFilePath
                    };

                    int count = StateManager.Default.SetItemWithErr();
                    DelegateHelper.PostShowItemsWithErrCallback(count);
                    DelegateHelper.FireError(this, ex);
                }
                finally
                {
                    bmp.DisposeSf();
                    bmpBarcode.DisposeSf();
                    DelegateHelper.PostItemScannedCallback(item);
                    StateManager.Default.ShowNextItemExpected();
                }
            }));
        }