Ejemplo n.º 1
0
        /// <summary>
        /// Formats the braille text input based on the braille job parameters
        /// </summary>
        /// <param name="source">the input text</param>
        /// <param name="job">the BrailleJob class</param>
        /// <returns>the formatted text</returns>
        public static string FormatBraille(string source, BrailleJob job)
        {
            int cols = job.CharactersPerLine;
            int rows = job.LinesPerPage;
            //bool isPef = OutputFormat.Pef.Equals(job.OutputFormat);
            //bool isUni = OutputFormat.Unicode.Equals(job.OutputFormat);
            string result = source;

            try
            {
                Encoding enc = RoboBrailleProcessor.GetEncodingByCountryCode(job.BrailleLanguage);
                switch (job.OutputFormat)
                {
                case OutputFormat.Pef:
                {
                    result = RoboBrailleProcessor.ConvertBrailleToUnicode(enc.GetBytes(result));

                    if (cols > 10 && rows > 2 && !String.IsNullOrWhiteSpace(result))
                    {
                        result = RoboBrailleProcessor.CreatePagination(result, cols);
                        if (PageNumbering.none.Equals(job.PageNumbering))
                        {
                            result = RoboBrailleProcessor.CreatePefFromUnicode(result, cols, rows, 0, true);
                        }
                        else
                        {
                            result = RoboBrailleProcessor.CreatePefFromUnicodeWithPageNumber(result, cols, rows, 0, true, PageNumbering.right.Equals(job.PageNumbering));
                        }
                    }
                    break;
                }

                case OutputFormat.Unicode:
                {
                    result = RoboBrailleProcessor.ConvertBrailleToUnicode(enc.GetBytes(result));

                    if (cols > 10 && rows > 2 && !String.IsNullOrWhiteSpace(result))
                    {
                        result = RoboBrailleProcessor.CreatePagination(result, cols);
                        result = RoboBrailleProcessor.CreateNewLine(result, rows);
                    }
                    break;
                }

                case OutputFormat.NACB:
                {
                    result = RoboBrailleProcessor.ConvertBrailleToUnicode(enc.GetBytes(result));
                    result = enc.GetString(RoboBrailleProcessor.ConvertUnicodeToNACB(result)).ToLowerInvariant();

                    if (cols > 10 && rows > 2 && !String.IsNullOrWhiteSpace(result))
                    {
                        result = RoboBrailleProcessor.CreatePagination(result, cols);
                        result = RoboBrailleProcessor.CreateNewLine(result, rows);
                    }
                    break;
                }

                default:
                {
                    if (cols > 10 && rows > 2 && !String.IsNullOrWhiteSpace(result))
                    {
                        result = RoboBrailleProcessor.CreatePagination(result, cols);
                        result = RoboBrailleProcessor.CreateNewLine(result, rows);
                    }
                    break;
                }
                }
                return(result);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message);
                return(source);
            }
        }
        public Task <Guid> SubmitWorkItem(BrailleJob job)
        {
            if (job == null)
            {
                return(null);
            }

            try
            {
                _context.Jobs.Add(job);
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage));
                throw new DbEntityValidationException(errorMessages);
            }

            var task = Task.Factory.StartNew(t =>
            {
                try
                {
                    string strBraille = null;
                    if (!string.IsNullOrWhiteSpace(job.TranslationTable))
                    {
                        strBraille = this.LouisBrailleConvert(job);
                    }
                    else
                    {
                        switch (job.BrailleLanguage)
                        {
                        case Language.daDK:
                        //case Language.enUS:
                        case Language.enGB:
                        case Language.nnNO:
                        case Language.isIS:
                        case Language.svSE:
                        //strBraille = this.SensusBrailleConvert(job);
                        //break;
                        default:
                            strBraille = this.LouisBrailleConvert(job);
                            break;
                        }
                    }

                    if (strBraille != null)
                    {
                        strBraille   = RoboBrailleProcessor.FormatBraille(strBraille, job);
                        Encoding enc = RoboBrailleProcessor.GetEncodingByCountryCode(job.BrailleLanguage);
                        switch (job.OutputFormat)
                        {
                        case OutputFormat.Pef:
                            {
                                job.ResultContent = Encoding.UTF8.GetBytes(strBraille);
                                break;
                            }

                        case OutputFormat.Unicode:
                            {
                                job.ResultContent = Encoding.Unicode.GetBytes(strBraille);
                                break;
                            }

                        case OutputFormat.NACB:
                            {
                                job.ResultContent = enc.GetBytes(strBraille);
                                break;
                            }

                        default:
                            {
                                job.ResultContent = enc.GetBytes(strBraille);
                                break;
                            }
                        }
                    }
                    else
                    {
                        RoboBrailleProcessor.SetJobFaulted(job, _context);
                        throw new Exception("Braille conversion result is null!");
                    }
                    string fileExtension = ".txt";
                    string mime          = "text/plain";
                    string fileName      = job.FileName;
                    var outputFormat     = (OutputFormat)job.OutputFormat;

                    switch (outputFormat)
                    {
                    case OutputFormat.Pef:
                        mime          = "application/x-pef";
                        fileExtension = ".pef";
                        break;

                    default:
                        fileExtension = ".txt";
                        mime          = "text/plain";
                        break;
                    }
                    job.DownloadCounter     = 0;
                    job.ResultFileExtension = fileExtension;
                    job.ResultMimeType      = mime;
                    job.Status                = JobStatus.Done;
                    job.FinishTime            = DateTime.Now;
                    _context.Entry(job).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                    RoboBrailleProcessor.SetJobFaulted(job, _context);
                    throw ex;
                }
            }, job);

            return(Task.FromResult(job.Id));
        }