001 package org.vmdb.hl7;
002
003 import java.util.*;
004
005 /**
006 * <p><Title:> Query By Parameters (QBP) Message. </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 is a quickly assembled message type used to demonstrate
011 * extensibility of the general library model. A more production ready
012 * solution would have been to develope a full, general purpose QBP/RSP
013 * pair of messages and extend them to the specific case of the QBP-Z01
014 * defined here, but the point was to show a quick and easy extension example.</p>
015 * @author Michael K. Martin
016 * @version 1.0
017 */
018
019 public class QBPMessage extends HL7Message {
020 private String sNm = "QBP_Z01";
021 private String sRl = "MSH QPD PID OBX RCP[DSC]";
022 private static final String MESSAGE_TYPE = "QBP";
023 private static final String EVENT_TYPE = "Z01";
024
025 /**
026 * Construct an empty QBP Message with type and event set. This object
027 * is ready to either build via a series of set and add calls, or to read in
028 * an HL7 or XML (future) message.
029 */
030 public QBPMessage() {
031 super();
032 setName( sNm );
033 setRule( sRl );
034 setMessageName( MESSAGE_TYPE, EVENT_TYPE );
035 initialize();
036 }
037
038 /**
039 * This is a brute-force intialization routine to create a populated
040 * message. This is faster and easier than implementing the full
041 * getXXXToWrite() logic used in "production" message classes.
042 */
043 private void initialize() {
044 if( vSegments == null )
045 vSegments = new Vector();
046 QPDSegment qpd = new QPDSegment();
047 qpd.initialize();
048 vSegments.add( qpd );
049 PIDSegment pid = new PIDSegment();
050 pid.initialize();
051 vSegments.add( pid );
052 OBXSegment obx = new OBXSegment();
053 obx.initialize();
054 vSegments.add( obx );
055 RCPSegment rcp = new RCPSegment();
056 rcp.initialize();
057 vSegments.add( rcp );
058 CEElement ceQName = new CEElement( "Z01", "Get Prevalence", "VMDB" );
059 QPDSegment sQPD = getQPD();
060 sQPD.setQueryName( ceQName );
061 CEElement ceModality = new CEElement( "R", "Real Time", "HL7" );
062 RCPSegment sRCP = getRCP();
063 sRCP.setModality( ceModality );
064 sRCP.setPriority( "I" );
065 }
066
067
068 /**
069 * Get the Input Parameter Field Description and Commentary
070 * segment from this message
071 * @return QPDSegment object
072 */
073 public QPDSegment getQPD() {
074 return (QPDSegment)findSegment( "QPD" );
075 }
076
077 /**
078 * Get the PID segment from this message.
079 * @return A reference to the PIDSegment object located at the appropriate place.
080 */
081 public PIDSegment getPID() {
082 return (PIDSegment)findSegment( "PID" );
083 }
084
085 /**
086 * Get the Observation segment (containing results) from this message
087 * @return OBXSegment object
088 */
089 public OBXSegment getOBX() {
090 return (OBXSegment)findSegment( "OBX" );
091 }
092
093 /**
094 * Get the Response Control Parameter segment from this message
095 * @return RCPSegment object
096 */
097 public RCPSegment getRCP() {
098 return (RCPSegment)findSegment( "RCP" );
099 }
100
101 // The accessors and mutators are based on VMDB usage of the QBP Message
102 // and are designed to provide easy access to message details
103
104 // MSH Field accessors inherited from HL7Message would logically
105 // appear here.
106
107 /**
108 * Set the Medical record number.<br><br>
109 * Medical record is defined in this logic as the identifier assigned by the
110 * sending facility. To work correctly, the sending facility identifier must
111 * be set <strong>before</strong> actions involving MRN.
112 * @param sMRN String with facility's MRN
113 */
114 public void setMedicalRecordNumber( String sMRN ) {
115 MSHSegment msh = getMSH();
116 String sThisFacility = msh.getSendingFacilityName();
117 PIDSegment pid = getPID();
118 boolean bFound = false;
119 // First try to update existing MRN
120 Iterator i = pid.listPatientIds();
121 while( i != null && i.hasNext() ) {
122 CXElement cxe = (CXElement)i.next();
123 if( cxe == null ) continue;
124 // Check assigner logic
125 String sAssigner = cxe.getAssigningAuthorityName();
126 if( sAssigner != null && sAssigner.equals( sThisFacility ) ) {
127 cxe.setIdentifier( sMRN );
128 bFound = true;
129 break;
130 }
131 }
132 // Otherwise add a new identifier
133 if( !bFound ) {
134 pid.addPatientId( sMRN, sThisFacility );
135 }
136 }
137
138 /**
139 * Get the Medical record number.<br><br>
140 * Medical record is defined in this logic as the identifier assigned by the
141 * sending facility. To work correctly, the sending facility identifier must
142 * be set <strong><i>before</i></strong> actions involving MRN.
143 * @return String with facility's MRN
144 */
145 public String getMedicalRecordNumber() {
146 MSHSegment msh = getMSH();
147 String sThisFacility = msh.getSendingFacilityName();
148 PIDSegment pid = getPID();
149 // First try to update existing MRN
150 Iterator i = pid.listPatientIds();
151 while( i.hasNext() ) {
152 CXElement cxe = (CXElement)i.next();
153 // Check assigner logic
154 if( cxe.getAssigningAuthorityName().equals( sThisFacility ) ) {
155 return cxe.getIdentifier();
156 }
157 }
158 return "";
159 }
160
161 /**
162 * Set the species as a predefined CE Element.<br><br>
163 * Often uses one of the set of predefined constants.<br>
164 * Note: If populated with a constant, subsequent calls to getSpecies() will return a
165 * reference to the constant (final static object) and cannot be modified
166 * through calls to the CEElement's methods. But why would you want to?
167 * @param ceSpecies CEElement populated with the coded species
168 */
169 public void setSpecies( CEElement ceSpecies ) {
170 PIDSegment pid = getPID();
171 pid.setSpecies( ceSpecies );
172 }
173
174 /**
175 * Set the species as the individual components of a CE Element
176 * @param sSpeciesCode String with the code for species
177 * @param sSpeciesText String with the species spelled out
178 * @param sCodeSystem String, usually "SCT," with the type of code
179 */
180 public void setSpecies( String sSpeciesCode,
181 String sSpeciesText,
182 String sCodeSystem ) {
183 PIDSegment pid = getPID();
184 pid.setSpecies( sSpeciesCode, sSpeciesText, sCodeSystem );
185 }
186
187 /**
188 * Get the species as a predefined CE Element.<br><br>
189 * Often uses one of the set of predefined constants.<br>
190 * Note: If setSpecies was called with a constant, subsequent calls to getSpecies() will return a
191 * reference to the constant (final static object) and cannot be modified
192 * through calls to the CEElement's methods. But why would you want to?
193 * @return A reference to the CEElement populated with species
194 */
195 public CEElement getSpecies() {
196 PIDSegment pid = getPID();
197 return pid.getSpecies();
198 }
199
200 /**
201 * Get the species text.
202 * @return String species spelled out
203 */
204 public String getSpeciesText() {
205 PIDSegment pid = getPID();
206 return pid.getSpeciesText();
207 }
208
209 /**
210 * Set the breed as a predefined CE Element.<br><br>
211 * Often uses one of the set of predefined constants.<br>
212 * Note: If populated with a constant, subsequent calls to getBreed() will return a
213 * reference to the constant (final static object) and cannot be modified
214 * through calls to the CEElement's methods. But why would you want to?
215 * @param ceBreed CEElement populated with breed
216 */
217 public void setBreed( CEElement ceBreed ) {
218 PIDSegment pid = getPID();
219 pid.setBreed( ceBreed );
220 }
221
222 /**
223 * Set the breed as the individual components of a CE Element.
224 * @param sBreedCode String snomed code for breed
225 * @param sBreedText String breed spelled out
226 * @param sCodeSystem String usually SCT with type of code
227 */
228 public void setBreed( String sBreedCode,
229 String sBreedText,
230 String sCodeSystem ) {
231 PIDSegment pid = getPID();
232 pid.setBreed( sBreedCode, sBreedText, sCodeSystem );
233 }
234
235 /**
236 * Get the breed as a predefined CE Element.<br><br>
237 * Often uses one of the set of predefined constants.<br>
238 * Note: If populated with a constant, subsequent calls to getBreed() will return a
239 * reference to the constant (final static object) and cannot be modified
240 * through calls to the CEElement's methods. But why would you want to?
241 * @return A reference to the CEElement populated with breed
242 */
243 public CEElement getBreed() {
244 PIDSegment pid = getPID();
245 return pid.getBreed();
246 }
247
248 /**
249 * Get the breed text.
250 * @return String breed spelled out
251 */
252 public String getBreedText() {
253 PIDSegment pid = getPID();
254 return pid.getBreedText();
255 }
256
257 /**
258 * Set the date of birth as ISO format data string YYYYMMDD
259 * @param sDOB String with date of birth
260 */
261 public void setDateOfBirth( String sBDate ) {
262 PIDSegment pid = getPID();
263 pid.setDateOfBirth( sBDate );
264 }
265
266 /**
267 * Set the date of birth as ISO format data string YYYYMMDD
268 * @param sDOB String with date of birth
269 */
270 public void setDateOfBirth( TSElement tsBDate ) {
271 PIDSegment pid = getPID();
272 pid.setDateOfBirth( tsBDate );
273 }
274
275 /**
276 * Get the date of birth.
277 * @return String with date of birth as YYYYMMDD
278 */
279 public String getDateOfBirth() {
280 PIDSegment pid = getPID();
281 return pid.getDateOfBirth();
282 }
283
284 /**
285 * Set the gender.
286 * @param sSex String with gender as M, C, T, F, S, O, U, H, or X
287 */
288 public void setSex( String sSex ) {
289 PIDSegment pid = getPID();
290 pid.setSex( sSex );
291 }
292
293 /**
294 * Get the gender.
295 * @return String with gender code
296 */
297 public String getSex() {
298 PIDSegment pid = getPID();
299 return pid.getSex();
300 }
301
302 /**
303 * Set the postal code of <strong>animal's</strong> residence.
304 * @param sPostalCode String with Zipcode or other postal code
305 */
306 public void setPatientZipcode( String sZipcode ) {
307 PIDSegment pid = getPID();
308 pid.setPatientZipcode( sZipcode );
309 }
310
311 /**
312 * Get the postal code.
313 * @return String with Zipcode or other postal code
314 */
315 public String getPatientZipcode() {
316 PIDSegment pid = getPID();
317 return pid.getPatientZipcode();
318 }
319
320 /**
321 * Set the Diagnosis for which we want the prevalence as a predefined CE Element.<br><br>
322 * Often uses one of the set of predefined constants
323 * @param ceDiagnosis CEElement populated with the coded Diagnosis
324 */
325 public void setDiagnosis( CEElement ceDiagnosis ) {
326 OBXSegment obx = getOBX();
327 obx.setObservationIdentifier( Loinc.DIAGNOSIS_NOM );
328 try {
329 obx.setObservationValueType( "CE" );
330 obx.setObservationValue( ceDiagnosis );
331 }
332 catch( MalformedFieldException mfe ) {
333 mfe.printStackTrace();
334 }
335 }
336
337 /**
338 * Set the Diagnosis as a predefined CE Element.<br><br>
339 * Often uses one of the set of predefined constants
340 * @param ceDiagnosis CEElement populated with the coded Diagnosis
341 */
342 public CEElement getDiagnosis() {
343 OBXSegment obx = getOBX();
344 CEElement ce = (CEElement)obx.getObservationValue();
345 return ce;
346 }
347
348 } // end class QBPMessage
349