Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 70 additions & 6 deletions NHapi20/NHapi.Base/Model/AbstractSegment.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
/// this file under either the MPL or the GPL. /// this file under either the MPL or the GPL.
*/ */


using NHapi.Base.Log;
using NHapi.Base.Parser;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Reflection; using System.Reflection;
using NHapi.Base; using System.Text.RegularExpressions;
using NHapi.Base.Parser;
using NHapi.Base.Log;


namespace NHapi.Base.Model namespace NHapi.Base.Model
{ {
Expand Down Expand Up @@ -70,7 +69,7 @@ static AbstractSegment()
log = HapiLogFactory.GetHapiLog(typeof(AbstractSegment)); log = HapiLogFactory.GetHapiLog(typeof(AbstractSegment));
} }


#endregion #endregion Constructor


/// <summary> Returns the Message to which this segment belongs. </summary> /// <summary> Returns the Message to which this segment belongs. </summary>
public virtual IMessage Message public virtual IMessage Message
Expand Down Expand Up @@ -171,7 +170,6 @@ public virtual IType GetField(int number, int rep)
"Can't get repetition " + rep + " from field " + number + " - maximum repetitions is only " + "Can't get repetition " + rep + " from field " + number + " - maximum repetitions is only " +
_items[number - 1].MaxRepetitions + " reps.", HL7Exception.APPLICATION_INTERNAL_ERROR); _items[number - 1].MaxRepetitions + " reps.", HL7Exception.APPLICATION_INTERNAL_ERROR);



//add a rep if necessary ... //add a rep if necessary ...
if (rep == currentReps) if (rep == currentReps)
{ {
Expand Down Expand Up @@ -382,6 +380,72 @@ protected internal virtual void add(Type c, bool required, int maxReps, int leng
_items.Add(new AbstractSegmentItem(c, required, maxReps, length, constructorArgs, description)); _items.Add(new AbstractSegmentItem(c, required, maxReps, length, constructorArgs, description));
} }


/// <summary>
/// Remove a valid index from a repeatable field.
/// </summary>
/// <param name="fieldNum">Repeatable field number</param>
/// <param name="index">0-based index to be removed</param>
public void RemoveRepetition(int fieldNum, int index)
{
if (fieldNum < 1 || fieldNum > _items.Count)
{
throw new HL7Exception($"Can't retrieve field {fieldNum} from segment {GetType().FullName} - there are only {_items[fieldNum - 1].Fields.Count} fields.", HL7Exception.APPLICATION_INTERNAL_ERROR);
}

var fields = _items[fieldNum - 1].Fields;

if (fields.Count == 0)
{
throw new HL7Exception($"Invalid index: {index}, structure {fields.GetType().FullName} has no repetitions");
}
if (fields.Count <= index)
{
throw new HL7Exception($"Invalid index: {index}, structure {fields.GetType().FullName} must be between 0 and {fields.Count - 1}");
}

fields.RemoveAt(index);

return;
}

/// <summary>
/// Remove a valid item from a repeatable field.
/// </summary>
/// <param name="fieldNum">Repeatable field number</param>
/// <param name="removeItem">Item to be removed</param>
public void RemoveRepetition(int fieldNum, IType removeItem)
{
if (fieldNum < 1 || fieldNum > _items.Count)
{
throw new HL7Exception($"Can't retrieve field {fieldNum} from segment {GetType().FullName} - there are only {_items[fieldNum - 1].Fields.Count} fields.", HL7Exception.APPLICATION_INTERNAL_ERROR);
}

var fields = _items[fieldNum - 1].Fields;

if (fields.Count == 0)
{
throw new HL7Exception($"Structure {fields.GetType().FullName} has no repetitions");
}
if (!fields.Contains(removeItem))
{
throw new HL7Exception($"Invalid item specified, structure {fields.GetType().FullName} does not contain {removeItem.ToString()}");
}

fields.Remove(removeItem);

return;
}

/// <summary>
/// Get the 0-based index for an AbstractSegmentItem with a description that matches the given name.
/// </summary>
/// <param name="name">Item name, with all whitespace removed</param>
/// <returns>0-based index, if found. Otherwise, -1</returns>
public int FindField(string name)
{
return _items.FindIndex(x => Regex.Replace(x.Description, @"\s", "") == name);
}

/// <summary> Called from GetField(...) methods. If a field has been requested that /// <summary> Called from GetField(...) methods. If a field has been requested that
/// doesn't exist (eg GetField(15) when only 10 fields in segment) adds Varies /// doesn't exist (eg GetField(15) when only 10 fields in segment) adds Varies
/// fields to the end of the segment up to the required number. /// fields to the end of the segment up to the required number.
Expand Down