Initial commit: update
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="de"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>HmiSchemaBootstrap.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">backend</a> > <a href="index.source.html" class="el_package">com.workbenchclassic.hmi</a> > <span class="el_source">HmiSchemaBootstrap.java</span></div><h1>HmiSchemaBootstrap.java</h1><pre class="source lang-java linenums">package com.workbenchclassic.hmi;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
/**
|
||||
* Idempotent schema creation on deployment for Oracle and SQL Server.
|
||||
* Existing CUST schema is used; no DBA user/schema creation on Oracle.
|
||||
* DDL runs under the permissions of dsCustNJTA (must have CREATE TABLE).
|
||||
*/
|
||||
<span class="nc" id="L20">public final class HmiSchemaBootstrap implements ServletContextListener {</span>
|
||||
<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>
|
||||
|
||||
@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 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>
|
||||
+ "ID " + shortText + "(64) NOT NULL PRIMARY KEY, PAGE_KEY " + shortText + "(120) NOT NULL UNIQUE, "
|
||||
+ "TITLE " + shortText + "(255) NOT NULL, MENU_GROUP " + shortText + "(120), "
|
||||
+ "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>
|
||||
+ "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>
|
||||
+ "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>
|
||||
+ "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>
|
||||
+ "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>
|
||||
+ "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>
|
||||
+ "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>
|
||||
+ "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>
|
||||
+ "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="L91"> }</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>
|
||||
|
||||
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>
|
||||
}
|
||||
}
|
||||
<span class="nc" id="L153"> @Override public void contextDestroyed(ServletContextEvent event) { ready = false; }</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