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 org.apache.log4j.Logger;
25  import org.eclipse.core.resources.IFolder;
26  import org.eclipse.core.resources.IProject;
27  import org.eclipse.core.resources.IResource;
28  import org.eclipse.core.runtime.CoreException;
29  import org.fosstrak.llrp.client.Repository;
30  import org.fosstrak.llrp.client.repository.sql.DerbyRepository;
31  import org.fosstrak.llrp.commander.ResourceCenter;
32  
33  /**
34  * Helper that checks the derby database folder. If the folder is missing or 
35  * is corrupt, the helper reports the issue.
36  * @author zhanghao
37  * @author sawielan
38  *
39  */
40  public class CheckRepository extends CheckItem {
41  	
42  	/** log4j logger. */
43  	private static Logger log = Logger.getLogger(CheckRepository.class);
44  	
45  	public boolean validate() {
46  		
47  		this.clearAllReport();
48  		
49  		IProject project = ResourceCenter.getInstance().getEclipseProject();
50  				
51  		try {
52  			IFolder dbFolder = project
53  					.getFolder(ResourceCenter.DB_SUBFOLDER);
54  
55  			if (!dbFolder.exists()) {
56  				addReportItem("Subfolder '" + ResourceCenter.DB_SUBFOLDER
57  						+ "' doesn't exist.", CATEGORY_ERROR);
58  
59  				return false;
60  			}
61  			
62  		} catch (Exception e) {
63  			return false;
64  		}
65  		Repository r = ResourceCenter.getInstance().getRepository();
66  		if (r instanceof DerbyRepository) {
67  			DerbyRepository repo = (DerbyRepository) r; 
68  			if (!repo.isHealth()) {
69  				addReportItem("JavaDB Repository doesn't exist or is corrupted.", CATEGORY_ERROR);
70  				return false;
71  			}
72  		}
73  		return true;
74  	}
75  	
76  	public void fix() {
77  		this.clearAllReport();
78  		
79  		IProject project = ResourceCenter.getInstance().getEclipseProject();
80  		// refresh the workspace...
81  		try {
82  			project.refreshLocal(IResource.DEPTH_INFINITE, null);
83  		} catch (CoreException e1) {
84  			e1.printStackTrace();
85  		}
86  		
87  		// check if the configuration folder exists.
88  		IFolder dbFolder = project.getFolder(
89  				ResourceCenter.DB_SUBFOLDER);
90  		if (!dbFolder.exists()) {
91  			try {
92  				log.info("create new db folder...");
93  				dbFolder.create(true, true, null);
94  				log.info("created db folder.");
95  			} catch (Exception e) {
96  				e.printStackTrace();
97  			}
98  		}
99  	}
100 }
101