Exemplo n.º 1
0
        private static void SimulateWorkerRole(object workerRoleParameters)
        {
            if (workerRoleParameters == null)
            {
                throw (new ArgumentNullException("workerRoleParameters"));
            }

            if (workerRoleParameters is WorkerRoleParameters)
            {
                WorkerRoleSimulation.SimulateWorkerRole((WorkerRoleParameters)workerRoleParameters);
            }
            else
            {
                throw (new ArgumentException("The argument [workerRoleParameters] must be of type [WorkerRoleParameters].", "workerRoleParameters"));
            }
        }
Exemplo n.º 2
0
        public static bool TenantDocumentPerformOcr(Tenant tenant, Document document, WorkerRoleParameters workerRoleParameters, out Exception exception)
        {
            var      result      = false;
            Document ocrDocument = null;
            List <DocumentTemplate> ocrDocumentTemplate = null;
            List <DocumentFragment> ocrDocumentFragment = null;

            exception = null;

            try
            {
                List <Template>        allTemplates = null;
                List <TemplateElement> allElements  = null;

                using (var context = new ContextTenant(tenant.DatabaseConnectionString))
                {
                    allTemplates = context
                                   .Templates
                                   .AsNoTracking()
                                   .Include(t => t.Elements)
                                   .Include(t => t.Elements.Select(x => x.ElementDetails))
                                   .Where(t => (t.IsActive == true) && (t.IsFinalized == true))
                                   .ToList();

                    if (allTemplates.Count <= 0)
                    {
                        throw (new Exception("No templates found."));
                    }
                }

                // workerRoleParameters???

                // Download file from azure.
                Stream stream = null;
                List <OcrResultInfo> ocrresultinfos = new List <OcrResultInfo> ();

                if (WorkerRoleSimulation.GetTenantDocumentFileStream(tenant, document, out stream, out exception))
                {
                    Image documentImage = null;

                    using (var image = Image.FromStream(stream))
                    {
                        documentImage = (Image)image.Clone();
                        // LeadTools code.
                        // Stream and image available.
                    }

                    // TODO: Replace with Server.MapPath.
                    //LeadToolsOCR leadtoolsocr = new LeadToolsOCR (workerRoleParameters.OcrEngineSettings.GetPathData().FullName, tenant.Id.ToString (), string.Empty, out exception);
                    //if (exception != null) { throw exception; }

                    List <LeadtoolsBarcodeData> barcodedata = null;
                    bool foundbarcode = false;                     // leadtoolsocr.ReadBarcode (documentImage, out barcodedata, out exception);
                    //if (exception != null) { throw exception; }

                    if (foundbarcode)
                    {
                        //What to do if multiple barcodes are found.
                        int  templateid = 0;
                        bool foundId    = int.TryParse(barcodedata.First().Value, out templateid);
                        if (!(foundId) || templateid <= 0)
                        {
                        }                          //What to do if Form Id is Not Found by the barcode
                        Template template = allTemplates.Where(t => t.Id == ((long)templateid)).ToArray().FirstOrDefault();
                        if (template.TemplateType == TemplateType.Form)
                        {
                            double _computeddifference = 0.0;
                            if ((documentImage.Width <= documentImage.Height) && (!(documentImage.Height <= 0)))
                            {
                                _computeddifference = ((Convert.ToDouble(documentImage.Width)) / (Convert.ToDouble(documentImage.Height)));
                            }
                            else
                            {
                                if (!(documentImage.Width <= 0))
                                {
                                    _computeddifference = documentImage.Height / documentImage.Width;
                                }
                            }
                            var elements = allElements.Where(e => e.TemplateId == (long)templateid).ToList();
                            List <ComputeCoordinates> cordlistlist = new List <ComputeCoordinates> ();
                            //cordlistlist = leadtoolsocr.GetAllZoneData (elements, _computeddifference, documentImage);

                            //what to do with form matched results


                            //foreach (ComputeCoordinates cordata in cordlistlist)
                            //{
                            //    returnedresult += cordata.Text;
                            //}
                        }
                    }
                    else
                    {
                        result = AutoOCR(workerRoleParameters.OcrEngineSettings, tenant, documentImage, document, allTemplates, out ocrDocument, out ocrDocumentFragment, out ocrDocumentTemplate, out exception);
                        if (exception != null)
                        {
                            throw exception;
                        }
                    }

                    // Perform OCR and time-consuming tasks.
                }
                else
                {
                    throw (exception);
                }

                using (var context = new ContextTenant(tenant.DatabaseConnectionString))
                {
                    using (var transaction = context.Database.BeginTransaction())
                    {
                        try
                        {
                            if (ocrDocument != null)
                            {
                                context.Documents.Attach(ocrDocument);
                                context.Entry(ocrDocument).State = EntityState.Modified;
                                context.SaveChanges();
                            }
                            if (ocrDocumentFragment != null)
                            {
                                foreach (var documentfragment in ocrDocumentFragment)
                                {
                                    context.DocumentFragments.Add(documentfragment);
                                    context.SaveChanges();
                                }
                            }
                            if (ocrDocumentTemplate != null)
                            {
                                foreach (var documenttemplate in ocrDocumentTemplate)
                                {
                                    context.DocumentTemplate.Add(documenttemplate);
                                    context.SaveChanges();
                                }
                            }
                            //context.Templates.Attach(templates[0]);
                            context.SaveChanges();

                            // Do not call this line from anywhere else.
                            transaction.Commit();
                        }
                        catch (Exception e)
                        {
                            transaction.Rollback();
                        }
                    }
                }

                result = true;
            }
            catch (Exception e)
            {
                exception = e;
            }

            return(result);
        }
Exemplo n.º 3
0
        private static void SimulateWorkerRole(WorkerRoleParameters workerRoleParameters)
        {
            Exception exception = null;

            Console.WriteLine();
            Console.WriteLine();
            Console.Write("OCR Thread Started.");

            try
            {
                workerRoleParameters.CancellationToken.ThrowIfCancellationRequested();

                do
                {
                    try
                    {
                        Document      document = null;
                        List <Tenant> tenants  = null;

                        workerRoleParameters.CancellationToken.ThrowIfCancellationRequested();

                        Console.WriteLine();
                        Console.WriteLine();
                        Console.Write("Getting tenants.");

                        if (Business.GetTenants(out tenants, out exception))
                        {
                            Console.WriteLine();
                            Console.Write($"Found {tenants.Count:N0} tenants.");

                            foreach (var tenant in tenants)
                            {
                                workerRoleParameters.CancellationToken.ThrowIfCancellationRequested();

                                Console.WriteLine();
                                Console.Write($"Processing tenant: [{tenant.Id}]: [{tenant.Domain}].");

                                Console.WriteLine();
                                Console.Write($"Getting queued document.");

                                if (Business.TenantDocumentGet(tenant, out document, out exception))
                                {
                                    if (document == null)
                                    {
                                        Console.WriteLine();
                                        Console.Write($"None found.");
                                    }
                                    else
                                    {
                                        Console.WriteLine();
                                        Console.Write($"Found document: Id: {document.Id}, Name: {document.Name}, Folder: {document.Folder?.Name}, User: {document.User.UserName}.");

                                        try
                                        {
                                            workerRoleParameters.CancellationToken.ThrowIfCancellationRequested();

                                            if (!WorkerRoleSimulation.TenantDocumentPerformOcr(tenant, document, workerRoleParameters, out exception))
                                            {
                                                Console.WriteLine();
                                                Console.Write($"Exception: {exception.Message}");
                                            }
                                        }
                                        catch (OperationCanceledException)
                                        {
                                            // Ignore.
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine();
                                            Console.Write($"Exception: {e.Message}");
                                        }
                                    }
                                }
                                else
                                {
                                    // TODO: Requires an exit strategy.
                                    Console.WriteLine();
                                    Console.Write($"Exception: {exception.Message}");
                                }

                                Thread.Sleep(1000);
                            }
                        }
                        else
                        {
                            // TODO: Requires an exit strategy.
                            Console.WriteLine();
                            Console.Write($"Exception: {exception.Message}");
                        }

                        Thread.Sleep(1000);
                        Console.WriteLine();
                        Console.Write("Working");
                    }
                    catch (OperationCanceledException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        // TODO: Log.
                        // TODO: Decide whether to exit loop.
                        Trace.TraceError(e.ToString());

                        throw;
                    }
                }while (!workerRoleParameters.CancellationToken.IsCancellationRequested);
            }
            catch (OperationCanceledException)
            {
                // Ignore.
            }
            catch
            {
                // TODO: Log.
                throw;
            }
        }