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 public int compare(String theS1, String theS2) {
17
18 if (theS1 == null && theS2 == null) {
19 return 0;
20 }
21 if (theS1 == null) {
22 return 1;
23 }
24 if (theS2 == null) {
25 return -1;
26 }
27
28 Message o1;
29 Message o2;
30 try {
31 o1 = ourParser.parse(theS1);
32 o2 = ourParser.parse(theS2);
33 } catch (HL7Exception e) {
34 throw new Error(e);
35 }
36
37 try {
38 String c1 = new Terser(o1).get("/MSH-10");
39 String c2 = new Terser(o2).get("/MSH-10");
40
41 c1 = StringUtils.defaultString(c1);
42 c2 = StringUtils.defaultString(c2);
43
44 if (StringUtils.isBlank(c1) && StringUtils.isBlank(c2)) {
45 return 0;
46 } else if (StringUtils.isBlank(c1)) {
47 return -1;
48 } else if (StringUtils.isBlank(c2)) {
49 return 1;
50 }
51
52 CommonTS ts1 = new CommonTS(c1);
53 CommonTS ts2 = new CommonTS(c2);
54
55 return ts1.getValueAsDate().compareTo(ts2.getValueAsDate());
56
57 } catch (HL7Exception e) {
58 throw new Error(e);
59 }
60
61 }
62
63 }