792 lines
84 KiB
HTML
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> > <a href="index.source.html" class="el_package">com.workbenchclassic.hmi</a> > <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("/hmi/v1")
|
|
@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 = "HMI_CSRF";
|
|
<span class="nc" id="L44"> private static final Set<String> WIDGET_TYPES = new HashSet<>();</span>
|
|
static {
|
|
<span class="nc" id="L46"> Collections.addAll(WIDGET_TYPES, "ui.text", "ui.table", "ui.button", "ui.header", "ui.image", "ui.script", "ui.curved-header", "ui.neon-actions", "ui.metal-panel", "ui.cheerpj", "ui.cheerpj-workplace", "mes.active-order",</span>
|
|
"mes.serial-scanner", "mes.station-status", "mes.booking-table", "mes.workplan",
|
|
"wb.station-tree", "wb.production-overview", "wb.station-config", "wb.workorders", "wb.history",
|
|
"wb.activities", "wb.activity-designer", "wb.contacts", "wb.erp-groups",
|
|
"wb.users", "wb.bom-import", "wb.part-import", "wb.station-import",
|
|
"wb.erp-import", "wb.contact-import", "wb.user-import");
|
|
<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("error", 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, "IMSAPI-Anmeldung erforderlich");</span>
|
|
<span class="nc bnc" id="L64" title="All 2 branches missed."> if (!HmiSchemaBootstrap.isReady()) return err(503, "HMI schema not ready; check server logs");</span>
|
|
<span class="nc bnc" id="L65" title="All 2 branches missed."> if (!allowed) return err(403, "Insufficient role");</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, "CSRF token missing or invalid") : 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 "*";</span>
|
|
<span class="nc bnc" id="L75" title="All 2 branches missed."> if (!station.matches("[A-Za-z0-9_.-]{1,120}")) throw new JSONException("Invalid station number");</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 ? "{}" : 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() > max) throw new JSONException("Invalid '" + key + "'");</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 < min || n > max) throw new JSONException("Out of range: " + k);</span>
|
|
<span class="nc" id="L88"> return n;</span>
|
|
}
|
|
<span class="nc" id="L90"> private static boolean legalKey(String s) { return s.matches("[A-Za-z0-9_.-]{1,120}"); }</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() > 1000000) throw new SQLException("JSON too large"); }</span>
|
|
<span class="nc" id="L97"> return sb.toString();</span>
|
|
<span class="nc" id="L98"> } catch (java.io.IOException e) { throw new SQLException("Cannot read CLOB", 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 "fontScale": out.put(key, boundedScale(input.getDouble(key))); break;</span>
|
|
<span class="nc bnc" id="L109" title="All 10 branches missed."> case "theme": { String s = input.getString(key); if (!s.equals("dark") && !s.equals("light") && !s.equals("system") && !s.equals("ios") && !s.equals("neon")) throw new JSONException("Invalid theme"); out.put(key, s); break; }</span>
|
|
<span class="nc bnc" id="L110" title="All 4 branches missed."> case "contrast": { String s = input.getString(key); if (!s.equals("normal") && !s.equals("high")) throw new JSONException("Invalid contrast"); out.put(key, s); break; }</span>
|
|
<span class="nc bnc" id="L111" title="All 2 branches missed."> case "accent": { String s = input.getString(key); if (!s.matches("#[0-9a-fA-F]{6}")) throw new JSONException("Invalid accent color"); out.put(key, s); break; }</span>
|
|
<span class="nc" id="L112"> default: throw new JSONException("Unknown preference: " + 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 < 0.75 || x > 2.0) throw new JSONException("fontScale must be 0.75 to 2.0"); return x; }</span>
|
|
static JSONObject validateTemplate(JSONObject layout) {
|
|
<span class="nc" id="L119"> JSONArray widgets = layout.getJSONArray("widgets");</span>
|
|
<span class="nc bnc" id="L120" title="All 2 branches missed."> if (widgets.length() > 80) throw new JSONException("Maximum 80 widgets");</span>
|
|
<span class="nc" id="L121"> Set<String> ids = new HashSet<>();</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 < widgets.length(); i++) {</span>
|
|
<span class="nc" id="L124"> JSONObject w = widgets.getJSONObject(i);</span>
|
|
<span class="nc" id="L125"> String widgetId = need(w, "id", 120); String type = need(w, "type", 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("Invalid or duplicate widget ID/type");</span>
|
|
<span class="nc bnc" id="L127" title="All 2 branches missed."> if ("ui.script".equals(type)) {</span>
|
|
<span class="nc" id="L128"> JSONObject properties = w.optJSONObject("props");</span>
|
|
<span class="nc bnc" id="L129" title="All 2 branches missed."> Object source = properties == null ? "" : properties.opt("script");</span>
|
|
<span class="nc bnc" id="L130" title="All 4 branches missed."> if (source != null && !(source instanceof String)) throw new JSONException("JavaScript must be text");</span>
|
|
<span class="nc bnc" id="L131" title="All 4 branches missed."> if (source instanceof String && ((String) source).length() > 8000)</span>
|
|
<span class="nc" id="L132"> throw new JSONException("JavaScript limited to 8000 characters");</span>
|
|
}
|
|
<span class="nc bnc" id="L134" title="All 4 branches missed."> if ("ui.cheerpj".equals(type) || "ui.cheerpj-workplace".equals(type)) {</span>
|
|
<span class="nc" id="L135"> JSONObject properties = w.optJSONObject("props");</span>
|
|
<span class="nc bnc" id="L136" title="All 2 branches missed."> if (properties == null) throw new JSONException("CheerpJ-Konfiguration fehlt");</span>
|
|
<span class="nc" id="L137"> String url = properties.optString("jnlpUrl", "");</span>
|
|
<span class="nc bnc" id="L138" title="All 8 branches missed."> if (url.length() > 1600 || (!url.isEmpty() && !url.startsWith("/backend/api/hmi/v1/cheerpj/workplace/") && !url.matches("https?://[^\\s]+")))</span>
|
|
<span class="nc" id="L139"> throw new JSONException("Ungültige JNLP-URL");</span>
|
|
<span class="nc bnc" id="L140" title="All 2 branches missed."> if ("ui.cheerpj-workplace".equals(type)</span>
|
|
<span class="nc bnc" id="L141" title="All 2 branches missed."> && (!"/backend/api/hmi/v1/cheerpj/workplace/manifest.jnlp".equals(url)</span>
|
|
<span class="nc bnc" id="L142" title="All 2 branches missed."> || properties.optInt("javaVersion", 8) != 11))</span>
|
|
<span class="nc" id="L143"> throw new JSONException("iTAC-Workplace-URL und Java 11 sind fest vorgegeben");</span>
|
|
<span class="nc bnc" id="L144" title="All 4 branches missed."> if (properties.has("manualStartOnly") && !(properties.opt("manualStartOnly") instanceof Boolean))</span>
|
|
<span class="nc" id="L145"> throw new JSONException("manualStartOnly muss boolean sein");</span>
|
|
<span class="nc" id="L146"> String displayMode = properties.optString("displayMode", "fit");</span>
|
|
<span class="nc bnc" id="L147" title="All 4 branches missed."> if (!"fit".equals(displayMode) && !"original".equals(displayMode))</span>
|
|
<span class="nc" id="L148"> throw new JSONException("CheerpJ displayMode must be fit or original");</span>
|
|
<span class="nc" id="L149"> int javaVersion = properties.optInt("javaVersion", 8);</span>
|
|
<span class="nc bnc" id="L150" title="All 6 branches missed."> if (javaVersion != 8 && javaVersion != 11 && javaVersion != 17)</span>
|
|
<span class="nc" id="L151"> throw new JSONException("CheerpJ Java-Version muss 8, 11 oder 17 sein");</span>
|
|
}
|
|
<span class="nc" id="L153"> JSONObject item = new JSONObject().put("id", widgetId).put("type", type)</span>
|
|
<span class="nc" id="L154"> .put("x", bounded(w,"x",0,11,0)).put("y",bounded(w,"y",0,200,0))</span>
|
|
<span class="nc" id="L155"> .put("w",bounded(w,"w",1,12,4)).put("h",bounded(w,"h",1,20,3))</span>
|
|
<span class="nc" id="L156"> .put("mandatory",w.optBoolean("mandatory",false)).put("personalizable",w.optBoolean("personalizable",true))</span>
|
|
<span class="nc" id="L157"> .put("visibleToRoles",validWidgetRoles(w.optJSONArray("visibleToRoles")))</span>
|
|
<span class="nc bnc" id="L158" title="All 2 branches missed."> .put("props",w.optJSONObject("props") == null ? new JSONObject() : w.getJSONObject("props"));</span>
|
|
<span class="nc bnc" id="L159" title="All 2 branches missed."> if ("ui.cheerpj-workplace".equals(type)) {</span>
|
|
<span class="nc" id="L160"> JSONArray visibility = item.getJSONArray("visibleToRoles");</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 < visibility.length(); k++)</span>
|
|
<span class="nc bnc" id="L163" title="All 2 branches missed."> if ("hmi-workplace".equals(visibility.optString(k))) allowed = true;</span>
|
|
<span class="nc bnc" id="L164" title="All 2 branches missed."> if (!allowed) throw new JSONException("iTAC Workplace requires the hmi-workplace visibility role");</span>
|
|
}
|
|
<span class="nc bnc" id="L166" title="All 4 branches missed."> if (item.optBoolean("mandatory") && item.getJSONArray("visibleToRoles").length() > 0)</span>
|
|
<span class="nc" id="L167"> throw new JSONException("Mandatory widgets cannot be restricted to specific roles");</span>
|
|
<span class="nc bnc" id="L168" title="All 2 branches missed."> if (item.getInt("x") + item.getInt("w") > 12) throw new JSONException("Widget exceeds grid width");</span>
|
|
<span class="nc bnc" id="L169" title="All 2 branches missed."> if (item.toString().length() > 16000) throw new JSONException("Widget props too large");</span>
|
|
<span class="nc" id="L170"> out.put(item);</span>
|
|
}
|
|
<span class="nc" id="L172"> cleaned.put("widgets", 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<String,String> scripts(JSONObject layout) {
|
|
<span class="nc" id="L177"> java.util.Map<String,String> map = new java.util.HashMap<>();</span>
|
|
<span class="nc" id="L178"> JSONArray items = layout.optJSONArray("widgets");</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<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 ("ui.script".equals(item.optString("type"))) {</span>
|
|
<span class="nc" id="L183"> JSONObject props = item.optJSONObject("props");</span>
|
|
<span class="nc bnc" id="L184" title="All 2 branches missed."> map.put(item.getString("id"), props == null ? "" : props.optString("script", ""));</span>
|
|
}
|
|
<span class="nc bnc" id="L186" title="All 4 branches missed."> if ("ui.cheerpj".equals(item.optString("type")) || "ui.cheerpj-workplace".equals(item.optString("type"))) {</span>
|
|
<span class="nc" id="L187"> JSONObject props = item.optJSONObject("props");</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("jnlpUrl", props.optString("jnlpUrl", ""))</span>
|
|
<span class="nc" id="L190"> .put("javaVersion", props.optInt("javaVersion", 8))</span>
|
|
<span class="nc" id="L191"> .put("autoStart", props.optBoolean("autoStart", false))</span>
|
|
<span class="nc" id="L192"> .put("manualStartOnly", props.optBoolean("manualStartOnly", false))</span>
|
|
<span class="nc" id="L193"> .put("displayMode", props.optString("displayMode", "fit"));</span>
|
|
<span class="nc" id="L194"> map.put(item.getString("id"), "CHEERPJ:" + item.optString("type") + ":" + 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() > 30) throw new JSONException("Too many widget roles");</span>
|
|
<span class="nc" id="L207"> java.util.Set<String> seen = new java.util.HashSet<>();</span>
|
|
<span class="nc bnc" id="L208" title="All 2 branches missed."> for (int i=0;i<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("Invalid widget role");</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() > 80) throw new JSONException("Too many overrides");</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("Invalid widget ID");</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("x") && !k.equals("y") && !k.equals("w") && !k.equals("h") && !k.equals("hidden")) throw new JSONException("Forbidden override");</span>
|
|
<span class="nc" id="L222"> JSONObject o = new JSONObject()</span>
|
|
<span class="nc" id="L223"> .put("x", bounded(p,"x",0,11,0)).put("y",bounded(p,"y",0,200,0))</span>
|
|
<span class="nc" id="L224"> .put("w",bounded(p,"w",1,12,4)).put("h",bounded(p,"h",1,20,3))</span>
|
|
<span class="nc" id="L225"> .put("hidden",p.optBoolean("hidden",false));</span>
|
|
<span class="nc bnc" id="L226" title="All 2 branches missed."> if (o.getInt("x") + o.getInt("w") > 12) throw new JSONException("Widget exceeds width");</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("diagnostics")
|
|
public Response diagnostics() {
|
|
<span class="nc bnc" id="L234" title="All 2 branches missed."> if (user() == null) return err(401, "IMSAPI-Anmeldung erforderlich");</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 && !isAdmin) return err(403, "hmi-admin role required");</span>
|
|
<span class="nc" id="L238"> JSONObject out = new JSONObject()</span>
|
|
<span class="nc bnc" id="L239" title="All 4 branches missed."> .put("access", !initialized && !isAdmin ? "INITIALIZATION_READ_ONLY" : "ADMIN")</span>
|
|
<span class="nc" id="L240"> .put("user", user())</span>
|
|
<span class="nc" id="L241"> .put("readyAtDeployment", HmiSchemaBootstrap.isReady())</span>
|
|
<span class="nc" id="L242"> .put("initializationPhase", HmiSchemaBootstrap.phase())</span>
|
|
<span class="nc" id="L243"> .put("failureCode", HmiSchemaBootstrap.failureCode())</span>
|
|
<span class="nc" id="L244"> .put("pool", "dsCustNJTA");</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("oracle");</span>
|
|
<span class="nc" id="L248"> boolean sqlServer = db.toLowerCase(java.util.Locale.ROOT).contains("sql server");</span>
|
|
<span class="nc bnc" id="L249" title="All 4 branches missed."> out.put("poolReachableNow", true).put("database", oracle ? "ORACLE" : sqlServer ? "SQL_SERVER" : "UNKNOWN")</span>
|
|
<span class="nc bnc" id="L250" title="All 2 branches missed."> .put("schema", oracle ? "CUST" : "cust");</span>
|
|
<span class="nc bnc" id="L251" title="All 2 branches missed."> try (PreparedStatement p = c.prepareStatement(oracle ? "SELECT 1 FROM DUAL" : "SELECT 1");</span>
|
|
<span class="nc" id="L252"> ResultSet rs = p.executeQuery()) {</span>
|
|
<span class="nc bnc" id="L253" title="All 4 branches missed."> out.put("connectionQueryOk", rs.next() && rs.getInt(1) == 1);</span>
|
|
}
|
|
<span class="nc" id="L255"> int found = 0;</span>
|
|
<span class="nc" id="L256"> String[] tables = {"HMI_APP_PAGE", "HMI_APP_LAYOUT", "HMI_APP_PREF", "HMI_APP_CSS_REVISION", "HMI_APP_SCHEMA_VERSION"};</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 ? "CUST" : "cust", t, new String[]{"TABLE"})) {</span>
|
|
<span class="nc bnc" id="L259" title="All 2 branches missed."> if (rs.next()) found++;</span>
|
|
}
|
|
}
|
|
<span class="nc" id="L262"> out.put("tablesFound", found).put("tablesExpected", 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, "Authenticated HMI diagnostics failed", e);</span>
|
|
<span class="nc" id="L267"> return json(out.put("poolReachableNow", false).put("connectionQueryOk", false).put("failure", "CHECK_GLASSFISH_LOG"));</span>
|
|
}
|
|
}
|
|
|
|
@GET @Path("status")
|
|
<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("ready",true).put("detail",HmiSchemaBootstrap.state())); }</span>
|
|
@GET @Path("widgets")
|
|
<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("type",t).put("name",t.replace("mes.", "MES: ").replace("ui.", "UI: ")));</span>
|
|
<span class="nc" id="L277"> return json(a);</span>
|
|
}
|
|
@GET @Path("roles")
|
|
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<String> seen=new java.util.TreeSet<>();</span>
|
|
<span class="nc" id="L283"> java.util.Collections.addAll(seen,"hmi-admin","hmi-designer","hmi-publisher","hmi-script-editor");</span>
|
|
<span class="nc" id="L284"> try(Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(</span>
|
|
<span class="nc" id="L285"> "SELECT ROLE_KEY FROM "+schema()+"HMI_APP_ROLE ORDER BY ROLE_KEY");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("key",name).put("description",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<String,JSONObject> byId) {
|
|
<span class="nc" id="L293"> java.util.Set<String> seen=new java.util.HashSet<>();</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("enabled"))return false;</span>
|
|
<span class="nc bnc" id="L299" title="All 2 branches missed."> current=page.isNull("parentPageId")?null:page.optString("parentPageId",null);</span>
|
|
<span class="nc" id="L300"> }</span>
|
|
<span class="nc" id="L301"> return true;</span>
|
|
}
|
|
@GET @Path("pages")
|
|
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<String,JSONObject> found=new java.util.LinkedHashMap<>();</span>
|
|
<span class="nc" id="L308"> try(Connection c=HmiDb.open();PreparedStatement ps=c.prepareStatement(</span>
|
|
<span class="nc" id="L309"> "SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID FROM " + schema() + "HMI_APP_PAGE ORDER BY SORT_ORDER,TITLE");</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("id",rs.getString(1)).put("key",rs.getString(2)).put("title",rs.getString(3))</span>
|
|
<span class="nc bnc" id="L314" title="All 2 branches missed."> .put("menuGroup",rs.getString(4)).put("order",rs.getInt(5)).put("enabled",rs.getInt(6)==1)</span>
|
|
<span class="nc bnc" id="L315" title="All 2 branches missed."> .put("parentPageId",parent==null?JSONObject.NULL:parent);</span>
|
|
<span class="nc" id="L316"> found.put(page.getString("id"),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("id"),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("parentPageId") || !input.has("parentPageId")) return null;</span>
|
|
<span class="nc" id="L325"> String value = input.getString("parentPageId").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("Invalid parentPageId");</span>
|
|
<span class="nc" id="L328"> return value;</span>
|
|
}
|
|
private static String pageMenu(JSONObject input) {
|
|
<span class="nc" id="L331"> String menu = input.optString("menuGroup", "Allgemein").trim();</span>
|
|
<span class="nc bnc" id="L332" title="All 6 branches missed."> if (menu.isEmpty() || menu.length() > 120 || menu.indexOf('/') >= 0)</span>
|
|
<span class="nc" id="L333"> throw new JSONException("Invalid root menu name");</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("SELECT MENU_GROUP FROM " + schema() + "HMI_APP_PAGE WHERE ID=?")) {</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("SELECT PARENT_PAGE_ID FROM " + schema() + "HMI_APP_PAGE WHERE ID=?")) {</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<String> visited = new java.util.HashSet<>();</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 > 32 || !visited.add(candidate) || candidate.equals(currentId))</span>
|
|
<span class="nc" id="L358"> throw new JSONException("Navigation cycle or depth limit detected");</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("Parent page not found");</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<String> seen = new java.util.HashSet<>();</span>
|
|
<span class="nc" id="L367"> java.util.ArrayDeque<String> pending = new java.util.ArrayDeque<>();</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("SELECT ID FROM " + schema() + "HMI_APP_PAGE WHERE PARENT_PAGE_ID=?")) {</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("Navigation cycle in stored page tree");</span>
|
|
<span class="nc bnc" id="L377" title="All 2 branches missed."> if (seen.size()>1000) throw new SQLException("Too many navigation pages");</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("UPDATE " + schema() + "HMI_APP_PAGE SET MENU_GROUP=? WHERE ID=?")) {</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("pages") @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,"key",120); if(!legalKey(key)) return err(400,"Invalid page key");</span>
|
|
<span class="nc" id="L395"> String title=need(input,"title",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,"order",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"> "INSERT INTO "+schema()+"HMI_APP_PAGE(ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID) VALUES (?,?,?,?,?,1,?)")) {</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,"page.created",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("id",pageId).put("key",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,"Cannot create page"); }</span>
|
|
}
|
|
@PUT @Path("pages/{pageId}") @Consumes(MediaType.APPLICATION_JSON)
|
|
public Response updatePage(@PathParam("pageId") 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,"title",255), menu=pageMenu(input), parent=parentId(input);</span>
|
|
<span class="nc" id="L421"> int order=bounded(input,"order",0,100000,0);</span>
|
|
<span class="nc" id="L422"> boolean enabled=input.optBoolean("enabled",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,"Page not found");}</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"> "UPDATE "+schema()+"HMI_APP_PAGE SET TITLE=?,MENU_GROUP=?,SORT_ORDER=?,ENABLED=?,PARENT_PAGE_ID=? WHERE ID=?")) {</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,"page.updated",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("ok",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,"Cannot update page"); }</span>
|
|
}
|
|
/** Rename a root menu without changing individual pages' parent relationships. */
|
|
@PUT @Path("navigation/root") @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,"previous",120), next=need(input,"next",120);</span>
|
|
<span class="nc bnc" id="L450" title="All 2 branches missed."> if(!next.equals(pageMenu(new JSONObject().put("menuGroup",next))))return err(400,"Invalid menu");</span>
|
|
<span class="nc" id="L451"> try(Connection c=HmiDb.open();PreparedStatement ps=c.prepareStatement(</span>
|
|
<span class="nc" id="L452"> "UPDATE "+schema()+"HMI_APP_PAGE SET MENU_GROUP=? WHERE MENU_GROUP=?")) {</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,"Root menu not found");</span>
|
|
<span class="nc" id="L456"> audit(c,"root.renamed",previous,next);</span>
|
|
<span class="nc" id="L457"> return json(new JSONObject().put("updatedPages",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("pages/{pageId}/templates")
|
|
public Response templates(@PathParam("pageId")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("SELECT ID,VERSION_NO,STATUS,UPDATED_BY FROM "+schema()+"HMI_APP_TEMPLATE WHERE PAGE_ID=? ORDER BY VERSION_NO DESC")) {</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("id",rs.getString(1)).put("version",rs.getInt(2)).put("status",rs.getString(3)).put("author",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("pages/{pageId}/templates") @Consumes(MediaType.APPLICATION_JSON)
|
|
public Response createTemplate(@PathParam("pageId") 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() && !scripts(layout).isEmpty())</span>
|
|
<span class="nc" id="L476"> return err(403, "JavaScript- und CheerpJ-Widgets erfordern hmi-script-editor");</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,"Page not found");</span>
|
|
<span class="nc" id="L479"> int version=1;</span>
|
|
<span class="nc" id="L480"> try(PreparedStatement p=c.prepareStatement("SELECT MAX(VERSION_NO) FROM "+schema()+"HMI_APP_TEMPLATE WHERE PAGE_ID=?")){</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("INSERT INTO "+schema()+"HMI_APP_TEMPLATE(ID,PAGE_ID,VERSION_NO,STATUS,LAYOUT_JSON,UPDATED_BY,UPDATED_AT) VALUES (?,?,?,?,?,?,?)")) {</span>
|
|
<span class="nc" id="L485"> p.setString(1,templateId);p.setString(2,pageId);p.setInt(3,version);p.setString(4,"DRAFT");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,"template.created",templateId,pageId);</span>
|
|
<span class="nc" id="L489"> return json(new JSONObject().put("id",templateId).put("version",version).put("status","DRAFT"));</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("templates/{templateId}") @Consumes(MediaType.APPLICATION_JSON)
|
|
public Response saveTemplate(@PathParam("templateId")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"> ? "SELECT LAYOUT_JSON FROM "+schema()+"HMI_APP_TEMPLATE WHERE ID=? AND STATUS='DRAFT' FOR UPDATE"</span>
|
|
<span class="nc" id="L506"> : "SELECT LAYOUT_JSON FROM "+schema()+"HMI_APP_TEMPLATE WITH (UPDLOCK, ROWLOCK) WHERE ID=? AND STATUS='DRAFT'";</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,"LAYOUT_JSON");</span>
|
|
}
|
|
}
|
|
<span class="nc bnc" id="L513" title="All 2 branches missed."> if(current==null) { c.rollback();return err(409,"Template not a draft or not found"); }</span>
|
|
<span class="nc bnc" id="L514" title="All 4 branches missed."> if (!scriptEditor() && scriptChanges(objectOrEmpty(current),layout)) {</span>
|
|
<span class="nc" id="L515"> c.rollback();return err(403,"Code- oder JNLP-Änderungen erfordern die Rolle hmi-script-editor");</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("widgets");</span>
|
|
<span class="nc" id="L520"> JSONArray savedWidgets=savedLayout.optJSONArray("widgets");</span>
|
|
<span class="nc bnc" id="L521" title="All 4 branches missed."> if(savedWidgets!=null)for(int i=0;i<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<incomingWidgets.length();j++)if(oldWidget.getString("id").equals(incomingWidgets.getJSONObject(j).getString("id"))){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("SELECT COUNT(*) FROM "+schema()+"HMI_APP_WIDGET_REVISION WHERE PAGE_ID=(SELECT PAGE_ID FROM "+schema()+"HMI_APP_TEMPLATE WHERE ID=?) AND WIDGET_ID=?")){</span>
|
|
<span class="nc" id="L527"> versioned.setString(1,templateId);versioned.setString(2,oldWidget.getString("id"));</span>
|
|
<span class="nc bnc" id="L528" title="All 6 branches missed."> try(ResultSet rs=versioned.executeQuery()){if(rs.next()&&rs.getInt(1)>0){c.rollback();return err(409,"Versioniertes Widget "+oldWidget.getString("id")+" zuerst auschecken und einchecken");}}</span>
|
|
<span class="nc bnc" id="L529" title="All 2 branches missed."> }</span>
|
|
}
|
|
<span class="nc" id="L531"> try(PreparedStatement p=c.prepareStatement("UPDATE "+schema()+"HMI_APP_TEMPLATE SET LAYOUT_JSON=?,UPDATED_BY=?,UPDATED_AT=? WHERE ID=? AND STATUS='DRAFT'")) {</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,"Template not a draft or not found");}</span>
|
|
<span class="nc bnc" id="L534" title="All 2 branches missed."> }</span>
|
|
<span class="nc" id="L535"> audit(c,"template.saved",templateId,"");</span>
|
|
<span class="nc bnc" id="L536" title="All 2 branches missed."> if(scriptChanges(objectOrEmpty(current),layout))audit(c,"widget.script.edited",templateId,"Code or JNLP configuration updated");</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("ok",true));</span>
|
|
<span class="nc" id="L541"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
|
|
}
|
|
@GET @Path("templates/{templateId}")
|
|
public Response getTemplate(@PathParam("templateId")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("SELECT ID,PAGE_ID,VERSION_NO,STATUS,LAYOUT_JSON FROM "+schema()+"HMI_APP_TEMPLATE WHERE ID=?")){</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,"Template not found");</span>
|
|
<span class="nc" id="L549"> return json(new JSONObject().put("id",rs.getString(1)).put("pageId",rs.getString(2))</span>
|
|
<span class="nc" id="L550"> .put("version",rs.getInt(3)).put("status",rs.getString(4))</span>
|
|
<span class="nc" id="L551"> .put("layout",objectOrEmpty(clob(rs,"LAYOUT_JSON"))));</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("templates/{templateId}/archive")
|
|
public Response archive(@PathParam("templateId")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("SELECT ID FROM "+schema()+"HMI_APP_ASSIGNMENT WHERE TEMPLATE_ID=?")) {</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,"Template is assigned; update assignments before archiving");}</span>
|
|
<span class="nc bnc" id="L561" title="All 2 branches missed."> }</span>
|
|
<span class="nc" id="L562"> try(PreparedStatement p=c.prepareStatement("UPDATE "+schema()+"HMI_APP_TEMPLATE SET STATUS='ARCHIVED' WHERE ID=? AND STATUS='PUBLISHED'")) {</span>
|
|
<span class="nc bnc" id="L563" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)return err(409,"Not published or not found");</span>
|
|
<span class="nc bnc" id="L564" title="All 2 branches missed."> }</span>
|
|
<span class="nc" id="L565"> audit(c,"template.archived",templateId,"");return json(new JSONObject().put("ok",true));</span>
|
|
<span class="nc bnc" id="L566" title="All 4 branches missed."> }catch(SQLException e){return dbError(e);}</span>
|
|
}
|
|
@POST @Path("templates/{templateId}/publish")
|
|
public Response publish(@PathParam("templateId")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("SELECT PAGE_ID,STATUS FROM "+schema()+"HMI_APP_TEMPLATE WHERE ID=?")) {</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,"Template not found");}</span>
|
|
<span class="nc" id="L579"> pageId=rs.getString(1);</span>
|
|
<span class="nc bnc" id="L580" title="All 2 branches missed."> if(!"DRAFT".equals(rs.getString(2))){c.rollback();return err(409,"Only drafts can be published");}</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("UPDATE "+schema()+"HMI_APP_TEMPLATE SET STATUS='PUBLISHED' WHERE ID=? AND STATUS='DRAFT'")){</span>
|
|
<span class="nc bnc" id="L586" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)throw new SQLException("Concurrent template publication");</span>
|
|
}
|
|
<span class="nc" id="L588"> audit(c,"template.published",templateId,pageId);c.commit();</span>
|
|
<span class="nc" id="L589"> return json(new JSONObject().put("ok",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("pages/{pageId}/effective")
|
|
public Response effective(@PathParam("pageId")String pageId,@QueryParam("station")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,"Page not found");</span>
|
|
<span class="nc bnc" id="L599" title="All 2 branches missed."> if(!designer()){</span>
|
|
<span class="nc" id="L600"> java.util.Set<String> seen=new java.util.HashSet<>();</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("id"))||!ancestor.getBoolean("enabled"))return err(404,"Page not found");</span>
|
|
<span class="nc bnc" id="L604" title="All 2 branches missed."> String parent=ancestor.isNull("parentPageId")?null:ancestor.optString("parentPageId",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&&ancestor==null)return err(404,"Page not found");</span>
|
|
<span class="nc" id="L607"> }</span>
|
|
}
|
|
<span class="nc" id="L609"> List<String> 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,"No published template assigned");</span>
|
|
<span class="nc" id="L612"> JSONObject prefs=new JSONObject().put("fontScale",1).put("theme","ios").put("contrast","normal").put("accent","#a40640");</span>
|
|
<span class="nc" id="L613"> merge(prefs,preference(c,"DEFAULT","*"));</span>
|
|
<span class="nc bnc" id="L614" title="All 2 branches missed."> for(String g:groups)merge(prefs,preference(c,"GROUP",g));</span>
|
|
<span class="nc bnc" id="L615" title="All 4 branches missed."> if(station!=null&&!station.isEmpty())merge(prefs,preference(c,"STATION",station));</span>
|
|
<span class="nc" id="L616"> merge(prefs,preference(c,"USER",user()));</span>
|
|
<span class="nc" id="L617"> JSONObject overrides=new JSONObject();String baseId="";</span>
|
|
<span class="nc" id="L618"> try(PreparedStatement p=c.prepareStatement("SELECT BASE_TEMPLATE_ID,OVERRIDES_JSON FROM "+schema()+"HMI_APP_LAYOUT WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?")){</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,"OVERRIDES_JSON"));}}</span>
|
|
}
|
|
<span class="nc" id="L622"> JSONObject layout=template.getJSONObject("layout");</span>
|
|
<span class="nc" id="L623"> JSONArray effectiveWidgets=new JSONArray();</span>
|
|
<span class="nc" id="L624"> java.util.Set<String> granted = HmiAuth.roles(request);</span>
|
|
<span class="nc bnc" id="L625" title="All 2 branches missed."> for(Object val:layout.getJSONArray("widgets")) {</span>
|
|
<span class="nc" id="L626"> JSONObject widget=new JSONObject(val.toString());String widgetId=widget.getString("id");</span>
|
|
// Security: role-restricted widgets must never be delivered in the effective payload.
|
|
<span class="nc" id="L628"> JSONArray required = widget.optJSONArray("visibleToRoles");</span>
|
|
<span class="nc bnc" id="L629" title="All 6 branches missed."> if (required!=null && required.length()>0 && !granted.contains("hmi-admin")) {</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<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("personalizable",true)&&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[]{"x","y","w","h"})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("mandatory",false)&&o.optBoolean("hidden",false))widget.put("hidden",true);</span>
|
|
<span class="nc bnc" id="L638" title="All 2 branches missed."> if(widget.getInt("x")+widget.getInt("w")>12)widget.put("x",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("page",page).put("templateId",template.getString("id"))</span>
|
|
<span class="nc" id="L643"> .put("version",template.getInt("version")).put("originalTemplateId",baseId)</span>
|
|
<span class="nc" id="L644"> .put("layout",new JSONObject().put("widgets",effectiveWidgets)).put("preferences",prefs));</span>
|
|
<span class="nc bnc" id="L645" title="All 8 branches missed."> }catch(Exception e){return dbError(e);}</span>
|
|
}
|
|
@PUT @Path("pages/{pageId}/layout") @Consumes(MediaType.APPLICATION_JSON)
|
|
public Response saveLayout(@PathParam("pageId")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("overrides"));</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,"Page not found");</span>
|
|
// Server enforcement: only published, personalizable widget IDs can be overridden.
|
|
<span class="nc" id="L656"> JSONObject template=resolveTemplate(c,pageId,in.optString("station",""),groups(c,user()));</span>
|
|
<span class="nc bnc" id="L657" title="All 2 branches missed."> if(template==null)return err(409,"Page has no published template");</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("layout").getJSONArray("widgets")) {</span>
|
|
<span class="nc" id="L660"> JSONObject w=(JSONObject)obj;String widgetId=w.getString("id");</span>
|
|
<span class="nc bnc" id="L661" title="All 4 branches missed."> if(!o.has(widgetId) || !w.optBoolean("personalizable",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("mandatory",false)&&override.optBoolean("hidden",false))return err(400,"Cannot hide mandatory widget");</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("UPDATE "+schema()+"HMI_APP_LAYOUT SET OVERRIDES_JSON=?,BASE_TEMPLATE_ID=?,UPDATED_AT=? WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?")){</span>
|
|
<span class="nc" id="L668"> clob(p,1,allowed.toString());p.setString(2,template.getString("id"));p.setTimestamp(3,Timestamp.from(Instant.now()));p.setString(4,user());p.setString(5,pageId);p.setString(6,stationKey(in.optString("station", "")));count=p.executeUpdate();</span>
|
|
}
|
|
<span class="nc bnc" id="L670" title="All 2 branches missed."> if(count==0){try(PreparedStatement p=c.prepareStatement("INSERT INTO "+schema()+"HMI_APP_LAYOUT(USER_ID,PAGE_ID,STATION_ID,BASE_TEMPLATE_ID,OVERRIDES_JSON,UPDATED_AT) VALUES(?,?,?,?,?,?)")){</span>
|
|
<span class="nc" id="L671"> p.setString(1,user());p.setString(2,pageId);p.setString(3,stationKey(in.optString("station", "")));p.setString(4,template.getString("id"));clob(p,5,allowed.toString());p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();</span>
|
|
}}
|
|
<span class="nc" id="L673"> audit(c,"user.layout",pageId,"Saved overrides");</span>
|
|
<span class="nc" id="L674"> return json(new JSONObject().put("ok",true).put("saved",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("preferences")
|
|
<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,"USER",user()));}catch(SQLException e){return dbError(e);}}</span>
|
|
@PUT @Path("preferences") @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("USER",user(),parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
|
|
@PUT @Path("groups/{group}/preferences") @Consumes(MediaType.APPLICATION_JSON)
|
|
<span class="nc bnc" id="L685" title="All 2 branches missed."> public Response groupPreferences(@PathParam("group")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,"Invalid group");</span>
|
|
<span class="nc" id="L687"> try{return storePreferences("GROUP",group,parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
|
|
@PUT @Path("groups/{group}/members/{member}")
|
|
public Response groupMember(@PathParam("group")String group,@PathParam("member")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()>255||member.isEmpty())return err(400,"Invalid group or member");</span>
|
|
<span class="nc" id="L692"> try(Connection c=HmiDb.open()){</span>
|
|
<span class="nc" id="L693"> try(PreparedStatement p=c.prepareStatement("SELECT USER_ID FROM "+schema()+"HMI_APP_GROUP_MEMBER WHERE GROUP_ID=? AND USER_ID=?")){</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("ok",true));}</span>
|
|
<span class="nc bnc" id="L695" title="All 2 branches missed."> }</span>
|
|
<span class="nc" id="L696"> try(PreparedStatement p=c.prepareStatement("INSERT INTO "+schema()+"HMI_APP_GROUP_MEMBER(USER_ID,GROUP_ID) VALUES (?,?)")){</span>
|
|
<span class="nc" id="L697"> p.setString(1,member);p.setString(2,group);p.executeUpdate();</span>
|
|
}
|
|
<span class="nc" id="L699"> audit(c,"group.member.added",group,member);return json(new JSONObject().put("ok",true));</span>
|
|
<span class="nc bnc" id="L700" title="All 2 branches missed."> }catch(SQLException e){return dbError(e);}</span>
|
|
}
|
|
@POST @Path("assignments") @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,"pageId",64), templateId=need(in,"templateId",64), type=need(in,"scopeType",20), scope=need(in,"scopeId",255);</span>
|
|
<span class="nc bnc" id="L706" title="All 6 branches missed."> if(!type.equals("GROUP")&&!type.equals("STATION")&&!type.equals("DEFAULT"))return err(400,"Invalid scope");</span>
|
|
<span class="nc" id="L707"> try(Connection c=HmiDb.open()){</span>
|
|
<span class="nc" id="L708"> try(PreparedStatement check=c.prepareStatement("SELECT STATUS FROM "+schema()+"HMI_APP_TEMPLATE WHERE ID=? AND PAGE_ID=?")){</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()||!"PUBLISHED".equals(rs.getString(1)))return err(400,"Only published templates can be assigned");}</span>
|
|
<span class="nc bnc" id="L711" title="All 2 branches missed."> }</span>
|
|
<span class="nc" id="L712"> try(PreparedStatement p=c.prepareStatement("INSERT INTO "+schema()+"HMI_APP_ASSIGNMENT(ID,PAGE_ID,SCOPE_TYPE,SCOPE_ID,TEMPLATE_ID,PRIORITY) VALUES(?,?,?,?,?,?)")){</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,"priority",0,1000,0));p.executeUpdate();</span>
|
|
}
|
|
<span class="nc" id="L715"> audit(c,"template.assigned",templateId,type+":"+scope);return json(new JSONObject().put("ok",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("UPDATE "+schema()+"HMI_APP_PREF SET PREFS_JSON=?,UPDATED_AT=? WHERE SCOPE_TYPE=? AND SCOPE_ID=?")){</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("INSERT INTO "+schema()+"HMI_APP_PREF(SCOPE_TYPE,SCOPE_ID,PREFS_JSON,UPDATED_AT) VALUES (?,?,?,?)")){</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,"preferences.saved",type+":"+identity,"");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("SELECT PREFS_JSON FROM "+schema()+"HMI_APP_PREF WHERE SCOPE_TYPE=? AND SCOPE_ID=?")){</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,"PREFS_JSON")):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<String> groups(Connection c,String userId)throws SQLException{
|
|
<span class="nc" id="L740"> List<String> a=new ArrayList<>();</span>
|
|
<span class="nc" id="L741"> try(PreparedStatement p=c.prepareStatement("SELECT GROUP_ID FROM "+schema()+"HMI_APP_GROUP_MEMBER WHERE USER_ID=? ORDER BY GROUP_ID")){</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"> "SELECT GROUP_KEY FROM "+schema()+"HMI_APP_PERSON_GROUP WHERE PERS_ID=? ORDER BY GROUP_KEY")) {</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("SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID FROM "+schema()+"HMI_APP_PAGE WHERE ID=?")){</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("id",rs.getString(1)).put("key",rs.getString(2)).put("title",rs.getString(3))</span>
|
|
<span class="nc bnc" id="L756" title="All 2 branches missed."> .put("menuGroup",rs.getString(4)).put("order",rs.getInt(5)).put("enabled",rs.getInt(6)!=0)</span>
|
|
<span class="nc bnc" id="L757" title="All 2 branches missed."> .put("parentPageId",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<String> groups)throws SQLException{
|
|
<span class="nc" id="L763"> List<String> ids=new ArrayList<>();</span>
|
|
<span class="nc bnc" id="L764" title="All 4 branches missed."> if(station!=null&&!station.isEmpty())ids.add("STATION:"+station);</span>
|
|
<span class="nc bnc" id="L765" title="All 2 branches missed."> for(String g:groups)ids.add("GROUP:"+g);</span>
|
|
<span class="nc" id="L766"> ids.add("DEFAULT:*");</span>
|
|
// Distinct selected precedence STATION > GROUP > 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(":",2);</span>
|
|
<span class="nc" id="L770"> try(PreparedStatement p=c.prepareStatement("SELECT T.ID,T.VERSION_NO,T.LAYOUT_JSON FROM "+schema()+"HMI_APP_ASSIGNMENT A JOIN "+schema()+"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")){</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("id",rs.getString(1)).put("version",rs.getInt(2)).put("layout",objectOrEmpty(clob(rs,"LAYOUT_JSON")));}</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("SELECT ID,VERSION_NO,LAYOUT_JSON FROM "+schema()+"HMI_APP_TEMPLATE WHERE PAGE_ID=? AND STATUS='PUBLISHED' ORDER BY VERSION_NO DESC")){</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("id",rs.getString(1)).put("version",rs.getInt(2)).put("layout",objectOrEmpty(clob(rs,"LAYOUT_JSON"))):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("INSERT INTO "+schema()+"HMI_APP_AUDIT(ID,ACTOR,ACTION_NAME,ENTITY_ID,DETAILS,CREATED_AT) VALUES(?,?,?,?,?,?)")){</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()>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,"HMI DB operation failed",ex);</span>
|
|
<span class="nc" id="L789"> return err(500,"Database operation failed; check server logs");</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> |