Files
WidgetWorkbench/backend/target/site/jacoco/com.workbenchclassic.hmi/HmiApiResource.java.html
2026-11-22 17:22:19 +00:00

792 lines
84 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>HmiApiResource.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">backend</a> &gt; <a href="index.source.html" class="el_package">com.workbenchclassic.hmi</a> &gt; <span class="el_source">HmiApiResource.java</span></div><h1>HmiApiResource.java</h1><pre class="source lang-java linenums">package com.workbenchclassic.hmi;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/** HMI REST API. Identity is based on server-verified IMSAPI regLogin. */
@Path(&quot;/hmi/v1&quot;)
@Produces(MediaType.APPLICATION_JSON)
<span class="nc" id="L39">public class HmiApiResource {</span>
@Context private SecurityContext security;
@Context private HttpServletRequest request;
<span class="nc" id="L42"> private static String schema() { return HmiDb.schemaPrefix(); }</span>
private static final String CSRF = &quot;HMI_CSRF&quot;;
<span class="nc" id="L44"> private static final Set&lt;String&gt; WIDGET_TYPES = new HashSet&lt;&gt;();</span>
static {
<span class="nc" id="L46"> Collections.addAll(WIDGET_TYPES, &quot;ui.text&quot;, &quot;ui.table&quot;, &quot;ui.button&quot;, &quot;ui.header&quot;, &quot;ui.image&quot;, &quot;ui.script&quot;, &quot;ui.curved-header&quot;, &quot;ui.neon-actions&quot;, &quot;ui.metal-panel&quot;, &quot;ui.cheerpj&quot;, &quot;ui.cheerpj-workplace&quot;, &quot;mes.active-order&quot;,</span>
&quot;mes.serial-scanner&quot;, &quot;mes.station-status&quot;, &quot;mes.booking-table&quot;, &quot;mes.workplan&quot;,
&quot;wb.station-tree&quot;, &quot;wb.production-overview&quot;, &quot;wb.station-config&quot;, &quot;wb.workorders&quot;, &quot;wb.history&quot;,
&quot;wb.activities&quot;, &quot;wb.activity-designer&quot;, &quot;wb.contacts&quot;, &quot;wb.erp-groups&quot;,
&quot;wb.users&quot;, &quot;wb.bom-import&quot;, &quot;wb.part-import&quot;, &quot;wb.station-import&quot;,
&quot;wb.erp-import&quot;, &quot;wb.contact-import&quot;, &quot;wb.user-import&quot;);
<span class="nc" id="L52"> }</span>
<span class="nc" id="L53"> private String user() { return HmiAuth.user(request); }</span>
<span class="nc" id="L54"> private boolean role(String name) { return HmiAuth.role(request,name); }</span>
<span class="nc" id="L55"> private boolean designer() { return HmiAuth.designer(request); }</span>
<span class="nc" id="L56"> private boolean publisher() { return HmiAuth.publisher(request); }</span>
<span class="nc" id="L57"> private boolean admin() { return HmiAuth.admin(request); }</span>
<span class="nc" id="L58"> private boolean scriptEditor() { return HmiAuth.scriptEditor(request); }</span>
<span class="nc bnc" id="L59" title="All 2 branches missed."> private Response json(Object obj) { return Response.ok(obj instanceof String ? obj : obj.toString(), MediaType.APPLICATION_JSON).build(); }</span>
<span class="nc" id="L60"> private Response err(int status, String msg) { return Response.status(status).type(MediaType.APPLICATION_JSON)</span>
<span class="nc" id="L61"> .entity(new JSONObject().put(&quot;error&quot;, msg).toString()).build(); }</span>
private Response denied(boolean allowed) {
<span class="nc bnc" id="L63" title="All 2 branches missed."> if (user() == null) return err(401, &quot;IMSAPI-Anmeldung erforderlich&quot;);</span>
<span class="nc bnc" id="L64" title="All 2 branches missed."> if (!HmiSchemaBootstrap.isReady()) return err(503, &quot;HMI schema not ready; check server logs&quot;);</span>
<span class="nc bnc" id="L65" title="All 2 branches missed."> if (!allowed) return err(403, &quot;Insufficient role&quot;);</span>
<span class="nc" id="L66"> return null;</span>
}
<span class="nc" id="L68"> private boolean csrfValid() { return HmiAuth.csrf(request); }</span>
private Response writeAllowed(boolean allowed) {
<span class="nc" id="L70"> Response r = denied(allowed);</span>
<span class="nc bnc" id="L71" title="All 4 branches missed."> return r != null ? r : (!csrfValid() ? err(403, &quot;CSRF token missing or invalid&quot;) : null);</span>
}
private static String stationKey(String station) {
<span class="nc bnc" id="L74" title="All 4 branches missed."> if (station == null || station.isEmpty()) return &quot;*&quot;;</span>
<span class="nc bnc" id="L75" title="All 2 branches missed."> if (!station.matches(&quot;[A-Za-z0-9_.-]{1,120}&quot;)) throw new JSONException(&quot;Invalid station number&quot;);</span>
<span class="nc" id="L76"> return station;</span>
}
<span class="nc" id="L78"> private static String id() { return UUID.randomUUID().toString(); }</span>
<span class="nc bnc" id="L79" title="All 2 branches missed."> private static JSONObject parse(String input) { return new JSONObject(input == null ? &quot;{}&quot; : input); }</span>
private static String need(JSONObject obj, String key, int max) {
<span class="nc" id="L81"> String s = obj.getString(key).trim();</span>
<span class="nc bnc" id="L82" title="All 4 branches missed."> if (s.isEmpty() || s.length() &gt; max) throw new JSONException(&quot;Invalid '&quot; + key + &quot;'&quot;);</span>
<span class="nc" id="L83"> return s;</span>
}
private static int bounded(JSONObject o, String k, int min, int max, int fallback) {
<span class="nc bnc" id="L86" title="All 2 branches missed."> int n = o.has(k) ? o.getInt(k) : fallback;</span>
<span class="nc bnc" id="L87" title="All 4 branches missed."> if (n &lt; min || n &gt; max) throw new JSONException(&quot;Out of range: &quot; + k);</span>
<span class="nc" id="L88"> return n;</span>
}
<span class="nc" id="L90"> private static boolean legalKey(String s) { return s.matches(&quot;[A-Za-z0-9_.-]{1,120}&quot;); }</span>
private static String clob(ResultSet rs, String col) throws SQLException {
<span class="nc" id="L92"> Reader reader = rs.getCharacterStream(col);</span>
<span class="nc bnc" id="L93" title="All 2 branches missed."> if (reader == null) return null;</span>
<span class="nc" id="L94"> try (Reader r = reader) {</span>
<span class="nc" id="L95"> StringBuilder sb = new StringBuilder(); char[] buf = new char[4096]; int n;</span>
<span class="nc bnc" id="L96" title="All 4 branches missed."> while ((n = r.read(buf)) != -1) { sb.append(buf, 0, n); if (sb.length() &gt; 1000000) throw new SQLException(&quot;JSON too large&quot;); }</span>
<span class="nc" id="L97"> return sb.toString();</span>
<span class="nc" id="L98"> } catch (java.io.IOException e) { throw new SQLException(&quot;Cannot read CLOB&quot;, e); }</span>
}
private static void clob(PreparedStatement ps, int i, String text) throws SQLException {
<span class="nc" id="L101"> ps.setCharacterStream(i, new StringReader(text), text.length());</span>
<span class="nc" id="L102"> }</span>
<span class="nc bnc" id="L103" title="All 4 branches missed."> private static JSONObject objectOrEmpty(String value) { return value == null || value.trim().isEmpty() ? new JSONObject() : new JSONObject(value); }</span>
private static JSONObject validatePreferences(JSONObject input) {
<span class="nc" id="L105"> JSONObject out = new JSONObject();</span>
<span class="nc bnc" id="L106" title="All 2 branches missed."> for (String key : input.keySet()) {</span>
<span class="nc bnc" id="L107" title="All 5 branches missed."> switch (key) {</span>
<span class="nc" id="L108"> case &quot;fontScale&quot;: out.put(key, boundedScale(input.getDouble(key))); break;</span>
<span class="nc bnc" id="L109" title="All 10 branches missed."> case &quot;theme&quot;: { String s = input.getString(key); if (!s.equals(&quot;dark&quot;) &amp;&amp; !s.equals(&quot;light&quot;) &amp;&amp; !s.equals(&quot;system&quot;) &amp;&amp; !s.equals(&quot;ios&quot;) &amp;&amp; !s.equals(&quot;neon&quot;)) throw new JSONException(&quot;Invalid theme&quot;); out.put(key, s); break; }</span>
<span class="nc bnc" id="L110" title="All 4 branches missed."> case &quot;contrast&quot;: { String s = input.getString(key); if (!s.equals(&quot;normal&quot;) &amp;&amp; !s.equals(&quot;high&quot;)) throw new JSONException(&quot;Invalid contrast&quot;); out.put(key, s); break; }</span>
<span class="nc bnc" id="L111" title="All 2 branches missed."> case &quot;accent&quot;: { String s = input.getString(key); if (!s.matches(&quot;#[0-9a-fA-F]{6}&quot;)) throw new JSONException(&quot;Invalid accent color&quot;); out.put(key, s); break; }</span>
<span class="nc" id="L112"> default: throw new JSONException(&quot;Unknown preference: &quot; + key);</span>
}
<span class="nc" id="L114"> }</span>
<span class="nc" id="L115"> return out;</span>
}
<span class="nc bnc" id="L117" title="All 6 branches missed."> private static double boundedScale(double x) { if (!Double.isFinite(x) || x &lt; 0.75 || x &gt; 2.0) throw new JSONException(&quot;fontScale must be 0.75 to 2.0&quot;); return x; }</span>
static JSONObject validateTemplate(JSONObject layout) {
<span class="nc" id="L119"> JSONArray widgets = layout.getJSONArray(&quot;widgets&quot;);</span>
<span class="nc bnc" id="L120" title="All 2 branches missed."> if (widgets.length() &gt; 80) throw new JSONException(&quot;Maximum 80 widgets&quot;);</span>
<span class="nc" id="L121"> Set&lt;String&gt; ids = new HashSet&lt;&gt;();</span>
<span class="nc" id="L122"> JSONObject cleaned = new JSONObject(); JSONArray out = new JSONArray();</span>
<span class="nc bnc" id="L123" title="All 2 branches missed."> for (int i = 0; i &lt; widgets.length(); i++) {</span>
<span class="nc" id="L124"> JSONObject w = widgets.getJSONObject(i);</span>
<span class="nc" id="L125"> String widgetId = need(w, &quot;id&quot;, 120); String type = need(w, &quot;type&quot;, 100);</span>
<span class="nc bnc" id="L126" title="All 6 branches missed."> if (!legalKey(widgetId) || !WIDGET_TYPES.contains(type) || !ids.add(widgetId)) throw new JSONException(&quot;Invalid or duplicate widget ID/type&quot;);</span>
<span class="nc bnc" id="L127" title="All 2 branches missed."> if (&quot;ui.script&quot;.equals(type)) {</span>
<span class="nc" id="L128"> JSONObject properties = w.optJSONObject(&quot;props&quot;);</span>
<span class="nc bnc" id="L129" title="All 2 branches missed."> Object source = properties == null ? &quot;&quot; : properties.opt(&quot;script&quot;);</span>
<span class="nc bnc" id="L130" title="All 4 branches missed."> if (source != null &amp;&amp; !(source instanceof String)) throw new JSONException(&quot;JavaScript must be text&quot;);</span>
<span class="nc bnc" id="L131" title="All 4 branches missed."> if (source instanceof String &amp;&amp; ((String) source).length() &gt; 8000)</span>
<span class="nc" id="L132"> throw new JSONException(&quot;JavaScript limited to 8000 characters&quot;);</span>
}
<span class="nc bnc" id="L134" title="All 4 branches missed."> if (&quot;ui.cheerpj&quot;.equals(type) || &quot;ui.cheerpj-workplace&quot;.equals(type)) {</span>
<span class="nc" id="L135"> JSONObject properties = w.optJSONObject(&quot;props&quot;);</span>
<span class="nc bnc" id="L136" title="All 2 branches missed."> if (properties == null) throw new JSONException(&quot;CheerpJ-Konfiguration fehlt&quot;);</span>
<span class="nc" id="L137"> String url = properties.optString(&quot;jnlpUrl&quot;, &quot;&quot;);</span>
<span class="nc bnc" id="L138" title="All 8 branches missed."> if (url.length() &gt; 1600 || (!url.isEmpty() &amp;&amp; !url.startsWith(&quot;/backend/api/hmi/v1/cheerpj/workplace/&quot;) &amp;&amp; !url.matches(&quot;https?://[^\\s]+&quot;)))</span>
<span class="nc" id="L139"> throw new JSONException(&quot;Ungültige JNLP-URL&quot;);</span>
<span class="nc bnc" id="L140" title="All 2 branches missed."> if (&quot;ui.cheerpj-workplace&quot;.equals(type)</span>
<span class="nc bnc" id="L141" title="All 2 branches missed."> &amp;&amp; (!&quot;/backend/api/hmi/v1/cheerpj/workplace/manifest.jnlp&quot;.equals(url)</span>
<span class="nc bnc" id="L142" title="All 2 branches missed."> || properties.optInt(&quot;javaVersion&quot;, 8) != 11))</span>
<span class="nc" id="L143"> throw new JSONException(&quot;iTAC-Workplace-URL und Java 11 sind fest vorgegeben&quot;);</span>
<span class="nc bnc" id="L144" title="All 4 branches missed."> if (properties.has(&quot;manualStartOnly&quot;) &amp;&amp; !(properties.opt(&quot;manualStartOnly&quot;) instanceof Boolean))</span>
<span class="nc" id="L145"> throw new JSONException(&quot;manualStartOnly muss boolean sein&quot;);</span>
<span class="nc" id="L146"> String displayMode = properties.optString(&quot;displayMode&quot;, &quot;fit&quot;);</span>
<span class="nc bnc" id="L147" title="All 4 branches missed."> if (!&quot;fit&quot;.equals(displayMode) &amp;&amp; !&quot;original&quot;.equals(displayMode))</span>
<span class="nc" id="L148"> throw new JSONException(&quot;CheerpJ displayMode must be fit or original&quot;);</span>
<span class="nc" id="L149"> int javaVersion = properties.optInt(&quot;javaVersion&quot;, 8);</span>
<span class="nc bnc" id="L150" title="All 6 branches missed."> if (javaVersion != 8 &amp;&amp; javaVersion != 11 &amp;&amp; javaVersion != 17)</span>
<span class="nc" id="L151"> throw new JSONException(&quot;CheerpJ Java-Version muss 8, 11 oder 17 sein&quot;);</span>
}
<span class="nc" id="L153"> JSONObject item = new JSONObject().put(&quot;id&quot;, widgetId).put(&quot;type&quot;, type)</span>
<span class="nc" id="L154"> .put(&quot;x&quot;, bounded(w,&quot;x&quot;,0,11,0)).put(&quot;y&quot;,bounded(w,&quot;y&quot;,0,200,0))</span>
<span class="nc" id="L155"> .put(&quot;w&quot;,bounded(w,&quot;w&quot;,1,12,4)).put(&quot;h&quot;,bounded(w,&quot;h&quot;,1,20,3))</span>
<span class="nc" id="L156"> .put(&quot;mandatory&quot;,w.optBoolean(&quot;mandatory&quot;,false)).put(&quot;personalizable&quot;,w.optBoolean(&quot;personalizable&quot;,true))</span>
<span class="nc" id="L157"> .put(&quot;visibleToRoles&quot;,validWidgetRoles(w.optJSONArray(&quot;visibleToRoles&quot;)))</span>
<span class="nc bnc" id="L158" title="All 2 branches missed."> .put(&quot;props&quot;,w.optJSONObject(&quot;props&quot;) == null ? new JSONObject() : w.getJSONObject(&quot;props&quot;));</span>
<span class="nc bnc" id="L159" title="All 2 branches missed."> if (&quot;ui.cheerpj-workplace&quot;.equals(type)) {</span>
<span class="nc" id="L160"> JSONArray visibility = item.getJSONArray(&quot;visibleToRoles&quot;);</span>
<span class="nc" id="L161"> boolean allowed = false;</span>
<span class="nc bnc" id="L162" title="All 2 branches missed."> for (int k = 0; k &lt; visibility.length(); k++)</span>
<span class="nc bnc" id="L163" title="All 2 branches missed."> if (&quot;hmi-workplace&quot;.equals(visibility.optString(k))) allowed = true;</span>
<span class="nc bnc" id="L164" title="All 2 branches missed."> if (!allowed) throw new JSONException(&quot;iTAC Workplace requires the hmi-workplace visibility role&quot;);</span>
}
<span class="nc bnc" id="L166" title="All 4 branches missed."> if (item.optBoolean(&quot;mandatory&quot;) &amp;&amp; item.getJSONArray(&quot;visibleToRoles&quot;).length() &gt; 0)</span>
<span class="nc" id="L167"> throw new JSONException(&quot;Mandatory widgets cannot be restricted to specific roles&quot;);</span>
<span class="nc bnc" id="L168" title="All 2 branches missed."> if (item.getInt(&quot;x&quot;) + item.getInt(&quot;w&quot;) &gt; 12) throw new JSONException(&quot;Widget exceeds grid width&quot;);</span>
<span class="nc bnc" id="L169" title="All 2 branches missed."> if (item.toString().length() &gt; 16000) throw new JSONException(&quot;Widget props too large&quot;);</span>
<span class="nc" id="L170"> out.put(item);</span>
}
<span class="nc" id="L172"> cleaned.put(&quot;widgets&quot;, out);</span>
<span class="nc" id="L173"> return cleaned;</span>
}
/** The server, not Vue, decides whether JavaScript content was added or changed. */
private static java.util.Map&lt;String,String&gt; scripts(JSONObject layout) {
<span class="nc" id="L177"> java.util.Map&lt;String,String&gt; map = new java.util.HashMap&lt;&gt;();</span>
<span class="nc" id="L178"> JSONArray items = layout.optJSONArray(&quot;widgets&quot;);</span>
<span class="nc bnc" id="L179" title="All 2 branches missed."> if (items == null) return map;</span>
<span class="nc bnc" id="L180" title="All 2 branches missed."> for (int n=0; n&lt;items.length(); n++) {</span>
<span class="nc" id="L181"> JSONObject item = items.getJSONObject(n);</span>
<span class="nc bnc" id="L182" title="All 2 branches missed."> if (&quot;ui.script&quot;.equals(item.optString(&quot;type&quot;))) {</span>
<span class="nc" id="L183"> JSONObject props = item.optJSONObject(&quot;props&quot;);</span>
<span class="nc bnc" id="L184" title="All 2 branches missed."> map.put(item.getString(&quot;id&quot;), props == null ? &quot;&quot; : props.optString(&quot;script&quot;, &quot;&quot;));</span>
}
<span class="nc bnc" id="L186" title="All 4 branches missed."> if (&quot;ui.cheerpj&quot;.equals(item.optString(&quot;type&quot;)) || &quot;ui.cheerpj-workplace&quot;.equals(item.optString(&quot;type&quot;))) {</span>
<span class="nc" id="L187"> JSONObject props = item.optJSONObject(&quot;props&quot;);</span>
<span class="nc" id="L188"> JSONObject selection = new JSONObject();</span>
<span class="nc bnc" id="L189" title="All 2 branches missed."> if (props != null) selection.put(&quot;jnlpUrl&quot;, props.optString(&quot;jnlpUrl&quot;, &quot;&quot;))</span>
<span class="nc" id="L190"> .put(&quot;javaVersion&quot;, props.optInt(&quot;javaVersion&quot;, 8))</span>
<span class="nc" id="L191"> .put(&quot;autoStart&quot;, props.optBoolean(&quot;autoStart&quot;, false))</span>
<span class="nc" id="L192"> .put(&quot;manualStartOnly&quot;, props.optBoolean(&quot;manualStartOnly&quot;, false))</span>
<span class="nc" id="L193"> .put(&quot;displayMode&quot;, props.optString(&quot;displayMode&quot;, &quot;fit&quot;));</span>
<span class="nc" id="L194"> map.put(item.getString(&quot;id&quot;), &quot;CHEERPJ:&quot; + item.optString(&quot;type&quot;) + &quot;:&quot; + selection.toString());</span>
}
}
<span class="nc" id="L197"> return map;</span>
}
static boolean scriptChanges(JSONObject previous, JSONObject proposed) {
<span class="nc bnc" id="L200" title="All 2 branches missed."> return !scripts(previous).equals(scripts(proposed));</span>
}
private static JSONArray validWidgetRoles(JSONArray roles) {
<span class="nc" id="L204"> JSONArray out = new JSONArray();</span>
<span class="nc bnc" id="L205" title="All 2 branches missed."> if (roles == null) return out;</span>
<span class="nc bnc" id="L206" title="All 2 branches missed."> if (roles.length() &gt; 30) throw new JSONException(&quot;Too many widget roles&quot;);</span>
<span class="nc" id="L207"> java.util.Set&lt;String&gt; seen = new java.util.HashSet&lt;&gt;();</span>
<span class="nc bnc" id="L208" title="All 2 branches missed."> for (int i=0;i&lt;roles.length();i++) {</span>
<span class="nc" id="L209"> String role = roles.getString(i);</span>
<span class="nc bnc" id="L210" title="All 4 branches missed."> if (!legalKey(role) || !seen.add(role)) throw new JSONException(&quot;Invalid widget role&quot;);</span>
<span class="nc" id="L211"> out.put(role);</span>
}
<span class="nc" id="L213"> return out;</span>
}
private static JSONObject validateOverrides(JSONObject override) {
<span class="nc" id="L216"> JSONObject out = new JSONObject();</span>
<span class="nc bnc" id="L217" title="All 2 branches missed."> if (override.keySet().size() &gt; 80) throw new JSONException(&quot;Too many overrides&quot;);</span>
<span class="nc bnc" id="L218" title="All 2 branches missed."> for (String id : override.keySet()) {</span>
<span class="nc bnc" id="L219" title="All 2 branches missed."> if (!legalKey(id)) throw new JSONException(&quot;Invalid widget ID&quot;);</span>
<span class="nc" id="L220"> JSONObject p = override.getJSONObject(id);</span>
<span class="nc bnc" id="L221" title="All 12 branches missed."> for (String k : p.keySet()) if (!k.equals(&quot;x&quot;) &amp;&amp; !k.equals(&quot;y&quot;) &amp;&amp; !k.equals(&quot;w&quot;) &amp;&amp; !k.equals(&quot;h&quot;) &amp;&amp; !k.equals(&quot;hidden&quot;)) throw new JSONException(&quot;Forbidden override&quot;);</span>
<span class="nc" id="L222"> JSONObject o = new JSONObject()</span>
<span class="nc" id="L223"> .put(&quot;x&quot;, bounded(p,&quot;x&quot;,0,11,0)).put(&quot;y&quot;,bounded(p,&quot;y&quot;,0,200,0))</span>
<span class="nc" id="L224"> .put(&quot;w&quot;,bounded(p,&quot;w&quot;,1,12,4)).put(&quot;h&quot;,bounded(p,&quot;h&quot;,1,20,3))</span>
<span class="nc" id="L225"> .put(&quot;hidden&quot;,p.optBoolean(&quot;hidden&quot;,false));</span>
<span class="nc bnc" id="L226" title="All 2 branches missed."> if (o.getInt(&quot;x&quot;) + o.getInt(&quot;w&quot;) &gt; 12) throw new JSONException(&quot;Widget exceeds width&quot;);</span>
<span class="nc" id="L227"> out.put(id,o);</span>
<span class="nc" id="L228"> }</span>
<span class="nc" id="L229"> return out;</span>
}
/** 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="L234" title="All 2 branches missed."> if (user() == null) return err(401, &quot;IMSAPI-Anmeldung erforderlich&quot;);</span>
<span class="nc" id="L235"> boolean initialized = HmiSchemaBootstrap.isReady();</span>
<span class="nc" id="L236"> boolean isAdmin = admin();</span>
<span class="nc bnc" id="L237" title="All 4 branches missed."> if (initialized &amp;&amp; !isAdmin) return err(403, &quot;hmi-admin role required&quot;);</span>
<span class="nc" id="L238"> JSONObject out = new JSONObject()</span>
<span class="nc bnc" id="L239" 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="L240"> .put(&quot;user&quot;, user())</span>
<span class="nc" id="L241"> .put(&quot;readyAtDeployment&quot;, HmiSchemaBootstrap.isReady())</span>
<span class="nc" id="L242"> .put(&quot;initializationPhase&quot;, HmiSchemaBootstrap.phase())</span>
<span class="nc" id="L243"> .put(&quot;failureCode&quot;, HmiSchemaBootstrap.failureCode())</span>
<span class="nc" id="L244"> .put(&quot;pool&quot;, &quot;dsCustNJTA&quot;);</span>
<span class="nc" id="L245"> try (Connection c = HmiDb.open()) {</span>
<span class="nc" id="L246"> String db = c.getMetaData().getDatabaseProductName();</span>
<span class="nc" id="L247"> boolean oracle = db.toLowerCase(java.util.Locale.ROOT).contains(&quot;oracle&quot;);</span>
<span class="nc" id="L248"> boolean sqlServer = db.toLowerCase(java.util.Locale.ROOT).contains(&quot;sql server&quot;);</span>
<span class="nc bnc" id="L249" 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="L250" title="All 2 branches missed."> .put(&quot;schema&quot;, oracle ? &quot;CUST&quot; : &quot;cust&quot;);</span>
<span class="nc bnc" id="L251" 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="L252"> ResultSet rs = p.executeQuery()) {</span>
<span class="nc bnc" id="L253" title="All 4 branches missed."> out.put(&quot;connectionQueryOk&quot;, rs.next() &amp;&amp; rs.getInt(1) == 1);</span>
}
<span class="nc" id="L255"> int found = 0;</span>
<span class="nc" id="L256"> 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="L257" title="All 2 branches missed."> for (String t : tables) {</span>
<span class="nc bnc" id="L258" 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="L259" title="All 2 branches missed."> if (rs.next()) found++;</span>
}
}
<span class="nc" id="L262"> out.put(&quot;tablesFound&quot;, found).put(&quot;tablesExpected&quot;, tables.length);</span>
<span class="nc" id="L263"> return json(out);</span>
<span class="nc" id="L264"> } catch (SQLException e) {</span>
<span class="nc" id="L265"> java.util.logging.Logger.getLogger(HmiApiResource.class.getName())</span>
<span class="nc" id="L266"> .log(java.util.logging.Level.WARNING, &quot;Authenticated HMI diagnostics failed&quot;, e);</span>
<span class="nc" id="L267"> 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="L272" 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="L274" title="All 2 branches missed."> public Response widgets() { Response r = denied(true); if (r!=null) return r;</span>
<span class="nc" id="L275"> JSONArray a = new JSONArray();</span>
<span class="nc bnc" id="L276" 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="L277"> return json(a);</span>
}
@GET @Path(&quot;roles&quot;)
public Response viewableRoleNames() {
<span class="nc bnc" id="L281" title="All 2 branches missed."> Response r=denied(designer()); if(r!=null)return r;</span>
<span class="nc" id="L282"> JSONArray out=new JSONArray(); java.util.Set&lt;String&gt; seen=new java.util.TreeSet&lt;&gt;();</span>
<span class="nc" id="L283"> 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="L284"> try(Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(</span>
<span class="nc" id="L285"> &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="L286" title="All 2 branches missed."> while(rs.next())seen.add(rs.getString(1));</span>
<span class="nc bnc" id="L287" 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="L288"> return json(out);</span>
<span class="nc" id="L289"> }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="L293"> java.util.Set&lt;String&gt; seen=new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L294"> String current=pageId;</span>
<span class="nc bnc" id="L295" title="All 2 branches missed."> while(current!=null) {</span>
<span class="nc bnc" id="L296" title="All 2 branches missed."> if(!seen.add(current))return false;</span>
<span class="nc" id="L297"> JSONObject page=byId.get(current);</span>
<span class="nc bnc" id="L298" title="All 4 branches missed."> if(page==null || !page.getBoolean(&quot;enabled&quot;))return false;</span>
<span class="nc bnc" id="L299" title="All 2 branches missed."> current=page.isNull(&quot;parentPageId&quot;)?null:page.optString(&quot;parentPageId&quot;,null);</span>
<span class="nc" id="L300"> }</span>
<span class="nc" id="L301"> return true;</span>
}
@GET @Path(&quot;pages&quot;)
public Response pages() {
<span class="nc bnc" id="L305" title="All 2 branches missed."> Response r = denied(true); if (r!=null) return r;</span>
<span class="nc" id="L306"> JSONArray out = new JSONArray();</span>
<span class="nc" id="L307"> java.util.Map&lt;String,JSONObject&gt; found=new java.util.LinkedHashMap&lt;&gt;();</span>
<span class="nc" id="L308"> try(Connection c=HmiDb.open();PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L309"> &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="L310"> ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L311" title="All 2 branches missed."> while(rs.next()) {</span>
<span class="nc" id="L312"> String parent=rs.getString(7);</span>
<span class="nc" id="L313"> 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="L314" 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="L315" title="All 2 branches missed."> .put(&quot;parentPageId&quot;,parent==null?JSONObject.NULL:parent);</span>
<span class="nc" id="L316"> found.put(page.getString(&quot;id&quot;),page);</span>
<span class="nc" id="L317"> }</span>
<span class="nc bnc" id="L318" 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="L319"> return json(out);</span>
<span class="nc" id="L320"> }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="L324" title="All 4 branches missed."> if (input.isNull(&quot;parentPageId&quot;) || !input.has(&quot;parentPageId&quot;)) return null;</span>
<span class="nc" id="L325"> String value = input.getString(&quot;parentPageId&quot;).trim();</span>
<span class="nc bnc" id="L326" title="All 2 branches missed."> if (value.isEmpty()) return null;</span>
<span class="nc bnc" id="L327" title="All 2 branches missed."> if (!legalKey(value)) throw new JSONException(&quot;Invalid parentPageId&quot;);</span>
<span class="nc" id="L328"> return value;</span>
}
private static String pageMenu(JSONObject input) {
<span class="nc" id="L331"> String menu = input.optString(&quot;menuGroup&quot;, &quot;Allgemein&quot;).trim();</span>
<span class="nc bnc" id="L332" title="All 6 branches missed."> if (menu.isEmpty() || menu.length() &gt; 120 || menu.indexOf('/') &gt;= 0)</span>
<span class="nc" id="L333"> throw new JSONException(&quot;Invalid root menu name&quot;);</span>
<span class="nc" id="L334"> return menu;</span>
}
private static String parentMenu(Connection c, String parentId) throws SQLException {
<span class="nc" id="L337"> try (PreparedStatement ps=c.prepareStatement(&quot;SELECT MENU_GROUP FROM &quot; + schema() + &quot;HMI_APP_PAGE WHERE ID=?&quot;)) {</span>
<span class="nc" id="L338"> ps.setString(1,parentId);</span>
<span class="nc" id="L339"> try (ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L340" 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="L345"> 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="L346"> ps.setString(1,id);</span>
<span class="nc" id="L347"> try (ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L348" 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="L353" title="All 2 branches missed."> if (parent == null) return;</span>
<span class="nc" id="L354"> java.util.Set&lt;String&gt; visited = new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L355"> String candidate = parent;</span>
<span class="nc bnc" id="L356" title="All 2 branches missed."> for (int depth=0;candidate!=null;depth++) {</span>
<span class="nc bnc" id="L357" title="All 6 branches missed."> if (depth &gt; 32 || !visited.add(candidate) || candidate.equals(currentId))</span>
<span class="nc" id="L358"> throw new JSONException(&quot;Navigation cycle or depth limit detected&quot;);</span>
<span class="nc bnc" id="L359" title="All 2 branches missed."> if (parentMenu(c,candidate)==null)</span>
<span class="nc" id="L360"> throw new JSONException(&quot;Parent page not found&quot;);</span>
<span class="nc" id="L361"> candidate = parentOf(c,candidate);</span>
}
<span class="nc" id="L363"> }</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="L366"> java.util.Set&lt;String&gt; seen = new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L367"> java.util.ArrayDeque&lt;String&gt; pending = new java.util.ArrayDeque&lt;&gt;();</span>
<span class="nc" id="L368"> pending.add(rootId);seen.add(rootId);</span>
<span class="nc bnc" id="L369" title="All 2 branches missed."> while (!pending.isEmpty()) {</span>
<span class="nc" id="L370"> String current=pending.remove();</span>
<span class="nc" id="L371"> 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="L372"> ps.setString(1,current);</span>
<span class="nc" id="L373"> try(ResultSet rs=ps.executeQuery()) {</span>
<span class="nc bnc" id="L374" title="All 2 branches missed."> while(rs.next()) {</span>
<span class="nc" id="L375"> String child=rs.getString(1);</span>
<span class="nc bnc" id="L376" 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="L377" title="All 2 branches missed."> if (seen.size()&gt;1000) throw new SQLException(&quot;Too many navigation pages&quot;);</span>
<span class="nc" id="L378"> pending.add(child);</span>
<span class="nc" id="L379"> }</span>
}
}
<span class="nc bnc" id="L382" title="All 2 branches missed."> if(!current.equals(rootId)) {</span>
<span class="nc" id="L383"> try(PreparedStatement ps=c.prepareStatement(&quot;UPDATE &quot; + schema() + &quot;HMI_APP_PAGE SET MENU_GROUP=? WHERE ID=?&quot;)) {</span>
<span class="nc" id="L384"> ps.setString(1,menu);ps.setString(2,current);ps.executeUpdate();</span>
}
}
<span class="nc" id="L387"> }</span>
<span class="nc" id="L388"> }</span>
@POST @Path(&quot;pages&quot;) @Consumes(MediaType.APPLICATION_JSON)
public Response createPage(String body) {
<span class="nc bnc" id="L391" title="All 2 branches missed."> Response r=writeAllowed(designer()); if(r!=null)return r;</span>
try {
<span class="nc" id="L393"> JSONObject input=parse(body);</span>
<span class="nc bnc" id="L394" 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="L395"> String title=need(input,&quot;title&quot;,255), pageId=id();</span>
<span class="nc" id="L396"> String menu=pageMenu(input), parent=parentId(input);</span>
<span class="nc" id="L397"> int order=bounded(input,&quot;order&quot;,0,100000,0);</span>
<span class="nc" id="L398"> try (Connection c=HmiDb.open()) {</span>
<span class="nc" id="L399"> c.setAutoCommit(false);</span>
try {
<span class="nc" id="L401"> validatePageParent(c,pageId,parent);</span>
<span class="nc bnc" id="L402" title="All 2 branches missed."> if(parent!=null)menu=parentMenu(c,parent);</span>
<span class="nc" id="L403"> try (PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L404"> &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="L405"> ps.setString(1,pageId);ps.setString(2,key);ps.setString(3,title);</span>
<span class="nc" id="L406"> ps.setString(4,menu);ps.setInt(5,order);ps.setString(6,parent);ps.executeUpdate();</span>
}
<span class="nc" id="L408"> audit(c,&quot;page.created&quot;,pageId,key);c.commit();</span>
<span class="nc" id="L409"> } catch (Exception ex) {c.rollback();throw ex;}</span>
}
<span class="nc" id="L411"> return json(new JSONObject().put(&quot;id&quot;,pageId).put(&quot;key&quot;,key));</span>
<span class="nc" id="L412"> } catch (JSONException e) { return err(400,e.getMessage()); } catch (SQLException e) { return dbError(e); }</span>
<span class="nc" id="L413"> 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="L417" title="All 2 branches missed."> Response r=writeAllowed(designer()); if(r!=null)return r;</span>
try {
<span class="nc" id="L419"> JSONObject input=parse(body);</span>
<span class="nc" id="L420"> String title=need(input,&quot;title&quot;,255), menu=pageMenu(input), parent=parentId(input);</span>
<span class="nc" id="L421"> int order=bounded(input,&quot;order&quot;,0,100000,0);</span>
<span class="nc" id="L422"> boolean enabled=input.optBoolean(&quot;enabled&quot;,true);</span>
<span class="nc" id="L423"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L424"> c.setAutoCommit(false);</span>
try {
<span class="nc bnc" id="L426" title="All 2 branches missed."> if (!pageExists(c,pageId)) {c.rollback();return err(404,&quot;Page not found&quot;);}</span>
<span class="nc" id="L427"> validatePageParent(c,pageId,parent);</span>
<span class="nc bnc" id="L428" title="All 2 branches missed."> if(parent!=null)menu=parentMenu(c,parent);</span>
<span class="nc" id="L429"> try(PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L430"> &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="L431"> ps.setString(1,title);ps.setString(2,menu);ps.setInt(3,order);</span>
<span class="nc bnc" id="L432" title="All 2 branches missed."> ps.setInt(4,enabled?1:0);ps.setString(5,parent);ps.setString(6,pageId);</span>
<span class="nc" id="L433"> ps.executeUpdate();</span>
}
<span class="nc" id="L435"> updateDescendantMenus(c,pageId,menu);</span>
<span class="nc" id="L436"> audit(c,&quot;page.updated&quot;,pageId,title);c.commit();</span>
<span class="nc" id="L437"> }catch(Exception ex){c.rollback();throw ex;}</span>
<span class="nc bnc" id="L438" title="All 2 branches missed."> }</span>
<span class="nc" id="L439"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L440"> } catch (JSONException e) { return err(400,e.getMessage()); } catch (SQLException e) { return dbError(e); }</span>
<span class="nc" id="L441"> 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="L446" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
try {
<span class="nc" id="L448"> JSONObject input=parse(body);</span>
<span class="nc" id="L449"> String previous=need(input,&quot;previous&quot;,120), next=need(input,&quot;next&quot;,120);</span>
<span class="nc bnc" id="L450" 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="L451"> try(Connection c=HmiDb.open();PreparedStatement ps=c.prepareStatement(</span>
<span class="nc" id="L452"> &quot;UPDATE &quot;+schema()+&quot;HMI_APP_PAGE SET MENU_GROUP=? WHERE MENU_GROUP=?&quot;)) {</span>
<span class="nc" id="L453"> ps.setString(1,next);ps.setString(2,previous);</span>
<span class="nc" id="L454"> int count=ps.executeUpdate();</span>
<span class="nc bnc" id="L455" title="All 2 branches missed."> if(count==0)return err(404,&quot;Root menu not found&quot;);</span>
<span class="nc" id="L456"> audit(c,&quot;root.renamed&quot;,previous,next);</span>
<span class="nc" id="L457"> return json(new JSONObject().put(&quot;updatedPages&quot;,count));</span>
<span class="nc bnc" id="L458" title="All 4 branches missed."> }</span>
<span class="nc" id="L459"> }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="L463" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
<span class="nc" id="L464"> JSONArray a=new JSONArray();</span>
<span class="nc" id="L465"> 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="L466" 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="L467"> return json(a);</span>
<span class="nc" id="L468"> }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="L472" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
try {
<span class="nc" id="L474"> JSONObject layout=validateTemplate(parse(body));</span>
<span class="nc bnc" id="L475" title="All 4 branches missed."> if (!scriptEditor() &amp;&amp; !scripts(layout).isEmpty())</span>
<span class="nc" id="L476"> return err(403, &quot;JavaScript- und CheerpJ-Widgets erfordern hmi-script-editor&quot;);</span>
<span class="nc" id="L477"> try(Connection c=HmiDb.open()) {</span>
<span class="nc bnc" id="L478" title="All 2 branches missed."> if(!pageExists(c,pageId))return err(404,&quot;Page not found&quot;);</span>
<span class="nc" id="L479"> int version=1;</span>
<span class="nc" id="L480"> 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="L481" 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="L483"> String templateId=id();</span>
<span class="nc" id="L484"> 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="L485"> 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="L486"> p.setString(6,user());p.setTimestamp(7,Timestamp.from(Instant.now()));p.executeUpdate();</span>
}
<span class="nc" id="L488"> audit(c,&quot;template.created&quot;,templateId,pageId);</span>
<span class="nc" id="L489"> 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="L490" title="All 2 branches missed."> }</span>
<span class="nc" id="L491"> } 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="L495" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
try {
<span class="nc" id="L497"> JSONObject layout=validateTemplate(parse(body));</span>
<span class="nc" id="L498"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L499"> c.setAutoCommit(false);</span>
try {
<span class="nc" id="L501"> String current = null;</span>
// Lock the draft while comparing its scripts and persisting the new version.
<span class="nc" id="L503"> boolean oracle = HmiDb.isOracle();</span>
<span class="nc bnc" id="L504" title="All 2 branches missed."> String sql = oracle</span>
<span class="nc" id="L505"> ? &quot;SELECT LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=? AND STATUS='DRAFT' FOR UPDATE&quot;</span>
<span class="nc" id="L506"> : &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="L507"> try(PreparedStatement locked=c.prepareStatement(sql)) {</span>
<span class="nc" id="L508"> locked.setString(1,templateId);</span>
<span class="nc" id="L509"> try(ResultSet rs=locked.executeQuery()) {</span>
<span class="nc bnc" id="L510" title="All 2 branches missed."> if(rs.next())current=clob(rs,&quot;LAYOUT_JSON&quot;);</span>
}
}
<span class="nc bnc" id="L513" 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="L514" title="All 4 branches missed."> if (!scriptEditor() &amp;&amp; scriptChanges(objectOrEmpty(current),layout)) {</span>
<span class="nc" id="L515"> c.rollback();return err(403,&quot;Code- oder JNLP-Änderungen erfordern die Rolle hmi-script-editor&quot;);</span>
}
// Widgets with revision history may only change through a valid checkout/checkin.
<span class="nc" id="L518"> JSONObject savedLayout=objectOrEmpty(current);</span>
<span class="nc" id="L519"> JSONArray incomingWidgets=layout.getJSONArray(&quot;widgets&quot;);</span>
<span class="nc" id="L520"> JSONArray savedWidgets=savedLayout.optJSONArray(&quot;widgets&quot;);</span>
<span class="nc bnc" id="L521" title="All 4 branches missed."> if(savedWidgets!=null)for(int i=0;i&lt;savedWidgets.length();i++){</span>
<span class="nc" id="L522"> JSONObject oldWidget=savedWidgets.getJSONObject(i);</span>
<span class="nc" id="L523"> JSONObject candidate=null;</span>
<span class="nc bnc" id="L524" title="All 4 branches missed."> for(int j=0;j&lt;incomingWidgets.length();j++)if(oldWidget.getString(&quot;id&quot;).equals(incomingWidgets.getJSONObject(j).getString(&quot;id&quot;))){candidate=incomingWidgets.getJSONObject(j);break;}</span>
<span class="nc bnc" id="L525" title="All 4 branches missed."> if(candidate==null||oldWidget.similar(candidate))continue;</span>
<span class="nc" id="L526"> try(PreparedStatement versioned=c.prepareStatement(&quot;SELECT COUNT(*) FROM &quot;+schema()+&quot;HMI_APP_WIDGET_REVISION WHERE PAGE_ID=(SELECT PAGE_ID FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?) AND WIDGET_ID=?&quot;)){</span>
<span class="nc" id="L527"> versioned.setString(1,templateId);versioned.setString(2,oldWidget.getString(&quot;id&quot;));</span>
<span class="nc bnc" id="L528" title="All 6 branches missed."> try(ResultSet rs=versioned.executeQuery()){if(rs.next()&amp;&amp;rs.getInt(1)&gt;0){c.rollback();return err(409,&quot;Versioniertes Widget &quot;+oldWidget.getString(&quot;id&quot;)+&quot; zuerst auschecken und einchecken&quot;);}}</span>
<span class="nc bnc" id="L529" title="All 2 branches missed."> }</span>
}
<span class="nc" id="L531"> 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="L532"> 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="L533" 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="L534" title="All 2 branches missed."> }</span>
<span class="nc" id="L535"> audit(c,&quot;template.saved&quot;,templateId,&quot;&quot;);</span>
<span class="nc bnc" id="L536" 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="L537"> c.commit();</span>
<span class="nc" id="L538"> } catch(SQLException | RuntimeException ex) {c.rollback();throw ex;}</span>
<span class="nc bnc" id="L539" title="All 8 branches missed."> }</span>
<span class="nc" id="L540"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L541"> }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="L545" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
<span class="nc" id="L546"> 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="L547"> p.setString(1,templateId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L548" title="All 2 branches missed."> if(!rs.next())return err(404,&quot;Template not found&quot;);</span>
<span class="nc" id="L549"> return json(new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;pageId&quot;,rs.getString(2))</span>
<span class="nc" id="L550"> .put(&quot;version&quot;,rs.getInt(3)).put(&quot;status&quot;,rs.getString(4))</span>
<span class="nc" id="L551"> .put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;))));</span>
<span class="nc bnc" id="L552" title="All 2 branches missed."> }</span>
<span class="nc bnc" id="L553" 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="L557" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc" id="L558"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L559"> 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="L560" 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="L561" title="All 2 branches missed."> }</span>
<span class="nc" id="L562"> 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="L563" 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="L564" title="All 2 branches missed."> }</span>
<span class="nc" id="L565"> audit(c,&quot;template.archived&quot;,templateId,&quot;&quot;);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L566" 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="L570" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc" id="L571"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L572"> c.setAutoCommit(false);</span>
try {
String pageId;
<span class="nc" id="L575"> 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="L576"> p.setString(1,templateId);</span>
<span class="nc" id="L577"> try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L578" title="All 2 branches missed."> if(!rs.next()){c.rollback();return err(404,&quot;Template not found&quot;);}</span>
<span class="nc" id="L579"> pageId=rs.getString(1);</span>
<span class="nc bnc" id="L580" 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="L581" title="All 4 branches missed."> }</span>
<span class="nc bnc" id="L582" 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="L585"> 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="L586" 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="L588"> audit(c,&quot;template.published&quot;,templateId,pageId);c.commit();</span>
<span class="nc" id="L589"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L590"> }catch(Exception ex){c.rollback();throw ex;}</span>
<span class="nc bnc" id="L591" 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="L595" title="All 2 branches missed."> Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L596"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L597"> JSONObject page=fetchPage(c,pageId);</span>
<span class="nc bnc" id="L598" title="All 2 branches missed."> if(page==null)return err(404,&quot;Page not found&quot;);</span>
<span class="nc bnc" id="L599" title="All 2 branches missed."> if(!designer()){</span>
<span class="nc" id="L600"> java.util.Set&lt;String&gt; seen=new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L601"> JSONObject ancestor=page;</span>
<span class="nc bnc" id="L602" title="All 2 branches missed."> while(ancestor!=null){</span>
<span class="nc bnc" id="L603" 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="L604" 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="L605" title="All 2 branches missed."> ancestor=parent==null?null:fetchPage(c,parent);</span>
<span class="nc bnc" id="L606" 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="L607"> }</span>
}
<span class="nc" id="L609"> List&lt;String&gt; groups=groups(c,user());</span>
<span class="nc" id="L610"> JSONObject template=resolveTemplate(c,pageId,station,groups);</span>
<span class="nc bnc" id="L611" title="All 2 branches missed."> if(template==null)return err(404,&quot;No published template assigned&quot;);</span>
<span class="nc" id="L612"> 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="L613"> merge(prefs,preference(c,&quot;DEFAULT&quot;,&quot;*&quot;));</span>
<span class="nc bnc" id="L614" title="All 2 branches missed."> for(String g:groups)merge(prefs,preference(c,&quot;GROUP&quot;,g));</span>
<span class="nc bnc" id="L615" 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="L616"> merge(prefs,preference(c,&quot;USER&quot;,user()));</span>
<span class="nc" id="L617"> JSONObject overrides=new JSONObject();String baseId=&quot;&quot;;</span>
<span class="nc" id="L618"> 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="L619"> p.setString(1,user());p.setString(2,pageId);p.setString(3, stationKey(station));</span>
<span class="nc bnc" id="L620" 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="L622"> JSONObject layout=template.getJSONObject(&quot;layout&quot;);</span>
<span class="nc" id="L623"> JSONArray effectiveWidgets=new JSONArray();</span>
<span class="nc" id="L624"> java.util.Set&lt;String&gt; granted = HmiAuth.roles(request);</span>
<span class="nc bnc" id="L625" title="All 2 branches missed."> for(Object val:layout.getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L626"> 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="L628"> JSONArray required = widget.optJSONArray(&quot;visibleToRoles&quot;);</span>
<span class="nc bnc" id="L629" 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="L630"> boolean allowed=false;</span>
<span class="nc bnc" id="L631" 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="L632" title="All 2 branches missed."> if(!allowed)continue;</span>
}
<span class="nc bnc" id="L634" title="All 4 branches missed."> if(widget.optBoolean(&quot;personalizable&quot;,true)&amp;&amp;overrides.has(widgetId)){</span>
<span class="nc" id="L635"> JSONObject o=overrides.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L636" 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="L637" 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="L638" 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="L640"> effectiveWidgets.put(widget);</span>
<span class="nc" id="L641"> }</span>
<span class="nc" id="L642"> return json(new JSONObject().put(&quot;page&quot;,page).put(&quot;templateId&quot;,template.getString(&quot;id&quot;))</span>
<span class="nc" id="L643"> .put(&quot;version&quot;,template.getInt(&quot;version&quot;)).put(&quot;originalTemplateId&quot;,baseId)</span>
<span class="nc" id="L644"> .put(&quot;layout&quot;,new JSONObject().put(&quot;widgets&quot;,effectiveWidgets)).put(&quot;preferences&quot;,prefs));</span>
<span class="nc bnc" id="L645" 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="L649" title="All 2 branches missed."> Response r=writeAllowed(true);if(r!=null)return r;</span>
try {
<span class="nc" id="L651"> JSONObject in=parse(body);</span>
<span class="nc" id="L652"> JSONObject o=validateOverrides(in.getJSONObject(&quot;overrides&quot;));</span>
<span class="nc" id="L653"> try(Connection c=HmiDb.open()){</span>
<span class="nc bnc" id="L654" 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="L656"> JSONObject template=resolveTemplate(c,pageId,in.optString(&quot;station&quot;,&quot;&quot;),groups(c,user()));</span>
<span class="nc bnc" id="L657" title="All 2 branches missed."> if(template==null)return err(409,&quot;Page has no published template&quot;);</span>
<span class="nc" id="L658"> JSONObject allowed=new JSONObject();</span>
<span class="nc bnc" id="L659" title="All 2 branches missed."> for(Object obj:template.getJSONObject(&quot;layout&quot;).getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L660"> JSONObject w=(JSONObject)obj;String widgetId=w.getString(&quot;id&quot;);</span>
<span class="nc bnc" id="L661" title="All 4 branches missed."> if(!o.has(widgetId) || !w.optBoolean(&quot;personalizable&quot;,true))continue;</span>
<span class="nc" id="L662"> JSONObject override=o.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L663" 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="L664"> allowed.put(widgetId,override);</span>
<span class="nc" id="L665"> }</span>
int count;
<span class="nc" id="L667"> 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="L668"> 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="L670" 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="L671"> 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="L673"> audit(c,&quot;user.layout&quot;,pageId,&quot;Saved overrides&quot;);</span>
<span class="nc" id="L674"> return json(new JSONObject().put(&quot;ok&quot;,true).put(&quot;saved&quot;,allowed.length()));</span>
<span class="nc bnc" id="L675" title="All 6 branches missed."> }</span>
<span class="nc" id="L676"> }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="L679" title="All 2 branches missed."> public Response myPreferences(){Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L680"> 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="L682" title="All 2 branches missed."> public Response myPreferencesSave(String body){Response r=writeAllowed(true);if(r!=null)return r;</span>
<span class="nc" id="L683"> 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="L685" 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="L686" title="All 2 branches missed."> if(!legalKey(group))return err(400,&quot;Invalid group&quot;);</span>
<span class="nc" id="L687"> 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="L690" title="All 2 branches missed."> Response r=writeAllowed(admin());if(r!=null)return r;</span>
<span class="nc bnc" id="L691" 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="L692"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L693"> 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="L694" 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="L695" title="All 2 branches missed."> }</span>
<span class="nc" id="L696"> 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="L697"> p.setString(1,member);p.setString(2,group);p.executeUpdate();</span>
}
<span class="nc" id="L699"> audit(c,&quot;group.member.added&quot;,group,member);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L700" 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="L703" 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="L705"> 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="L706" 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="L707"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L708"> 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="L709"> check.setString(1,templateId);check.setString(2,pageId);</span>
<span class="nc bnc" id="L710" 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="L711" title="All 2 branches missed."> }</span>
<span class="nc" id="L712"> 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="L713"> 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="L715"> 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="L716" title="All 2 branches missed."> }</span>
<span class="nc" id="L717"> }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="L720"> JSONObject prefs=validatePreferences(input);</span>
<span class="nc" id="L721"> try(Connection c=HmiDb.open()){</span>
int count;
<span class="nc" id="L723"> 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="L724"> 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="L726" 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="L727"> 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="L729"> audit(c,&quot;preferences.saved&quot;,type+&quot;:&quot;+identity,&quot;&quot;);return json(prefs);</span>
<span class="nc" id="L730"> }catch(SQLException e){return dbError(e);}</span>
}
private JSONObject preference(Connection c,String type,String identity)throws SQLException{
<span class="nc" id="L733"> 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="L734"> p.setString(1,type);p.setString(2,identity);</span>
<span class="nc bnc" id="L735" 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="L738" 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="L740"> List&lt;String&gt; a=new ArrayList&lt;&gt;();</span>
<span class="nc" id="L741"> 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="L742" 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="L744"> String pers=HmiAuth.persId(c,userId);</span>
<span class="nc bnc" id="L745" title="All 2 branches missed."> if(pers!=null)try(PreparedStatement p=c.prepareStatement(</span>
<span class="nc" id="L746"> &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="L747"> p.setString(1,pers);</span>
<span class="nc bnc" id="L748" 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="L750"> return a;</span>
}
private JSONObject fetchPage(Connection c,String pageId)throws SQLException{
<span class="nc" id="L753"> 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="L754"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L755" 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="L756" 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="L757" 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="L761" 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="L763"> List&lt;String&gt; ids=new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L764" title="All 4 branches missed."> if(station!=null&amp;&amp;!station.isEmpty())ids.add(&quot;STATION:&quot;+station);</span>
<span class="nc bnc" id="L765" title="All 2 branches missed."> for(String g:groups)ids.add(&quot;GROUP:&quot;+g);</span>
<span class="nc" id="L766"> ids.add(&quot;DEFAULT:*&quot;);</span>
// Distinct selected precedence STATION &gt; GROUP &gt; DEFAULT; highest priority within a scope.
<span class="nc bnc" id="L768" title="All 2 branches missed."> for(String selection:ids){</span>
<span class="nc" id="L769"> String[] parts=selection.split(&quot;:&quot;,2);</span>
<span class="nc" id="L770"> 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="L771"> p.setString(1,pageId);p.setString(2,parts[0]);p.setString(3,parts[1]);</span>
<span class="nc bnc" id="L772" 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="L773" title="All 2 branches missed."> }</span>
<span class="nc" id="L774"> }</span>
<span class="nc" id="L775"> 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="L776"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L777" 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="L782"> 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="L783"> p.setString(1,id());p.setString(2,user());p.setString(3,action);p.setString(4,entity);</span>
<span class="nc bnc" id="L784" 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="L786"> }</span>
private Response dbError(Exception ex){
<span class="nc" id="L788"> 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="L789"> 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.14.202510111229</span></div></body></html>