View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.check;
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FilenameFilter;
27  import java.io.IOException;
28  import java.net.URL;
29  
30  import org.apache.log4j.Logger;
31  import org.eclipse.core.resources.IFile;
32  import org.eclipse.core.resources.IFolder;
33  import org.eclipse.core.resources.IProject;
34  import org.eclipse.core.resources.IWorkspaceRoot;
35  import org.eclipse.core.resources.ResourcesPlugin;
36  import org.eclipse.core.runtime.*;
37  import org.fosstrak.llrp.commander.LLRPPlugin;
38  import org.fosstrak.llrp.commander.ResourceCenter;
39  
40  /**
41  * Performs a sanity check on the eclipse folder required by the LLRP Commander. 
42  * In case that the folder is corrupt, the method <code>fixIt</code> tries to 
43  * auto-repair the corrupt parts of the folder. 
44  * @author zhanghao
45  * @author sawielan
46  *
47  */
48  public class CheckEclipseProject extends CheckItem {
49  
50  	/** log4j logger. */
51  	private static Logger log = Logger.getLogger(CheckEclipseProject.class);
52  	
53  	public boolean validate() {
54  		
55  		this.clearAllReport();
56  		
57  		String projectName = ResourceCenter.getInstance().getEclipseProjectName();
58  		IProject project = ResourceCenter.getInstance().getEclipseProject();
59  		
60  		if (null == project) {
61  			addReportItem("Eclipse Project '" + projectName + "' doesn't exist.", CATEGORY_ERROR);
62  //			addReportItem("If you are using this tool for the first time, please click the 'Fix it!' button to " +
63  //					"initialize the project folder.", this.CATEGORY_INFO);
64  			return false;
65  		}
66  		
67  		try {
68  			// open if necessary
69  			if (project.exists() && !project.isOpen()) {
70  				project.open(null);
71  			}
72  
73  			IFolder msgFolder = project
74  					.getFolder(ResourceCenter.REPO_SUBFOLDER);
75  			IFolder draftFolder = project
76  					.getFolder(ResourceCenter.DRAFT_SUBFOLDER);
77  
78  			if (!msgFolder.exists()) {
79  				addReportItem("Subfolder '" + ResourceCenter.REPO_SUBFOLDER
80  						+ "' doesn't exist.", CATEGORY_ERROR);
81  			}
82  
83  			if (!draftFolder.exists()) {
84  				addReportItem("Subfolder '" + ResourceCenter.DRAFT_SUBFOLDER
85  						+ "' doesn't exist.", CATEGORY_ERROR);
86  			}
87  			
88  			if (!msgFolder.exists() || !draftFolder.exists()) {
89  				return false;
90  			}
91  
92  		} catch (Exception e) {
93  			return false;
94  		}
95  	
96  		return true;
97  	}
98  	
99  	public void fix() {
100 		
101 		this.clearAllReport();
102 		
103 		String projectName = ResourceCenter.getInstance().getEclipseProjectName();
104 		
105 		IProgressMonitor progressMonitor = new NullProgressMonitor();
106 
107 		try {
108 			IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
109 			IProject project = myWorkspaceRoot.getProject(projectName);
110 			if (!project.exists()) {
111 				project.create(null);
112 				project.open(null);
113 				
114 				log.debug("created project " + projectName);
115 				addReportItem("Project '" + projectName	+ "' created.", CATEGORY_FIX);
116 			}
117 			
118 			if (project.exists() && !project.isOpen()) {
119 				try {
120 					project.open(null);
121 				} catch (Exception e) {
122 					// recreate the project ...
123 					// first remove it from eclipse cache stuff...
124 					project.delete(true, null);
125 					project.create(null);
126 					project.open(null);
127 				}
128 				log.debug("opened project " + projectName);
129 			}
130 			
131 			//Try to add subfolder for repository messages
132 			IFolder msgFolder = project.getFolder(ResourceCenter.REPO_SUBFOLDER);
133 			if (!msgFolder.exists()) {
134 				msgFolder.create(true, false, progressMonitor);
135 				
136 				addReportItem("Subfolder '" + ResourceCenter.REPO_SUBFOLDER	+ "' created.", CATEGORY_FIX);
137 			}
138 			
139 			
140 			IFolder draftFolder = project.getFolder(ResourceCenter.DRAFT_SUBFOLDER);
141 			if (!draftFolder.exists()) {
142 				draftFolder.create(true, true, progressMonitor);
143 				
144 				addReportItem("Subfolder '" + ResourceCenter.DRAFT_SUBFOLDER + "' created.", CATEGORY_FIX);
145 				
146 				URL bundleRoot = LLRPPlugin.getDefault().getBundle().getEntry("/sampleXML");
147 				
148 				try {
149 					URL fileURL = FileLocator.toFileURL(bundleRoot);
150 					File folderSource = new File(fileURL.getPath());
151 					
152 					FilenameFilter filter = new FilenameFilter() {
153 				        public boolean accept(File dir, String name) {
154 				            return name.endsWith(".llrp");
155 				        }
156 				    };
157 				    
158 				    String[] sampleFileNames = folderSource.list(filter);
159 				    
160 				    for (int i = 0; i < sampleFileNames.length; i ++) {
161 				    	String urlFile = fileURL.getPath() + "/" + sampleFileNames[i];
162 				    	File sampleFile = new File(urlFile);
163 				    	
164 				    	IFile file = project.getFile(ResourceCenter.DRAFT_SUBFOLDER + "/" + sampleFileNames[i]);
165 				    	
166 				    	file.create(new FileInputStream(sampleFile), true, progressMonitor);
167 				    }
168 				    
169 				} catch (IOException ioe) {
170 					ioe.printStackTrace();
171 				} catch (Exception e) {
172 					e.printStackTrace();
173 				} 
174 				
175 				log.debug("fixed project");
176 			}
177 			
178 //			IFolder sampleFolder = project.getFolder(ResourceCenter.SAMPLE_SUBFOLDER);
179 //			if (!sampleFolder.exists()) {
180 //				sampleFolder.create(true, true, progressMonitor);
181 //				
182 //				addReportItem("Subfolder '" + ResourceCenter.SAMPLE_SUBFOLDER + "' created.", CATEGORY_FIX);
183 //				
184 //			    URL bundleRoot = LLRPPlugin.getDefault().getBundle().getEntry("/sampleXML");
185 //				
186 //				try {
187 //					URL fileURL = FileLocator.toFileURL(bundleRoot);
188 //					File folderSource = new File(fileURL.getPath());
189 //					
190 //					FilenameFilter filter = new FilenameFilter() {
191 //				        public boolean accept(File dir, String name) {
192 //				            return name.endsWith(".llrp");
193 //				        }
194 //				    };
195 //				    
196 //				    String[] sampleFileNames = folderSource.list(filter);
197 //				    
198 //				    for (int i = 0; i < sampleFileNames.length; i ++) {
199 //				    	String urlFile = fileURL.getPath() + "/" + sampleFileNames[i];
200 //				    	File sampleFile = new File(urlFile);
201 //				    	
202 //				    	//String urlTargetFile = sampleFolder.getLocationURI() + "/" + sampleFileNames[i];
203 //				    	//File sampleTargetFile = new File(urlTargetFile);
204 //				    	
205 //				    	IFile file = project.getFile(ResourceCenter.SAMPLE_SUBFOLDER + "/" + sampleFileNames[i]);
206 //				    	
207 //				    	file.create(new FileInputStream(sampleFile), true, progressMonitor);
208 //				    	
209 //				    	//copyFile(sampleFile, sampleTargetFile);
210 //				    }
211 //				    
212 //				} catch (IOException ioe) {
213 //					ioe.printStackTrace();
214 //				} catch (Exception e) {
215 //					e.printStackTrace();
216 //				} 
217 //			}
218 
219 		} catch (CoreException coe) {
220 			coe.printStackTrace();
221 		}
222 	}
223 	
224 }