Workplace Widget

This commit is contained in:
chris
2026-10-26 19:10:24 +00:00
parent 1f7c8d93cc
commit 90af5db9ad
33 changed files with 767 additions and 575 deletions

View File

@@ -225,548 +225,551 @@ import org.json.JSONObject;
<span class="nc" id="L225"> }</span>
<span class="nc" id="L226"> return out;</span>
}
/** Requires a GlassFish administrator role; tests dsCustNJTA LIVE and returns safe metadata. */
/** After initial setup: admin only. During failed first initialization: IMSAPI read-only diagnostics. */
@GET @Path(&quot;diagnostics&quot;)
public Response diagnostics() {
<span class="nc bnc" id="L231" title="All 2 branches missed."> if (user() == null) return err(401, &quot;IMSAPI-Anmeldung erforderlich&quot;);</span>
<span class="nc bnc" id="L232" title="All 2 branches missed."> if (!admin()) return err(403, &quot;hmi-admin role required&quot;);</span>
<span class="nc" id="L233"> JSONObject out = new JSONObject()</span>
<span class="nc" id="L234"> .put(&quot;user&quot;, user())</span>
<span class="nc" id="L235"> .put(&quot;readyAtDeployment&quot;, HmiSchemaBootstrap.isReady())</span>
<span class="nc" id="L236"> .put(&quot;initializationPhase&quot;, HmiSchemaBootstrap.phase())</span>
<span class="nc" id="L237"> .put(&quot;failureCode&quot;, HmiSchemaBootstrap.failureCode())</span>
<span class="nc" id="L238"> .put(&quot;pool&quot;, &quot;dsCustNJTA&quot;);</span>
<span class="nc" id="L239"> try (Connection c = HmiDb.open()) {</span>
<span class="nc" id="L240"> String db = c.getMetaData().getDatabaseProductName();</span>
<span class="nc" id="L241"> boolean oracle = db.toLowerCase(java.util.Locale.ROOT).contains(&quot;oracle&quot;);</span>
<span class="nc" id="L242"> boolean sqlServer = db.toLowerCase(java.util.Locale.ROOT).contains(&quot;sql server&quot;);</span>
<span class="nc bnc" id="L243" title="All 4 branches missed."> out.put(&quot;poolReachableNow&quot;, true).put(&quot;database&quot;, oracle ? &quot;ORACLE&quot; : sqlServer ? &quot;SQL_SERVER&quot; : &quot;UNKNOWN&quot;)</span>
<span class="nc bnc" id="L244" title="All 2 branches missed."> .put(&quot;schema&quot;, oracle ? &quot;CUST&quot; : &quot;cust&quot;);</span>
<span class="nc bnc" id="L245" title="All 2 branches missed."> try (PreparedStatement p = c.prepareStatement(oracle ? &quot;SELECT 1 FROM DUAL&quot; : &quot;SELECT 1&quot;);</span>
<span class="nc" id="L246"> ResultSet rs = p.executeQuery()) {</span>
<span class="nc bnc" id="L247" title="All 4 branches missed."> out.put(&quot;connectionQueryOk&quot;, rs.next() &amp;&amp; rs.getInt(1) == 1);</span>
<span class="nc" id="L232"> boolean initialized = HmiSchemaBootstrap.isReady();</span>
<span class="nc" id="L233"> boolean isAdmin = admin();</span>
<span class="nc bnc" id="L234" title="All 4 branches missed."> if (initialized &amp;&amp; !isAdmin) return err(403, &quot;hmi-admin role required&quot;);</span>
<span class="nc" id="L235"> JSONObject out = new JSONObject()</span>
<span class="nc bnc" id="L236" title="All 4 branches missed."> .put(&quot;access&quot;, !initialized &amp;&amp; !isAdmin ? &quot;INITIALIZATION_READ_ONLY&quot; : &quot;ADMIN&quot;)</span>
<span class="nc" id="L237"> .put(&quot;user&quot;, user())</span>
<span class="nc" id="L238"> .put(&quot;readyAtDeployment&quot;, HmiSchemaBootstrap.isReady())</span>
<span class="nc" id="L239"> .put(&quot;initializationPhase&quot;, HmiSchemaBootstrap.phase())</span>
<span class="nc" id="L240"> .put(&quot;failureCode&quot;, HmiSchemaBootstrap.failureCode())</span>
<span class="nc" id="L241"> .put(&quot;pool&quot;, &quot;dsCustNJTA&quot;);</span>
<span class="nc" id="L242"> try (Connection c = HmiDb.open()) {</span>
<span class="nc" id="L243"> String db = c.getMetaData().getDatabaseProductName();</span>
<span class="nc" id="L244"> boolean oracle = db.toLowerCase(java.util.Locale.ROOT).contains(&quot;oracle&quot;);</span>
<span class="nc" id="L245"> boolean sqlServer = db.toLowerCase(java.util.Locale.ROOT).contains(&quot;sql server&quot;);</span>
<span class="nc bnc" id="L246" title="All 4 branches missed."> out.put(&quot;poolReachableNow&quot;, true).put(&quot;database&quot;, oracle ? &quot;ORACLE&quot; : sqlServer ? &quot;SQL_SERVER&quot; : &quot;UNKNOWN&quot;)</span>
<span class="nc bnc" id="L247" title="All 2 branches missed."> .put(&quot;schema&quot;, oracle ? &quot;CUST&quot; : &quot;cust&quot;);</span>
<span class="nc bnc" id="L248" title="All 2 branches missed."> try (PreparedStatement p = c.prepareStatement(oracle ? &quot;SELECT 1 FROM DUAL&quot; : &quot;SELECT 1&quot;);</span>
<span class="nc" id="L249"> ResultSet rs = p.executeQuery()) {</span>
<span class="nc bnc" id="L250" title="All 4 branches missed."> out.put(&quot;connectionQueryOk&quot;, rs.next() &amp;&amp; rs.getInt(1) == 1);</span>
}
<span class="nc" id="L249"> int found = 0;</span>
<span class="nc" id="L250"> String[] tables = {&quot;HMI_APP_PAGE&quot;, &quot;HMI_APP_LAYOUT&quot;, &quot;HMI_APP_PREF&quot;, &quot;HMI_APP_CSS_REVISION&quot;, &quot;HMI_APP_SCHEMA_VERSION&quot;};</span>
<span class="nc bnc" id="L251" title="All 2 branches missed."> for (String t : tables) {</span>
<span class="nc bnc" id="L252" title="All 2 branches missed."> try (ResultSet rs = c.getMetaData().getTables(null, oracle ? &quot;CUST&quot; : &quot;cust&quot;, t, new String[]{&quot;TABLE&quot;})) {</span>
<span class="nc bnc" id="L253" title="All 2 branches missed."> if (rs.next()) found++;</span>
<span class="nc" id="L252"> int found = 0;</span>
<span class="nc" id="L253"> String[] tables = {&quot;HMI_APP_PAGE&quot;, &quot;HMI_APP_LAYOUT&quot;, &quot;HMI_APP_PREF&quot;, &quot;HMI_APP_CSS_REVISION&quot;, &quot;HMI_APP_SCHEMA_VERSION&quot;};</span>
<span class="nc bnc" id="L254" title="All 2 branches missed."> for (String t : tables) {</span>
<span class="nc bnc" id="L255" title="All 2 branches missed."> try (ResultSet rs = c.getMetaData().getTables(null, oracle ? &quot;CUST&quot; : &quot;cust&quot;, t, new String[]{&quot;TABLE&quot;})) {</span>
<span class="nc bnc" id="L256" title="All 2 branches missed."> if (rs.next()) found++;</span>
}
}
<span class="nc" id="L256"> out.put(&quot;tablesFound&quot;, found).put(&quot;tablesExpected&quot;, tables.length);</span>
<span class="nc" id="L257"> return json(out);</span>
<span class="nc" id="L258"> } catch (SQLException e) {</span>
<span class="nc" id="L259"> java.util.logging.Logger.getLogger(HmiApiResource.class.getName())</span>
<span class="nc" id="L260"> .log(java.util.logging.Level.WARNING, &quot;Authenticated HMI diagnostics failed&quot;, e);</span>
<span class="nc" id="L261"> return json(out.put(&quot;poolReachableNow&quot;, false).put(&quot;connectionQueryOk&quot;, false).put(&quot;failure&quot;, &quot;CHECK_GLASSFISH_LOG&quot;));</span>
<span class="nc" id="L259"> out.put(&quot;tablesFound&quot;, found).put(&quot;tablesExpected&quot;, tables.length);</span>
<span class="nc" id="L260"> return json(out);</span>
<span class="nc" id="L261"> } catch (SQLException e) {</span>
<span class="nc" id="L262"> java.util.logging.Logger.getLogger(HmiApiResource.class.getName())</span>
<span class="nc" id="L263"> .log(java.util.logging.Level.WARNING, &quot;Authenticated HMI diagnostics failed&quot;, e);</span>
<span class="nc" id="L264"> return json(out.put(&quot;poolReachableNow&quot;, false).put(&quot;connectionQueryOk&quot;, false).put(&quot;failure&quot;, &quot;CHECK_GLASSFISH_LOG&quot;));</span>
}
}
@GET @Path(&quot;status&quot;)
<span class="nc bnc" id="L266" title="All 2 branches missed."> public Response status() { Response r = denied(admin()); return r != null ? r : json(new JSONObject().put(&quot;ready&quot;,true).put(&quot;detail&quot;,HmiSchemaBootstrap.state())); }</span>
<span class="nc bnc" id="L269" title="All 2 branches missed."> public Response status() { Response r = denied(admin()); return r != null ? r : json(new JSONObject().put(&quot;ready&quot;,true).put(&quot;detail&quot;,HmiSchemaBootstrap.state())); }</span>
@GET @Path(&quot;widgets&quot;)
<span class="nc bnc" id="L268" title="All 2 branches missed."> public Response widgets() { Response r = denied(true); if (r!=null) return r;</span>
<span class="nc" id="L269"> JSONArray a = new JSONArray();</span>
<span class="nc bnc" id="L270" title="All 2 branches missed."> for (String t : WIDGET_TYPES) a.put(new JSONObject().put(&quot;type&quot;,t).put(&quot;name&quot;,t.replace(&quot;mes.&quot;, &quot;MES: &quot;).replace(&quot;ui.&quot;, &quot;UI: &quot;)));</span>
<span class="nc" id="L271"> return json(a);</span>
<span class="nc bnc" id="L271" title="All 2 branches missed."> public Response widgets() { Response r = denied(true); if (r!=null) return r;</span>
<span class="nc" id="L272"> JSONArray a = new JSONArray();</span>
<span class="nc bnc" id="L273" title="All 2 branches missed."> for (String t : WIDGET_TYPES) a.put(new JSONObject().put(&quot;type&quot;,t).put(&quot;name&quot;,t.replace(&quot;mes.&quot;, &quot;MES: &quot;).replace(&quot;ui.&quot;, &quot;UI: &quot;)));</span>
<span class="nc" id="L274"> return json(a);</span>
}
@GET @Path(&quot;roles&quot;)
public Response viewableRoleNames() {
<span class="nc bnc" id="L275" title="All 2 branches missed."> Response r=denied(designer()); if(r!=null)return r;</span>
<span class="nc" id="L276"> JSONArray out=new JSONArray(); java.util.Set&lt;String&gt; seen=new java.util.TreeSet&lt;&gt;();</span>
<span class="nc" id="L277"> java.util.Collections.addAll(seen,&quot;hmi-admin&quot;,&quot;hmi-designer&quot;,&quot;hmi-publisher&quot;,&quot;hmi-script-editor&quot;);</span>
<span class="nc" id="L278"> try(Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(</span>
<span class="nc" id="L279"> &quot;SELECT ROLE_KEY FROM &quot;+schema()+&quot;HMI_APP_ROLE ORDER BY ROLE_KEY&quot;);ResultSet rs=p.executeQuery()) {</span>
<span class="nc bnc" id="L280" title="All 2 branches missed."> while(rs.next())seen.add(rs.getString(1));</span>
<span class="nc bnc" id="L281" title="All 2 branches missed."> for(String name:seen)out.put(new JSONObject().put(&quot;key&quot;,name).put(&quot;description&quot;,name));</span>
<span class="nc" id="L282"> return json(out);</span>
<span class="nc" id="L283"> }catch(SQLException e){return dbError(e);}</span>
<span class="nc bnc" id="L278" title="All 2 branches missed."> Response r=denied(designer()); if(r!=null)return r;</span>
<span class="nc" id="L279"> JSONArray out=new JSONArray(); java.util.Set&lt;String&gt; seen=new java.util.TreeSet&lt;&gt;();</span>
<span class="nc" id="L280"> java.util.Collections.addAll(seen,&quot;hmi-admin&quot;,&quot;hmi-designer&quot;,&quot;hmi-publisher&quot;,&quot;hmi-script-editor&quot;);</span>
<span class="nc" id="L281"> try(Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(</span>
<span class="nc" id="L282"> &quot;SELECT ROLE_KEY FROM &quot;+schema()+&quot;HMI_APP_ROLE ORDER BY ROLE_KEY&quot;);ResultSet rs=p.executeQuery()) {</span>
<span class="nc bnc" id="L283" title="All 2 branches missed."> while(rs.next())seen.add(rs.getString(1));</span>
<span class="nc bnc" id="L284" title="All 2 branches missed."> for(String name:seen)out.put(new JSONObject().put(&quot;key&quot;,name).put(&quot;description&quot;,name));</span>
<span class="nc" id="L285"> return json(out);</span>
<span class="nc" id="L286"> }catch(SQLException e){return dbError(e);}</span>
}
/** A hidden parent also hides the complete subtree for normal operators. */
private static boolean visiblePage(String pageId, java.util.Map&lt;String,JSONObject&gt; byId) {
<span class="nc" id="L287"> java.util.Set&lt;String&gt; seen=new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L288"> String current=pageId;</span>
<span class="nc bnc" id="L289" title="All 2 branches missed."> while(current!=null) {</span>
<span class="nc bnc" id="L290" title="All 2 branches missed."> if(!seen.add(current))return false;</span>
<span class="nc" id="L291"> JSONObject page=byId.get(current);</span>
<span class="nc bnc" id="L292" title="All 4 branches missed."> if(page==null || !page.getBoolean(&quot;enabled&quot;))return false;</span>
<span class="nc bnc" id="L293" title="All 2 branches missed."> current=page.isNull(&quot;parentPageId&quot;)?null:page.optString(&quot;parentPageId&quot;,null);</span>
<span class="nc" id="L294"> }</span>
<span class="nc" id="L295"> return true;</span>
<span class="nc" id="L290"> java.util.Set&lt;String&gt; seen=new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L291"> String current=pageId;</span>
<span class="nc bnc" id="L292" title="All 2 branches missed."> while(current!=null) {</span>
<span class="nc bnc" id="L293" title="All 2 branches missed."> if(!seen.add(current))return false;</span>
<span class="nc" id="L294"> JSONObject page=byId.get(current);</span>
<span class="nc bnc" id="L295" title="All 4 branches missed."> if(page==null || !page.getBoolean(&quot;enabled&quot;))return false;</span>
<span class="nc bnc" id="L296" title="All 2 branches missed."> current=page.isNull(&quot;parentPageId&quot;)?null:page.optString(&quot;parentPageId&quot;,null);</span>
<span class="nc" id="L297"> }</span>
<span class="nc" id="L298"> return true;</span>
}
@GET @Path(&quot;pages&quot;)
public Response pages() {
<span class="nc bnc" id="L299" title="All 2 branches missed."> Response r = denied(true); if (r!=null) return r;</span>
<span class="nc" id="L300"> JSONArray out = new JSONArray();</span>
<span class="nc" id="L301"> java.util.Map&lt;String,JSONObject&gt; found=new java.util.LinkedHashMap&lt;&gt;();</span>
<span class="nc" id="L302"> try(Connection c=HmiDb.open();PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L303"> &quot;SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID FROM &quot; + schema() + &quot;HMI_APP_PAGE ORDER BY SORT_ORDER,TITLE&quot;);</span>
<span class="nc" id="L304"> ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L305" title="All 2 branches missed."> while(rs.next()) {</span>
<span class="nc" id="L306"> String parent=rs.getString(7);</span>
<span class="nc" id="L307"> JSONObject page=new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;key&quot;,rs.getString(2)).put(&quot;title&quot;,rs.getString(3))</span>
<span class="nc bnc" id="L308" title="All 2 branches missed."> .put(&quot;menuGroup&quot;,rs.getString(4)).put(&quot;order&quot;,rs.getInt(5)).put(&quot;enabled&quot;,rs.getInt(6)==1)</span>
<span class="nc bnc" id="L309" title="All 2 branches missed."> .put(&quot;parentPageId&quot;,parent==null?JSONObject.NULL:parent);</span>
<span class="nc" id="L310"> found.put(page.getString(&quot;id&quot;),page);</span>
<span class="nc" id="L311"> }</span>
<span class="nc bnc" id="L312" title="All 6 branches missed."> for(JSONObject page:found.values()) if(designer()||visiblePage(page.getString(&quot;id&quot;),found))out.put(page);</span>
<span class="nc" id="L313"> return json(out);</span>
<span class="nc" id="L314"> }catch(SQLException e){return dbError(e);}</span>
<span class="nc bnc" id="L302" title="All 2 branches missed."> Response r = denied(true); if (r!=null) return r;</span>
<span class="nc" id="L303"> JSONArray out = new JSONArray();</span>
<span class="nc" id="L304"> java.util.Map&lt;String,JSONObject&gt; found=new java.util.LinkedHashMap&lt;&gt;();</span>
<span class="nc" id="L305"> try(Connection c=HmiDb.open();PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L306"> &quot;SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID FROM &quot; + schema() + &quot;HMI_APP_PAGE ORDER BY SORT_ORDER,TITLE&quot;);</span>
<span class="nc" id="L307"> ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L308" title="All 2 branches missed."> while(rs.next()) {</span>
<span class="nc" id="L309"> String parent=rs.getString(7);</span>
<span class="nc" id="L310"> JSONObject page=new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;key&quot;,rs.getString(2)).put(&quot;title&quot;,rs.getString(3))</span>
<span class="nc bnc" id="L311" title="All 2 branches missed."> .put(&quot;menuGroup&quot;,rs.getString(4)).put(&quot;order&quot;,rs.getInt(5)).put(&quot;enabled&quot;,rs.getInt(6)==1)</span>
<span class="nc bnc" id="L312" title="All 2 branches missed."> .put(&quot;parentPageId&quot;,parent==null?JSONObject.NULL:parent);</span>
<span class="nc" id="L313"> found.put(page.getString(&quot;id&quot;),page);</span>
<span class="nc" id="L314"> }</span>
<span class="nc bnc" id="L315" title="All 6 branches missed."> for(JSONObject page:found.values()) if(designer()||visiblePage(page.getString(&quot;id&quot;),found))out.put(page);</span>
<span class="nc" id="L316"> return json(out);</span>
<span class="nc" id="L317"> }catch(SQLException e){return dbError(e);}</span>
}
/** Parent references are validated server-side to prevent broken or cyclic navigation. */
private static String parentId(JSONObject input) {
<span class="nc bnc" id="L318" title="All 4 branches missed."> if (input.isNull(&quot;parentPageId&quot;) || !input.has(&quot;parentPageId&quot;)) return null;</span>
<span class="nc" id="L319"> String value = input.getString(&quot;parentPageId&quot;).trim();</span>
<span class="nc bnc" id="L320" title="All 2 branches missed."> if (value.isEmpty()) return null;</span>
<span class="nc bnc" id="L321" title="All 2 branches missed."> if (!legalKey(value)) throw new JSONException(&quot;Invalid parentPageId&quot;);</span>
<span class="nc" id="L322"> return value;</span>
<span class="nc bnc" id="L321" title="All 4 branches missed."> if (input.isNull(&quot;parentPageId&quot;) || !input.has(&quot;parentPageId&quot;)) return null;</span>
<span class="nc" id="L322"> String value = input.getString(&quot;parentPageId&quot;).trim();</span>
<span class="nc bnc" id="L323" title="All 2 branches missed."> if (value.isEmpty()) return null;</span>
<span class="nc bnc" id="L324" title="All 2 branches missed."> if (!legalKey(value)) throw new JSONException(&quot;Invalid parentPageId&quot;);</span>
<span class="nc" id="L325"> return value;</span>
}
private static String pageMenu(JSONObject input) {
<span class="nc" id="L325"> String menu = input.optString(&quot;menuGroup&quot;, &quot;Allgemein&quot;).trim();</span>
<span class="nc bnc" id="L326" title="All 6 branches missed."> if (menu.isEmpty() || menu.length() &gt; 120 || menu.indexOf('/') &gt;= 0)</span>
<span class="nc" id="L327"> throw new JSONException(&quot;Invalid root menu name&quot;);</span>
<span class="nc" id="L328"> return menu;</span>
<span class="nc" id="L328"> String menu = input.optString(&quot;menuGroup&quot;, &quot;Allgemein&quot;).trim();</span>
<span class="nc bnc" id="L329" title="All 6 branches missed."> if (menu.isEmpty() || menu.length() &gt; 120 || menu.indexOf('/') &gt;= 0)</span>
<span class="nc" id="L330"> throw new JSONException(&quot;Invalid root menu name&quot;);</span>
<span class="nc" id="L331"> return menu;</span>
}
private static String parentMenu(Connection c, String parentId) throws SQLException {
<span class="nc" id="L331"> try (PreparedStatement ps=c.prepareStatement(&quot;SELECT MENU_GROUP FROM &quot; + schema() + &quot;HMI_APP_PAGE WHERE ID=?&quot;)) {</span>
<span class="nc" id="L332"> ps.setString(1,parentId);</span>
<span class="nc" id="L333"> try (ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L334" title="All 2 branches missed."> return rs.next() ? rs.getString(1) : null;</span>
<span class="nc" id="L334"> try (PreparedStatement ps=c.prepareStatement(&quot;SELECT MENU_GROUP FROM &quot; + schema() + &quot;HMI_APP_PAGE WHERE ID=?&quot;)) {</span>
<span class="nc" id="L335"> ps.setString(1,parentId);</span>
<span class="nc" id="L336"> try (ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L337" title="All 2 branches missed."> return rs.next() ? rs.getString(1) : null;</span>
}
}
}
private static String parentOf(Connection c, String id) throws SQLException {
<span class="nc" id="L339"> try (PreparedStatement ps=c.prepareStatement(&quot;SELECT PARENT_PAGE_ID FROM &quot; + schema() + &quot;HMI_APP_PAGE WHERE ID=?&quot;)) {</span>
<span class="nc" id="L340"> ps.setString(1,id);</span>
<span class="nc" id="L341"> try (ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L342" title="All 2 branches missed."> return rs.next() ? rs.getString(1) : null;</span>
<span class="nc" id="L342"> try (PreparedStatement ps=c.prepareStatement(&quot;SELECT PARENT_PAGE_ID FROM &quot; + schema() + &quot;HMI_APP_PAGE WHERE ID=?&quot;)) {</span>
<span class="nc" id="L343"> ps.setString(1,id);</span>
<span class="nc" id="L344"> try (ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L345" title="All 2 branches missed."> return rs.next() ? rs.getString(1) : null;</span>
}
}
}
private static void validatePageParent(Connection c, String currentId, String parent) throws SQLException {
<span class="nc bnc" id="L347" title="All 2 branches missed."> if (parent == null) return;</span>
<span class="nc" id="L348"> java.util.Set&lt;String&gt; visited = new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L349"> String candidate = parent;</span>
<span class="nc bnc" id="L350" title="All 2 branches missed."> for (int depth=0;candidate!=null;depth++) {</span>
<span class="nc bnc" id="L351" title="All 6 branches missed."> if (depth &gt; 32 || !visited.add(candidate) || candidate.equals(currentId))</span>
<span class="nc" id="L352"> throw new JSONException(&quot;Navigation cycle or depth limit detected&quot;);</span>
<span class="nc bnc" id="L353" title="All 2 branches missed."> if (parentMenu(c,candidate)==null)</span>
<span class="nc" id="L354"> throw new JSONException(&quot;Parent page not found&quot;);</span>
<span class="nc" id="L355"> candidate = parentOf(c,candidate);</span>
<span class="nc bnc" id="L350" title="All 2 branches missed."> if (parent == null) return;</span>
<span class="nc" id="L351"> java.util.Set&lt;String&gt; visited = new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L352"> String candidate = parent;</span>
<span class="nc bnc" id="L353" title="All 2 branches missed."> for (int depth=0;candidate!=null;depth++) {</span>
<span class="nc bnc" id="L354" title="All 6 branches missed."> if (depth &gt; 32 || !visited.add(candidate) || candidate.equals(currentId))</span>
<span class="nc" id="L355"> throw new JSONException(&quot;Navigation cycle or depth limit detected&quot;);</span>
<span class="nc bnc" id="L356" title="All 2 branches missed."> if (parentMenu(c,candidate)==null)</span>
<span class="nc" id="L357"> throw new JSONException(&quot;Parent page not found&quot;);</span>
<span class="nc" id="L358"> candidate = parentOf(c,candidate);</span>
}
<span class="nc" id="L357"> }</span>
<span class="nc" id="L360"> }</span>
/** Propagate the root-menu name through all descendants when a page changes group. */
private static void updateDescendantMenus(Connection c, String rootId, String menu) throws SQLException {
<span class="nc" id="L360"> java.util.Set&lt;String&gt; seen = new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L361"> java.util.ArrayDeque&lt;String&gt; pending = new java.util.ArrayDeque&lt;&gt;();</span>
<span class="nc" id="L362"> pending.add(rootId);seen.add(rootId);</span>
<span class="nc bnc" id="L363" title="All 2 branches missed."> while (!pending.isEmpty()) {</span>
<span class="nc" id="L364"> String current=pending.remove();</span>
<span class="nc" id="L365"> try(PreparedStatement ps=c.prepareStatement(&quot;SELECT ID FROM &quot; + schema() + &quot;HMI_APP_PAGE WHERE PARENT_PAGE_ID=?&quot;)) {</span>
<span class="nc" id="L366"> ps.setString(1,current);</span>
<span class="nc" id="L367"> try(ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L368" title="All 2 branches missed."> while(rs.next()) {</span>
<span class="nc" id="L369"> String child=rs.getString(1);</span>
<span class="nc bnc" id="L370" title="All 2 branches missed."> if (!seen.add(child)) throw new SQLException(&quot;Navigation cycle in stored page tree&quot;);</span>
<span class="nc bnc" id="L371" title="All 2 branches missed."> if (seen.size()&gt;1000) throw new SQLException(&quot;Too many navigation pages&quot;);</span>
<span class="nc" id="L372"> pending.add(child);</span>
<span class="nc" id="L373"> }</span>
<span class="nc" id="L363"> java.util.Set&lt;String&gt; seen = new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L364"> java.util.ArrayDeque&lt;String&gt; pending = new java.util.ArrayDeque&lt;&gt;();</span>
<span class="nc" id="L365"> pending.add(rootId);seen.add(rootId);</span>
<span class="nc bnc" id="L366" title="All 2 branches missed."> while (!pending.isEmpty()) {</span>
<span class="nc" id="L367"> String current=pending.remove();</span>
<span class="nc" id="L368"> try(PreparedStatement ps=c.prepareStatement(&quot;SELECT ID FROM &quot; + schema() + &quot;HMI_APP_PAGE WHERE PARENT_PAGE_ID=?&quot;)) {</span>
<span class="nc" id="L369"> ps.setString(1,current);</span>
<span class="nc" id="L370"> try(ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L371" title="All 2 branches missed."> while(rs.next()) {</span>
<span class="nc" id="L372"> String child=rs.getString(1);</span>
<span class="nc bnc" id="L373" title="All 2 branches missed."> if (!seen.add(child)) throw new SQLException(&quot;Navigation cycle in stored page tree&quot;);</span>
<span class="nc bnc" id="L374" title="All 2 branches missed."> if (seen.size()&gt;1000) throw new SQLException(&quot;Too many navigation pages&quot;);</span>
<span class="nc" id="L375"> pending.add(child);</span>
<span class="nc" id="L376"> }</span>
}
}
<span class="nc bnc" id="L376" title="All 2 branches missed."> if(!current.equals(rootId)) {</span>
<span class="nc" id="L377"> try(PreparedStatement ps=c.prepareStatement(&quot;UPDATE &quot; + schema() + &quot;HMI_APP_PAGE SET MENU_GROUP=? WHERE ID=?&quot;)) {</span>
<span class="nc" id="L378"> ps.setString(1,menu);ps.setString(2,current);ps.executeUpdate();</span>
<span class="nc bnc" id="L379" title="All 2 branches missed."> if(!current.equals(rootId)) {</span>
<span class="nc" id="L380"> try(PreparedStatement ps=c.prepareStatement(&quot;UPDATE &quot; + schema() + &quot;HMI_APP_PAGE SET MENU_GROUP=? WHERE ID=?&quot;)) {</span>
<span class="nc" id="L381"> ps.setString(1,menu);ps.setString(2,current);ps.executeUpdate();</span>
}
}
<span class="nc" id="L381"> }</span>
<span class="nc" id="L382"> }</span>
<span class="nc" id="L384"> }</span>
<span class="nc" id="L385"> }</span>
@POST @Path(&quot;pages&quot;) @Consumes(MediaType.APPLICATION_JSON)
public Response createPage(String body) {
<span class="nc bnc" id="L385" title="All 2 branches missed."> Response r=writeAllowed(designer()); if(r!=null)return r;</span>
<span class="nc bnc" id="L388" title="All 2 branches missed."> Response r=writeAllowed(designer()); if(r!=null)return r;</span>
try {
<span class="nc" id="L387"> JSONObject input=parse(body);</span>
<span class="nc bnc" id="L388" title="All 2 branches missed."> String key=need(input,&quot;key&quot;,120); if(!legalKey(key)) return err(400,&quot;Invalid page key&quot;);</span>
<span class="nc" id="L389"> String title=need(input,&quot;title&quot;,255), pageId=id();</span>
<span class="nc" id="L390"> String menu=pageMenu(input), parent=parentId(input);</span>
<span class="nc" id="L391"> int order=bounded(input,&quot;order&quot;,0,100000,0);</span>
<span class="nc" id="L392"> try (Connection c=HmiDb.open()) {</span>
<span class="nc" id="L393"> c.setAutoCommit(false);</span>
<span class="nc" id="L390"> JSONObject input=parse(body);</span>
<span class="nc bnc" id="L391" title="All 2 branches missed."> String key=need(input,&quot;key&quot;,120); if(!legalKey(key)) return err(400,&quot;Invalid page key&quot;);</span>
<span class="nc" id="L392"> String title=need(input,&quot;title&quot;,255), pageId=id();</span>
<span class="nc" id="L393"> String menu=pageMenu(input), parent=parentId(input);</span>
<span class="nc" id="L394"> int order=bounded(input,&quot;order&quot;,0,100000,0);</span>
<span class="nc" id="L395"> try (Connection c=HmiDb.open()) {</span>
<span class="nc" id="L396"> c.setAutoCommit(false);</span>
try {
<span class="nc" id="L395"> validatePageParent(c,pageId,parent);</span>
<span class="nc bnc" id="L396" title="All 2 branches missed."> if(parent!=null)menu=parentMenu(c,parent);</span>
<span class="nc" id="L397"> try (PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L398"> &quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_PAGE(ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID) VALUES (?,?,?,?,?,1,?)&quot;)) {</span>
<span class="nc" id="L399"> ps.setString(1,pageId);ps.setString(2,key);ps.setString(3,title);</span>
<span class="nc" id="L400"> ps.setString(4,menu);ps.setInt(5,order);ps.setString(6,parent);ps.executeUpdate();</span>
<span class="nc" id="L398"> validatePageParent(c,pageId,parent);</span>
<span class="nc bnc" id="L399" title="All 2 branches missed."> if(parent!=null)menu=parentMenu(c,parent);</span>
<span class="nc" id="L400"> try (PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L401"> &quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_PAGE(ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID) VALUES (?,?,?,?,?,1,?)&quot;)) {</span>
<span class="nc" id="L402"> ps.setString(1,pageId);ps.setString(2,key);ps.setString(3,title);</span>
<span class="nc" id="L403"> ps.setString(4,menu);ps.setInt(5,order);ps.setString(6,parent);ps.executeUpdate();</span>
}
<span class="nc" id="L402"> audit(c,&quot;page.created&quot;,pageId,key);c.commit();</span>
<span class="nc" id="L403"> } catch (Exception ex) {c.rollback();throw ex;}</span>
<span class="nc" id="L405"> audit(c,&quot;page.created&quot;,pageId,key);c.commit();</span>
<span class="nc" id="L406"> } catch (Exception ex) {c.rollback();throw ex;}</span>
}
<span class="nc" id="L405"> return json(new JSONObject().put(&quot;id&quot;,pageId).put(&quot;key&quot;,key));</span>
<span class="nc" id="L406"> } catch (JSONException e) { return err(400,e.getMessage()); } catch (SQLException e) { return dbError(e); }</span>
<span class="nc" id="L407"> catch (Exception e) { return err(400,&quot;Cannot create page&quot;); }</span>
<span class="nc" id="L408"> return json(new JSONObject().put(&quot;id&quot;,pageId).put(&quot;key&quot;,key));</span>
<span class="nc" id="L409"> } catch (JSONException e) { return err(400,e.getMessage()); } catch (SQLException e) { return dbError(e); }</span>
<span class="nc" id="L410"> catch (Exception e) { return err(400,&quot;Cannot create page&quot;); }</span>
}
@PUT @Path(&quot;pages/{pageId}&quot;) @Consumes(MediaType.APPLICATION_JSON)
public Response updatePage(@PathParam(&quot;pageId&quot;) String pageId, String body) {
<span class="nc bnc" id="L411" title="All 2 branches missed."> Response r=writeAllowed(designer()); if(r!=null)return r;</span>
<span class="nc bnc" id="L414" title="All 2 branches missed."> Response r=writeAllowed(designer()); if(r!=null)return r;</span>
try {
<span class="nc" id="L413"> JSONObject input=parse(body);</span>
<span class="nc" id="L414"> String title=need(input,&quot;title&quot;,255), menu=pageMenu(input), parent=parentId(input);</span>
<span class="nc" id="L415"> int order=bounded(input,&quot;order&quot;,0,100000,0);</span>
<span class="nc" id="L416"> boolean enabled=input.optBoolean(&quot;enabled&quot;,true);</span>
<span class="nc" id="L417"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L418"> c.setAutoCommit(false);</span>
<span class="nc" id="L416"> JSONObject input=parse(body);</span>
<span class="nc" id="L417"> String title=need(input,&quot;title&quot;,255), menu=pageMenu(input), parent=parentId(input);</span>
<span class="nc" id="L418"> int order=bounded(input,&quot;order&quot;,0,100000,0);</span>
<span class="nc" id="L419"> boolean enabled=input.optBoolean(&quot;enabled&quot;,true);</span>
<span class="nc" id="L420"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L421"> c.setAutoCommit(false);</span>
try {
<span class="nc bnc" id="L420" title="All 2 branches missed."> if (!pageExists(c,pageId)) {c.rollback();return err(404,&quot;Page not found&quot;);}</span>
<span class="nc" id="L421"> validatePageParent(c,pageId,parent);</span>
<span class="nc bnc" id="L422" title="All 2 branches missed."> if(parent!=null)menu=parentMenu(c,parent);</span>
<span class="nc" id="L423"> try(PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L424"> &quot;UPDATE &quot;+schema()+&quot;HMI_APP_PAGE SET TITLE=?,MENU_GROUP=?,SORT_ORDER=?,ENABLED=?,PARENT_PAGE_ID=? WHERE ID=?&quot;)) {</span>
<span class="nc" id="L425"> ps.setString(1,title);ps.setString(2,menu);ps.setInt(3,order);</span>
<span class="nc bnc" id="L426" title="All 2 branches missed."> ps.setInt(4,enabled?1:0);ps.setString(5,parent);ps.setString(6,pageId);</span>
<span class="nc" id="L427"> ps.executeUpdate();</span>
<span class="nc bnc" id="L423" title="All 2 branches missed."> if (!pageExists(c,pageId)) {c.rollback();return err(404,&quot;Page not found&quot;);}</span>
<span class="nc" id="L424"> validatePageParent(c,pageId,parent);</span>
<span class="nc bnc" id="L425" title="All 2 branches missed."> if(parent!=null)menu=parentMenu(c,parent);</span>
<span class="nc" id="L426"> try(PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L427"> &quot;UPDATE &quot;+schema()+&quot;HMI_APP_PAGE SET TITLE=?,MENU_GROUP=?,SORT_ORDER=?,ENABLED=?,PARENT_PAGE_ID=? WHERE ID=?&quot;)) {</span>
<span class="nc" id="L428"> ps.setString(1,title);ps.setString(2,menu);ps.setInt(3,order);</span>
<span class="nc bnc" id="L429" title="All 2 branches missed."> ps.setInt(4,enabled?1:0);ps.setString(5,parent);ps.setString(6,pageId);</span>
<span class="nc" id="L430"> ps.executeUpdate();</span>
}
<span class="nc" id="L429"> updateDescendantMenus(c,pageId,menu);</span>
<span class="nc" id="L430"> audit(c,&quot;page.updated&quot;,pageId,title);c.commit();</span>
<span class="nc" id="L431"> }catch(Exception ex){c.rollback();throw ex;}</span>
<span class="nc bnc" id="L432" title="All 2 branches missed."> }</span>
<span class="nc" id="L433"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L434"> } catch (JSONException e) { return err(400,e.getMessage()); } catch (SQLException e) { return dbError(e); }</span>
<span class="nc" id="L435"> catch (Exception e) { return err(400,&quot;Cannot update page&quot;); }</span>
<span class="nc" id="L432"> updateDescendantMenus(c,pageId,menu);</span>
<span class="nc" id="L433"> audit(c,&quot;page.updated&quot;,pageId,title);c.commit();</span>
<span class="nc" id="L434"> }catch(Exception ex){c.rollback();throw ex;}</span>
<span class="nc bnc" id="L435" title="All 2 branches missed."> }</span>
<span class="nc" id="L436"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L437"> } catch (JSONException e) { return err(400,e.getMessage()); } catch (SQLException e) { return dbError(e); }</span>
<span class="nc" id="L438"> catch (Exception e) { return err(400,&quot;Cannot update page&quot;); }</span>
}
/** Rename a root menu without changing individual pages' parent relationships. */
@PUT @Path(&quot;navigation/root&quot;) @Consumes(MediaType.APPLICATION_JSON)
public Response renameRootMenu(String body) {
<span class="nc bnc" id="L440" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
<span class="nc bnc" id="L443" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
try {
<span class="nc" id="L442"> JSONObject input=parse(body);</span>
<span class="nc" id="L443"> String previous=need(input,&quot;previous&quot;,120), next=need(input,&quot;next&quot;,120);</span>
<span class="nc bnc" id="L444" title="All 2 branches missed."> if(!next.equals(pageMenu(new JSONObject().put(&quot;menuGroup&quot;,next))))return err(400,&quot;Invalid menu&quot;);</span>
<span class="nc" id="L445"> try(Connection c=HmiDb.open();PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L446"> &quot;UPDATE &quot;+schema()+&quot;HMI_APP_PAGE SET MENU_GROUP=? WHERE MENU_GROUP=?&quot;)) {</span>
<span class="nc" id="L447"> ps.setString(1,next);ps.setString(2,previous);</span>
<span class="nc" id="L448"> int count=ps.executeUpdate();</span>
<span class="nc bnc" id="L449" title="All 2 branches missed."> if(count==0)return err(404,&quot;Root menu not found&quot;);</span>
<span class="nc" id="L450"> audit(c,&quot;root.renamed&quot;,previous,next);</span>
<span class="nc" id="L451"> return json(new JSONObject().put(&quot;updatedPages&quot;,count));</span>
<span class="nc bnc" id="L452" title="All 4 branches missed."> }</span>
<span class="nc" id="L453"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L445"> JSONObject input=parse(body);</span>
<span class="nc" id="L446"> String previous=need(input,&quot;previous&quot;,120), next=need(input,&quot;next&quot;,120);</span>
<span class="nc bnc" id="L447" title="All 2 branches missed."> if(!next.equals(pageMenu(new JSONObject().put(&quot;menuGroup&quot;,next))))return err(400,&quot;Invalid menu&quot;);</span>
<span class="nc" id="L448"> try(Connection c=HmiDb.open();PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L449"> &quot;UPDATE &quot;+schema()+&quot;HMI_APP_PAGE SET MENU_GROUP=? WHERE MENU_GROUP=?&quot;)) {</span>
<span class="nc" id="L450"> ps.setString(1,next);ps.setString(2,previous);</span>
<span class="nc" id="L451"> int count=ps.executeUpdate();</span>
<span class="nc bnc" id="L452" title="All 2 branches missed."> if(count==0)return err(404,&quot;Root menu not found&quot;);</span>
<span class="nc" id="L453"> audit(c,&quot;root.renamed&quot;,previous,next);</span>
<span class="nc" id="L454"> return json(new JSONObject().put(&quot;updatedPages&quot;,count));</span>
<span class="nc bnc" id="L455" title="All 4 branches missed."> }</span>
<span class="nc" id="L456"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
}
@GET @Path(&quot;pages/{pageId}/templates&quot;)
public Response templates(@PathParam(&quot;pageId&quot;)String pageId) {
<span class="nc bnc" id="L457" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
<span class="nc" id="L458"> JSONArray a=new JSONArray();</span>
<span class="nc" id="L459"> try (Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(&quot;SELECT ID,VERSION_NO,STATUS,UPDATED_BY FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE PAGE_ID=? ORDER BY VERSION_NO DESC&quot;)) {</span>
<span class="nc bnc" id="L460" title="All 2 branches missed."> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){while(rs.next())a.put(new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;status&quot;,rs.getString(3)).put(&quot;author&quot;,rs.getString(4)));}</span>
<span class="nc" id="L461"> return json(a);</span>
<span class="nc" id="L462"> }catch(SQLException e){return dbError(e);}</span>
<span class="nc bnc" id="L460" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
<span class="nc" id="L461"> JSONArray a=new JSONArray();</span>
<span class="nc" id="L462"> try (Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(&quot;SELECT ID,VERSION_NO,STATUS,UPDATED_BY FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE PAGE_ID=? ORDER BY VERSION_NO DESC&quot;)) {</span>
<span class="nc bnc" id="L463" title="All 2 branches missed."> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){while(rs.next())a.put(new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;status&quot;,rs.getString(3)).put(&quot;author&quot;,rs.getString(4)));}</span>
<span class="nc" id="L464"> return json(a);</span>
<span class="nc" id="L465"> }catch(SQLException e){return dbError(e);}</span>
}
@POST @Path(&quot;pages/{pageId}/templates&quot;) @Consumes(MediaType.APPLICATION_JSON)
public Response createTemplate(@PathParam(&quot;pageId&quot;) String pageId,String body) {
<span class="nc bnc" id="L466" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
<span class="nc bnc" id="L469" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
try {
<span class="nc" id="L468"> JSONObject layout=validateTemplate(parse(body));</span>
<span class="nc bnc" id="L469" title="All 4 branches missed."> if (!scriptEditor() &amp;&amp; !scripts(layout).isEmpty())</span>
<span class="nc" id="L470"> return err(403, &quot;JavaScript- und CheerpJ-Widgets erfordern hmi-script-editor&quot;);</span>
<span class="nc" id="L471"> try(Connection c=HmiDb.open()) {</span>
<span class="nc bnc" id="L472" title="All 2 branches missed."> if(!pageExists(c,pageId))return err(404,&quot;Page not found&quot;);</span>
<span class="nc" id="L473"> int version=1;</span>
<span class="nc" id="L474"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT MAX(VERSION_NO) FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE PAGE_ID=?&quot;)){</span>
<span class="nc bnc" id="L475" title="All 2 branches missed."> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){if(rs.next())version=rs.getInt(1)+1;}</span>
<span class="nc" id="L471"> JSONObject layout=validateTemplate(parse(body));</span>
<span class="nc bnc" id="L472" title="All 4 branches missed."> if (!scriptEditor() &amp;&amp; !scripts(layout).isEmpty())</span>
<span class="nc" id="L473"> return err(403, &quot;JavaScript- und CheerpJ-Widgets erfordern hmi-script-editor&quot;);</span>
<span class="nc" id="L474"> try(Connection c=HmiDb.open()) {</span>
<span class="nc bnc" id="L475" title="All 2 branches missed."> if(!pageExists(c,pageId))return err(404,&quot;Page not found&quot;);</span>
<span class="nc" id="L476"> int version=1;</span>
<span class="nc" id="L477"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT MAX(VERSION_NO) FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE PAGE_ID=?&quot;)){</span>
<span class="nc bnc" id="L478" title="All 2 branches missed."> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){if(rs.next())version=rs.getInt(1)+1;}</span>
}
<span class="nc" id="L477"> String templateId=id();</span>
<span class="nc" id="L478"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_TEMPLATE(ID,PAGE_ID,VERSION_NO,STATUS,LAYOUT_JSON,UPDATED_BY,UPDATED_AT) VALUES (?,?,?,?,?,?,?)&quot;)) {</span>
<span class="nc" id="L479"> p.setString(1,templateId);p.setString(2,pageId);p.setInt(3,version);p.setString(4,&quot;DRAFT&quot;);clob(p,5,layout.toString());</span>
<span class="nc" id="L480"> p.setString(6,user());p.setTimestamp(7,Timestamp.from(Instant.now()));p.executeUpdate();</span>
<span class="nc" id="L480"> String templateId=id();</span>
<span class="nc" id="L481"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_TEMPLATE(ID,PAGE_ID,VERSION_NO,STATUS,LAYOUT_JSON,UPDATED_BY,UPDATED_AT) VALUES (?,?,?,?,?,?,?)&quot;)) {</span>
<span class="nc" id="L482"> p.setString(1,templateId);p.setString(2,pageId);p.setInt(3,version);p.setString(4,&quot;DRAFT&quot;);clob(p,5,layout.toString());</span>
<span class="nc" id="L483"> p.setString(6,user());p.setTimestamp(7,Timestamp.from(Instant.now()));p.executeUpdate();</span>
}
<span class="nc" id="L482"> audit(c,&quot;template.created&quot;,templateId,pageId);</span>
<span class="nc" id="L483"> return json(new JSONObject().put(&quot;id&quot;,templateId).put(&quot;version&quot;,version).put(&quot;status&quot;,&quot;DRAFT&quot;));</span>
<span class="nc bnc" id="L484" title="All 2 branches missed."> }</span>
<span class="nc" id="L485"> } catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L485"> audit(c,&quot;template.created&quot;,templateId,pageId);</span>
<span class="nc" id="L486"> return json(new JSONObject().put(&quot;id&quot;,templateId).put(&quot;version&quot;,version).put(&quot;status&quot;,&quot;DRAFT&quot;));</span>
<span class="nc bnc" id="L487" title="All 2 branches missed."> }</span>
<span class="nc" id="L488"> } catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
}
@PUT @Path(&quot;templates/{templateId}&quot;) @Consumes(MediaType.APPLICATION_JSON)
public Response saveTemplate(@PathParam(&quot;templateId&quot;)String templateId,String body){
<span class="nc bnc" id="L489" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
<span class="nc bnc" id="L492" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
try {
<span class="nc" id="L491"> JSONObject layout=validateTemplate(parse(body));</span>
<span class="nc" id="L492"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L493"> c.setAutoCommit(false);</span>
<span class="nc" id="L494"> JSONObject layout=validateTemplate(parse(body));</span>
<span class="nc" id="L495"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L496"> c.setAutoCommit(false);</span>
try {
<span class="nc" id="L495"> String current = null;</span>
<span class="nc" id="L498"> String current = null;</span>
// Lock the draft while comparing its scripts and persisting the new version.
<span class="nc" id="L497"> boolean oracle = HmiDb.isOracle();</span>
<span class="nc bnc" id="L498" title="All 2 branches missed."> String sql = oracle</span>
<span class="nc" id="L499"> ? &quot;SELECT LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=? AND STATUS='DRAFT' FOR UPDATE&quot;</span>
<span class="nc" id="L500"> : &quot;SELECT LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WITH (UPDLOCK, ROWLOCK) WHERE ID=? AND STATUS='DRAFT'&quot;;</span>
<span class="nc" id="L501"> try(PreparedStatement locked=c.prepareStatement(sql)) {</span>
<span class="nc" id="L502"> locked.setString(1,templateId);</span>
<span class="nc" id="L503"> try(ResultSet rs=locked.executeQuery()) {</span>
<span class="nc bnc" id="L504" title="All 2 branches missed."> if(rs.next())current=clob(rs,&quot;LAYOUT_JSON&quot;);</span>
<span class="nc" id="L500"> boolean oracle = HmiDb.isOracle();</span>
<span class="nc bnc" id="L501" title="All 2 branches missed."> String sql = oracle</span>
<span class="nc" id="L502"> ? &quot;SELECT LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=? AND STATUS='DRAFT' FOR UPDATE&quot;</span>
<span class="nc" id="L503"> : &quot;SELECT LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WITH (UPDLOCK, ROWLOCK) WHERE ID=? AND STATUS='DRAFT'&quot;;</span>
<span class="nc" id="L504"> try(PreparedStatement locked=c.prepareStatement(sql)) {</span>
<span class="nc" id="L505"> locked.setString(1,templateId);</span>
<span class="nc" id="L506"> try(ResultSet rs=locked.executeQuery()) {</span>
<span class="nc bnc" id="L507" title="All 2 branches missed."> if(rs.next())current=clob(rs,&quot;LAYOUT_JSON&quot;);</span>
}
}
<span class="nc bnc" id="L507" title="All 2 branches missed."> if(current==null) { c.rollback();return err(409,&quot;Template not a draft or not found&quot;); }</span>
<span class="nc bnc" id="L508" title="All 4 branches missed."> if (!scriptEditor() &amp;&amp; scriptChanges(objectOrEmpty(current),layout)) {</span>
<span class="nc" id="L509"> c.rollback();return err(403,&quot;Code- oder JNLP-Änderungen erfordern die Rolle hmi-script-editor&quot;);</span>
<span class="nc bnc" id="L510" title="All 2 branches missed."> if(current==null) { c.rollback();return err(409,&quot;Template not a draft or not found&quot;); }</span>
<span class="nc bnc" id="L511" title="All 4 branches missed."> if (!scriptEditor() &amp;&amp; scriptChanges(objectOrEmpty(current),layout)) {</span>
<span class="nc" id="L512"> c.rollback();return err(403,&quot;Code- oder JNLP-Änderungen erfordern die Rolle hmi-script-editor&quot;);</span>
}
<span class="nc" id="L511"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET LAYOUT_JSON=?,UPDATED_BY=?,UPDATED_AT=? WHERE ID=? AND STATUS='DRAFT'&quot;)) {</span>
<span class="nc" id="L512"> clob(p,1,layout.toString());p.setString(2,user());p.setTimestamp(3,Timestamp.from(Instant.now()));p.setString(4,templateId);</span>
<span class="nc bnc" id="L513" title="All 2 branches missed."> if(p.executeUpdate()==0) {c.rollback();return err(409,&quot;Template not a draft or not found&quot;);}</span>
<span class="nc bnc" id="L514" title="All 2 branches missed."> }</span>
<span class="nc" id="L515"> audit(c,&quot;template.saved&quot;,templateId,&quot;&quot;);</span>
<span class="nc bnc" id="L516" title="All 2 branches missed."> if(scriptChanges(objectOrEmpty(current),layout))audit(c,&quot;widget.script.edited&quot;,templateId,&quot;Code or JNLP configuration updated&quot;);</span>
<span class="nc" id="L517"> c.commit();</span>
<span class="nc" id="L518"> } catch(SQLException | RuntimeException ex) {c.rollback();throw ex;}</span>
<span class="nc bnc" id="L519" title="All 6 branches missed."> }</span>
<span class="nc" id="L520"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L521"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L514"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET LAYOUT_JSON=?,UPDATED_BY=?,UPDATED_AT=? WHERE ID=? AND STATUS='DRAFT'&quot;)) {</span>
<span class="nc" id="L515"> clob(p,1,layout.toString());p.setString(2,user());p.setTimestamp(3,Timestamp.from(Instant.now()));p.setString(4,templateId);</span>
<span class="nc bnc" id="L516" title="All 2 branches missed."> if(p.executeUpdate()==0) {c.rollback();return err(409,&quot;Template not a draft or not found&quot;);}</span>
<span class="nc bnc" id="L517" title="All 2 branches missed."> }</span>
<span class="nc" id="L518"> audit(c,&quot;template.saved&quot;,templateId,&quot;&quot;);</span>
<span class="nc bnc" id="L519" title="All 2 branches missed."> if(scriptChanges(objectOrEmpty(current),layout))audit(c,&quot;widget.script.edited&quot;,templateId,&quot;Code or JNLP configuration updated&quot;);</span>
<span class="nc" id="L520"> c.commit();</span>
<span class="nc" id="L521"> } catch(SQLException | RuntimeException ex) {c.rollback();throw ex;}</span>
<span class="nc bnc" id="L522" title="All 6 branches missed."> }</span>
<span class="nc" id="L523"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L524"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
}
@GET @Path(&quot;templates/{templateId}&quot;)
public Response getTemplate(@PathParam(&quot;templateId&quot;)String templateId) {
<span class="nc bnc" id="L525" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
<span class="nc" id="L526"> try(Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(&quot;SELECT ID,PAGE_ID,VERSION_NO,STATUS,LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?&quot;)){</span>
<span class="nc" id="L527"> p.setString(1,templateId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L528" title="All 2 branches missed."> if(!rs.next())return err(404,&quot;Template not found&quot;);</span>
<span class="nc" id="L529"> return json(new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;pageId&quot;,rs.getString(2))</span>
<span class="nc" id="L530"> .put(&quot;version&quot;,rs.getInt(3)).put(&quot;status&quot;,rs.getString(4))</span>
<span class="nc" id="L531"> .put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;))));</span>
<span class="nc bnc" id="L532" title="All 2 branches missed."> }</span>
<span class="nc bnc" id="L533" title="All 4 branches missed."> }catch(SQLException e){return dbError(e);}</span>
<span class="nc bnc" id="L528" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
<span class="nc" id="L529"> try(Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(&quot;SELECT ID,PAGE_ID,VERSION_NO,STATUS,LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?&quot;)){</span>
<span class="nc" id="L530"> p.setString(1,templateId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L531" title="All 2 branches missed."> if(!rs.next())return err(404,&quot;Template not found&quot;);</span>
<span class="nc" id="L532"> return json(new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;pageId&quot;,rs.getString(2))</span>
<span class="nc" id="L533"> .put(&quot;version&quot;,rs.getInt(3)).put(&quot;status&quot;,rs.getString(4))</span>
<span class="nc" id="L534"> .put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;))));</span>
<span class="nc bnc" id="L535" title="All 2 branches missed."> }</span>
<span class="nc bnc" id="L536" title="All 4 branches missed."> }catch(SQLException e){return dbError(e);}</span>
}
@POST @Path(&quot;templates/{templateId}/archive&quot;)
public Response archive(@PathParam(&quot;templateId&quot;)String templateId) {
<span class="nc bnc" id="L537" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc" id="L538"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L539"> try(PreparedStatement chk=c.prepareStatement(&quot;SELECT ID FROM &quot;+schema()+&quot;HMI_APP_ASSIGNMENT WHERE TEMPLATE_ID=?&quot;)) {</span>
<span class="nc bnc" id="L540" title="All 4 branches missed."> chk.setString(1,templateId);try(ResultSet rs=chk.executeQuery()){if(rs.next())return err(409,&quot;Template is assigned; update assignments before archiving&quot;);}</span>
<span class="nc bnc" id="L541" title="All 2 branches missed."> }</span>
<span class="nc" id="L542"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET STATUS='ARCHIVED' WHERE ID=? AND STATUS='PUBLISHED'&quot;)) {</span>
<span class="nc bnc" id="L543" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)return err(409,&quot;Not published or not found&quot;);</span>
<span class="nc bnc" id="L540" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc" id="L541"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L542"> try(PreparedStatement chk=c.prepareStatement(&quot;SELECT ID FROM &quot;+schema()+&quot;HMI_APP_ASSIGNMENT WHERE TEMPLATE_ID=?&quot;)) {</span>
<span class="nc bnc" id="L543" title="All 4 branches missed."> chk.setString(1,templateId);try(ResultSet rs=chk.executeQuery()){if(rs.next())return err(409,&quot;Template is assigned; update assignments before archiving&quot;);}</span>
<span class="nc bnc" id="L544" title="All 2 branches missed."> }</span>
<span class="nc" id="L545"> audit(c,&quot;template.archived&quot;,templateId,&quot;&quot;);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L546" title="All 4 branches missed."> }catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L545"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET STATUS='ARCHIVED' WHERE ID=? AND STATUS='PUBLISHED'&quot;)) {</span>
<span class="nc bnc" id="L546" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)return err(409,&quot;Not published or not found&quot;);</span>
<span class="nc bnc" id="L547" title="All 2 branches missed."> }</span>
<span class="nc" id="L548"> audit(c,&quot;template.archived&quot;,templateId,&quot;&quot;);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L549" title="All 4 branches missed."> }catch(SQLException e){return dbError(e);}</span>
}
@POST @Path(&quot;templates/{templateId}/publish&quot;)
public Response publish(@PathParam(&quot;templateId&quot;)String templateId) {
<span class="nc bnc" id="L550" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc" id="L551"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L552"> c.setAutoCommit(false);</span>
<span class="nc bnc" id="L553" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc" id="L554"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L555"> c.setAutoCommit(false);</span>
try {
String pageId;
<span class="nc" id="L555"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT PAGE_ID,STATUS FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?&quot;)) {</span>
<span class="nc" id="L556"> p.setString(1,templateId);</span>
<span class="nc" id="L557"> try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L558" title="All 2 branches missed."> if(!rs.next()){c.rollback();return err(404,&quot;Template not found&quot;);}</span>
<span class="nc" id="L559"> pageId=rs.getString(1);</span>
<span class="nc bnc" id="L560" title="All 2 branches missed."> if(!&quot;DRAFT&quot;.equals(rs.getString(2))){c.rollback();return err(409,&quot;Only drafts can be published&quot;);}</span>
<span class="nc bnc" id="L561" title="All 4 branches missed."> }</span>
<span class="nc bnc" id="L562" title="All 4 branches missed."> }</span>
<span class="nc" id="L558"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT PAGE_ID,STATUS FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?&quot;)) {</span>
<span class="nc" id="L559"> p.setString(1,templateId);</span>
<span class="nc" id="L560"> try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L561" title="All 2 branches missed."> if(!rs.next()){c.rollback();return err(404,&quot;Template not found&quot;);}</span>
<span class="nc" id="L562"> pageId=rs.getString(1);</span>
<span class="nc bnc" id="L563" title="All 2 branches missed."> if(!&quot;DRAFT&quot;.equals(rs.getString(2))){c.rollback();return err(409,&quot;Only drafts can be published&quot;);}</span>
<span class="nc bnc" id="L564" title="All 4 branches missed."> }</span>
<span class="nc bnc" id="L565" title="All 4 branches missed."> }</span>
// Keep other published templates available for group/station assignments.
// Default page resolves highest published version; explicit assignment stays pinned.
<span class="nc" id="L565"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET STATUS='PUBLISHED' WHERE ID=? AND STATUS='DRAFT'&quot;)){</span>
<span class="nc bnc" id="L566" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)throw new SQLException(&quot;Concurrent template publication&quot;);</span>
<span class="nc" id="L568"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET STATUS='PUBLISHED' WHERE ID=? AND STATUS='DRAFT'&quot;)){</span>
<span class="nc bnc" id="L569" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)throw new SQLException(&quot;Concurrent template publication&quot;);</span>
}
<span class="nc" id="L568"> audit(c,&quot;template.published&quot;,templateId,pageId);c.commit();</span>
<span class="nc" id="L569"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L570"> }catch(Exception ex){c.rollback();throw ex;}</span>
<span class="nc bnc" id="L571" title="All 4 branches missed."> }catch(Exception e){return dbError(e);}</span>
<span class="nc" id="L571"> audit(c,&quot;template.published&quot;,templateId,pageId);c.commit();</span>
<span class="nc" id="L572"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L573"> }catch(Exception ex){c.rollback();throw ex;}</span>
<span class="nc bnc" id="L574" title="All 4 branches missed."> }catch(Exception e){return dbError(e);}</span>
}
@GET @Path(&quot;pages/{pageId}/effective&quot;)
public Response effective(@PathParam(&quot;pageId&quot;)String pageId,@QueryParam(&quot;station&quot;)String station){
<span class="nc bnc" id="L575" title="All 2 branches missed."> Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L576"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L577"> JSONObject page=fetchPage(c,pageId);</span>
<span class="nc bnc" id="L578" title="All 2 branches missed."> if(page==null)return err(404,&quot;Page not found&quot;);</span>
<span class="nc bnc" id="L579" title="All 2 branches missed."> if(!designer()){</span>
<span class="nc" id="L580"> java.util.Set&lt;String&gt; seen=new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L581"> JSONObject ancestor=page;</span>
<span class="nc bnc" id="L582" title="All 2 branches missed."> while(ancestor!=null){</span>
<span class="nc bnc" id="L583" title="All 4 branches missed."> if(!seen.add(ancestor.getString(&quot;id&quot;))||!ancestor.getBoolean(&quot;enabled&quot;))return err(404,&quot;Page not found&quot;);</span>
<span class="nc bnc" id="L584" title="All 2 branches missed."> String parent=ancestor.isNull(&quot;parentPageId&quot;)?null:ancestor.optString(&quot;parentPageId&quot;,null);</span>
<span class="nc bnc" id="L585" title="All 2 branches missed."> ancestor=parent==null?null:fetchPage(c,parent);</span>
<span class="nc bnc" id="L586" title="All 4 branches missed."> if(parent!=null&amp;&amp;ancestor==null)return err(404,&quot;Page not found&quot;);</span>
<span class="nc" id="L587"> }</span>
<span class="nc bnc" id="L578" title="All 2 branches missed."> Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L579"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L580"> JSONObject page=fetchPage(c,pageId);</span>
<span class="nc bnc" id="L581" title="All 2 branches missed."> if(page==null)return err(404,&quot;Page not found&quot;);</span>
<span class="nc bnc" id="L582" title="All 2 branches missed."> if(!designer()){</span>
<span class="nc" id="L583"> java.util.Set&lt;String&gt; seen=new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L584"> JSONObject ancestor=page;</span>
<span class="nc bnc" id="L585" title="All 2 branches missed."> while(ancestor!=null){</span>
<span class="nc bnc" id="L586" title="All 4 branches missed."> if(!seen.add(ancestor.getString(&quot;id&quot;))||!ancestor.getBoolean(&quot;enabled&quot;))return err(404,&quot;Page not found&quot;);</span>
<span class="nc bnc" id="L587" title="All 2 branches missed."> String parent=ancestor.isNull(&quot;parentPageId&quot;)?null:ancestor.optString(&quot;parentPageId&quot;,null);</span>
<span class="nc bnc" id="L588" title="All 2 branches missed."> ancestor=parent==null?null:fetchPage(c,parent);</span>
<span class="nc bnc" id="L589" title="All 4 branches missed."> if(parent!=null&amp;&amp;ancestor==null)return err(404,&quot;Page not found&quot;);</span>
<span class="nc" id="L590"> }</span>
}
<span class="nc" id="L589"> List&lt;String&gt; groups=groups(c,user());</span>
<span class="nc" id="L590"> JSONObject template=resolveTemplate(c,pageId,station,groups);</span>
<span class="nc bnc" id="L591" title="All 2 branches missed."> if(template==null)return err(404,&quot;No published template assigned&quot;);</span>
<span class="nc" id="L592"> JSONObject prefs=new JSONObject().put(&quot;fontScale&quot;,1).put(&quot;theme&quot;,&quot;ios&quot;).put(&quot;contrast&quot;,&quot;normal&quot;).put(&quot;accent&quot;,&quot;#a40640&quot;);</span>
<span class="nc" id="L593"> merge(prefs,preference(c,&quot;DEFAULT&quot;,&quot;*&quot;));</span>
<span class="nc bnc" id="L594" title="All 2 branches missed."> for(String g:groups)merge(prefs,preference(c,&quot;GROUP&quot;,g));</span>
<span class="nc bnc" id="L595" title="All 4 branches missed."> if(station!=null&amp;&amp;!station.isEmpty())merge(prefs,preference(c,&quot;STATION&quot;,station));</span>
<span class="nc" id="L596"> merge(prefs,preference(c,&quot;USER&quot;,user()));</span>
<span class="nc" id="L597"> JSONObject overrides=new JSONObject();String baseId=&quot;&quot;;</span>
<span class="nc" id="L598"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT BASE_TEMPLATE_ID,OVERRIDES_JSON FROM &quot;+schema()+&quot;HMI_APP_LAYOUT WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?&quot;)){</span>
<span class="nc" id="L599"> p.setString(1,user());p.setString(2,pageId);p.setString(3, stationKey(station));</span>
<span class="nc bnc" id="L600" title="All 2 branches missed."> try(ResultSet rs=p.executeQuery()) {if(rs.next()){baseId=rs.getString(1);overrides=objectOrEmpty(clob(rs,&quot;OVERRIDES_JSON&quot;));}}</span>
<span class="nc" id="L592"> List&lt;String&gt; groups=groups(c,user());</span>
<span class="nc" id="L593"> JSONObject template=resolveTemplate(c,pageId,station,groups);</span>
<span class="nc bnc" id="L594" title="All 2 branches missed."> if(template==null)return err(404,&quot;No published template assigned&quot;);</span>
<span class="nc" id="L595"> JSONObject prefs=new JSONObject().put(&quot;fontScale&quot;,1).put(&quot;theme&quot;,&quot;ios&quot;).put(&quot;contrast&quot;,&quot;normal&quot;).put(&quot;accent&quot;,&quot;#a40640&quot;);</span>
<span class="nc" id="L596"> merge(prefs,preference(c,&quot;DEFAULT&quot;,&quot;*&quot;));</span>
<span class="nc bnc" id="L597" title="All 2 branches missed."> for(String g:groups)merge(prefs,preference(c,&quot;GROUP&quot;,g));</span>
<span class="nc bnc" id="L598" title="All 4 branches missed."> if(station!=null&amp;&amp;!station.isEmpty())merge(prefs,preference(c,&quot;STATION&quot;,station));</span>
<span class="nc" id="L599"> merge(prefs,preference(c,&quot;USER&quot;,user()));</span>
<span class="nc" id="L600"> JSONObject overrides=new JSONObject();String baseId=&quot;&quot;;</span>
<span class="nc" id="L601"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT BASE_TEMPLATE_ID,OVERRIDES_JSON FROM &quot;+schema()+&quot;HMI_APP_LAYOUT WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?&quot;)){</span>
<span class="nc" id="L602"> p.setString(1,user());p.setString(2,pageId);p.setString(3, stationKey(station));</span>
<span class="nc bnc" id="L603" title="All 2 branches missed."> try(ResultSet rs=p.executeQuery()) {if(rs.next()){baseId=rs.getString(1);overrides=objectOrEmpty(clob(rs,&quot;OVERRIDES_JSON&quot;));}}</span>
}
<span class="nc" id="L602"> JSONObject layout=template.getJSONObject(&quot;layout&quot;);</span>
<span class="nc" id="L603"> JSONArray effectiveWidgets=new JSONArray();</span>
<span class="nc" id="L604"> java.util.Set&lt;String&gt; granted = HmiAuth.roles(request);</span>
<span class="nc bnc" id="L605" title="All 2 branches missed."> for(Object val:layout.getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L606"> JSONObject widget=new JSONObject(val.toString());String widgetId=widget.getString(&quot;id&quot;);</span>
<span class="nc" id="L605"> JSONObject layout=template.getJSONObject(&quot;layout&quot;);</span>
<span class="nc" id="L606"> JSONArray effectiveWidgets=new JSONArray();</span>
<span class="nc" id="L607"> java.util.Set&lt;String&gt; granted = HmiAuth.roles(request);</span>
<span class="nc bnc" id="L608" title="All 2 branches missed."> for(Object val:layout.getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L609"> JSONObject widget=new JSONObject(val.toString());String widgetId=widget.getString(&quot;id&quot;);</span>
// Security: role-restricted widgets must never be delivered in the effective payload.
<span class="nc" id="L608"> JSONArray required = widget.optJSONArray(&quot;visibleToRoles&quot;);</span>
<span class="nc bnc" id="L609" title="All 6 branches missed."> if (required!=null &amp;&amp; required.length()&gt;0 &amp;&amp; !granted.contains(&quot;hmi-admin&quot;)) {</span>
<span class="nc" id="L610"> boolean allowed=false;</span>
<span class="nc bnc" id="L611" title="All 4 branches missed."> for(int i=0;i&lt;required.length();i++) if(granted.contains(required.optString(i))) {allowed=true;break;}</span>
<span class="nc bnc" id="L612" title="All 2 branches missed."> if(!allowed)continue;</span>
<span class="nc" id="L611"> JSONArray required = widget.optJSONArray(&quot;visibleToRoles&quot;);</span>
<span class="nc bnc" id="L612" title="All 6 branches missed."> if (required!=null &amp;&amp; required.length()&gt;0 &amp;&amp; !granted.contains(&quot;hmi-admin&quot;)) {</span>
<span class="nc" id="L613"> boolean allowed=false;</span>
<span class="nc bnc" id="L614" title="All 4 branches missed."> for(int i=0;i&lt;required.length();i++) if(granted.contains(required.optString(i))) {allowed=true;break;}</span>
<span class="nc bnc" id="L615" title="All 2 branches missed."> if(!allowed)continue;</span>
}
<span class="nc bnc" id="L614" title="All 4 branches missed."> if(widget.optBoolean(&quot;personalizable&quot;,true)&amp;&amp;overrides.has(widgetId)){</span>
<span class="nc" id="L615"> JSONObject o=overrides.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L616" title="All 4 branches missed."> for(String key:new String[]{&quot;x&quot;,&quot;y&quot;,&quot;w&quot;,&quot;h&quot;})if(o.has(key))widget.put(key,o.getInt(key));</span>
<span class="nc bnc" id="L617" title="All 4 branches missed."> if(!widget.optBoolean(&quot;mandatory&quot;,false)&amp;&amp;o.optBoolean(&quot;hidden&quot;,false))widget.put(&quot;hidden&quot;,true);</span>
<span class="nc bnc" id="L618" title="All 2 branches missed."> if(widget.getInt(&quot;x&quot;)+widget.getInt(&quot;w&quot;)&gt;12)widget.put(&quot;x&quot;,0);</span>
<span class="nc bnc" id="L617" title="All 4 branches missed."> if(widget.optBoolean(&quot;personalizable&quot;,true)&amp;&amp;overrides.has(widgetId)){</span>
<span class="nc" id="L618"> JSONObject o=overrides.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L619" title="All 4 branches missed."> for(String key:new String[]{&quot;x&quot;,&quot;y&quot;,&quot;w&quot;,&quot;h&quot;})if(o.has(key))widget.put(key,o.getInt(key));</span>
<span class="nc bnc" id="L620" title="All 4 branches missed."> if(!widget.optBoolean(&quot;mandatory&quot;,false)&amp;&amp;o.optBoolean(&quot;hidden&quot;,false))widget.put(&quot;hidden&quot;,true);</span>
<span class="nc bnc" id="L621" title="All 2 branches missed."> if(widget.getInt(&quot;x&quot;)+widget.getInt(&quot;w&quot;)&gt;12)widget.put(&quot;x&quot;,0);</span>
}
<span class="nc" id="L620"> effectiveWidgets.put(widget);</span>
<span class="nc" id="L621"> }</span>
<span class="nc" id="L622"> return json(new JSONObject().put(&quot;page&quot;,page).put(&quot;templateId&quot;,template.getString(&quot;id&quot;))</span>
<span class="nc" id="L623"> .put(&quot;version&quot;,template.getInt(&quot;version&quot;)).put(&quot;originalTemplateId&quot;,baseId)</span>
<span class="nc" id="L624"> .put(&quot;layout&quot;,new JSONObject().put(&quot;widgets&quot;,effectiveWidgets)).put(&quot;preferences&quot;,prefs));</span>
<span class="nc bnc" id="L625" title="All 8 branches missed."> }catch(Exception e){return dbError(e);}</span>
<span class="nc" id="L623"> effectiveWidgets.put(widget);</span>
<span class="nc" id="L624"> }</span>
<span class="nc" id="L625"> return json(new JSONObject().put(&quot;page&quot;,page).put(&quot;templateId&quot;,template.getString(&quot;id&quot;))</span>
<span class="nc" id="L626"> .put(&quot;version&quot;,template.getInt(&quot;version&quot;)).put(&quot;originalTemplateId&quot;,baseId)</span>
<span class="nc" id="L627"> .put(&quot;layout&quot;,new JSONObject().put(&quot;widgets&quot;,effectiveWidgets)).put(&quot;preferences&quot;,prefs));</span>
<span class="nc bnc" id="L628" title="All 8 branches missed."> }catch(Exception e){return dbError(e);}</span>
}
@PUT @Path(&quot;pages/{pageId}/layout&quot;) @Consumes(MediaType.APPLICATION_JSON)
public Response saveLayout(@PathParam(&quot;pageId&quot;)String pageId,String body){
<span class="nc bnc" id="L629" title="All 2 branches missed."> Response r=writeAllowed(true);if(r!=null)return r;</span>
<span class="nc bnc" id="L632" title="All 2 branches missed."> Response r=writeAllowed(true);if(r!=null)return r;</span>
try {
<span class="nc" id="L631"> JSONObject in=parse(body);</span>
<span class="nc" id="L632"> JSONObject o=validateOverrides(in.getJSONObject(&quot;overrides&quot;));</span>
<span class="nc" id="L633"> try(Connection c=HmiDb.open()){</span>
<span class="nc bnc" id="L634" title="All 2 branches missed."> if(!pageExists(c,pageId))return err(404,&quot;Page not found&quot;);</span>
<span class="nc" id="L634"> JSONObject in=parse(body);</span>
<span class="nc" id="L635"> JSONObject o=validateOverrides(in.getJSONObject(&quot;overrides&quot;));</span>
<span class="nc" id="L636"> try(Connection c=HmiDb.open()){</span>
<span class="nc bnc" id="L637" title="All 2 branches missed."> if(!pageExists(c,pageId))return err(404,&quot;Page not found&quot;);</span>
// Server enforcement: only published, personalizable widget IDs can be overridden.
<span class="nc" id="L636"> JSONObject template=resolveTemplate(c,pageId,in.optString(&quot;station&quot;,&quot;&quot;),groups(c,user()));</span>
<span class="nc bnc" id="L637" title="All 2 branches missed."> if(template==null)return err(409,&quot;Page has no published template&quot;);</span>
<span class="nc" id="L638"> JSONObject allowed=new JSONObject();</span>
<span class="nc bnc" id="L639" title="All 2 branches missed."> for(Object obj:template.getJSONObject(&quot;layout&quot;).getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L640"> JSONObject w=(JSONObject)obj;String widgetId=w.getString(&quot;id&quot;);</span>
<span class="nc bnc" id="L641" title="All 4 branches missed."> if(!o.has(widgetId) || !w.optBoolean(&quot;personalizable&quot;,true))continue;</span>
<span class="nc" id="L642"> JSONObject override=o.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L643" title="All 4 branches missed."> if(w.optBoolean(&quot;mandatory&quot;,false)&amp;&amp;override.optBoolean(&quot;hidden&quot;,false))return err(400,&quot;Cannot hide mandatory widget&quot;);</span>
<span class="nc" id="L644"> allowed.put(widgetId,override);</span>
<span class="nc" id="L645"> }</span>
<span class="nc" id="L639"> JSONObject template=resolveTemplate(c,pageId,in.optString(&quot;station&quot;,&quot;&quot;),groups(c,user()));</span>
<span class="nc bnc" id="L640" title="All 2 branches missed."> if(template==null)return err(409,&quot;Page has no published template&quot;);</span>
<span class="nc" id="L641"> JSONObject allowed=new JSONObject();</span>
<span class="nc bnc" id="L642" title="All 2 branches missed."> for(Object obj:template.getJSONObject(&quot;layout&quot;).getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L643"> JSONObject w=(JSONObject)obj;String widgetId=w.getString(&quot;id&quot;);</span>
<span class="nc bnc" id="L644" title="All 4 branches missed."> if(!o.has(widgetId) || !w.optBoolean(&quot;personalizable&quot;,true))continue;</span>
<span class="nc" id="L645"> JSONObject override=o.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L646" title="All 4 branches missed."> if(w.optBoolean(&quot;mandatory&quot;,false)&amp;&amp;override.optBoolean(&quot;hidden&quot;,false))return err(400,&quot;Cannot hide mandatory widget&quot;);</span>
<span class="nc" id="L647"> allowed.put(widgetId,override);</span>
<span class="nc" id="L648"> }</span>
int count;
<span class="nc" id="L647"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_LAYOUT SET OVERRIDES_JSON=?,BASE_TEMPLATE_ID=?,UPDATED_AT=? WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?&quot;)){</span>
<span class="nc" id="L648"> clob(p,1,allowed.toString());p.setString(2,template.getString(&quot;id&quot;));p.setTimestamp(3,Timestamp.from(Instant.now()));p.setString(4,user());p.setString(5,pageId);p.setString(6,stationKey(in.optString(&quot;station&quot;, &quot;&quot;)));count=p.executeUpdate();</span>
<span class="nc" id="L650"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_LAYOUT SET OVERRIDES_JSON=?,BASE_TEMPLATE_ID=?,UPDATED_AT=? WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?&quot;)){</span>
<span class="nc" id="L651"> clob(p,1,allowed.toString());p.setString(2,template.getString(&quot;id&quot;));p.setTimestamp(3,Timestamp.from(Instant.now()));p.setString(4,user());p.setString(5,pageId);p.setString(6,stationKey(in.optString(&quot;station&quot;, &quot;&quot;)));count=p.executeUpdate();</span>
}
<span class="nc bnc" id="L650" title="All 2 branches missed."> if(count==0){try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_LAYOUT(USER_ID,PAGE_ID,STATION_ID,BASE_TEMPLATE_ID,OVERRIDES_JSON,UPDATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L651"> p.setString(1,user());p.setString(2,pageId);p.setString(3,stationKey(in.optString(&quot;station&quot;, &quot;&quot;)));p.setString(4,template.getString(&quot;id&quot;));clob(p,5,allowed.toString());p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();</span>
<span class="nc bnc" id="L653" title="All 2 branches missed."> if(count==0){try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_LAYOUT(USER_ID,PAGE_ID,STATION_ID,BASE_TEMPLATE_ID,OVERRIDES_JSON,UPDATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L654"> p.setString(1,user());p.setString(2,pageId);p.setString(3,stationKey(in.optString(&quot;station&quot;, &quot;&quot;)));p.setString(4,template.getString(&quot;id&quot;));clob(p,5,allowed.toString());p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();</span>
}}
<span class="nc" id="L653"> audit(c,&quot;user.layout&quot;,pageId,&quot;Saved overrides&quot;);</span>
<span class="nc" id="L654"> return json(new JSONObject().put(&quot;ok&quot;,true).put(&quot;saved&quot;,allowed.length()));</span>
<span class="nc bnc" id="L655" title="All 6 branches missed."> }</span>
<span class="nc" id="L656"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L656"> audit(c,&quot;user.layout&quot;,pageId,&quot;Saved overrides&quot;);</span>
<span class="nc" id="L657"> return json(new JSONObject().put(&quot;ok&quot;,true).put(&quot;saved&quot;,allowed.length()));</span>
<span class="nc bnc" id="L658" title="All 6 branches missed."> }</span>
<span class="nc" id="L659"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
}
@GET @Path(&quot;preferences&quot;)
<span class="nc bnc" id="L659" title="All 2 branches missed."> public Response myPreferences(){Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L660"> try(Connection c=HmiDb.open()){return json(preference(c,&quot;USER&quot;,user()));}catch(SQLException e){return dbError(e);}}</span>
<span class="nc bnc" id="L662" title="All 2 branches missed."> public Response myPreferences(){Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L663"> try(Connection c=HmiDb.open()){return json(preference(c,&quot;USER&quot;,user()));}catch(SQLException e){return dbError(e);}}</span>
@PUT @Path(&quot;preferences&quot;) @Consumes(MediaType.APPLICATION_JSON)
<span class="nc bnc" id="L662" title="All 2 branches missed."> public Response myPreferencesSave(String body){Response r=writeAllowed(true);if(r!=null)return r;</span>
<span class="nc" id="L663"> try{return storePreferences(&quot;USER&quot;,user(),parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
<span class="nc bnc" id="L665" title="All 2 branches missed."> public Response myPreferencesSave(String body){Response r=writeAllowed(true);if(r!=null)return r;</span>
<span class="nc" id="L666"> try{return storePreferences(&quot;USER&quot;,user(),parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
@PUT @Path(&quot;groups/{group}/preferences&quot;) @Consumes(MediaType.APPLICATION_JSON)
<span class="nc bnc" id="L665" title="All 2 branches missed."> public Response groupPreferences(@PathParam(&quot;group&quot;)String group,String body){Response r=writeAllowed(admin());if(r!=null)return r;</span>
<span class="nc bnc" id="L666" title="All 2 branches missed."> if(!legalKey(group))return err(400,&quot;Invalid group&quot;);</span>
<span class="nc" id="L667"> try{return storePreferences(&quot;GROUP&quot;,group,parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
<span class="nc bnc" id="L668" title="All 2 branches missed."> public Response groupPreferences(@PathParam(&quot;group&quot;)String group,String body){Response r=writeAllowed(admin());if(r!=null)return r;</span>
<span class="nc bnc" id="L669" title="All 2 branches missed."> if(!legalKey(group))return err(400,&quot;Invalid group&quot;);</span>
<span class="nc" id="L670"> try{return storePreferences(&quot;GROUP&quot;,group,parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
@PUT @Path(&quot;groups/{group}/members/{member}&quot;)
public Response groupMember(@PathParam(&quot;group&quot;)String group,@PathParam(&quot;member&quot;)String member){
<span class="nc bnc" id="L670" title="All 2 branches missed."> Response r=writeAllowed(admin());if(r!=null)return r;</span>
<span class="nc bnc" id="L671" title="All 6 branches missed."> if(!legalKey(group)||member.length()&gt;255||member.isEmpty())return err(400,&quot;Invalid group or member&quot;);</span>
<span class="nc" id="L672"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L673"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT USER_ID FROM &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER WHERE GROUP_ID=? AND USER_ID=?&quot;)){</span>
<span class="nc bnc" id="L674" title="All 4 branches missed."> p.setString(1,group);p.setString(2,member);try(ResultSet rs=p.executeQuery()){if(rs.next())return json(new JSONObject().put(&quot;ok&quot;,true));}</span>
<span class="nc bnc" id="L675" title="All 2 branches missed."> }</span>
<span class="nc" id="L676"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER(USER_ID,GROUP_ID) VALUES (?,?)&quot;)){</span>
<span class="nc" id="L677"> p.setString(1,member);p.setString(2,group);p.executeUpdate();</span>
<span class="nc bnc" id="L673" title="All 2 branches missed."> Response r=writeAllowed(admin());if(r!=null)return r;</span>
<span class="nc bnc" id="L674" title="All 6 branches missed."> if(!legalKey(group)||member.length()&gt;255||member.isEmpty())return err(400,&quot;Invalid group or member&quot;);</span>
<span class="nc" id="L675"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L676"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT USER_ID FROM &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER WHERE GROUP_ID=? AND USER_ID=?&quot;)){</span>
<span class="nc bnc" id="L677" title="All 4 branches missed."> p.setString(1,group);p.setString(2,member);try(ResultSet rs=p.executeQuery()){if(rs.next())return json(new JSONObject().put(&quot;ok&quot;,true));}</span>
<span class="nc bnc" id="L678" title="All 2 branches missed."> }</span>
<span class="nc" id="L679"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER(USER_ID,GROUP_ID) VALUES (?,?)&quot;)){</span>
<span class="nc" id="L680"> p.setString(1,member);p.setString(2,group);p.executeUpdate();</span>
}
<span class="nc" id="L679"> audit(c,&quot;group.member.added&quot;,group,member);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L680" title="All 2 branches missed."> }catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L682"> audit(c,&quot;group.member.added&quot;,group,member);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L683" title="All 2 branches missed."> }catch(SQLException e){return dbError(e);}</span>
}
@POST @Path(&quot;assignments&quot;) @Consumes(MediaType.APPLICATION_JSON)
<span class="nc bnc" id="L683" title="All 2 branches missed."> public Response assign(String body){Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc bnc" id="L686" title="All 2 branches missed."> public Response assign(String body){Response r=writeAllowed(publisher());if(r!=null)return r;</span>
try{
<span class="nc" id="L685"> JSONObject in=parse(body);String pageId=need(in,&quot;pageId&quot;,64), templateId=need(in,&quot;templateId&quot;,64), type=need(in,&quot;scopeType&quot;,20), scope=need(in,&quot;scopeId&quot;,255);</span>
<span class="nc bnc" id="L686" title="All 6 branches missed."> if(!type.equals(&quot;GROUP&quot;)&amp;&amp;!type.equals(&quot;STATION&quot;)&amp;&amp;!type.equals(&quot;DEFAULT&quot;))return err(400,&quot;Invalid scope&quot;);</span>
<span class="nc" id="L687"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L688"> try(PreparedStatement check=c.prepareStatement(&quot;SELECT STATUS FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=? AND PAGE_ID=?&quot;)){</span>
<span class="nc" id="L689"> check.setString(1,templateId);check.setString(2,pageId);</span>
<span class="nc bnc" id="L690" title="All 6 branches missed."> try(ResultSet rs=check.executeQuery()){if(!rs.next()||!&quot;PUBLISHED&quot;.equals(rs.getString(1)))return err(400,&quot;Only published templates can be assigned&quot;);}</span>
<span class="nc bnc" id="L691" title="All 2 branches missed."> }</span>
<span class="nc" id="L692"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_ASSIGNMENT(ID,PAGE_ID,SCOPE_TYPE,SCOPE_ID,TEMPLATE_ID,PRIORITY) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L693"> p.setString(1,id());p.setString(2,pageId);p.setString(3,type);p.setString(4,scope);p.setString(5,templateId);p.setInt(6,bounded(in,&quot;priority&quot;,0,1000,0));p.executeUpdate();</span>
<span class="nc" id="L688"> JSONObject in=parse(body);String pageId=need(in,&quot;pageId&quot;,64), templateId=need(in,&quot;templateId&quot;,64), type=need(in,&quot;scopeType&quot;,20), scope=need(in,&quot;scopeId&quot;,255);</span>
<span class="nc bnc" id="L689" title="All 6 branches missed."> if(!type.equals(&quot;GROUP&quot;)&amp;&amp;!type.equals(&quot;STATION&quot;)&amp;&amp;!type.equals(&quot;DEFAULT&quot;))return err(400,&quot;Invalid scope&quot;);</span>
<span class="nc" id="L690"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L691"> try(PreparedStatement check=c.prepareStatement(&quot;SELECT STATUS FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=? AND PAGE_ID=?&quot;)){</span>
<span class="nc" id="L692"> check.setString(1,templateId);check.setString(2,pageId);</span>
<span class="nc bnc" id="L693" title="All 6 branches missed."> try(ResultSet rs=check.executeQuery()){if(!rs.next()||!&quot;PUBLISHED&quot;.equals(rs.getString(1)))return err(400,&quot;Only published templates can be assigned&quot;);}</span>
<span class="nc bnc" id="L694" title="All 2 branches missed."> }</span>
<span class="nc" id="L695"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_ASSIGNMENT(ID,PAGE_ID,SCOPE_TYPE,SCOPE_ID,TEMPLATE_ID,PRIORITY) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L696"> p.setString(1,id());p.setString(2,pageId);p.setString(3,type);p.setString(4,scope);p.setString(5,templateId);p.setInt(6,bounded(in,&quot;priority&quot;,0,1000,0));p.executeUpdate();</span>
}
<span class="nc" id="L695"> audit(c,&quot;template.assigned&quot;,templateId,type+&quot;:&quot;+scope);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L696" title="All 2 branches missed."> }</span>
<span class="nc" id="L697"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L698"> audit(c,&quot;template.assigned&quot;,templateId,type+&quot;:&quot;+scope);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L699" title="All 2 branches missed."> }</span>
<span class="nc" id="L700"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
}
private Response storePreferences(String type,String identity,JSONObject input){
<span class="nc" id="L700"> JSONObject prefs=validatePreferences(input);</span>
<span class="nc" id="L701"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L703"> JSONObject prefs=validatePreferences(input);</span>
<span class="nc" id="L704"> try(Connection c=HmiDb.open()){</span>
int count;
<span class="nc" id="L703"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_PREF SET PREFS_JSON=?,UPDATED_AT=? WHERE SCOPE_TYPE=? AND SCOPE_ID=?&quot;)){</span>
<span class="nc" id="L704"> clob(p,1,prefs.toString());p.setTimestamp(2,Timestamp.from(Instant.now()));p.setString(3,type);p.setString(4,identity);count=p.executeUpdate();</span>
<span class="nc" id="L706"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_PREF SET PREFS_JSON=?,UPDATED_AT=? WHERE SCOPE_TYPE=? AND SCOPE_ID=?&quot;)){</span>
<span class="nc" id="L707"> clob(p,1,prefs.toString());p.setTimestamp(2,Timestamp.from(Instant.now()));p.setString(3,type);p.setString(4,identity);count=p.executeUpdate();</span>
}
<span class="nc bnc" id="L706" title="All 2 branches missed."> if(count==0){try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_PREF(SCOPE_TYPE,SCOPE_ID,PREFS_JSON,UPDATED_AT) VALUES (?,?,?,?)&quot;)){</span>
<span class="nc" id="L707"> p.setString(1,type);p.setString(2,identity);clob(p,3,prefs.toString());p.setTimestamp(4,Timestamp.from(Instant.now()));p.executeUpdate();</span>
<span class="nc bnc" id="L709" title="All 2 branches missed."> if(count==0){try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_PREF(SCOPE_TYPE,SCOPE_ID,PREFS_JSON,UPDATED_AT) VALUES (?,?,?,?)&quot;)){</span>
<span class="nc" id="L710"> p.setString(1,type);p.setString(2,identity);clob(p,3,prefs.toString());p.setTimestamp(4,Timestamp.from(Instant.now()));p.executeUpdate();</span>
}}
<span class="nc" id="L709"> audit(c,&quot;preferences.saved&quot;,type+&quot;:&quot;+identity,&quot;&quot;);return json(prefs);</span>
<span class="nc" id="L710"> }catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L712"> audit(c,&quot;preferences.saved&quot;,type+&quot;:&quot;+identity,&quot;&quot;);return json(prefs);</span>
<span class="nc" id="L713"> }catch(SQLException e){return dbError(e);}</span>
}
private JSONObject preference(Connection c,String type,String identity)throws SQLException{
<span class="nc" id="L713"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT PREFS_JSON FROM &quot;+schema()+&quot;HMI_APP_PREF WHERE SCOPE_TYPE=? AND SCOPE_ID=?&quot;)){</span>
<span class="nc" id="L714"> p.setString(1,type);p.setString(2,identity);</span>
<span class="nc bnc" id="L715" title="All 2 branches missed."> try(ResultSet rs=p.executeQuery()){return rs.next()?objectOrEmpty(clob(rs,&quot;PREFS_JSON&quot;)):new JSONObject();}</span>
<span class="nc" id="L716"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT PREFS_JSON FROM &quot;+schema()+&quot;HMI_APP_PREF WHERE SCOPE_TYPE=? AND SCOPE_ID=?&quot;)){</span>
<span class="nc" id="L717"> p.setString(1,type);p.setString(2,identity);</span>
<span class="nc bnc" id="L718" title="All 2 branches missed."> try(ResultSet rs=p.executeQuery()){return rs.next()?objectOrEmpty(clob(rs,&quot;PREFS_JSON&quot;)):new JSONObject();}</span>
}
}
<span class="nc bnc" id="L718" title="All 2 branches missed."> private static void merge(JSONObject target,JSONObject changes){for(String k:changes.keySet())target.put(k,changes.get(k));}</span>
<span class="nc bnc" id="L721" title="All 2 branches missed."> private static void merge(JSONObject target,JSONObject changes){for(String k:changes.keySet())target.put(k,changes.get(k));}</span>
private List&lt;String&gt; groups(Connection c,String userId)throws SQLException{
<span class="nc" id="L720"> List&lt;String&gt; a=new ArrayList&lt;&gt;();</span>
<span class="nc" id="L721"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT GROUP_ID FROM &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER WHERE USER_ID=? ORDER BY GROUP_ID&quot;)){</span>
<span class="nc bnc" id="L722" title="All 2 branches missed."> p.setString(1,userId);try(ResultSet rs=p.executeQuery()){while(rs.next())a.add(rs.getString(1));}</span>
<span class="nc" id="L723"> List&lt;String&gt; a=new ArrayList&lt;&gt;();</span>
<span class="nc" id="L724"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT GROUP_ID FROM &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER WHERE USER_ID=? ORDER BY GROUP_ID&quot;)){</span>
<span class="nc bnc" id="L725" title="All 2 branches missed."> p.setString(1,userId);try(ResultSet rs=p.executeQuery()){while(rs.next())a.add(rs.getString(1));}</span>
}
<span class="nc" id="L724"> String pers=HmiAuth.persId(c,userId);</span>
<span class="nc bnc" id="L725" title="All 2 branches missed."> if(pers!=null)try(PreparedStatement p=c.prepareStatement(</span>
<span class="nc" id="L726"> &quot;SELECT GROUP_KEY FROM &quot;+schema()+&quot;HMI_APP_PERSON_GROUP WHERE PERS_ID=? ORDER BY GROUP_KEY&quot;)) {</span>
<span class="nc" id="L727"> p.setString(1,pers);</span>
<span class="nc bnc" id="L728" title="All 4 branches missed."> try(ResultSet rs=p.executeQuery()) { while(rs.next())if(!a.contains(rs.getString(1)))a.add(rs.getString(1)); }</span>
<span class="nc" id="L727"> String pers=HmiAuth.persId(c,userId);</span>
<span class="nc bnc" id="L728" title="All 2 branches missed."> if(pers!=null)try(PreparedStatement p=c.prepareStatement(</span>
<span class="nc" id="L729"> &quot;SELECT GROUP_KEY FROM &quot;+schema()+&quot;HMI_APP_PERSON_GROUP WHERE PERS_ID=? ORDER BY GROUP_KEY&quot;)) {</span>
<span class="nc" id="L730"> p.setString(1,pers);</span>
<span class="nc bnc" id="L731" title="All 4 branches missed."> try(ResultSet rs=p.executeQuery()) { while(rs.next())if(!a.contains(rs.getString(1)))a.add(rs.getString(1)); }</span>
}
<span class="nc" id="L730"> return a;</span>
<span class="nc" id="L733"> return a;</span>
}
private JSONObject fetchPage(Connection c,String pageId)throws SQLException{
<span class="nc" id="L733"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID FROM &quot;+schema()+&quot;HMI_APP_PAGE WHERE ID=?&quot;)){</span>
<span class="nc" id="L734"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L735" title="All 2 branches missed."> return rs.next()?new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;key&quot;,rs.getString(2)).put(&quot;title&quot;,rs.getString(3))</span>
<span class="nc bnc" id="L736" title="All 2 branches missed."> .put(&quot;menuGroup&quot;,rs.getString(4)).put(&quot;order&quot;,rs.getInt(5)).put(&quot;enabled&quot;,rs.getInt(6)!=0)</span>
<span class="nc bnc" id="L737" title="All 2 branches missed."> .put(&quot;parentPageId&quot;,rs.getString(7)==null?JSONObject.NULL:rs.getString(7)):null;</span>
<span class="nc" id="L736"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID FROM &quot;+schema()+&quot;HMI_APP_PAGE WHERE ID=?&quot;)){</span>
<span class="nc" id="L737"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L738" title="All 2 branches missed."> return rs.next()?new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;key&quot;,rs.getString(2)).put(&quot;title&quot;,rs.getString(3))</span>
<span class="nc bnc" id="L739" title="All 2 branches missed."> .put(&quot;menuGroup&quot;,rs.getString(4)).put(&quot;order&quot;,rs.getInt(5)).put(&quot;enabled&quot;,rs.getInt(6)!=0)</span>
<span class="nc bnc" id="L740" title="All 2 branches missed."> .put(&quot;parentPageId&quot;,rs.getString(7)==null?JSONObject.NULL:rs.getString(7)):null;</span>
}
}
}
<span class="nc bnc" id="L741" title="All 2 branches missed."> private boolean pageExists(Connection c,String pageId)throws SQLException{return fetchPage(c,pageId)!=null;}</span>
<span class="nc bnc" id="L744" title="All 2 branches missed."> private boolean pageExists(Connection c,String pageId)throws SQLException{return fetchPage(c,pageId)!=null;}</span>
private JSONObject resolveTemplate(Connection c,String pageId,String station,List&lt;String&gt; groups)throws SQLException{
<span class="nc" id="L743"> List&lt;String&gt; ids=new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L744" title="All 4 branches missed."> if(station!=null&amp;&amp;!station.isEmpty())ids.add(&quot;STATION:&quot;+station);</span>
<span class="nc bnc" id="L745" title="All 2 branches missed."> for(String g:groups)ids.add(&quot;GROUP:&quot;+g);</span>
<span class="nc" id="L746"> ids.add(&quot;DEFAULT:*&quot;);</span>
<span class="nc" id="L746"> List&lt;String&gt; ids=new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L747" title="All 4 branches missed."> if(station!=null&amp;&amp;!station.isEmpty())ids.add(&quot;STATION:&quot;+station);</span>
<span class="nc bnc" id="L748" title="All 2 branches missed."> for(String g:groups)ids.add(&quot;GROUP:&quot;+g);</span>
<span class="nc" id="L749"> ids.add(&quot;DEFAULT:*&quot;);</span>
// Distinct selected precedence STATION &gt; GROUP &gt; DEFAULT; highest priority within a scope.
<span class="nc bnc" id="L748" title="All 2 branches missed."> for(String selection:ids){</span>
<span class="nc" id="L749"> String[] parts=selection.split(&quot;:&quot;,2);</span>
<span class="nc" id="L750"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT T.ID,T.VERSION_NO,T.LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_ASSIGNMENT A JOIN &quot;+schema()+&quot;HMI_APP_TEMPLATE T ON A.TEMPLATE_ID=T.ID WHERE A.PAGE_ID=? AND A.SCOPE_TYPE=? AND A.SCOPE_ID=? AND T.STATUS='PUBLISHED' ORDER BY A.PRIORITY DESC,T.VERSION_NO DESC&quot;)){</span>
<span class="nc" id="L751"> p.setString(1,pageId);p.setString(2,parts[0]);p.setString(3,parts[1]);</span>
<span class="nc bnc" id="L752" title="All 4 branches missed."> try(ResultSet rs=p.executeQuery()){if(rs.next())return new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;)));}</span>
<span class="nc bnc" id="L753" title="All 2 branches missed."> }</span>
<span class="nc" id="L754"> }</span>
<span class="nc" id="L755"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID,VERSION_NO,LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE PAGE_ID=? AND STATUS='PUBLISHED' ORDER BY VERSION_NO DESC&quot;)){</span>
<span class="nc" id="L756"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L757" title="All 2 branches missed."> return rs.next()?new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;))):null;</span>
<span class="nc bnc" id="L751" title="All 2 branches missed."> for(String selection:ids){</span>
<span class="nc" id="L752"> String[] parts=selection.split(&quot;:&quot;,2);</span>
<span class="nc" id="L753"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT T.ID,T.VERSION_NO,T.LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_ASSIGNMENT A JOIN &quot;+schema()+&quot;HMI_APP_TEMPLATE T ON A.TEMPLATE_ID=T.ID WHERE A.PAGE_ID=? AND A.SCOPE_TYPE=? AND A.SCOPE_ID=? AND T.STATUS='PUBLISHED' ORDER BY A.PRIORITY DESC,T.VERSION_NO DESC&quot;)){</span>
<span class="nc" id="L754"> p.setString(1,pageId);p.setString(2,parts[0]);p.setString(3,parts[1]);</span>
<span class="nc bnc" id="L755" title="All 4 branches missed."> try(ResultSet rs=p.executeQuery()){if(rs.next())return new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;)));}</span>
<span class="nc bnc" id="L756" title="All 2 branches missed."> }</span>
<span class="nc" id="L757"> }</span>
<span class="nc" id="L758"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID,VERSION_NO,LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE PAGE_ID=? AND STATUS='PUBLISHED' ORDER BY VERSION_NO DESC&quot;)){</span>
<span class="nc" id="L759"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L760" title="All 2 branches missed."> return rs.next()?new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;))):null;</span>
}
}
}
private void audit(Connection c,String action,String entity,String details)throws SQLException{
<span class="nc" id="L762"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_AUDIT(ID,ACTOR,ACTION_NAME,ENTITY_ID,DETAILS,CREATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L763"> p.setString(1,id());p.setString(2,user());p.setString(3,action);p.setString(4,entity);</span>
<span class="nc bnc" id="L764" title="All 2 branches missed."> p.setString(5,details.length()&gt;1000?details.substring(0,1000):details);p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();</span>
<span class="nc" id="L765"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_AUDIT(ID,ACTOR,ACTION_NAME,ENTITY_ID,DETAILS,CREATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L766"> p.setString(1,id());p.setString(2,user());p.setString(3,action);p.setString(4,entity);</span>
<span class="nc bnc" id="L767" title="All 2 branches missed."> p.setString(5,details.length()&gt;1000?details.substring(0,1000):details);p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();</span>
}
<span class="nc" id="L766"> }</span>
<span class="nc" id="L769"> }</span>
private Response dbError(Exception ex){
<span class="nc" id="L768"> java.util.logging.Logger.getLogger(HmiApiResource.class.getName()).log(java.util.logging.Level.SEVERE,&quot;HMI DB operation failed&quot;,ex);</span>
<span class="nc" id="L769"> return err(500,&quot;Database operation failed; check server logs&quot;);</span>
<span class="nc" id="L771"> java.util.logging.Logger.getLogger(HmiApiResource.class.getName()).log(java.util.logging.Level.SEVERE,&quot;HMI DB operation failed&quot;,ex);</span>
<span class="nc" id="L772"> return err(500,&quot;Database operation failed; check server logs&quot;);</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>