示例#1
0
        public ScanFile InsertScanImage(int bookID, PartOfBook partOfBook, bool useOCR, string userName, string computer, string comment, byte[] image, bool obalkyKnihCZ)
        {
            //kontrola vstupnich parametru
            if (bookID == 0)
                throw new ArgumentNullException("Neplatný parametr identifikátor publikace.");

            string extension = null;

            switch (partOfBook)
            {
                case PartOfBook.FrontCover:
                    extension = FileFormat.Jpg.ToString();
                    break;
                case PartOfBook.TableOfContents:
                    extension = FileFormat.Tif.ToString();
                    break;
                default:
                    throw new ArgumentException(String.Format("Neplatný parametr '{0}' skenovaná část publikace.", partOfBook));
            }

            if (String.IsNullOrEmpty(userName))
                throw new ArgumentNullException("Neplatný parametr jméno uživatele.");

            if (String.IsNullOrEmpty(computer))
                throw new ArgumentNullException("Neplatný parametr název počítače.");

            if ((image == null) || (image.Length == 0))
                throw new ArgumentNullException("Naskenovaný obrázek je prázdný.");

            //kontrola existence publikace
            Book book = BookComponent.Instance.GetByID(bookID);
            if (book == null)
                throw new ApplicationException(String.Format("Záznam publikace (ID={0}) neexistuje.", bookID));

            if (book.HasPartOfBook(partOfBook))
                throw new ApplicationException(String.Format("Záznam publikace (ID={0}) již obsahuje část '{1}'.", bookID, partOfBook.ToDisplay()));

            //vytvoreni nazvu souboru
            ScanFile result = new ScanFile();
            result.FileName = String.Format("{0}.{1}", book.GetFileName(), extension.ToLower());

            //ulozenie souboru naskenovaneho obrazku
            string filePath = null;

            try
            {
                filePath = Path.Combine(book.GetDirectoryPath(), result.FileName);
                result.PageCount = ImageFunctions.WriteFile(filePath, image);
            }
            catch (Exception ex)
            {
                throw new ApplicationException(String.Format("Nepodařilo se uložit naskenovaný soubor '{0}' na disk: {1}.", filePath, ex.Message));
            }

            //ulozenie zaznamu do databaze
            ScanFileRepository repository = new ScanFileRepository();

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    result.BookID = book.BookID;
                    result.PartOfBook = partOfBook;
                    result.UseOCR = (partOfBook == PartOfBook.FrontCover ? false : useOCR);
                    result.Comment = comment.Left(1000);
                    result.Created = DateTime.Now;
                    result.Modified = result.Created;
                    result.Status = StatusCode.Scanned;
                    repository.Create(result);

                    LogOperation(result.ScanFileID, userName, computer, result.Modified, result.Comment, result.Status);

                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(String.Format("Nepodařilo se uložit data souboru publikace (ID={0}) do databáze.", bookID), ex);
                }
            }

            //operace dokonceni pro obalky a obsahy bez OCR
            switch (result.PartOfBook)
            {
                case PartOfBook.FrontCover:
                    if (obalkyKnihCZ)
                    {
                        result = ImportObalkyKnih(result.ScanFileID, userName, computer);
                    }
                    break;

                case PartOfBook.TableOfContents:
                    if (!result.UseOCR)
                    {
                        result = CompleteContents(result.ScanFileID, userName, computer);
                    }
                    break;

                default:
                    break;
            }

            return result;
        }