Initial commit: update
This commit is contained in:
@@ -21,135 +21,214 @@ import javax.servlet.ServletContextListener;
|
||||
<span class="nc" id="L21"> private static final Logger LOG = Logger.getLogger(HmiSchemaBootstrap.class.getName());</span>
|
||||
<span class="nc" id="L22"> private static volatile boolean ready = false;</span>
|
||||
<span class="nc" id="L23"> private static volatile String state = "Not initialized";</span>
|
||||
<span class="nc" id="L24"> static boolean isReady() { return ready; }</span>
|
||||
<span class="nc" id="L25"> static String state() { return state; }</span>
|
||||
// The public health endpoint exposes only fixed diagnostic codes, never SQL errors or connection credentials.
|
||||
<span class="nc" id="L25"> private static volatile String phase = "NOT_STARTED";</span>
|
||||
<span class="nc" id="L26"> private static volatile String code = "NONE";</span>
|
||||
<span class="nc" id="L27"> private static volatile boolean poolConnected = false;</span>
|
||||
<span class="nc" id="L28"> private static volatile String databaseKind = "UNKNOWN";</span>
|
||||
<span class="nc" id="L29"> static boolean isReady() { return ready; }</span>
|
||||
<span class="nc" id="L30"> static String state() { return state; }</span>
|
||||
<span class="nc" id="L31"> static String phase() { return phase; }</span>
|
||||
<span class="nc" id="L32"> static String failureCode() { return code; }</span>
|
||||
<span class="nc" id="L33"> static boolean poolConnected() { return poolConnected; }</span>
|
||||
<span class="nc" id="L34"> static String databaseKind() { return databaseKind; }</span>
|
||||
|
||||
|
||||
@Override public void contextInitialized(ServletContextEvent event) {
|
||||
<span class="nc" id="L28"> try (Connection c = HmiDb.open()) {</span>
|
||||
<span class="nc" id="L29"> String product = c.getMetaData().getDatabaseProductName().toLowerCase(Locale.ROOT);</span>
|
||||
<span class="nc" id="L30"> boolean oracle = product.contains("oracle");</span>
|
||||
<span class="nc bnc" id="L31" title="All 4 branches missed."> boolean sqlServer = product.contains("microsoft sql server") || product.contains("sql server");</span>
|
||||
<span class="nc bnc" id="L32" title="All 4 branches missed."> if (!oracle && !sqlServer) throw new SQLException("Unsupported dsCustNJTA database: " + product);</span>
|
||||
<span class="nc bnc" id="L33" title="All 2 branches missed."> if (sqlServer) createSqlServerSchema(c);</span>
|
||||
<span class="nc" id="L34"> else assertOracleSchemaExists(c);</span>
|
||||
<span class="nc" id="L38"> ready = false;</span>
|
||||
<span class="nc" id="L39"> poolConnected = false;</span>
|
||||
<span class="nc" id="L40"> databaseKind = "UNKNOWN";</span>
|
||||
<span class="nc" id="L41"> code = "NONE";</span>
|
||||
<span class="nc" id="L42"> phase = "DATASOURCE_LOOKUP";</span>
|
||||
<span class="nc" id="L43"> try (Connection c = HmiDb.open()) {</span>
|
||||
<span class="nc" id="L44"> poolConnected = true;</span>
|
||||
<span class="nc" id="L45"> phase = "DATABASE_DETECTION";</span>
|
||||
<span class="nc" id="L46"> String product = c.getMetaData().getDatabaseProductName().toLowerCase(Locale.ROOT);</span>
|
||||
<span class="nc" id="L47"> boolean oracle = product.contains("oracle");</span>
|
||||
<span class="nc bnc" id="L48" title="All 4 branches missed."> boolean sqlServer = product.contains("microsoft sql server") || product.contains("sql server");</span>
|
||||
<span class="nc bnc" id="L49" title="All 4 branches missed."> if (!oracle && !sqlServer) {</span>
|
||||
<span class="nc" id="L50"> code = "UNSUPPORTED_DATABASE";</span>
|
||||
<span class="nc" id="L51"> throw new SQLException("Unsupported dsCustNJTA database: " + product);</span>
|
||||
}
|
||||
<span class="nc bnc" id="L53" title="All 2 branches missed."> databaseKind = oracle ? "ORACLE" : "SQL_SERVER";</span>
|
||||
<span class="nc" id="L54"> phase = "SCHEMA_CHECK";</span>
|
||||
<span class="nc bnc" id="L55" title="All 2 branches missed."> if (sqlServer) createSqlServerSchema(c);</span>
|
||||
<span class="nc" id="L56"> else assertOracleSchemaExists(c);</span>
|
||||
|
||||
<span class="nc bnc" id="L36" title="All 2 branches missed."> String prefix = oracle ? "CUST." : "cust.";</span>
|
||||
<span class="nc bnc" id="L37" title="All 2 branches missed."> String text = oracle ? "CLOB" : "NVARCHAR(MAX)";</span>
|
||||
<span class="nc bnc" id="L38" title="All 2 branches missed."> String shortText = oracle ? "VARCHAR2" : "NVARCHAR";</span>
|
||||
<span class="nc bnc" id="L39" title="All 2 branches missed."> String numeric = oracle ? "NUMBER(10)" : "INT";</span>
|
||||
<span class="nc bnc" id="L40" title="All 2 branches missed."> String date = oracle ? "TIMESTAMP" : "DATETIME2";</span>
|
||||
<span class="nc bnc" id="L41" title="All 2 branches missed."> String now = oracle ? "SYSTIMESTAMP" : "SYSUTCDATETIME()";</span>
|
||||
<span class="nc" id="L42"> createIfMissing(c, "HMI_APP_PAGE", "CREATE TABLE " + prefix + "HMI_APP_PAGE ("</span>
|
||||
<span class="nc" id="L58"> phase = "SCHEMA_INITIALIZATION";</span>
|
||||
<span class="nc bnc" id="L59" title="All 2 branches missed."> String prefix = oracle ? "CUST." : "cust.";</span>
|
||||
<span class="nc bnc" id="L60" title="All 2 branches missed."> String text = oracle ? "CLOB" : "NVARCHAR(MAX)";</span>
|
||||
<span class="nc bnc" id="L61" title="All 2 branches missed."> String shortText = oracle ? "VARCHAR2" : "NVARCHAR";</span>
|
||||
<span class="nc bnc" id="L62" title="All 2 branches missed."> String numeric = oracle ? "NUMBER(10)" : "INT";</span>
|
||||
<span class="nc bnc" id="L63" title="All 2 branches missed."> String date = oracle ? "TIMESTAMP" : "DATETIME2";</span>
|
||||
<span class="nc bnc" id="L64" title="All 2 branches missed."> String now = oracle ? "SYSTIMESTAMP" : "SYSUTCDATETIME()";</span>
|
||||
<span class="nc" id="L65"> createIfMissing(c, "HMI_APP_PAGE", "CREATE TABLE " + prefix + "HMI_APP_PAGE ("</span>
|
||||
+ "ID " + shortText + "(64) NOT NULL PRIMARY KEY, PAGE_KEY " + shortText + "(120) NOT NULL UNIQUE, "
|
||||
+ "TITLE " + shortText + "(255) NOT NULL, MENU_GROUP " + shortText + "(120), "
|
||||
+ "PARENT_PAGE_ID " + shortText + "(64), "
|
||||
+ "SORT_ORDER " + numeric + " DEFAULT 0 NOT NULL, ENABLED " + numeric + " DEFAULT 1 NOT NULL)");
|
||||
<span class="nc" id="L46"> createIfMissing(c, "HMI_APP_TEMPLATE", "CREATE TABLE " + prefix + "HMI_APP_TEMPLATE ("</span>
|
||||
// V5.2: compatible migration for existing Oracle and SQL Server installations.
|
||||
// Never drops or recreates HMI_APP_PAGE, preserving all published templates/layouts.
|
||||
<span class="nc" id="L72"> addParentPageColumnIfMissing(c, oracle);</span>
|
||||
<span class="nc" id="L73"> createIfMissing(c, "HMI_APP_TEMPLATE", "CREATE TABLE " + prefix + "HMI_APP_TEMPLATE ("</span>
|
||||
+ "ID " + shortText + "(64) NOT NULL PRIMARY KEY, PAGE_ID " + shortText + "(64) NOT NULL, "
|
||||
+ "VERSION_NO " + numeric + " NOT NULL, STATUS " + shortText + "(20) NOT NULL, "
|
||||
+ "LAYOUT_JSON " + text + " NOT NULL, UPDATED_BY " + shortText + "(255), "
|
||||
+ "UPDATED_AT " + date + " DEFAULT " + now + " NOT NULL)");
|
||||
<span class="nc" id="L51"> createIfMissing(c, "HMI_APP_ASSIGNMENT", "CREATE TABLE " + prefix + "HMI_APP_ASSIGNMENT ("</span>
|
||||
<span class="nc" id="L78"> createIfMissing(c, "HMI_APP_ASSIGNMENT", "CREATE TABLE " + prefix + "HMI_APP_ASSIGNMENT ("</span>
|
||||
+ "ID " + shortText + "(64) NOT NULL PRIMARY KEY, PAGE_ID " + shortText + "(64) NOT NULL, "
|
||||
+ "SCOPE_TYPE " + shortText + "(20) NOT NULL, SCOPE_ID " + shortText + "(255) NOT NULL, "
|
||||
+ "TEMPLATE_ID " + shortText + "(64) NOT NULL, PRIORITY " + numeric + " DEFAULT 0 NOT NULL)");
|
||||
<span class="nc" id="L55"> createIfMissing(c, "HMI_APP_LAYOUT", "CREATE TABLE " + prefix + "HMI_APP_LAYOUT ("</span>
|
||||
<span class="nc" id="L82"> createIfMissing(c, "HMI_APP_LAYOUT", "CREATE TABLE " + prefix + "HMI_APP_LAYOUT ("</span>
|
||||
+ "USER_ID " + shortText + "(255) NOT NULL, PAGE_ID " + shortText + "(64) NOT NULL, "
|
||||
+ "STATION_ID " + shortText + "(120) DEFAULT '*' NOT NULL, BASE_TEMPLATE_ID " + shortText + "(64), OVERRIDES_JSON " + text + " NOT NULL, "
|
||||
+ "UPDATED_AT " + date + " DEFAULT " + now + " NOT NULL, "
|
||||
+ "PRIMARY KEY (USER_ID, PAGE_ID, STATION_ID))");
|
||||
<span class="nc" id="L60"> createIfMissing(c, "HMI_APP_PREF", "CREATE TABLE " + prefix + "HMI_APP_PREF ("</span>
|
||||
<span class="nc" id="L87"> createIfMissing(c, "HMI_APP_PREF", "CREATE TABLE " + prefix + "HMI_APP_PREF ("</span>
|
||||
+ "SCOPE_TYPE " + shortText + "(20) NOT NULL, SCOPE_ID " + shortText + "(255) NOT NULL, "
|
||||
+ "PREFS_JSON " + text + " NOT NULL, UPDATED_AT " + date + " DEFAULT " + now + " NOT NULL, "
|
||||
+ "PRIMARY KEY (SCOPE_TYPE, SCOPE_ID))");
|
||||
<span class="nc" id="L64"> createIfMissing(c, "HMI_APP_GROUP_MEMBER", "CREATE TABLE " + prefix + "HMI_APP_GROUP_MEMBER ("</span>
|
||||
<span class="nc" id="L91"> createIfMissing(c, "HMI_APP_GROUP_MEMBER", "CREATE TABLE " + prefix + "HMI_APP_GROUP_MEMBER ("</span>
|
||||
+ "USER_ID " + shortText + "(255) NOT NULL, GROUP_ID " + shortText + "(120) NOT NULL, "
|
||||
+ "PRIMARY KEY (USER_ID, GROUP_ID))");
|
||||
<span class="nc" id="L67"> createIfMissing(c, "HMI_APP_AUDIT", "CREATE TABLE " + prefix + "HMI_APP_AUDIT ("</span>
|
||||
<span class="nc" id="L94"> createIfMissing(c, "HMI_APP_AUDIT", "CREATE TABLE " + prefix + "HMI_APP_AUDIT ("</span>
|
||||
+ "ID " + shortText + "(64) NOT NULL PRIMARY KEY, ACTOR " + shortText + "(255) NOT NULL, "
|
||||
+ "ACTION_NAME " + shortText + "(120) NOT NULL, ENTITY_ID " + shortText + "(255), "
|
||||
+ "DETAILS " + shortText + "(1000), CREATED_AT " + date + " DEFAULT " + now + " NOT NULL)");
|
||||
// Versioned ios.css content, separate from the immutable WAR fallback.
|
||||
<span class="nc" id="L72"> createIfMissing(c, "HMI_APP_CSS_REVISION", "CREATE TABLE " + prefix + "HMI_APP_CSS_REVISION ("</span>
|
||||
<span class="nc" id="L99"> createIfMissing(c, "HMI_APP_CSS_REVISION", "CREATE TABLE " + prefix + "HMI_APP_CSS_REVISION ("</span>
|
||||
+ "ID " + shortText + "(64) NOT NULL PRIMARY KEY, VERSION_NO " + numeric + " NOT NULL UNIQUE, "
|
||||
+ "STATUS " + shortText + "(20) NOT NULL, CSS_CONTENT " + text + " NOT NULL, "
|
||||
+ "UPDATED_BY " + shortText + "(255), UPDATED_AT " + date + " DEFAULT " + now + " NOT NULL)");
|
||||
<span class="nc" id="L76"> createIfMissing(c, "HMI_APP_SCHEMA_VERSION", "CREATE TABLE " + prefix + "HMI_APP_SCHEMA_VERSION ("</span>
|
||||
// V5: Person-oriented roles, groups and uploaded HMI image assets.
|
||||
<span class="nc bnc" id="L104" title="All 2 branches missed."> String binary = oracle ? "BLOB" : "VARBINARY(MAX)";</span>
|
||||
<span class="nc" id="L105"> createIfMissing(c, "HMI_APP_ROLE", "CREATE TABLE " + prefix + "HMI_APP_ROLE ("</span>
|
||||
+ "ROLE_KEY " + shortText + "(120) NOT NULL PRIMARY KEY, DESCRIPTION " + shortText + "(255))");
|
||||
<span class="nc" id="L107"> createIfMissing(c, "HMI_APP_GROUP_DEF", "CREATE TABLE " + prefix + "HMI_APP_GROUP_DEF ("</span>
|
||||
+ "GROUP_KEY " + shortText + "(120) NOT NULL PRIMARY KEY, DESCRIPTION " + shortText + "(255))");
|
||||
<span class="nc" id="L109"> createIfMissing(c, "HMI_APP_GROUP_ROLE", "CREATE TABLE " + prefix + "HMI_APP_GROUP_ROLE ("</span>
|
||||
+ "GROUP_KEY " + shortText + "(120) NOT NULL, ROLE_KEY " + shortText + "(120) NOT NULL, PRIMARY KEY (GROUP_KEY,ROLE_KEY))");
|
||||
<span class="nc" id="L111"> createIfMissing(c, "HMI_APP_PERSON_ROLE", "CREATE TABLE " + prefix + "HMI_APP_PERSON_ROLE ("</span>
|
||||
+ "PERS_ID " + shortText + "(120) NOT NULL, ROLE_KEY " + shortText + "(120) NOT NULL, PRIMARY KEY (PERS_ID,ROLE_KEY))");
|
||||
<span class="nc" id="L113"> createIfMissing(c, "HMI_APP_PERSON_GROUP", "CREATE TABLE " + prefix + "HMI_APP_PERSON_GROUP ("</span>
|
||||
+ "PERS_ID " + shortText + "(120) NOT NULL, GROUP_KEY " + shortText + "(120) NOT NULL, PRIMARY KEY (PERS_ID,GROUP_KEY))");
|
||||
<span class="nc" id="L115"> createIfMissing(c, "HMI_APP_LOGIN_PERSON", "CREATE TABLE " + prefix + "HMI_APP_LOGIN_PERSON ("</span>
|
||||
+ "LOGIN_ID " + shortText + "(255) NOT NULL PRIMARY KEY, PERS_ID " + shortText + "(120) NOT NULL)");
|
||||
<span class="nc" id="L117"> createIfMissing(c, "HMI_APP_ASSET", "CREATE TABLE " + prefix + "HMI_APP_ASSET ("</span>
|
||||
+ "ID " + shortText + "(64) NOT NULL PRIMARY KEY, ORIGINAL_NAME " + shortText + "(255) NOT NULL, "
|
||||
+ "MIME_TYPE " + shortText + "(80) NOT NULL, CONTENT " + binary + " NOT NULL, "
|
||||
+ "CONTENT_SIZE " + numeric + " NOT NULL, UPDATED_BY " + shortText + "(255) NOT NULL)");
|
||||
<span class="nc" id="L121"> createIfMissing(c, "HMI_APP_SCHEMA_VERSION", "CREATE TABLE " + prefix + "HMI_APP_SCHEMA_VERSION ("</span>
|
||||
+ "VERSION_NO " + numeric + " NOT NULL PRIMARY KEY, INSTALLED_AT " + date + " DEFAULT " + now + " NOT NULL)");
|
||||
// Fail closed when a v1 schema exists: do not alter existing PKs on a production database.
|
||||
// See hmi-docs/migrate-v1-to-v2-*.sql for non-destructive, operator-reviewed migration.
|
||||
<span class="nc" id="L80"> verifyLayoutStationKey(c, oracle);</span>
|
||||
<span class="nc" id="L81"> try (PreparedStatement check = c.prepareStatement("SELECT VERSION_NO FROM " + prefix + "HMI_APP_SCHEMA_VERSION WHERE VERSION_NO = 3");</span>
|
||||
<span class="nc" id="L82"> ResultSet rs = check.executeQuery()) {</span>
|
||||
<span class="nc bnc" id="L83" title="All 2 branches missed."> if (!rs.next()) {</span>
|
||||
<span class="nc" id="L84"> try (PreparedStatement insert = c.prepareStatement("INSERT INTO " + prefix + "HMI_APP_SCHEMA_VERSION(VERSION_NO) VALUES (3)")) {</span>
|
||||
<span class="nc" id="L85"> insert.executeUpdate();</span>
|
||||
<span class="nc" id="L86"> } catch (SQLException concurrent) { // A second cluster member may have inserted simultaneously.</span>
|
||||
<span class="nc" id="L87"> try (PreparedStatement confirm = c.prepareStatement("SELECT VERSION_NO FROM " + prefix + "HMI_APP_SCHEMA_VERSION WHERE VERSION_NO = 3");</span>
|
||||
<span class="nc" id="L88"> ResultSet rs2 = confirm.executeQuery()) {</span>
|
||||
<span class="nc bnc" id="L89" title="All 2 branches missed."> if (!rs2.next()) throw concurrent;</span>
|
||||
<span class="nc" id="L125"> phase = "SCHEMA_VERIFICATION";</span>
|
||||
<span class="nc" id="L126"> verifyLayoutStationKey(c, oracle);</span>
|
||||
<span class="nc" id="L127"> try (PreparedStatement check = c.prepareStatement("SELECT VERSION_NO FROM " + prefix + "HMI_APP_SCHEMA_VERSION WHERE VERSION_NO = 6");</span>
|
||||
<span class="nc" id="L128"> ResultSet rs = check.executeQuery()) {</span>
|
||||
<span class="nc bnc" id="L129" title="All 2 branches missed."> if (!rs.next()) {</span>
|
||||
<span class="nc" id="L130"> try (PreparedStatement insert = c.prepareStatement("INSERT INTO " + prefix + "HMI_APP_SCHEMA_VERSION(VERSION_NO) VALUES (6)")) {</span>
|
||||
<span class="nc" id="L131"> insert.executeUpdate();</span>
|
||||
<span class="nc" id="L132"> } catch (SQLException concurrent) { // A second cluster member may have inserted simultaneously.</span>
|
||||
<span class="nc" id="L133"> try (PreparedStatement confirm = c.prepareStatement("SELECT VERSION_NO FROM " + prefix + "HMI_APP_SCHEMA_VERSION WHERE VERSION_NO = 6");</span>
|
||||
<span class="nc" id="L134"> ResultSet rs2 = confirm.executeQuery()) {</span>
|
||||
<span class="nc bnc" id="L135" title="All 2 branches missed."> if (!rs2.next()) throw concurrent;</span>
|
||||
}
|
||||
<span class="nc" id="L91"> }</span>
|
||||
<span class="nc" id="L137"> }</span>
|
||||
}
|
||||
}
|
||||
<span class="nc" id="L94"> HmiDb.useDatabase(oracle);</span>
|
||||
<span class="nc" id="L95"> ready = true;</span>
|
||||
<span class="nc bnc" id="L96" title="All 2 branches missed."> state = "Initialized: " + product + ", schema " + (oracle ? "CUST" : "cust") + ", version 3";</span>
|
||||
<span class="nc" id="L97"> LOG.info("HMI " + state);</span>
|
||||
<span class="nc" id="L98"> } catch (Exception ex) {</span>
|
||||
<span class="nc" id="L99"> ready = false;</span>
|
||||
<span class="nc" id="L100"> state = "Database initialization failed; inspect GlassFish logs";</span>
|
||||
<span class="nc" id="L101"> LOG.log(Level.SEVERE, "HMI schema initialization FAILED; HMI endpoints remain unavailable", ex);</span>
|
||||
<span class="nc" id="L102"> }</span>
|
||||
<span class="nc" id="L103"> }</span>
|
||||
<span class="nc" id="L140"> HmiDb.useDatabase(oracle);</span>
|
||||
<span class="nc" id="L141"> ready = true;</span>
|
||||
<span class="nc" id="L142"> phase = "READY";</span>
|
||||
<span class="nc" id="L143"> code = "NONE";</span>
|
||||
<span class="nc bnc" id="L144" title="All 2 branches missed."> state = "Initialized: " + product + ", schema " + (oracle ? "CUST" : "cust") + ", version 6";</span>
|
||||
<span class="nc" id="L145"> LOG.info("HMI " + state);</span>
|
||||
<span class="nc" id="L146"> } catch (Exception ex) {</span>
|
||||
<span class="nc" id="L147"> ready = false;</span>
|
||||
<span class="nc bnc" id="L148" title="All 2 branches missed."> if ("NONE".equals(code)) {</span>
|
||||
<span class="nc bnc" id="L149" title="All 2 branches missed."> if (!poolConnected) code = "POOL_CONNECTION_FAILED";</span>
|
||||
<span class="nc bnc" id="L150" title="All 2 branches missed."> else if ("SCHEMA_VERIFICATION".equals(phase)) code = "SCHEMA_MIGRATION_REQUIRED";</span>
|
||||
<span class="nc bnc" id="L151" title="All 4 branches missed."> else if ("SCHEMA_INITIALIZATION".equals(phase) || "SCHEMA_CHECK".equals(phase)) code = "SCHEMA_SETUP_FAILED";</span>
|
||||
<span class="nc" id="L152"> else code = "DATABASE_ERROR";</span>
|
||||
}
|
||||
<span class="nc" id="L154"> state = "Database initialization failed; inspect GlassFish logs";</span>
|
||||
<span class="nc" id="L155"> LOG.log(Level.SEVERE, "HMI schema initialization FAILED [" + code + ", phase=" + phase + "]; HMI endpoints remain unavailable", ex);</span>
|
||||
<span class="nc" id="L156"> }</span>
|
||||
<span class="nc" id="L157"> }</span>
|
||||
|
||||
private void verifyLayoutStationKey(Connection c, boolean oracle) throws SQLException {
|
||||
<span class="nc" id="L106"> DatabaseMetaData md = c.getMetaData();</span>
|
||||
<span class="nc bnc" id="L107" title="All 2 branches missed."> String schema = oracle ? "CUST" : "cust";</span>
|
||||
<span class="nc" id="L108"> boolean station = false, user = false, page = false;</span>
|
||||
<span class="nc" id="L109"> try (ResultSet columns = md.getColumns(null, schema, "HMI_APP_LAYOUT", "STATION_ID")) {</span>
|
||||
<span class="nc bnc" id="L110" title="All 2 branches missed."> if (!columns.next()) throw new SQLException("HMI schema v1 layout detected (STATION_ID missing); apply documented v1 to v2 migration");</span>
|
||||
}
|
||||
<span class="nc" id="L112"> try (ResultSet pk = md.getPrimaryKeys(null, schema, "HMI_APP_LAYOUT")) {</span>
|
||||
<span class="nc bnc" id="L113" title="All 2 branches missed."> while (pk.next()) {</span>
|
||||
<span class="nc" id="L114"> String name = pk.getString("COLUMN_NAME");</span>
|
||||
<span class="nc" id="L115"> station |= "STATION_ID".equalsIgnoreCase(name);</span>
|
||||
<span class="nc" id="L116"> user |= "USER_ID".equalsIgnoreCase(name);</span>
|
||||
<span class="nc" id="L117"> page |= "PAGE_ID".equalsIgnoreCase(name);</span>
|
||||
<span class="nc" id="L118"> }</span>
|
||||
}
|
||||
<span class="nc bnc" id="L120" title="All 6 branches missed."> if (!station || !user || !page) {</span>
|
||||
<span class="nc" id="L121"> throw new SQLException("HMI_APP_LAYOUT requires composite primary key USER_ID, PAGE_ID, STATION_ID; apply documented v1 to v2 migration");</span>
|
||||
}
|
||||
<span class="nc" id="L123"> }</span>
|
||||
private void createSqlServerSchema(Connection c) throws SQLException {
|
||||
<span class="nc" id="L125"> try (Statement st = c.createStatement()) {</span>
|
||||
<span class="nc" id="L126"> st.execute("IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = 'cust') EXEC('CREATE SCHEMA cust')");</span>
|
||||
}
|
||||
<span class="nc" id="L128"> }</span>
|
||||
private void assertOracleSchemaExists(Connection c) throws SQLException {
|
||||
<span class="nc" id="L130"> try (PreparedStatement ps = c.prepareStatement("SELECT 1 FROM ALL_USERS WHERE USERNAME = 'CUST'");</span>
|
||||
<span class="nc" id="L131"> ResultSet rs = ps.executeQuery()) {</span>
|
||||
<span class="nc bnc" id="L132" title="All 2 branches missed."> if (!rs.next()) throw new SQLException("Oracle schema CUST does not exist. Provision it first using DBA privileges.");</span>
|
||||
}
|
||||
<span class="nc" id="L134"> }</span>
|
||||
private void createIfMissing(Connection c, String table, String ddl) throws SQLException {
|
||||
<span class="nc bnc" id="L136" title="All 2 branches missed."> if (exists(c, table)) return;</span>
|
||||
<span class="nc" id="L137"> try (Statement st = c.createStatement()) {</span>
|
||||
<span class="nc" id="L138"> st.execute(ddl);</span>
|
||||
<span class="nc" id="L139"> LOG.info("Created HMI table CUST." + table);</span>
|
||||
<span class="nc" id="L140"> } catch (SQLException e) {</span>
|
||||
<span class="nc bnc" id="L141" title="All 2 branches missed."> if (!exists(c, table)) throw e; // Only suppress a concurrent, successful table creation.</span>
|
||||
<span class="nc" id="L142"> }</span>
|
||||
<span class="nc" id="L143"> }</span>
|
||||
private boolean exists(Connection c, String table) throws SQLException {
|
||||
<span class="nc" id="L145"> DatabaseMetaData m = c.getMetaData();</span>
|
||||
<span class="nc" id="L146"> try (ResultSet rs = m.getTables(null, "CUST", table, new String[]{"TABLE"})) {</span>
|
||||
<span class="nc bnc" id="L147" title="All 2 branches missed."> if (rs.next()) return true;</span>
|
||||
<span class="nc bnc" id="L148" title="All 2 branches missed."> }</span>
|
||||
<span class="nc" id="L149"> try (ResultSet rs = m.getTables(null, "cust", table, new String[]{"TABLE"})) {</span>
|
||||
<span class="nc" id="L150"> return rs.next();</span>
|
||||
private boolean pageParentColumnExists(Connection c, boolean oracle) throws SQLException {
|
||||
<span class="nc" id="L160"> DatabaseMetaData md = c.getMetaData();</span>
|
||||
<span class="nc bnc" id="L161" title="All 2 branches missed."> String schemaName = oracle ? "CUST" : "cust";</span>
|
||||
<span class="nc" id="L162"> try (ResultSet rs = md.getColumns(null, schemaName, "HMI_APP_PAGE", "PARENT_PAGE_ID")) {</span>
|
||||
<span class="nc bnc" id="L163" title="All 2 branches missed."> if (rs.next()) return true;</span>
|
||||
<span class="nc bnc" id="L164" title="All 2 branches missed."> }</span>
|
||||
// Some SQL Server drivers return the schema with different capitalization.
|
||||
<span class="nc" id="L166"> try (ResultSet rs = md.getColumns(null, schemaName.toUpperCase(Locale.ROOT), "HMI_APP_PAGE", "PARENT_PAGE_ID")) {</span>
|
||||
<span class="nc" id="L167"> return rs.next();</span>
|
||||
}
|
||||
}
|
||||
<span class="nc" id="L153"> @Override public void contextDestroyed(ServletContextEvent event) { ready = false; }</span>
|
||||
private void addParentPageColumnIfMissing(Connection c, boolean oracle) throws SQLException {
|
||||
<span class="nc bnc" id="L171" title="All 2 branches missed."> if (pageParentColumnExists(c, oracle)) return;</span>
|
||||
<span class="nc bnc" id="L172" title="All 2 branches missed."> String ddl = oracle</span>
|
||||
<span class="nc" id="L173"> ? "ALTER TABLE CUST.HMI_APP_PAGE ADD (PARENT_PAGE_ID VARCHAR2(64))"</span>
|
||||
<span class="nc" id="L174"> : "ALTER TABLE cust.HMI_APP_PAGE ADD PARENT_PAGE_ID NVARCHAR(64) NULL";</span>
|
||||
<span class="nc" id="L175"> try (Statement st = c.createStatement()) {</span>
|
||||
<span class="nc" id="L176"> st.execute(ddl);</span>
|
||||
<span class="nc" id="L177"> LOG.info("HMI schema V5.2: added HMI_APP_PAGE.PARENT_PAGE_ID");</span>
|
||||
<span class="nc" id="L178"> } catch (SQLException error) {</span>
|
||||
// A second GlassFish instance might have applied the same migration.
|
||||
<span class="nc bnc" id="L180" title="All 2 branches missed."> if (!pageParentColumnExists(c, oracle)) throw error;</span>
|
||||
<span class="nc" id="L181"> }</span>
|
||||
<span class="nc" id="L182"> }</span>
|
||||
|
||||
private void verifyLayoutStationKey(Connection c, boolean oracle) throws SQLException {
|
||||
<span class="nc" id="L185"> DatabaseMetaData md = c.getMetaData();</span>
|
||||
<span class="nc bnc" id="L186" title="All 2 branches missed."> String schema = oracle ? "CUST" : "cust";</span>
|
||||
<span class="nc" id="L187"> boolean station = false, user = false, page = false;</span>
|
||||
<span class="nc" id="L188"> try (ResultSet columns = md.getColumns(null, schema, "HMI_APP_LAYOUT", "STATION_ID")) {</span>
|
||||
<span class="nc bnc" id="L189" title="All 2 branches missed."> if (!columns.next()) throw new SQLException("HMI schema v1 layout detected (STATION_ID missing); apply documented v1 to v2 migration");</span>
|
||||
}
|
||||
<span class="nc" id="L191"> try (ResultSet pk = md.getPrimaryKeys(null, schema, "HMI_APP_LAYOUT")) {</span>
|
||||
<span class="nc bnc" id="L192" title="All 2 branches missed."> while (pk.next()) {</span>
|
||||
<span class="nc" id="L193"> String name = pk.getString("COLUMN_NAME");</span>
|
||||
<span class="nc" id="L194"> station |= "STATION_ID".equalsIgnoreCase(name);</span>
|
||||
<span class="nc" id="L195"> user |= "USER_ID".equalsIgnoreCase(name);</span>
|
||||
<span class="nc" id="L196"> page |= "PAGE_ID".equalsIgnoreCase(name);</span>
|
||||
<span class="nc" id="L197"> }</span>
|
||||
}
|
||||
<span class="nc bnc" id="L199" title="All 6 branches missed."> if (!station || !user || !page) {</span>
|
||||
<span class="nc" id="L200"> throw new SQLException("HMI_APP_LAYOUT requires composite primary key USER_ID, PAGE_ID, STATION_ID; apply documented v1 to v2 migration");</span>
|
||||
}
|
||||
<span class="nc" id="L202"> }</span>
|
||||
private void createSqlServerSchema(Connection c) throws SQLException {
|
||||
<span class="nc" id="L204"> try (Statement st = c.createStatement()) {</span>
|
||||
<span class="nc" id="L205"> st.execute("IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = 'cust') EXEC('CREATE SCHEMA cust')");</span>
|
||||
}
|
||||
<span class="nc" id="L207"> }</span>
|
||||
private void assertOracleSchemaExists(Connection c) throws SQLException {
|
||||
<span class="nc" id="L209"> try (PreparedStatement ps = c.prepareStatement("SELECT 1 FROM ALL_USERS WHERE USERNAME = 'CUST'");</span>
|
||||
<span class="nc" id="L210"> ResultSet rs = ps.executeQuery()) {</span>
|
||||
<span class="nc bnc" id="L211" title="All 2 branches missed."> if (!rs.next()) throw new SQLException("Oracle schema CUST does not exist. Provision it first using DBA privileges.");</span>
|
||||
}
|
||||
<span class="nc" id="L213"> }</span>
|
||||
private void createIfMissing(Connection c, String table, String ddl) throws SQLException {
|
||||
<span class="nc bnc" id="L215" title="All 2 branches missed."> if (exists(c, table)) return;</span>
|
||||
<span class="nc" id="L216"> try (Statement st = c.createStatement()) {</span>
|
||||
<span class="nc" id="L217"> st.execute(ddl);</span>
|
||||
<span class="nc" id="L218"> LOG.info("Created HMI table CUST." + table);</span>
|
||||
<span class="nc" id="L219"> } catch (SQLException e) {</span>
|
||||
<span class="nc bnc" id="L220" title="All 2 branches missed."> if (!exists(c, table)) throw e; // Only suppress a concurrent, successful table creation.</span>
|
||||
<span class="nc" id="L221"> }</span>
|
||||
<span class="nc" id="L222"> }</span>
|
||||
private boolean exists(Connection c, String table) throws SQLException {
|
||||
<span class="nc" id="L224"> DatabaseMetaData m = c.getMetaData();</span>
|
||||
<span class="nc" id="L225"> try (ResultSet rs = m.getTables(null, "CUST", table, new String[]{"TABLE"})) {</span>
|
||||
<span class="nc bnc" id="L226" title="All 2 branches missed."> if (rs.next()) return true;</span>
|
||||
<span class="nc bnc" id="L227" title="All 2 branches missed."> }</span>
|
||||
<span class="nc" id="L228"> try (ResultSet rs = m.getTables(null, "cust", table, new String[]{"TABLE"})) {</span>
|
||||
<span class="nc" id="L229"> return rs.next();</span>
|
||||
}
|
||||
}
|
||||
<span class="nc" id="L232"> @Override public void contextDestroyed(ServletContextEvent event) { ready = false; phase = "STOPPED"; }</span>
|
||||
}
|
||||
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.4.201905082037</span></div></body></html>
|
||||
Reference in New Issue
Block a user