/* ItemFormatter.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; using Microsoft.Office.Interop.Word; using zaaReloaded2.LabModel; using zaaReloaded2.Controller.Comments; namespace zaaReloaded2.Formatter { /// /// Wraps a and provides methods to format it. /// public class ItemFormatter { #region Properties /// /// Gets or sets the LabItem wrapped by this ItemFormatter. /// public LabItem LabItem { get; set; } /// /// Gets or sets the ReferenceStyle to use in formatting. /// public ReferenceStyle ReferenceStyle { get; set; } /// /// Gets or sets the style to use for abnormal values. /// public AbnormalStyle AbnormalStyle { get; set; } /// /// Gets or sets a flag that indicates whether this ItemFormatter /// has been used, i.e. whether the LabItem was written to a /// document. /// public bool HasBeenUsed { get; set; } /// /// Gets or sets a flag that tells the formatter to include or /// not include the material indicator in the formatted output. /// Default is true. /// /// /// For example, items that are selected with a wildcard may /// contain the material info, while expressly chosen items /// may not. /// public bool IncludeMaterial { get; set; } /// /// Gets whether the Item is marked as blacklisted in the thesaurus. /// public bool IsBlacklisted { get { return LabItem.IsBlacklisted; } } /// /// Gets or sets the item's comment. /// public ItemComment Comment { get; set; } #endregion #region Constructor /// /// Creates a new ItemFormatter that wraps a . /// /// LabItem to wrap in this ItemFormatter. public ItemFormatter(LabItem labItem, ReferenceStyle referenceStyle, AbnormalStyle abnormalStyle) { IncludeMaterial = true; LabItem = labItem; ReferenceStyle = referenceStyle; AbnormalStyle = abnormalStyle; } #endregion #region Methods /// /// Formats and writes the LabItem details to a word . /// /// Word document to write to. public void WriteToDocument(Formatter formatter) { string reference; if ( LabItem.HasLimitsOrNormal && ( ReferenceStyle == ReferenceStyle.Always || (ReferenceStyle == ReferenceStyle.IfAbnormal && !LabItem.IsNormal) || (ReferenceStyle == ReferenceStyle.IfSpecialItem && LabItem.AlwaysPrintLimits) || (ReferenceStyle == ReferenceStyle.IfSpecialOrAbnormal && (!LabItem.IsNormal || LabItem.AlwaysPrintLimits)) ) ) { string normal; if (LabItem.HasLowerLimit && LabItem.HasUpperLimit) { normal = String.Format("{0}-{1}", LabItem.LowerLimit, LabItem.UpperLimit); } else { if (LabItem.HasLowerLimit) { normal = String.Format("> {0}", LabItem.LowerLimit); } else if (LabItem.HasUpperLimit) { normal = String.Format("< {0}", LabItem.UpperLimit); } else { normal = LabItem.Normal; } } reference = String.Format(" ({0})", normal); } else { reference = String.Empty; } string unit; if (LabItem.HasUnit) { string space = LabItem.Unit.StartsWith("/") ? String.Empty : " "; unit = String.Format("{0}{1}", space, LabItem.Unit); } else { unit = String.Empty; } string value; if (LabItem.IsNumerical) { // Format the numerical value; this will convert // decimal points to commas as needed. int precision = LabItem.PreferredPrecision; if (precision >= 0) { value = LabItem.NumericalValue.ToString("F" + precision); } else { // PreferredPrecision is negative, i.e. use precision as-is value = String.Format("{0}", LabItem.NumericalValue); } } else { value = LabItem.Value; } string comment = String.Empty; if (Comment != null) { comment = Comment.BuildComment(); if (comment != String.Empty) comment = " " + comment; } string name = IncludeMaterial ? LabItem.QualifiedName : LabItem.Name; string output = String.Format( "{0} {1}{2}{3}{4}", name, value, unit, reference, comment ); if (!LabItem.IsNormal) { output = AbnormalStyle.ToMarkup(false) + output + AbnormalStyle.ToMarkup(true); } formatter.Write(output); HasBeenUsed = true; } #endregion } }