View Javadoc
1 package com.bonevich.erj.app; 2 3 import com.bonevich.erj.db.*; 4 import com.bonevich.erj.db.DatabaseLogin; 5 import com.bonevich.erj.db.IDdlGenerator; 6 import com.bonevich.erj.db.SupportedDatabase; 7 import com.bonevich.erj.model.Schema; 8 import com.bonevich.erj.ui.ErjFrame; 9 import com.bonevich.erj.xml.DatabaseSupportDigester; 10 import com.bonevich.util.CollectionUtilities; 11 import com.bonevich.util.Functor; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 16 import java.util.*; 17 import java.util.prefs.BackingStoreException; 18 import java.util.prefs.Preferences; 19 import java.util.prefs.PreferencesFactory; 20 21 /*** 22 * ClassDescription 23 * 24 * @author jbonevic 25 * @version $Id: Application.html,v 1.1 2009/03/07 17:55:55 jbonevic Exp $ 26 */ 27 public class Application 28 { 29 private final static Log _logger = LogFactory.getLog(Application.class); 30 private final static String DB_SUPPORT_PATH = "com/bonevich/erj/xml/dbsupport.xml"; 31 32 private Project _currentProject; 33 private List _supportedDatabases = new LinkedList(); 34 private static SupportedDatabase _defaultDatabase = null; 35 //private Preferences _prefs; 36 37 /*** 38 * Constructor for Application. 39 */ 40 public Application() 41 { 42 super(); 43 } 44 45 /*** 46 * Track the current Project (i.e. might be a Project in the process 47 * of being loaded). 48 */ 49 public Project getCurrentProject() 50 { 51 return _currentProject; 52 } 53 54 /*** 55 * Sets the currentProject. 56 * @param currentProject The currentProject to set 57 */ 58 public void setCurrentProject(Project currentProject) 59 { 60 _currentProject = currentProject; 61 ErjFrame.getInstance().setupProjectResources(_currentProject); 62 } 63 64 public Project createDefaultProject() 65 { 66 Project project = new Project(this); 67 68 // init datatypes with defaults 69 70 Schema model = new Schema(); 71 model.setName(Schema.NEW_SCHEMA_STR); 72 model.setIdentifier(Schema.NEW_SCHEMA_STR); 73 project.setModel(model); 74 project.setTargetDatabase(DatabaseLogin.getDefaultInstance()); 75 project.setName(model.getName()); 76 project.createDiagram(); 77 project.markAsNew(); 78 79 setCurrentProject(project); 80 return project; 81 } 82 83 public Project loadProject(String filepath) 84 { 85 Project project = new Project(this); 86 project.setFilepath(filepath); 87 project.load(); 88 89 setCurrentProject(project); 90 return project; 91 } 92 93 /*** 94 * Returns the supportedDatabases. 95 * @return List 96 */ 97 public List getSupportedDatabases() 98 { 99 return _supportedDatabases; 100 } 101 102 /*** 103 * Returns the supportedDatabases. 104 * @return List 105 */ 106 public SupportedDatabase getSupportedDatabase(String dbName) 107 { 108 Iterator itr = _supportedDatabases.iterator(); 109 while (itr.hasNext()) 110 { 111 SupportedDatabase db = (SupportedDatabase) itr.next(); 112 if (db.getDisplayName().equals(dbName)) 113 { 114 return db; 115 } 116 } 117 return _defaultDatabase; 118 } 119 120 /*** 121 * Returns the defaultDatabase. 122 * @return SupportedDatabase 123 */ 124 public static SupportedDatabase getDefaultDatabase() 125 { 126 return _defaultDatabase; 127 } 128 129 /*** 130 * Sets the supportedDatabases. 131 * @param supportedDatabases The supportedDatabases to set 132 */ 133 public void addSupportedDatabase(SupportedDatabase supportedDatabase) 134 { 135 if (supportedDatabase.getName().equals(SupportedDatabase.DEFAULT_DB)) 136 { 137 _defaultDatabase = supportedDatabase; 138 } 139 _supportedDatabases.add(supportedDatabase); 140 } 141 142 /*** 143 * Returns the supportedDatabases. 144 * @return List 145 */ 146 public Vector getSupportedDatabaseNames() 147 { 148 return CollectionUtilities.map( 149 new Functor() 150 { 151 public Object function(Object arg) 152 { 153 SupportedDatabase db = (SupportedDatabase) arg; 154 return db.getDisplayName(); 155 } 156 }, 157 _supportedDatabases 158 ); 159 } 160 161 /*** 162 * Setup database support. 163 */ 164 public void initDatabaseSupport() 165 { 166 DatabaseSupportDigester digester = new DatabaseSupportDigester(this); 167 digester.digest(DB_SUPPORT_PATH); 168 } 169 170 /*** 171 * Load the application preferences 172 */ 173 public void initPreferences() 174 { 175 Iterator itr = getSupportedDatabases().iterator(); 176 while (itr.hasNext()) 177 { 178 SupportedDatabase db = (SupportedDatabase) itr.next(); 179 Preferences dbPrefs = Preferences.userNodeForPackage(db.getClass()); 180 try 181 { 182 if (dbPrefs.nodeExists(db.getName())) 183 { 184 Preferences prefs = dbPrefs.node(db.getName()); 185 String[] generatorNames = prefs.childrenNames(); 186 for (int j = 0; j < generatorNames.length; j++) 187 { 188 String generatorName = generatorNames[j]; 189 IDdlGenerator generator = db.addGenerator(generatorName); 190 try 191 { 192 if (prefs.nodeExists(generatorName)) 193 { 194 Preferences generatorPrefs = prefs.node(generatorName); 195 String[] optionIds = generatorPrefs.keys(); 196 for (int i = 0; i < optionIds.length; i++) 197 { 198 generator.setOption(optionIds[i], generatorPrefs.getBoolean(optionIds[i], false)); 199 } 200 } 201 } 202 catch(Exception e) 203 { 204 _logger.warn("No preferences found for generator: " + generatorName); 205 } 206 } 207 } 208 } 209 catch (Exception e) 210 { 211 _logger.warn("No preferences found for database support: " + db.getName()); 212 } 213 } 214 } 215 216 /*** 217 * Save the application preferences 218 */ 219 public void savePreferences() 220 { 221 Iterator itr = getSupportedDatabases().iterator(); 222 while (itr.hasNext()) 223 { 224 SupportedDatabase db = (SupportedDatabase) itr.next(); 225 Preferences pkgPrefs = Preferences.userNodeForPackage(db.getClass()); 226 Preferences dbPrefs = pkgPrefs.node(db.getName()); 227 try 228 { 229 dbPrefs.removeNode(); 230 } 231 catch (BackingStoreException e) 232 { 233 _logger.warn("Unable to remove preferences in preparation for save!"); 234 } 235 // recreate the node 236 dbPrefs = pkgPrefs.node(db.getName()); 237 238 Iterator genNames = db.getGeneratorNames(); 239 while (genNames.hasNext()) 240 { 241 String genName = (String) genNames.next(); 242 IDdlGenerator generator = db.getGenerator(genName); 243 Preferences genPrefs = dbPrefs.node(genName); 244 245 GenerationOptionMetaData md = generator.getOptionMetaData(); 246 Iterator options = md.getOptionIds(); 247 while (options.hasNext()) 248 { 249 String optionId = (String) options.next(); 250 genPrefs.putBoolean(optionId, generator.isOptionSet(optionId)); 251 } 252 } 253 } 254 } 255 256 }

This page was automatically generated by Maven