1 64 65 package com.jcorporate.expresso.services.test; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.dbobj.DBObject; 69 import com.jcorporate.expresso.core.dbobj.Schema; 70 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 71 import com.jcorporate.expresso.core.utility.DBTool; 72 import com.jcorporate.expresso.kernel.InstallLog; 73 import com.jcorporate.expresso.services.dbobj.SchemaList; 74 import org.apache.log4j.Logger; 75 76 import java.util.Enumeration ; 77 import java.util.Vector ; 78 79 80 87 public class SchemaCreator { 88 static Logger log = Logger.getLogger(SchemaCreator.class); 89 static String dataContext = null;; 90 91 94 public SchemaCreator() { 95 } 96 97 public String getDataContext() { 98 return dataContext; 99 } 100 101 public void setDataContext(String newValue) { 102 dataContext = newValue; 103 } 104 105 private static String getMyContext() { 106 if (dataContext == null) { 107 return TestSystemInitializer.getTestContext(); 108 } else { 109 return dataContext; 110 } 111 } 112 113 114 124 public static boolean schemaExists(Schema oneSchema) 125 throws DBException { 126 DBObject oneMember; 127 128 for (Enumeration e = oneSchema.getMembers(); e.hasMoreElements();) { 129 oneMember = (DBObject) e.nextElement(); 130 oneMember.setDataContext(getMyContext()); 131 if (log.isDebugEnabled()) { 132 log.debug("Checking table " + oneMember.getJDBCMetaData().getTargetTable()); 133 } 134 135 try { 136 oneMember.count(); 137 if (log.isDebugEnabled()) { 138 log.debug("Table " + oneMember.getJDBCMetaData().getTargetTable() + " exists"); 139 } 140 } catch (Throwable de) { 141 142 return false; 144 } 145 } 146 147 SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 148 sl.setDataContext(dataContext); 149 sl.setField(SchemaList.FLD_SCHEMA_CLASS, oneSchema.getClass().getName()); 150 if (!sl.find()) { 151 return false; 152 } 153 154 return true; 155 } 156 157 162 public static synchronized boolean ensureSchemaExists(Schema oneSchema, InstallLog installLog) 163 throws DBException { 164 Vector toCreate = new Vector (); 165 166 if (!schemaExists(oneSchema)) { 167 toCreate.add(oneSchema); 168 } else { 169 170 return true; 172 } 173 174 DBTool.createTables(installLog, toCreate, getMyContext()); 175 176 for (Enumeration e = toCreate.elements(); e.hasMoreElements();) { 177 Schema s = (Schema) e.nextElement(); 178 179 if (!s.getClass().getName().equals("com.jcorporate.expresso.core.ExpressoSchema")) { 180 SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 181 sl.setDataContext(getMyContext()); 182 sl.setField("SchemaClass", s.getClass().getName()); 183 184 if (!sl.find()) { 185 sl.setField("Descrip", s.getDefaultDescription()); 186 sl.setField("ComponentCode", 187 s.getDefaultComponentCode()); 188 sl.add(); 189 } else { 190 log.info("Schema Class " + s.getClass().getName() + 191 " already is listed as a registered schema"); 192 } 193 } 194 } 195 196 DBTool.setupSecurity(installLog, toCreate, getMyContext()); 197 DBTool.populateTables(installLog, toCreate, getMyContext()); 198 DBTool.setupConfig(installLog, toCreate, getMyContext()); 199 DBTool.otherSetups(installLog, toCreate, getMyContext()); 200 201 if (!oneSchema.getClass().getName().equals("com.jcorporate.expresso.core.ExpressoSchema")) { 202 SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 203 sl.setDataContext(getMyContext()); 204 sl.setField("SchemaClass", oneSchema.getClass().getName()); 205 206 if (!sl.find()) { 207 sl.setField("Descrip", oneSchema.getDefaultDescription()); 208 sl.setField("ComponentCode", 209 oneSchema.getDefaultComponentCode()); 210 sl.add(); 211 } 212 } 213 214 return false; 215 } 216 217 } | Popular Tags |