View Javadoc
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  	 * @return Returns null if not found
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  	 * @return the tableFiles
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 //	public void dumpImportedToWorkDirectory(File theDirectory) {
116 //		initIdToProfileGroups();
117 //		
118 //		IOUtils.deleteAllFromDirectory(theDirectory);
119 //
120 //		ourLog.info("Flushing work files to directory: " + theDirectory.getAbsolutePath());
121 //		
122 //		int index = 0;
123 //		for (ProfileGroup next : myIdToProfileGroups.values()) {
124 //			if (isBlank(next.getSourceUrl())) {
125 //				continue;
126 //			}
127 //			
128 //			index++;
129 //			String seq = StringUtils.leftPad(Integer.toString(index), 10, '0');
130 //
131 //			File nextFile = new File(theDirectory, next.getId() + "-" + seq + ".xml");
132 //
133 //			Writer nextWriter;
134 //			try {
135 //				nextWriter = new OutputStreamWriter(new FileOutputStream(nextFile), Charset.forName("UTF-8"));
136 //				nextWriter.append(next.exportConfigToXml());
137 //				nextWriter.close();
138 //			} catch (IOException e) {
139 //				ourLog.error("Failed to export profile group to file: " + nextFile.getAbsolutePath(), e);
140 //			}
141 //		
142 //		} // for
143 //
144 //	}
145 
146 }