001 package org.vmdb.hl7; 002 003 import java.util.*; 004 005 /** 006 * <p><Title:> Repeat Element. </p> 007 * <p>Description: HL7 Network Connectivity For VMDB. </p> 008 * <p>Copyright: Copyright (c) 2002-2003. </p> 009 * <p>Company: Veterinary Medical Database (VMDB). </p> 010 * <p>This class is an invisible element who's sole purpose is to provide a container 011 * for zero or more elements in a repeat. It handles the details of outputting the 012 * correct delimiters in HL7 and XML tags in XML representation. 013 * @author Michael K. Martin 014 * @version 1.0 015 */ 016 017 public class RepeatElement extends HL7Element { 018 protected Vector vElements; 019 020 private String sNm = "XX"; 021 private String sRl = ""; 022 023 /** 024 * Construct a Composite ID Element using the default name and type constants.<br><br> 025 * The only level repeats occur is Field (is this correct?) so this always sets 026 * level to FIELD. The name and type are dependent on the contained field(s) which 027 * are by definition all the same type. 028 */ 029 public RepeatElement() { 030 super( FIELD ); 031 setName( sNm ); 032 setRule( sRl ); 033 setType( "XX" ); 034 } 035 036 /** 037 * Get an Iterator over all of the repeats. 038 * @return Iterator over HL7Element derived objects or null if no values have been assigned. 039 */ 040 public Iterator iterator() { 041 if( vElements == null ) return null; 042 else return vElements.iterator(); 043 } 044 045 /** 046 * Count the number of elements in the repeat. 047 * @return integer number of elements. 048 */ 049 public int size() { 050 if( vElements == null ) 051 return 0; 052 else 053 return vElements.size(); 054 } 055 056 057 /** 058 * Return this element to its empty state. In this case removing all 059 * repeating fields if any. 060 */ 061 public void clear() { 062 if( vElements != null ) 063 vElements.clear(); 064 } 065 066 /** 067 * Add an element to the end of the repeat.<br><br> 068 * Sets the name and type of the added element based on the name and type 069 * assigned to this repeat via its inherited setName() and setType() 070 * methods. 071 * @param hlE HL7Element derived Element object to add. 072 */ 073 public void addElement( HL7Element hlE ) { 074 if( vElements == null ) { 075 vElements = new Vector(); 076 } 077 hlE.setName( getName() ); 078 if( hlE instanceof SimpleElement ) 079 hlE.setType( getType() ); 080 vElements.add( hlE ); 081 } 082 083 /** 084 * Get the first element of the repeat.<br><br> 085 * This is a convenience method useful because we sometimes just want one value 086 * and don't care if we ignore later repeating values. 087 * @return HL7Element derived object. 088 */ 089 public HL7Element firstElement() { 090 if( vElements != null && vElements.elementAt(0) instanceof HL7Element ) 091 return (HL7Element)vElements.elementAt(0); 092 else 093 return null; 094 } 095 096 /** 097 * Get the value of the first element of the repeat.<br><br> 098 * This is a convenience method useful because we sometimes just want one value 099 * and don't care if we ignore later repeating values. 100 * @return HL7Element derived object. 101 */ 102 public String getValue() { 103 if( vElements != null && vElements.elementAt(0) instanceof HL7Element ) 104 return ((HL7Element)vElements.elementAt(0)).getValue(); 105 else 106 return ""; 107 } 108 109 /** 110 * Extend the inherited toString() method to properly delimit repeating fields. 111 * @return String formatted as delimited HL7 for this field. 112 */ 113 public String toString() { 114 int size = size(); 115 int iField = 0; 116 if( size == 0 ) return ""; 117 StringBuffer sb = new StringBuffer(); 118 Iterator i = iterator(); 119 while( i.hasNext() ) { 120 HL7Element eNext = (HL7Element)i.next(); 121 if( iField++ > 0 && !eNext.empty() ) { 122 sb.append( getSeparator( REP_SEP ) ); 123 } 124 sb.append( eNext.toString() ); 125 } 126 return sb.toString(); 127 } 128 129 /** 130 * Extend the inherited toHL7String() method to properly delimit repeating fields. 131 * @return String formatted as delimited HL7 for this field. 132 */ 133 public String toHL7String() { 134 int size = size(); 135 int iField = 0; 136 if( size == 0 ) return ""; 137 StringBuffer sb = new StringBuffer(); 138 Iterator i = iterator(); 139 while( i.hasNext() ) { 140 HL7Element eNext = (HL7Element)i.next(); 141 if( iField++ > 0 && !eNext.empty() ) { 142 sb.append( getSeparator( REP_SEP ) ); 143 } 144 sb.append( eNext.toHL7String() ); 145 } 146 return sb.toString(); 147 } 148 149 /** 150 * Extend the inherited toXML() method to properly format repeating fields. 151 * @param iDepth int to determine depth to indent XML for readability. 152 * @return String formatted XML 153 */ 154 public String toXML( int iDepth ) { 155 if( size() == 0 ) return ""; 156 Iterator i = iterator(); 157 StringBuffer sb = new StringBuffer(); 158 while( i.hasNext() ) { 159 HL7Element eNext = (HL7Element)i.next(); 160 sb.append( eNext.toXML( iDepth ) ); 161 } 162 return sb.toString(); 163 } 164 165 }// End class RepeatElement 166