View Javadoc

1   package ca.uhn.hl7v2.testpanel.util;
2   
3   import java.util.Comparator;
4   
5   import org.apache.commons.lang.StringUtils;
6   
7   import ca.uhn.hl7v2.HL7Exception;
8   import ca.uhn.hl7v2.model.Message;
9   import ca.uhn.hl7v2.model.primitive.CommonTS;
10  import ca.uhn.hl7v2.parser.PipeParser;
11  import ca.uhn.hl7v2.util.Terser;
12  
13  public class Hl7V2MessageStringComparatorByTimestamp implements Comparator<String> {
14  	private static final PipeParser ourParser = PipeParser.getInstanceWithNoValidation();
15  
16  	@Override
17  	public int compare(String theS1, String theS2) {
18  
19  		if (theS1 == null && theS2 == null) {
20  			return 0;
21  		}
22  		if (theS1 == null) {
23  			return 1;
24  		}
25  		if (theS2 == null) {
26  			return -1;
27  		}
28  
29  		Message o1;
30  		Message o2;
31  		try {
32  			o1 = ourParser.parse(theS1);
33  			o2 = ourParser.parse(theS2);
34  		} catch (HL7Exception e) {
35  			throw new Error(e);
36  		}
37  
38  		try {
39  			String c1 = new Terser(o1).get("/MSH-10");
40  			String c2 = new Terser(o2).get("/MSH-10");
41  
42  			c1 = StringUtils.defaultString(c1);
43  			c2 = StringUtils.defaultString(c2);
44  
45  			if (StringUtils.isBlank(c1) && StringUtils.isBlank(c2)) {
46  				return 0;
47  			} else if (StringUtils.isBlank(c1)) {
48  					return -1;
49  			} else if (StringUtils.isBlank(c2)) {
50  				return 1;
51  			}
52  
53  			CommonTS ts1 = new CommonTS(c1);
54  			CommonTS ts2 = new CommonTS(c2);
55  			
56  			return ts1.getValueAsDate().compareTo(ts2.getValueAsDate());
57  
58  		} catch (HL7Exception e) {
59  			throw new Error(e);
60  		}
61  
62  	}
63  
64  }