Files
WidgetWorkbench/backend/target/site/jacoco/com.workbenchclassic.hmi/HmiSchemaBootstrap.java.html
2026-10-20 20:22:38 +00:00

155 lines
16 KiB
HTML

<?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> &gt; <a href="index.source.html" class="el_package">com.workbenchclassic.hmi</a> &gt; <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 = &quot;Not initialized&quot;;</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(&quot;oracle&quot;);</span>
<span class="nc bnc" id="L31" title="All 4 branches missed."> boolean sqlServer = product.contains(&quot;microsoft sql server&quot;) || product.contains(&quot;sql server&quot;);</span>
<span class="nc bnc" id="L32" title="All 4 branches missed."> if (!oracle &amp;&amp; !sqlServer) throw new SQLException(&quot;Unsupported dsCustNJTA database: &quot; + 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 ? &quot;CUST.&quot; : &quot;cust.&quot;;</span>
<span class="nc bnc" id="L37" title="All 2 branches missed."> String text = oracle ? &quot;CLOB&quot; : &quot;NVARCHAR(MAX)&quot;;</span>
<span class="nc bnc" id="L38" title="All 2 branches missed."> String shortText = oracle ? &quot;VARCHAR2&quot; : &quot;NVARCHAR&quot;;</span>
<span class="nc bnc" id="L39" title="All 2 branches missed."> String numeric = oracle ? &quot;NUMBER(10)&quot; : &quot;INT&quot;;</span>
<span class="nc bnc" id="L40" title="All 2 branches missed."> String date = oracle ? &quot;TIMESTAMP&quot; : &quot;DATETIME2&quot;;</span>
<span class="nc bnc" id="L41" title="All 2 branches missed."> String now = oracle ? &quot;SYSTIMESTAMP&quot; : &quot;SYSUTCDATETIME()&quot;;</span>
<span class="nc" id="L42"> createIfMissing(c, &quot;HMI_APP_PAGE&quot;, &quot;CREATE TABLE &quot; + prefix + &quot;HMI_APP_PAGE (&quot;</span>
+ &quot;ID &quot; + shortText + &quot;(64) NOT NULL PRIMARY KEY, PAGE_KEY &quot; + shortText + &quot;(120) NOT NULL UNIQUE, &quot;
+ &quot;TITLE &quot; + shortText + &quot;(255) NOT NULL, MENU_GROUP &quot; + shortText + &quot;(120), &quot;
+ &quot;SORT_ORDER &quot; + numeric + &quot; DEFAULT 0 NOT NULL, ENABLED &quot; + numeric + &quot; DEFAULT 1 NOT NULL)&quot;);
<span class="nc" id="L46"> createIfMissing(c, &quot;HMI_APP_TEMPLATE&quot;, &quot;CREATE TABLE &quot; + prefix + &quot;HMI_APP_TEMPLATE (&quot;</span>
+ &quot;ID &quot; + shortText + &quot;(64) NOT NULL PRIMARY KEY, PAGE_ID &quot; + shortText + &quot;(64) NOT NULL, &quot;
+ &quot;VERSION_NO &quot; + numeric + &quot; NOT NULL, STATUS &quot; + shortText + &quot;(20) NOT NULL, &quot;
+ &quot;LAYOUT_JSON &quot; + text + &quot; NOT NULL, UPDATED_BY &quot; + shortText + &quot;(255), &quot;
+ &quot;UPDATED_AT &quot; + date + &quot; DEFAULT &quot; + now + &quot; NOT NULL)&quot;);
<span class="nc" id="L51"> createIfMissing(c, &quot;HMI_APP_ASSIGNMENT&quot;, &quot;CREATE TABLE &quot; + prefix + &quot;HMI_APP_ASSIGNMENT (&quot;</span>
+ &quot;ID &quot; + shortText + &quot;(64) NOT NULL PRIMARY KEY, PAGE_ID &quot; + shortText + &quot;(64) NOT NULL, &quot;
+ &quot;SCOPE_TYPE &quot; + shortText + &quot;(20) NOT NULL, SCOPE_ID &quot; + shortText + &quot;(255) NOT NULL, &quot;
+ &quot;TEMPLATE_ID &quot; + shortText + &quot;(64) NOT NULL, PRIORITY &quot; + numeric + &quot; DEFAULT 0 NOT NULL)&quot;);
<span class="nc" id="L55"> createIfMissing(c, &quot;HMI_APP_LAYOUT&quot;, &quot;CREATE TABLE &quot; + prefix + &quot;HMI_APP_LAYOUT (&quot;</span>
+ &quot;USER_ID &quot; + shortText + &quot;(255) NOT NULL, PAGE_ID &quot; + shortText + &quot;(64) NOT NULL, &quot;
+ &quot;STATION_ID &quot; + shortText + &quot;(120) DEFAULT '*' NOT NULL, BASE_TEMPLATE_ID &quot; + shortText + &quot;(64), OVERRIDES_JSON &quot; + text + &quot; NOT NULL, &quot;
+ &quot;UPDATED_AT &quot; + date + &quot; DEFAULT &quot; + now + &quot; NOT NULL, &quot;
+ &quot;PRIMARY KEY (USER_ID, PAGE_ID, STATION_ID))&quot;);
<span class="nc" id="L60"> createIfMissing(c, &quot;HMI_APP_PREF&quot;, &quot;CREATE TABLE &quot; + prefix + &quot;HMI_APP_PREF (&quot;</span>
+ &quot;SCOPE_TYPE &quot; + shortText + &quot;(20) NOT NULL, SCOPE_ID &quot; + shortText + &quot;(255) NOT NULL, &quot;
+ &quot;PREFS_JSON &quot; + text + &quot; NOT NULL, UPDATED_AT &quot; + date + &quot; DEFAULT &quot; + now + &quot; NOT NULL, &quot;
+ &quot;PRIMARY KEY (SCOPE_TYPE, SCOPE_ID))&quot;);
<span class="nc" id="L64"> createIfMissing(c, &quot;HMI_APP_GROUP_MEMBER&quot;, &quot;CREATE TABLE &quot; + prefix + &quot;HMI_APP_GROUP_MEMBER (&quot;</span>
+ &quot;USER_ID &quot; + shortText + &quot;(255) NOT NULL, GROUP_ID &quot; + shortText + &quot;(120) NOT NULL, &quot;
+ &quot;PRIMARY KEY (USER_ID, GROUP_ID))&quot;);
<span class="nc" id="L67"> createIfMissing(c, &quot;HMI_APP_AUDIT&quot;, &quot;CREATE TABLE &quot; + prefix + &quot;HMI_APP_AUDIT (&quot;</span>
+ &quot;ID &quot; + shortText + &quot;(64) NOT NULL PRIMARY KEY, ACTOR &quot; + shortText + &quot;(255) NOT NULL, &quot;
+ &quot;ACTION_NAME &quot; + shortText + &quot;(120) NOT NULL, ENTITY_ID &quot; + shortText + &quot;(255), &quot;
+ &quot;DETAILS &quot; + shortText + &quot;(1000), CREATED_AT &quot; + date + &quot; DEFAULT &quot; + now + &quot; NOT NULL)&quot;);
// Versioned ios.css content, separate from the immutable WAR fallback.
<span class="nc" id="L72"> createIfMissing(c, &quot;HMI_APP_CSS_REVISION&quot;, &quot;CREATE TABLE &quot; + prefix + &quot;HMI_APP_CSS_REVISION (&quot;</span>
+ &quot;ID &quot; + shortText + &quot;(64) NOT NULL PRIMARY KEY, VERSION_NO &quot; + numeric + &quot; NOT NULL UNIQUE, &quot;
+ &quot;STATUS &quot; + shortText + &quot;(20) NOT NULL, CSS_CONTENT &quot; + text + &quot; NOT NULL, &quot;
+ &quot;UPDATED_BY &quot; + shortText + &quot;(255), UPDATED_AT &quot; + date + &quot; DEFAULT &quot; + now + &quot; NOT NULL)&quot;);
<span class="nc" id="L76"> createIfMissing(c, &quot;HMI_APP_SCHEMA_VERSION&quot;, &quot;CREATE TABLE &quot; + prefix + &quot;HMI_APP_SCHEMA_VERSION (&quot;</span>
+ &quot;VERSION_NO &quot; + numeric + &quot; NOT NULL PRIMARY KEY, INSTALLED_AT &quot; + date + &quot; DEFAULT &quot; + now + &quot; NOT NULL)&quot;);
// 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(&quot;SELECT VERSION_NO FROM &quot; + prefix + &quot;HMI_APP_SCHEMA_VERSION WHERE VERSION_NO = 3&quot;);</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(&quot;INSERT INTO &quot; + prefix + &quot;HMI_APP_SCHEMA_VERSION(VERSION_NO) VALUES (3)&quot;)) {</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(&quot;SELECT VERSION_NO FROM &quot; + prefix + &quot;HMI_APP_SCHEMA_VERSION WHERE VERSION_NO = 3&quot;);</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 = &quot;Initialized: &quot; + product + &quot;, schema &quot; + (oracle ? &quot;CUST&quot; : &quot;cust&quot;) + &quot;, version 3&quot;;</span>
<span class="nc" id="L97"> LOG.info(&quot;HMI &quot; + 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 = &quot;Database initialization failed; inspect GlassFish logs&quot;;</span>
<span class="nc" id="L101"> LOG.log(Level.SEVERE, &quot;HMI schema initialization FAILED; HMI endpoints remain unavailable&quot;, 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 ? &quot;CUST&quot; : &quot;cust&quot;;</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, &quot;HMI_APP_LAYOUT&quot;, &quot;STATION_ID&quot;)) {</span>
<span class="nc bnc" id="L110" title="All 2 branches missed."> if (!columns.next()) throw new SQLException(&quot;HMI schema v1 layout detected (STATION_ID missing); apply documented v1 to v2 migration&quot;);</span>
}
<span class="nc" id="L112"> try (ResultSet pk = md.getPrimaryKeys(null, schema, &quot;HMI_APP_LAYOUT&quot;)) {</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(&quot;COLUMN_NAME&quot;);</span>
<span class="nc" id="L115"> station |= &quot;STATION_ID&quot;.equalsIgnoreCase(name);</span>
<span class="nc" id="L116"> user |= &quot;USER_ID&quot;.equalsIgnoreCase(name);</span>
<span class="nc" id="L117"> page |= &quot;PAGE_ID&quot;.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(&quot;HMI_APP_LAYOUT requires composite primary key USER_ID, PAGE_ID, STATION_ID; apply documented v1 to v2 migration&quot;);</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(&quot;IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = 'cust') EXEC('CREATE SCHEMA cust')&quot;);</span>
}
<span class="nc" id="L128"> }</span>
private void assertOracleSchemaExists(Connection c) throws SQLException {
<span class="nc" id="L130"> try (PreparedStatement ps = c.prepareStatement(&quot;SELECT 1 FROM ALL_USERS WHERE USERNAME = 'CUST'&quot;);</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(&quot;Oracle schema CUST does not exist. Provision it first using DBA privileges.&quot;);</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(&quot;Created HMI table CUST.&quot; + 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, &quot;CUST&quot;, table, new String[]{&quot;TABLE&quot;})) {</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, &quot;cust&quot;, table, new String[]{&quot;TABLE&quot;})) {</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>