diff --git a/zaaReloaded2/Commands.cs b/zaaReloaded2/Commands.cs index c40a37d..2a023b0 100755 --- a/zaaReloaded2/Commands.cs +++ b/zaaReloaded2/Commands.cs @@ -203,6 +203,7 @@ namespace zaaReloaded2 Word.Window activeWindow = word.ActiveWindow; Word.Selection selection = activeWindow.Selection; Word.Paragraphs paragraphs = selection.Paragraphs; + Importer.IImporter importer = null; if (!(paragraphs.Count > 1 || (selection.Text.Length > 1 && selection.Text.EndsWith("\r")))) { @@ -221,10 +222,10 @@ namespace zaaReloaded2 } // Don't release the COM object here // Bovender.ComHelpers.ReleaseComObject(doc); + importer = autoDetector.CreateImporter(); } Logger.Info("DoFormat: Importing"); - ZaaImporter importer = new ZaaImporter(); importer.Import(selection.Text); Formatter.Formatter formatter = new Formatter.Formatter(activeDocument); diff --git a/zaaReloaded2/Demo/Demo.docx b/zaaReloaded2/Demo/Demo.docx index a81f68b..c54495e 100755 Binary files a/zaaReloaded2/Demo/Demo.docx and b/zaaReloaded2/Demo/Demo.docx differ diff --git a/zaaReloaded2/Importer/AutoDetector.cs b/zaaReloaded2/Importer/AutoDetector.cs index b1798eb..e02f31b 100755 --- a/zaaReloaded2/Importer/AutoDetector.cs +++ b/zaaReloaded2/Importer/AutoDetector.cs @@ -27,8 +27,14 @@ namespace zaaReloaded2.Importer { class AutoDetector { + #region Properties + + public ImportMode ImportMode { get; private set; } + + #endregion + #region Public methods - + /// /// Attempts to automatically detect laboratory data in the Word /// document. @@ -53,6 +59,7 @@ namespace zaaReloaded2.Importer if (document.Bookmarks.Exists("Labor")) { + Logger.Info("Detect: Found lab bookmark"); i = GetParagraphIndex( document, document.Bookmarks["Labor"].Range.Paragraphs[1]); @@ -65,10 +72,10 @@ namespace zaaReloaded2.Importer // lines such as "Tel. (09 31) 201-39432; -39126", which // happen to structurally resemble a paragraph with // laboratory items. - if (LaurisTimePoint.IsTimeStampLine( - document.Paragraphs[i].Range.Text)) + if (IsTimeStampParagraph(document.Paragraphs[i])) { start = document.Paragraphs[i]; + Logger.Info("Detect: Found time stamp line in paragraph #{0}", i); break; } i++; @@ -76,11 +83,14 @@ namespace zaaReloaded2.Importer if (start != null) { + Logger.Info("Detect: Determining lab block"); end = start; while (i <= document.Paragraphs.Count - 1) { - if (!IsLabParagraph(document.Paragraphs[i+1])) + Paragraph p = document.Paragraphs[i+1]; + if (!IsLabParagraph(p) && !IsEmptyParagraph(p)) { + Logger.Info("Detect: Last lab paragraph is #{0}", i); end = document.Paragraphs[i]; break; } @@ -90,9 +100,23 @@ namespace zaaReloaded2.Importer document.Range(start.Range.Start, end.Range.End).Select(); return true; } + Logger.Warn("Detect: Did not find lab block!"); return false; } + public IImporter CreateImporter() + { + switch (ImportMode) + { + case ImportMode.Zaa: + return new ZaaImporter.ZaaImporter(); + case ImportMode.Clinic: + return new ClinicImporter.ClinicImporter(); + default: + throw new InvalidOperationException("Cannot create Importer for undefined import mode!"); + } + } + #endregion #region Private methods @@ -109,9 +133,10 @@ namespace zaaReloaded2.Importer // the lab mode *must* be ZAA, because it will be a line in the form // "(17.09.2015-201710:44:00) Cyclosporin-A vor Gabe: 130 µg/l;" which does not // occur in the clinic format. - if ((_mode == Mode.Undefined) && isZaaTimePoint && !isCinicTimePoint) + if ((ImportMode == ImportMode.Undefined) && isZaaTimePoint && !isCinicTimePoint) { - _mode = Mode.Zaa; + Logger.Info("IsTimeStampParagraph: Found ZAA time stamp, setting mode to ZAA"); + ImportMode = ImportMode.Zaa; } return isCinicTimePoint || isZaaTimePoint; } @@ -131,24 +156,26 @@ namespace zaaReloaded2.Importer { string text = paragraph.Range.Text; bool isLabParagraph = false; - switch (_mode) + switch (ImportMode) { - case Mode.Undefined: + case ImportMode.Undefined: if (LaurisParagraph.ResemblesLaurisParagraph(text) || LaurisTimePoint.IsTimeStampLine(text)) { - _mode = Mode.Zaa; + ImportMode = ImportMode.Zaa; + Logger.Info("IsLabParagraph: Setting mode to ZAA"); isLabParagraph = true; } else if (ClinicLine.ResemblesClinicLine(text) || ClinicTimePoint.IsTimeStampLine(text)) { - _mode = Mode.Clinic; + ImportMode = ImportMode.Clinic; + Logger.Info("IsLabParagraph: Setting mode to Clinic"); isLabParagraph = true; } break; - case Mode.Zaa: + case ImportMode.Zaa: isLabParagraph = LaurisParagraph.ResemblesLaurisParagraph(text) || LaurisTimePoint.IsTimeStampLine(text); break; - case Mode.Clinic: + case ImportMode.Clinic: isLabParagraph = ClinicLine.ResemblesClinicLine(text) || ClinicTimePoint.IsTimeStampLine(text); break; default: @@ -169,19 +196,20 @@ namespace zaaReloaded2.Importer { return document.Range(0, paragraph.Range.Start).Paragraphs.Count; } + + private bool IsEmptyParagraph(Paragraph paragraph) + { + string text = paragraph.Range.Text; + return String.IsNullOrWhiteSpace(text); + } #endregion - #region Fields + #region Class logger - private enum Mode - { - Undefined, - Zaa, - Clinic - } + private static NLog.Logger Logger { get { return _logger.Value; } } - private Mode _mode; + private static readonly Lazy _logger = new Lazy(() => NLog.LogManager.GetCurrentClassLogger()); #endregion } diff --git a/zaaReloaded2/Importer/ClinicImporter/ClinicLine.cs b/zaaReloaded2/Importer/ClinicImporter/ClinicLine.cs index 43459c9..3716ce7 100755 --- a/zaaReloaded2/Importer/ClinicImporter/ClinicLine.cs +++ b/zaaReloaded2/Importer/ClinicImporter/ClinicLine.cs @@ -121,7 +121,7 @@ namespace zaaReloaded2.Importer.ClinicImporter #region Fields - static readonly Regex _expectedFormat = new Regex(@"\t(?[^:]+:(\t[^\t]+){3})"); + static readonly Regex _expectedFormat = new Regex(@"\t(?[^:]+:(\t([^\t]+)?){3})"); Thesaurus.Parameters _parameterDictionary; Thesaurus.Units _unitDictionary; diff --git a/zaaReloaded2/Importer/ImportMode.cs b/zaaReloaded2/Importer/ImportMode.cs new file mode 100755 index 0000000..994bc8f --- /dev/null +++ b/zaaReloaded2/Importer/ImportMode.cs @@ -0,0 +1,31 @@ +/* ImportMode.cs + * part of zaaReloaded2 + * + * Copyright 2017 Daniel Kraus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace zaaReloaded2.Importer +{ + public enum ImportMode + { + Undefined, + Zaa, + Clinic + } +}