zaaReloaded2/zaaReloaded2/Medication/Formatter.cs

211 lines
7.6 KiB
C#
Executable File

using Microsoft.Office.Interop.Word;
/* Formatter.cs
* part of zaaReloaded2
*
* Copyright 2015-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.Medication
{
/// <summary>
/// Formats prescriptions
/// </summary>
public class Formatter
{
#region Properties
public IList<Prescription> Prescriptions { get; set; }
#endregion
#region Constructor
public Formatter() { }
public Formatter(IList<Prescription> prescriptions)
: this()
{
Prescriptions = prescriptions;
}
#endregion
#region Methods
/// <summary>
/// Writes a block of prescriptions with one column to a
/// Word document.
/// </summary>
/// <param name="document"></param>
public void FormatOneColumn(Document document)
{
DoFormat("Medikation einspaltig formatieren",
document,
writer =>
{
foreach (Prescription p in Prescriptions)
{
writer.WriteLine(p.ToString());
}
});
}
/// <summary>
/// Writes a block of prescriptions with two columns to a
/// Word document.
/// </summary>
/// <param name="document"></param>
public void FormatTwoColumns(Document document)
{
DoFormat("Medikation zweispaltig formatieren",
document,
writer =>
{
int half = Prescriptions.Count / 2 + Prescriptions.Count % 2;
for (int i = 0; i < half; i++)
{
writer.Write(Prescriptions[i].ToString());
if (i + half < Prescriptions.Count)
{
writer.Write("\t" + Prescriptions[i + half].ToString());
}
writer.WriteLine();
}
});
}
/// <summary>
/// Creates a table containing all prescriptions and copies it to
/// the clipboard.
/// </summary>
public void CreatePrescriptionsTable()
{
throw new NotImplementedException();
}
#endregion
#region Private methods
void AddDisclaimer(zaaReloaded2.Formatter.DocumentWriter writer)
{
if (HasMMF())
{
writer.WriteLine();
writer.WriteLine("<b>Hinweis: Während und nach Therapie mit Mycophenolsäurederivaten wie CellCept\u00ae " +
"und Myfortic\u00ae müssen Frauen und Männer eine Schwangerschaft sicher verhüten (siehe Rote-Hand-Brief zu " +
"CellCept\u00ae vom 10.11.2015).</b>");
writer.WriteLine();
}
writer.WriteLine("<highlight><b>Bitte Medikation überprüfen!</b></highlight>");
}
/// <summary>
/// Creates a paragraph and character styles in the document.
/// </summary>
void CreateStyles(Document document)
{
if (document != null)
{
Style style;
// Don't see a better way to check for the existence of a particular
// paragraph style than by using a try...catch construction.
try
{
style = document.Styles[Properties.Settings.Default.DrugsParagraph];
}
catch
{
// Add default paragraph style for laboratory
style = document.Styles.Add(Properties.Settings.Default.DrugsParagraph);
style.Font.Size = 10; // pt
style.Font.Bold = 0;
style.Font.Italic = 0;
style.Font.Underline = 0;
style.ParagraphFormat.SpaceAfter = 0;
style.ParagraphFormat.SpaceBefore = 0;
style.ParagraphFormat.LeftIndent = 0; // pt
style.ParagraphFormat.FirstLineIndent = 0; // pt
style.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
style.ParagraphFormat.TabStops.ClearAll();
int tabStop = 108; // 108 pt = 2.5 in = 3.8 cm
int halfWay = 227; // 227 pt = 3.15 in = 8 cm
style.ParagraphFormat.TabStops.Add(tabStop);
style.ParagraphFormat.TabStops.Add(halfWay);
style.ParagraphFormat.TabStops.Add(halfWay + tabStop);
}
// try
// {
// style = document.Styles[Properties.Settings.Default.DrugsHeader];
// }
// catch
// {
// // Add header paragraph style for laboratory
// style = document.Styles.Add(Properties.Settings.Default.DrugsHeader);
// style.Font.Size = 10; // pt
// style.Font.Bold = 1;
// style.Font.Italic = 0;
// style.Font.Underline = WdUnderline.wdUnderlineSingle;
// style.ParagraphFormat.SpaceAfter = 0;
// style.ParagraphFormat.SpaceBefore = 12;
// style.ParagraphFormat.LeftIndent = 36; // pt
// style.ParagraphFormat.FirstLineIndent = -36; // pt
// style.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
// style.set_NextParagraphStyle(document.Styles[Properties.Settings.Default.DrugsParagraph]);
// }
}
}
/// <summary>
/// Does the heavy lifting in a DRY way.
/// </summary>
void DoFormat(string description, Document document,
Action<zaaReloaded2.Formatter.DocumentWriter> outputAction)
{
if (document == null)
{
throw new ArgumentNullException(
"Cannot format prescriptions because no document was given.");
}
Helpers.StartUndo(description);
zaaReloaded2.Formatter.DocumentWriter writer = new zaaReloaded2.Formatter.DocumentWriter(document);
CreateStyles(document);
writer.Write(String.Format("<style:{0}>", Properties.Settings.Default.DrugsParagraph));
outputAction(writer);
AddDisclaimer(writer);
// writer.Write("</style>"); // causes COM exceptions, needs fix
writer.Flush();
Helpers.EndUndo();
}
/// <summary>
/// Determines whether MMF or MPA is contained in the prescriptions.
/// </summary>
/// <returns>True if MMF or MPA is prescribed.</returns>
bool HasMMF()
{
return Prescriptions.FirstOrDefault(p => p.IsMmf) != null;
}
#endregion
}
}