View Javadoc

1   package gov.nasa.pds.ltdt.gui;
2   
3   import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
4   import gov.nasa.pds.ltdt.gui.util.DictionaryUtility;
5   import gov.nasa.pds.ltdt.gui.util.Utility;
6   import gov.nasa.pds.tools.dict.DictionaryWriter;
7   
8   import java.io.BufferedReader;
9   import java.io.BufferedWriter;
10  import java.io.File;
11  import java.io.FileReader;
12  import java.io.FileWriter;
13  import java.io.IOException;
14  import java.util.ArrayList;
15  import java.util.Enumeration;
16  import java.util.List;
17  import java.util.Properties;
18  
19  import javax.swing.JOptionPane;
20  
21  public class ProjectSave {
22  	
23  	
24  	/*** 
25  	 * Save a project
26  	 * @param props
27  	 * @param window
28  	 * @return Status: SUCCESS, ERROR if any IOException occurred
29  	 */
30  	public static int save (Properties props, MainWindow window) {
31  				
32  		String log_message = "";
33  		
34  		
35  
36  		if ((LTDTKeys.UNTITLED).equals(props.getProperty(LTDTKeys.PROJECTNAME))) {
37  			ProjectCreateDialog dlg = new ProjectCreateDialog(props, window, LTDTKeys.SAVEASTITLE, LTDTKeys.SAVEAS);
38  			return dlg.getCreateSaveAsStatus();
39  		}
40  		else {
41  
42  			if ("true".equals(props.getProperty(LTDTKeys.TEMPLATEEXISTS))) {
43  				
44  				String templateFileName = Utility.getProjectTemplateFileName(props);
45  				
46  				try { 		   
47  					saveTemplate(window, templateFileName); 
48  				} catch(IOException exc) {  			
49  					log_message = log_message + "Template file."+ exc.getMessage() + "\n";
50  				}
51  			}
52  			
53  			//	Update WDD file
54  			try {
55  			
56  				if ("true".equals(props.getProperty(LTDTKeys.WDDEXISTS))) {
57  					
58  					// write to wdd file bases on window.WDD dictionary
59  					String wddFileName = Utility.getProjectWDDFileName(props);
60  
61  					DictionaryUtility.writeWDD(window, wddFileName);
62  					
63  				}
64  			}
65  			catch (IOException ie) {
66  				log_message = log_message + "Working Data Dictionary. "+ ie.getMessage() + "\n";			
67  			}
68  			
69  		
70  			// Update configuration information.
71  			try {
72  							
73  				// read in the property file and write out entries with updated values
74  				// any entries that's not part of the property definitions are preserved 
75  				// 
76  				String propFileName = Utility.getProjectPropertyFileName(props);
77  				saveProjectPropertyFile(props, propFileName, false);
78  	
79  			}
80  			catch (IOException ie) {
81  				log_message = log_message + "Project property file. "+ie.getMessage() + "\n";			
82  			}
83  					
84  			if (log_message.length()>0) {
85  				JOptionPane.showMessageDialog(
86  						window, 
87  						"IOException while saving data to the following files: \n" + log_message + 
88  						"Please examine the files.");
89  				props.setProperty(LTDTKeys.PROJECTALTERED, "false");
90  				return LTDTKeys.ERROR;
91  			}
92  			
93  			//reportSaveStatus(props, window);
94  			
95  			props.setProperty(LTDTKeys.PROJECTALTERED, "false");
96  			
97  			return LTDTKeys.SUCCESS;
98  		}	
99  		
100 	}
101 	
102 	/***
103 	 * Save an existing project to a new location with a new name.
104 	 * @param props
105 	 * @param window
106 	 * @param newProjectName
107 	 * @param newProjectLocation
108 	 * @return SUCCESS, ERROR
109 	 */
110 	public static int saveAs (Properties props, MainWindow window, String newProjectName, String newProjectLocation) {
111 
112 		
113 		String log_message = "";
114 		String newProjectNameL = newProjectName.toLowerCase();
115 
116 		// Copy over template
117 		if ("true".equals(props.getProperty(LTDTKeys.TEMPLATEEXISTS))) {
118 			
119 			String templateFileName = Utility.getNewProjectTemplateFileName(newProjectLocation);
120 			
121 			try { 		   
122 				saveTemplate(window, templateFileName); 
123 			} catch(IOException exc) {  			
124 				log_message = log_message + "Template file\n";
125 			}
126 		}
127 
128 		
129 		try {
130 			
131 			// read in the properties from cache and write them out
132 			// to the project property file at the new project directory 
133 			
134 			String newFileName = Utility.getNewProjectPropertyFileName(newProjectLocation);
135 			saveProjectPropertyFile(props, newFileName, true, newProjectNameL, newProjectLocation);
136 
137 		}
138 		catch (IOException ie) {
139 			log_message = log_message + "Project property file. "+ie.getMessage()+ "\n";			
140 		}
141 		
142 		//	Update WDD file
143 		try {
144 	
145 
146 			if ("true".equals(props.getProperty(LTDTKeys.WDDEXISTS))) {
147 				
148 				
149 				// write window.WDD to a file
150 
151 				String wddFileName = newProjectLocation+LTDTKeys.PROJECTWDDSUFFIX;
152 
153 				DictionaryUtility.writeWDD(window, wddFileName);
154 			}
155 		}
156 		catch (IOException ie) {
157 			log_message = log_message + "Working Data Dictionary.  "+ie.getMessage()+ "\n";			
158 		}
159 		
160 		// register new project information to LTDT tool property (i.e. $HOME/.ltdt)
161 		try { 
162 			registerProject(newProjectNameL, newProjectLocation);
163 		}
164 		catch (IOException ex) {
165 			log_message = log_message + "LTDT tool property file. "+ex.getMessage() + "\n";	
166 		}
167 		
168 
169 		
170 		
171 		
172 		if (log_message.length()>0) {
173 			JOptionPane.showMessageDialog(
174 					window, 
175 					"IOException while saving data to the following files: \n" + log_message + 
176 					"Please examine the files.");
177 			props.setProperty(LTDTKeys.PROJECTALTERED, "false");
178 			return LTDTKeys.ERROR;
179 		}
180 		
181 		props.setProperty(LTDTKeys.PROJECTNAME, newProjectNameL);
182 		props.setProperty(LTDTKeys.PROJECTDIR, newProjectLocation);
183 				
184 		//reportSaveStatus(props, window);
185 				
186 		window.setTitle("LTDTool (Project Name: "+newProjectNameL+")");
187 		props.setProperty(LTDTKeys.PROJECTALTERED, "false");
188 		
189 		return LTDTKeys.SUCCESS;
190 	}// end SaveAs
191 	
192 	
193 	
194 	/***
195 	 * Write status to the log file and display a confirmation on the window
196 	 * @param props
197 	 * @param window
198 	 * @return
199 	 */
200 	private static int reportSaveStatus(Properties props, MainWindow window) {
201 		String log_message, debug_log_message = null;
202 		String logfile_name = null;
203 		
204 		String projectName=props.getProperty(LTDTKeys.PROJECTNAME);			
205 		String projDir = props.getProperty(LTDTKeys.PROJECTDIR);
206 				
207 		try {
208 			logfile_name=Utility.getProjectLogFileName(props);							
209 			log_message = "Project "+projectName+" saved.";
210 			
211 			if ("true".equals(props.getProperty("debug"))) {					
212 				debug_log_message = log_message + " with configuration setting \n";
213 					
214 				for (Enumeration e= props.propertyNames(); e.hasMoreElements();) {
215 					String key = e.nextElement().toString();
216 					debug_log_message = debug_log_message + key+"="+props.getProperty(key)+"\n";
217 				}
218 			
219 				Utility.writeLogEntry(logfile_name, debug_log_message);
220 			}
221 			else 
222 				Utility.writeLogEntry(logfile_name, log_message);
223 			
224 			//JOptionPane.showMessageDialog(
225 					//window, 
226 					//"Project "+props.getProperty(LTDTKeys.PROJECTNAME)+" saved successfully");					
227 		}
228 		catch (IOException e) {
229 			JOptionPane.showMessageDialog(window, 
230 					"IOExeption. Error writing to project log "+logfile_name, 
231 					"Error", JOptionPane.ERROR_MESSAGE);
232 			return LTDTKeys.ERROR;
233 		}
234 		
235 		return LTDTKeys.SUCCESS;
236 	}
237 
238 	private static void saveTemplate (MainWindow window, String templateFileName) throws IOException {
239 		Utility.writeTemplate(window, templateFileName);
240 	}
241 	
242 	
243 	
244 	/***
245 	 * 
246 	 * @param props
247 	 * @param fileName
248 	 * @param isNew Flag to signal if the updated project property file should go to a new location (true, for new, save as), or kept in the same location (false, for save)
249 	 * @throws IOException
250 	 */
251 	
252 	private static void saveProjectPropertyFile (
253 			Properties props, String fileName, boolean isNew) throws IOException {
254 		saveProjectPropertyFile (props, fileName, false, null, null);
255 	}
256 	
257 	/***
258 	 * 
259 	 * @param props
260 	 * @param fn
261 	 * @param isNew
262 	 * @param newProjectName Needed if isNew is true.
263 	 * @param newProjectLocation Needed if isNew is true
264 	 * @throws IOException
265 	 */
266 	private static void saveProjectPropertyFile (
267 			Properties props, String fn, boolean isNew, String newProjectName, String newProjectLocation) throws IOException {
268 		
269 		BufferedReader reader = null;
270 		BufferedWriter writer = null;
271 		List tempPropFile = new ArrayList(); // temporary storage of property contents
272 		List tempPropList = new ArrayList(); // temporary storage of property names for checking property update completeness
273 		
274 		try {
275 			File oldFile = new File(fn);
276 	
277 			if (!isNew) {
278 	
279 				String oldFileName = fn;
280 				
281 				reader = new BufferedReader ( new FileReader(oldFileName));
282 
283 				for (String line=reader.readLine(); line!=null; line=reader.readLine()) {
284 					
285 					line = line.trim();
286 					if (!line.startsWith("#")) {
287 						//if (line.length()>0 && line.contains("=")) {
288 						if (line.length()>0 && line.indexOf("=")!=-1) {
289 						String [] str = line.split("=");
290 
291 							String value = props.getProperty(str[0]);
292 							if ( value.length()>0) {
293 								
294 								tempPropFile.add(str[0]+"="+value+"\n");								
295 							}
296 							else {
297 								tempPropFile.add(line+"\n");
298 							}
299 							tempPropList.add(str[0]);
300 						
301 						}
302 						else tempPropFile.add(line+"\n");
303 					}
304 					else {
305 						tempPropFile.add(line+"\n");
306 					}
307 				}
308 
309 				// double check and make sure no property value was left out
310 				for (int x=0; x<LTDTKeys.PROPERTYARRAY.length; x++) {				
311 					// if this property has not be updated, put it into the property file
312 					if (!tempPropList.contains(LTDTKeys.PROPERTYARRAY[x])) { 
313 						tempPropFile.add(LTDTKeys.PROPERTYARRAY[x]+"="+props.getProperty(LTDTKeys.PROPERTYARRAY[x])+"\n");
314 					}
315 					
316 				}
317 			
318 				oldFile.delete();	
319 				writer = new BufferedWriter ( new FileWriter(oldFileName));
320 				
321 				for (int y=0; y<tempPropFile.size(); y++) {
322 					writer.write((String)tempPropFile.get(y));
323 				}
324 							
325 				reader.close();
326 				writer.close();	
327 				
328 			}
329 		
330 			else {
331 				writer = new BufferedWriter (new FileWriter(fn, false));
332 				
333 				for (int i=0; i<LTDTKeys.PROPERTYARRAY.length; i++) {
334 					String keyword = LTDTKeys.PROPERTYARRAY[i];
335 					if ((LTDTKeys.PROJECTNAME).equals(keyword)) {
336 						writer.write(keyword+"="+newProjectName.toLowerCase()+"\n");
337 					}
338 					else if ((LTDTKeys.PROJECTDIR).equals(keyword)) {
339 						writer.write(keyword+"="+newProjectLocation+"\n");
340 					}
341 					else {				
342 						writer.write(keyword+"="+(String)props.getProperty(keyword)+"\n");
343 					}
344 				}
345 				writer.close();
346 				
347 			}
348 		}
349 		finally {
350 			tempPropFile.clear();
351 			tempPropList.clear();
352 			if (reader!= null) 
353 				try { reader.close(); } catch (IOException e) {}; //do nothing
354 			if (writer!=null)
355 				try { writer.close(); } catch (IOException e) {} //do nothing
356 		
357 		}
358 	}
359 	
360 	
361 	
362 	private static void registerProject(String newProjectName, String newProjectLocation) throws IOException {
363 		
364 		BufferedWriter writer= null;
365 		try {
366 			writer = new BufferedWriter ( new FileWriter(LTDTKeys.LTDTPROPFILE, true));
367 			writer.write (newProjectName.toLowerCase()+"="+newProjectLocation+"\n");
368 		}
369 		finally {
370 			if (writer!=null) try {writer.close();} catch (IOException e) {} // do nothing
371 		}
372 	}
373 	
374 	
375 }