1 package ca.uhn.hl7v2.testpanel.model.conf;
2
3 import java.io.File;
4 import java.io.FileReader;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 import jakarta.xml.bind.JAXB;
15 import jakarta.xml.bind.annotation.XmlAccessType;
16 import jakarta.xml.bind.annotation.XmlAccessorType;
17 import jakarta.xml.bind.annotation.XmlElement;
18 import jakarta.xml.bind.annotation.XmlType;
19
20 import org.apache.commons.lang.ObjectUtils;
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.commons.lang.Validate;
23 import org.apache.commons.lang.builder.HashCodeBuilder;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import ca.uhn.hl7v2.conf.ProfileException;
28 import ca.uhn.hl7v2.conf.store.CodeStore;
29 import ca.uhn.hl7v2.testpanel.model.AbstractModelClass;
30 import ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree;
31
32 @XmlAccessorType(XmlAccessType.FIELD)
33 @XmlType(name = "TableFile")
34 public class TableFile extends AbstractModelClass implements CodeStore {
35
36 public static final String PROP_TABLES = TableFile.class.getName() + "_TABLES";
37 private static final Logger ourLog = LoggerFactory.getLogger(TableFile.class);
38
39 private transient File myFileName;
40
41 @XmlElement(name = "id")
42 private String myId;
43
44 @XmlElement(name = "name")
45 private String myName;
46
47 @XmlElement(name = "table")
48 private List<Table> myTables = new ArrayList<Table>();
49
50 private transient Map<String, Table> myCodeSystemIdToTable;
51
52 private transient boolean myUnsaved;
53
54 public Table addTable() {
55 Set<String> codes = getCodeSystemIdToTable().keySet();
56 int csNum = 0;
57 String tableCodeSystemId;
58 do {
59 csNum++;
60 tableCodeSystemId = "HL7" + Hl7V2MessageTree.toHl7Table(csNum);
61 } while (codes.contains(tableCodeSystemId));
62
63 ourLog.info("Adding table {}", tableCodeSystemId);
64
65 Table newTable = new Table();
66 newTable.setCodeSystemId(tableCodeSystemId);
67 newTable.setCodeSystemName("HL7 Table " + csNum);
68 myTables.add(newTable);
69
70 firePropertyChange(PROP_TABLES, null, null);
71 newTable.setTableFile(this);
72
73 updateCodeSystemIdToTable();
74
75 return newTable;
76 }
77
78 void updateCodeSystemIdToTable() {
79 myCodeSystemIdToTable = null;
80 }
81
82 private Map<String, Table> getCodeSystemIdToTable() {
83 if (myCodeSystemIdToTable == null) {
84 myCodeSystemIdToTable = new HashMap<String, Table>();
85 for (Table next : getTables()) {
86 myCodeSystemIdToTable.put(next.getCodeSystemId(), next);
87 }
88 }
89 return myCodeSystemIdToTable;
90 }
91
92
93
94
95 @Override
96 public boolean equals(Object theObj) {
97 if (!(theObj instanceof TableFile)) {
98 return false;
99 }
100
101 return ObjectUtils.equals(myId, ((TableFile) theObj).myId);
102 }
103
104 @Override
105 public Object exportConfigToXml() {
106 return null;
107 }
108
109 public void flushToFile() throws IOException {
110 Validate.notNull(myFileName);
111
112 File tableFile = (myFileName);
113 FileWriter w = new FileWriter(tableFile, false);
114
115 JAXB.marshal(this, w);
116 w.close();
117 }
118
119
120
121
122 public File getFileName() {
123 return myFileName;
124 }
125
126
127
128
129 public String getId() {
130 return myId;
131 }
132
133
134
135
136 public String getName() {
137 return StringUtils.defaultString(myName);
138 }
139
140
141
142
143 public List<Table> getTables() {
144 if (myTables == null) {
145 myTables = new ArrayList<Table>();
146 }
147 return myTables;
148 }
149
150
151
152
153 @Override
154 public int hashCode() {
155 HashCodeBuilder b = new HashCodeBuilder();
156 b.append(myId);
157 return b.toHashCode();
158 }
159
160
161
162
163 public boolean isUnsaved() {
164 return myUnsaved;
165 }
166
167
168
169
170
171 public void setFileName(File theFileName) {
172 myFileName = theFileName;
173 }
174
175
176
177
178
179 public void setId(String theId) {
180 myId = theId;
181 }
182
183
184
185
186
187 public void setName(String theName) {
188 myName = theName;
189 }
190
191
192
193
194
195 public void setTables(List<Table> theTables) {
196 myTables = theTables;
197 }
198
199
200
201
202
203 public void setUnsaved(boolean theUnsaved) {
204 myUnsaved = theUnsaved;
205 }
206
207
208
209
210 @Override
211 public String toString() {
212 return "TableFile[id=" + myId + "]";
213 }
214
215 public static TableFile readFromFile(File theFile) throws IOException {
216 TableFile retVal = JAXB.unmarshal(new FileReader(theFile), TableFile.class);
217 retVal.setFileName(theFile);
218 Collections.sort(retVal.myTables);
219
220 for (Table next : retVal.getTables()) {
221 next.setTableFile(retVal);
222 }
223
224 return retVal;
225 }
226
227 public String[] getValidCodes(String theCodeSystem) throws ProfileException {
228 return getCodeSystemIdToTable().get(theCodeSystem).getCodesAsArray();
229 }
230
231 public boolean knowsCodes(String theCodeSystem) {
232 return getCodeSystemIdToTable().containsKey(theCodeSystem);
233 }
234
235 public boolean isValidCode(String theCodeSystem, String theCode) {
236 return getCodeSystemIdToTable().get(theCodeSystem).isValidCode(theCode);
237 }
238
239 public void removeTable(Table theTable) {
240 myTables.remove(theTable);
241 firePropertyChange(PROP_TABLES, null, null);
242 }
243
244 }