View Javadoc

1   package ca.uhn.hl7v2.testpanel.model.conf;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.UUID;
9   
10  import org.apache.commons.lang.StringUtils;
11  import org.slf4j.Logger;
12  
13  import ca.uhn.hl7v2.testpanel.controller.Prefs;
14  import ca.uhn.hl7v2.testpanel.model.AbstractModelClass;
15  
16  public class TableFileList extends AbstractModelClass {
17  
18  	private static final Logger ourLog = org.slf4j.LoggerFactory.getLogger(TableFileList.class);
19  	public static final String PROP_FILES = TableFileList.class.getName() + "_FILES";
20  	
21  	private HashMap<String, TableFile> myIdToTableFiles;
22  	private List<TableFile> myTableFiles = new ArrayList<TableFile>();
23  
24  	public TableFileList() {
25  		List<File> files = Prefs.getInstance().getOpenTableFiles();
26  		for (File file : files) {
27  			ourLog.info("Loading table file {}", file.getAbsoluteFile());
28  			try {
29  				TableFile next = TableFile.readFromFile(file);
30  				if (!myTableFiles.contains(next)) {
31  					myTableFiles.add(next);
32  				}
33  			} catch (IOException e) {
34  				continue;
35  			}
36  		}
37  	}
38  	
39  	public void addNewFile(File theFileName) {
40  		TableFile file = new TableFile();
41  		file.setId(UUID.randomUUID().toString());
42  		file.setFileName(theFileName);
43  
44  		if (!myTableFiles.contains(file)) {
45  			myIdToTableFiles = null;
46  			myTableFiles.add(file);
47  		}
48  		
49  		try {
50  			file.flushToFile();
51  		} catch (IOException e) {
52  			ourLog.error("Failed to save file!", e);
53  			return;
54  		}
55  		
56  		firePropertyChange(PROP_FILES, null, null);
57  		
58  		updatePrefs();
59  	}
60  
61  	@Override
62  	public Object exportConfigToXml() {
63  		return null;
64  	} 
65  	
66  	public TableFile getTableFile(String theTableFileId) {
67  		if (myIdToTableFiles == null) {
68  			myIdToTableFiles = new HashMap<String, TableFile>();
69  			for (TableFile next : myTableFiles) {
70  				myIdToTableFiles.put(next.getId(), next);
71  			}
72  		}
73  		return myIdToTableFiles.get(theTableFileId);
74  	}
75  
76  	/**
77  	 * @return the tableFiles
78  	 */
79  	public List<TableFile> getTableFiles() {
80  		return myTableFiles;
81  	}
82  
83  	public void importFile(TableFile theFile) {
84  		getTableFiles();
85  		myTableFiles.add(theFile);
86  		firePropertyChange(PROP_FILES, null, null);
87  		updatePrefs();
88  	}
89  
90  	public boolean openFile(File theChosenFile) {
91  		try {
92  			TableFile newFile = TableFile.readFromFile(theChosenFile);
93  			if (!myTableFiles.contains(newFile)) {
94  				myIdToTableFiles = null;
95  				myTableFiles.add(newFile);
96  			}
97  			firePropertyChange(PROP_FILES, null, null);
98  			
99  			if (StringUtils.isNotBlank(newFile.getId())) {
100 				return true;
101 			}
102 		} catch (IOException e) {
103 			return false;
104 		}
105 		return false;
106 	}
107 
108 	public void removeTableFile(TableFile theSelectedFileOrTable) {
109 		myTableFiles.remove(theSelectedFileOrTable);
110 		myIdToTableFiles = null;
111 		firePropertyChange(PROP_FILES, null, null);
112 	}
113 
114 	private void updatePrefs() {
115 		ArrayList<File> files = new ArrayList<File>();
116 		for (TableFile next : myTableFiles) {
117 			if (next.getFileName() != null) {
118 				files.add(next.getFileName());
119 			}
120 		}
121 		Prefs.getInstance().setOpenTableFiles(files);
122 	}
123 	
124 }