1 package org.tigris.scarab.services; 2 3 48 49 import java.lang.reflect.Method ; 50 import java.sql.Connection ; 51 import java.sql.Statement ; 52 import java.sql.ResultSet ; 53 import java.util.Locale ; 54 import java.util.List ; 55 import java.util.MissingResourceException ; 56 import java.util.Iterator ; 57 58 import org.apache.fulcrum.BaseService; 59 import org.apache.fulcrum.InitializationException; 60 import org.apache.fulcrum.localization.Localization; 61 import org.apache.turbine.Turbine; 62 63 import org.apache.torque.Torque; 64 import org.apache.torque.util.Criteria; 65 66 import org.tigris.scarab.om.GlobalParameter; 67 import org.tigris.scarab.om.GlobalParameterManager; 68 import org.tigris.scarab.om.GlobalParameterPeer; 69 import org.tigris.scarab.util.Log; 70 import org.tigris.scarab.util.ScarabConstants; 71 72 79 public class DatabaseInitializer 80 extends BaseService 81 { 82 private static final String PRE_L10N = "pre-l10n"; 83 private static final String STARTED_L10N = "started"; 84 private static final String POST_L10N = "post-l10n"; 85 private static final String DB_L10N_STATE = "db-l10n-state"; 86 87 private static final String MODULE_DOMAIN = "module-domain"; 89 private static final String MODULE_PORT = "module-port"; 90 private static final String MODULE_SCHEME = "module-scheme"; 91 private static final String MODULE_SCRIPT_NAME = "module-script-name"; 92 93 94 97 private static final String [][] BEAN_METHODS = 98 { 99 {"InitDbScarabBundle", "MITList", "Name"}, 100 {"InitDbScarabBundle", "Attribute", "Name", "Description"}, 101 {"InitDbScarabBundle", "AttributeOption", "Name"}, 102 {"InitDbScarabBundle", "IssueType", "Name", "Description"}, 103 {"InitDbScarabBundle", "AttributeGroup", "Name", "Description"}, 104 {"InitDbScarabBundle", "RModuleAttribute", "DisplayValue"}, 105 {"InitDbScarabBundle", "Scope", "Name"} 106 }; 107 108 111 public void init() 112 throws InitializationException 113 { 114 try 115 { 116 String dbState = 117 GlobalParameterManager.getString(DB_L10N_STATE); 118 if (PRE_L10N.equals(dbState) || STARTED_L10N.equals(dbState)) 119 { 120 long start = System.currentTimeMillis(); 121 Log.get().info("New scarab database; localizing strings for '" + 122 ScarabConstants.DEFAULT_LOCALE.getDisplayName() + "'..."); 123 GlobalParameterManager.setString(DB_L10N_STATE, STARTED_L10N); 124 initdb(ScarabConstants.DEFAULT_LOCALE); 125 GlobalParameterManager.setString(DB_L10N_STATE, POST_L10N); 126 Log.get().info("Done localizing. Time elapsed = " + 127 (System.currentTimeMillis()-start)/1000.0 + " s"); 128 } 129 130 checkNewHttpParameters(); 131 } 132 catch (Exception e) 133 { 134 e.printStackTrace(); 135 throw new InitializationException( 136 "Failed to localize default data!", e); } 138 139 setInit(true); 141 } 142 143 protected String [][] getInputData() 144 { 145 return BEAN_METHODS; 146 } 147 148 private void initdb(Locale defaultLocale) 149 throws Exception 150 { 151 String [][] methodNames = getInputData(); 152 Class [] stringSig = {String .class}; 153 Class [] critSig = {Criteria.class}; 154 155 for (int m=0; m<methodNames.length; m++) 156 { 157 String [] row = methodNames[m]; 158 String omClassName = "org.tigris.scarab.om." + row[1]; 159 Class peerClass = Class.forName(omClassName + "Peer"); 160 Object peerObject = peerClass.newInstance(); 161 162 Method doSelect = peerClass.getMethod("doSelect", critSig); 163 Object [] critArg = {new Criteria()}; 164 List omlist = (List )doSelect.invoke(peerObject, critArg); 165 if (!omlist.isEmpty()) 166 { 167 Class omClass = Class.forName(omClassName); 168 int nbrBeanMethods = row.length - 2; 169 Method [] getters = new Method [nbrBeanMethods]; 170 Method [] setters = new Method [nbrBeanMethods]; 171 for (int n=2; n<row.length; n++) 172 { 173 getters[n-2] = omClass.getMethod("get"+row[n], null); 174 setters[n-2] = omClass.getMethod("set"+row[n], stringSig); 175 } 176 Method save = omClass.getMethod("save", null); 177 178 Iterator i = omlist.iterator(); 179 while (i.hasNext()) 180 { 181 Object om = i.next(); 182 for (int n=0; n<getters.length; n++) 183 { 184 Log.get().debug("Converting " + row[1] + '.' + 185 getters[n].getName()); 186 String key = (String )getters[n].invoke(om, null); 187 String value = null; 188 189 if (key != null) 191 { 192 try 193 { 194 value = Localization.getString(row[0], 195 defaultLocale, 196 key); 197 } 198 catch (MissingResourceException e) 199 { 200 Log.get().debug("Missing database initialization " 201 + "resource: " + e.getMessage()); 202 } 203 } 204 if (value != null) 205 { 206 Object [] arg = {value}; 207 setters[n].invoke(om, arg); 208 } 209 } 210 save.invoke(om, null); 211 } 212 } 213 } 214 } 215 216 private void checkNewHttpParameters() 217 throws Exception 218 { 219 String oldDomain = GlobalParameterManager 220 .getString(ScarabConstants.HTTP_DOMAIN); 221 String oldScheme = GlobalParameterManager 222 .getString(ScarabConstants.HTTP_SCHEME); 223 String oldScriptName = GlobalParameterManager 224 .getString(ScarabConstants.HTTP_SCRIPT_NAME); 225 String oldPort = GlobalParameterManager 226 .getString(ScarabConstants.HTTP_PORT); 227 228 if (oldDomain.equals("")) 229 { 230 Criteria crit = new Criteria(); 233 crit.add(GlobalParameterPeer.NAME, MODULE_DOMAIN); 234 List parameters = GlobalParameterPeer.doSelect(crit); 235 if (!parameters.isEmpty()) 236 { 237 oldDomain = ((GlobalParameter)parameters.get(0)).getValue(); 238 } 239 240 crit = new Criteria(); 241 crit.add(GlobalParameterPeer.NAME, MODULE_SCHEME); 242 parameters = GlobalParameterPeer.doSelect(crit); 243 if (!parameters.isEmpty()) 244 { 245 oldScheme = ((GlobalParameter)parameters.get(0)).getValue(); 246 } 247 248 crit = new Criteria(); 249 crit.add(GlobalParameterPeer.NAME, MODULE_SCRIPT_NAME); 250 parameters = GlobalParameterPeer.doSelect(crit); 251 if (!parameters.isEmpty()) 252 { 253 oldScriptName = 254 ((GlobalParameter)parameters.get(0)).getValue(); 255 } 256 257 crit = new Criteria(); 258 crit.add(GlobalParameterPeer.NAME, MODULE_PORT); 259 parameters = GlobalParameterPeer.doSelect(crit); 260 if (!parameters.isEmpty()) 261 { 262 oldPort = ((GlobalParameter)parameters.get(0)).getValue(); 263 } 264 } 265 266 String newValue = Turbine.getConfiguration() 267 .getString(ScarabConstants.HTTP_DOMAIN); 268 if (newValue != null && newValue.trim().length() != 0 269 && !newValue.equals(oldDomain)) 270 { 271 GlobalParameterManager 272 .setString(ScarabConstants.HTTP_DOMAIN, newValue); 273 fixIssueIdCounters(oldDomain, newValue); 274 } 275 276 newValue = Turbine.getConfiguration() 277 .getString(ScarabConstants.HTTP_SCHEME); 278 if (newValue != null && newValue.trim().length() != 0 279 && !newValue.equals(oldScheme)) 280 { 281 GlobalParameterManager 282 .setString(ScarabConstants.HTTP_SCHEME, newValue); 283 } 284 285 newValue = Turbine.getConfiguration() 286 .getString(ScarabConstants.HTTP_SCRIPT_NAME); 287 if (newValue != null && newValue.trim().length() != 0 288 && !newValue.equals(oldScriptName)) 289 { 290 GlobalParameterManager 291 .setString(ScarabConstants.HTTP_SCRIPT_NAME, newValue); 292 } 293 294 newValue = Turbine.getConfiguration() 295 .getString(ScarabConstants.HTTP_PORT); 296 if (newValue != null && newValue.trim().length() != 0 297 && !newValue.equals(oldPort)) 298 { 299 GlobalParameterManager 300 .setString(ScarabConstants.HTTP_PORT, newValue); 301 } 302 } 303 304 private void fixIssueIdCounters(String oldDomain, String newDomain) 305 throws Exception 306 { 307 String sql = 308 "select NEXT_ID from ID_TABLE where TABLE_NAME='ID_TABLE'"; 309 Connection con = null; 310 try 311 { 312 con = Torque.getConnection(); 313 Statement s = con.createStatement(); 314 ResultSet rs = s.executeQuery(sql); 315 int maxId = 0; 316 if (rs.next()) 317 { 318 maxId = rs.getInt(1); 319 } 320 s.close(); 321 322 for (int id = 1000; id <= maxId; id++) 323 { 324 sql = "select TABLE_NAME from ID_TABLE where ID_TABLE_ID=" 325 + id; 326 s = con.createStatement(); 327 rs = s.executeQuery(sql); 328 if (rs.next()) 329 { 330 String oldKey = rs.getString(1); 331 s.close(); 332 int hyphenPos = oldKey.indexOf('-'); 333 String newKey = null; 334 if ( (oldDomain == null || oldDomain.length() == 0) 335 && hyphenPos <= 0) 336 { 337 newKey = newDomain + '-' + oldKey; 338 } 339 else 340 { 341 String prefix = oldKey.substring(0, hyphenPos); 342 String code = oldKey.substring(hyphenPos+1); 343 if (prefix.equals(oldDomain)) 344 { 345 newKey = newDomain + '-' + code; 346 } 347 } 348 349 if (newKey != null) 350 { 351 sql = "update ID_TABLE set TABLE_NAME='" + newKey + 352 "' where ID_TABLE_ID=" + id; 353 s = con.createStatement(); 354 s.executeUpdate(sql); 355 s.close(); 356 } 357 } 358 } 359 } 360 finally 361 { 362 if (con != null) 363 { 364 con.close(); 365 } 366 } 367 } 368 } 369 | Popular Tags |