1 package ca.uhn.hl7v2.testpanel.model.conf;
2
3 import static org.apache.commons.lang.StringUtils.*;
4
5 import java.io.File;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.OutputStreamWriter;
9 import java.io.Writer;
10 import java.nio.charset.Charset;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.slf4j.Logger;
17
18 import ca.uhn.hl7v2.conf.ProfileException;
19 import ca.uhn.hl7v2.hoh.util.IOUtils;
20 import ca.uhn.hl7v2.testpanel.controller.Prefs;
21 import ca.uhn.hl7v2.testpanel.model.AbstractModelClass;
22 import ca.uhn.hl7v2.testpanel.model.conf.ProfileGroup.Entry;
23
24 public class ProfileFileList extends AbstractModelClass {
25
26 private static final Logger ourLog = org.slf4j.LoggerFactory.getLogger(ProfileFileList.class);
27
28 public static final String PROP_FILES = ProfileFileList.class.getName() + "_FILES";
29
30 private HashMap<String, ProfileGroup> myIdToProfileGroups;
31
32 private List<ProfileGroup> myProfileGroups = new ArrayList<ProfileGroup>();
33
34 private TableFileList myTableFileList;
35
36 public ProfileFileList(TableFileList theTableFileList) {
37 myTableFileList = theTableFileList;
38
39 myProfileGroups = Prefs.getOpenProfiles();
40 for (ProfileGroup nextGroup : myProfileGroups) {
41 for (Entry nextEntry : nextGroup.getEntries()) {
42 nextEntry.setParentProfileGroup(nextGroup);
43 }
44 }
45 }
46
47 public void addNewFile(File theFile) throws IOException, ProfileException {
48 ProfileProxy file = ProfileProxy.loadFromFile(theFile);
49 myProfileGroups.add(new ProfileGroup(file));
50 myIdToProfileGroups = null;
51
52 firePropertyChange(PROP_FILES, null, null);
53
54 updatePrefs();
55 }
56
57 @Override
58 public Object exportConfigToXml() {
59 return null;
60 }
61
62
63
64
65 public ProfileGroup getProfile(String theProfileId) {
66 initIdToProfileGroups();
67 return myIdToProfileGroups.get(theProfileId);
68 }
69
70 private void initIdToProfileGroups() {
71 if (myIdToProfileGroups == null) {
72 myIdToProfileGroups = new HashMap<String, ProfileGroup>();
73 for (ProfileGroup next : myProfileGroups) {
74 myIdToProfileGroups.put(next.getId(), next);
75 }
76 }
77 }
78
79
80
81
82 public List<ProfileGroup> getProfiles() {
83 return myProfileGroups;
84 }
85
86 public void newProfileGroup(String theName) {
87 myProfileGroups.add(new ProfileGroup(theName));
88 myIdToProfileGroups = null;
89 firePropertyChange(PROP_FILES, null, null);
90 }
91
92 public void removeProfile(ProfileProxy theSelectedFileOrTable) {
93 myProfileGroups.remove(theSelectedFileOrTable);
94 myIdToProfileGroups = null;
95 firePropertyChange(PROP_FILES, null, null);
96 }
97
98 public void removeProfileGroup(ProfileGroup theSel) {
99 myProfileGroups.remove(myProfileGroups.indexOf(theSel));
100 myIdToProfileGroups = null;
101 firePropertyChange(PROP_FILES, null, null);
102 }
103
104 public void updatePrefs() {
105 ourLog.info("Flushing profile files to preferences");
106 Prefs.getInstance().setOpenProfiles(myProfileGroups, myTableFileList);
107 }
108
109 public void importFile(ProfileGroup theProfileGroup) {
110 myProfileGroups.add(theProfileGroup);
111 firePropertyChange(PROP_FILES, null, null);
112 updatePrefs();
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 }