zaaReloaded2/zaaReloaded2/Formatter/ItemFormatter.cs

210 lines
6.7 KiB
C#
Executable File

/* 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
{
/// <summary>
/// Wraps a <see cref="LabItem"/> and provides methods to format it.
/// </summary>
public class ItemFormatter
{
#region Properties
/// <summary>
/// Gets or sets the LabItem wrapped by this ItemFormatter.
/// </summary>
public LabItem LabItem { get; set; }
/// <summary>
/// Gets or sets the ReferenceStyle to use in formatting.
/// </summary>
public ReferenceStyle ReferenceStyle { get; set; }
/// <summary>
/// Gets or sets the style to use for abnormal values.
/// </summary>
public AbnormalStyle AbnormalStyle { get; set; }
/// <summary>
/// Gets or sets a flag that indicates whether this ItemFormatter
/// has been used, i.e. whether the LabItem was written to a
/// document.
/// </summary>
public bool HasBeenUsed { get; set; }
/// <summary>
/// Gets or sets a flag that tells the formatter to include or
/// not include the material indicator in the formatted output.
/// Default is true.
/// </summary>
/// <remarks>
/// For example, items that are selected with a wildcard may
/// contain the material info, while expressly chosen items
/// may not.
/// </remarks>
public bool IncludeMaterial { get; set; }
/// <summary>
/// Gets whether the Item is marked as blacklisted in the thesaurus.
/// </summary>
public bool IsBlacklisted { get { return LabItem.IsBlacklisted; } }
/// <summary>
/// Gets or sets the item's comment.
/// </summary>
public ItemComment Comment { get; set; }
#endregion
#region Constructor
/// <summary>
/// Creates a new ItemFormatter that wraps a <paramref name="labItem"/>.
/// </summary>
/// <param name="labItem">LabItem to wrap in this ItemFormatter.</param>
public ItemFormatter(LabItem labItem,
ReferenceStyle referenceStyle,
AbnormalStyle abnormalStyle)
{
IncludeMaterial = true;
LabItem = labItem;
ReferenceStyle = referenceStyle;
AbnormalStyle = abnormalStyle;
}
#endregion
#region Methods
/// <summary>
/// Formats and writes the LabItem details to a word <paramref name="document"/>.
/// </summary>
/// <param name="document">Word document to write to.</param>
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
}
}