Initial commit: update
This commit is contained in:
@@ -0,0 +1,496 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="de"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>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. All SQL is parameterized; auth comes only from GlassFish principal. */
|
||||
@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", "mes.active-order",</span>
|
||||
"mes.serial-scanner", "mes.station-status", "mes.booking-table", "mes.workplan",
|
||||
"wb.station-tree", "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>
|
||||
private String user() {
|
||||
<span class="nc bnc" id="L54" title="All 4 branches missed."> return security == null || security.getUserPrincipal() == null ? null : security.getUserPrincipal().getName();</span>
|
||||
}
|
||||
<span class="nc bnc" id="L56" title="All 4 branches missed."> private boolean role(String name) { return security != null && security.isUserInRole(name); }</span>
|
||||
<span class="nc bnc" id="L57" title="All 6 branches missed."> private boolean designer() { return role("hmi-admin") || role("hmi-designer") || role("hmi-publisher"); }</span>
|
||||
<span class="nc bnc" id="L58" title="All 4 branches missed."> private boolean publisher() { return role("hmi-admin") || role("hmi-publisher"); }</span>
|
||||
<span class="nc" id="L59"> private boolean admin() { return role("hmi-admin"); }</span>
|
||||
<span class="nc bnc" id="L60" 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="L61"> private Response err(int status, String msg) { return Response.status(status).type(MediaType.APPLICATION_JSON)</span>
|
||||
<span class="nc" id="L62"> .entity(new JSONObject().put("error", msg).toString()).build(); }</span>
|
||||
private Response denied(boolean allowed) {
|
||||
<span class="nc bnc" id="L64" title="All 2 branches missed."> if (user() == null) return err(401, "GlassFish authentication required");</span>
|
||||
<span class="nc bnc" id="L65" title="All 2 branches missed."> if (!HmiSchemaBootstrap.isReady()) return err(503, "HMI schema not ready; check server logs");</span>
|
||||
<span class="nc bnc" id="L66" title="All 2 branches missed."> if (!allowed) return err(403, "Insufficient role");</span>
|
||||
<span class="nc" id="L67"> return null;</span>
|
||||
}
|
||||
private boolean csrfValid() {
|
||||
<span class="nc" id="L70"> HttpSession session = request.getSession(false);</span>
|
||||
<span class="nc bnc" id="L71" title="All 2 branches missed."> if (session == null) return false;</span>
|
||||
<span class="nc" id="L72"> Object token = session.getAttribute(CSRF);</span>
|
||||
<span class="nc" id="L73"> String provided = request.getHeader("X-CSRF-Token");</span>
|
||||
<span class="nc bnc" id="L74" title="All 4 branches missed."> if (!(token instanceof String) || provided == null) return false;</span>
|
||||
<span class="nc" id="L75"> return java.security.MessageDigest.isEqual(((String) token).getBytes(java.nio.charset.StandardCharsets.UTF_8),</span>
|
||||
<span class="nc" id="L76"> provided.getBytes(java.nio.charset.StandardCharsets.UTF_8));</span>
|
||||
}
|
||||
private Response writeAllowed(boolean allowed) {
|
||||
<span class="nc" id="L79"> Response r = denied(allowed);</span>
|
||||
<span class="nc bnc" id="L80" 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="L83" title="All 4 branches missed."> if (station == null || station.isEmpty()) return "*";</span>
|
||||
<span class="nc bnc" id="L84" 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="L85"> return station;</span>
|
||||
}
|
||||
<span class="nc" id="L87"> private static String id() { return UUID.randomUUID().toString(); }</span>
|
||||
<span class="nc bnc" id="L88" 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="L90"> String s = obj.getString(key).trim();</span>
|
||||
<span class="nc bnc" id="L91" title="All 4 branches missed."> if (s.isEmpty() || s.length() > max) throw new JSONException("Invalid '" + key + "'");</span>
|
||||
<span class="nc" id="L92"> return s;</span>
|
||||
}
|
||||
private static int bounded(JSONObject o, String k, int min, int max, int fallback) {
|
||||
<span class="nc bnc" id="L95" title="All 2 branches missed."> int n = o.has(k) ? o.getInt(k) : fallback;</span>
|
||||
<span class="nc bnc" id="L96" title="All 4 branches missed."> if (n < min || n > max) throw new JSONException("Out of range: " + k);</span>
|
||||
<span class="nc" id="L97"> return n;</span>
|
||||
}
|
||||
<span class="nc" id="L99"> 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="L101"> Reader reader = rs.getCharacterStream(col);</span>
|
||||
<span class="nc bnc" id="L102" title="All 2 branches missed."> if (reader == null) return null;</span>
|
||||
<span class="nc" id="L103"> try (Reader r = reader) {</span>
|
||||
<span class="nc" id="L104"> StringBuilder sb = new StringBuilder(); char[] buf = new char[4096]; int n;</span>
|
||||
<span class="nc bnc" id="L105" 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="L106"> return sb.toString();</span>
|
||||
<span class="nc" id="L107"> } 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="L110"> ps.setCharacterStream(i, new StringReader(text), text.length());</span>
|
||||
<span class="nc" id="L111"> }</span>
|
||||
<span class="nc bnc" id="L112" 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="L114"> JSONObject out = new JSONObject();</span>
|
||||
<span class="nc bnc" id="L115" title="All 2 branches missed."> for (String key : input.keySet()) {</span>
|
||||
<span class="nc bnc" id="L116" title="All 5 branches missed."> switch (key) {</span>
|
||||
<span class="nc" id="L117"> case "fontScale": out.put(key, boundedScale(input.getDouble(key))); break;</span>
|
||||
<span class="nc bnc" id="L118" title="All 8 branches missed."> case "theme": { String s = input.getString(key); if (!s.equals("dark") && !s.equals("light") && !s.equals("system") && !s.equals("ios")) throw new JSONException("Invalid theme"); out.put(key, s); break; }</span>
|
||||
<span class="nc bnc" id="L119" 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="L120" 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="L121"> default: throw new JSONException("Unknown preference: " + key);</span>
|
||||
}
|
||||
<span class="nc" id="L123"> }</span>
|
||||
<span class="nc" id="L124"> return out;</span>
|
||||
}
|
||||
<span class="nc bnc" id="L126" 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>
|
||||
private static JSONObject validateTemplate(JSONObject layout) {
|
||||
<span class="nc" id="L128"> JSONArray widgets = layout.getJSONArray("widgets");</span>
|
||||
<span class="nc bnc" id="L129" title="All 2 branches missed."> if (widgets.length() > 80) throw new JSONException("Maximum 80 widgets");</span>
|
||||
<span class="nc" id="L130"> Set<String> ids = new HashSet<>();</span>
|
||||
<span class="nc" id="L131"> JSONObject cleaned = new JSONObject(); JSONArray out = new JSONArray();</span>
|
||||
<span class="nc bnc" id="L132" title="All 2 branches missed."> for (int i = 0; i < widgets.length(); i++) {</span>
|
||||
<span class="nc" id="L133"> JSONObject w = widgets.getJSONObject(i);</span>
|
||||
<span class="nc" id="L134"> String widgetId = need(w, "id", 120); String type = need(w, "type", 100);</span>
|
||||
<span class="nc bnc" id="L135" 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" id="L136"> JSONObject item = new JSONObject().put("id", widgetId).put("type", type)</span>
|
||||
<span class="nc" id="L137"> .put("x", bounded(w,"x",0,11,0)).put("y",bounded(w,"y",0,200,0))</span>
|
||||
<span class="nc" id="L138"> .put("w",bounded(w,"w",1,12,4)).put("h",bounded(w,"h",1,20,3))</span>
|
||||
<span class="nc" id="L139"> .put("mandatory",w.optBoolean("mandatory",false)).put("personalizable",w.optBoolean("personalizable",true))</span>
|
||||
<span class="nc bnc" id="L140" title="All 2 branches missed."> .put("props",w.optJSONObject("props") == null ? new JSONObject() : w.getJSONObject("props"));</span>
|
||||
<span class="nc bnc" id="L141" 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="L142" title="All 2 branches missed."> if (item.toString().length() > 16000) throw new JSONException("Widget props too large");</span>
|
||||
<span class="nc" id="L143"> out.put(item);</span>
|
||||
}
|
||||
<span class="nc" id="L145"> cleaned.put("widgets", out);</span>
|
||||
<span class="nc" id="L146"> return cleaned;</span>
|
||||
}
|
||||
private static JSONObject validateOverrides(JSONObject override) {
|
||||
<span class="nc" id="L149"> JSONObject out = new JSONObject();</span>
|
||||
<span class="nc bnc" id="L150" title="All 2 branches missed."> if (override.keySet().size() > 80) throw new JSONException("Too many overrides");</span>
|
||||
<span class="nc bnc" id="L151" title="All 2 branches missed."> for (String id : override.keySet()) {</span>
|
||||
<span class="nc bnc" id="L152" title="All 2 branches missed."> if (!legalKey(id)) throw new JSONException("Invalid widget ID");</span>
|
||||
<span class="nc" id="L153"> JSONObject p = override.getJSONObject(id);</span>
|
||||
<span class="nc bnc" id="L154" 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="L155"> JSONObject o = new JSONObject()</span>
|
||||
<span class="nc" id="L156"> .put("x", bounded(p,"x",0,11,0)).put("y",bounded(p,"y",0,200,0))</span>
|
||||
<span class="nc" id="L157"> .put("w",bounded(p,"w",1,12,4)).put("h",bounded(p,"h",1,20,3))</span>
|
||||
<span class="nc" id="L158"> .put("hidden",p.optBoolean("hidden",false));</span>
|
||||
<span class="nc bnc" id="L159" title="All 2 branches missed."> if (o.getInt("x") + o.getInt("w") > 12) throw new JSONException("Widget exceeds width");</span>
|
||||
<span class="nc" id="L160"> out.put(id,o);</span>
|
||||
<span class="nc" id="L161"> }</span>
|
||||
<span class="nc" id="L162"> return out;</span>
|
||||
}
|
||||
@GET @Path("security/csrf")
|
||||
public Response csrf() {
|
||||
<span class="nc bnc" id="L166" title="All 2 branches missed."> Response r = denied(true); if (r != null) return r;</span>
|
||||
<span class="nc" id="L167"> HttpSession s = request.getSession(true);</span>
|
||||
<span class="nc" id="L168"> String token = (String) s.getAttribute(CSRF);</span>
|
||||
<span class="nc bnc" id="L169" title="All 2 branches missed."> if (token == null) {</span>
|
||||
<span class="nc" id="L170"> byte[] b = new byte[32]; new java.security.SecureRandom().nextBytes(b);</span>
|
||||
<span class="nc" id="L171"> token = java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(b);</span>
|
||||
<span class="nc" id="L172"> s.setAttribute(CSRF, token);</span>
|
||||
}
|
||||
<span class="nc" id="L174"> return json(new JSONObject().put("csrf", token).put("user", user())</span>
|
||||
<span class="nc" id="L175"> .put("designer",designer()).put("publisher",publisher()).put("admin",admin()));</span>
|
||||
}
|
||||
@GET @Path("status")
|
||||
<span class="nc bnc" id="L178" 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="L180" title="All 2 branches missed."> public Response widgets() { Response r = denied(true); if (r!=null) return r;</span>
|
||||
<span class="nc" id="L181"> JSONArray a = new JSONArray();</span>
|
||||
<span class="nc bnc" id="L182" 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="L183"> return json(a);</span>
|
||||
}
|
||||
@GET @Path("pages")
|
||||
public Response pages() {
|
||||
<span class="nc bnc" id="L187" title="All 2 branches missed."> Response r = denied(true); if (r!=null) return r;</span>
|
||||
<span class="nc" id="L188"> JSONArray a = new JSONArray();</span>
|
||||
<span class="nc" id="L189"> try (Connection c=HmiDb.open(); PreparedStatement p=c.prepareStatement(</span>
|
||||
<span class="nc" id="L190"> "SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED FROM " + schema() + "HMI_APP_PAGE ORDER BY SORT_ORDER,TITLE"); ResultSet rs=p.executeQuery()) {</span>
|
||||
<span class="nc bnc" id="L191" title="All 2 branches missed."> while (rs.next()) {</span>
|
||||
<span class="nc bnc" id="L192" title="All 4 branches missed."> if (rs.getInt("ENABLED") == 0 && !designer()) continue;</span>
|
||||
<span class="nc" id="L193"> a.put(new JSONObject().put("id",rs.getString(1)).put("key",rs.getString(2)).put("title",rs.getString(3))</span>
|
||||
<span class="nc bnc" id="L194" 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" id="L196"> return json(a);</span>
|
||||
<span class="nc" id="L197"> } catch (SQLException e) { return dbError(e); }</span>
|
||||
}
|
||||
@POST @Path("pages") @Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response createPage(String body) {
|
||||
<span class="nc bnc" id="L201" title="All 2 branches missed."> Response r=writeAllowed(designer()); if(r!=null)return r;</span>
|
||||
try {
|
||||
<span class="nc" id="L203"> JSONObject in=parse(body);</span>
|
||||
<span class="nc bnc" id="L204" title="All 2 branches missed."> String key=need(in,"key",120); if(!legalKey(key)) return err(400,"Invalid page key");</span>
|
||||
<span class="nc" id="L205"> String title=need(in,"title",255), pageId=id();</span>
|
||||
<span class="nc bnc" id="L206" title="All 2 branches missed."> String menu=in.optString("menuGroup","HMI"); if(menu.length()>120)return err(400,"Menu too long");</span>
|
||||
<span class="nc" id="L207"> int order=bounded(in,"order",0,100000,0);</span>
|
||||
<span class="nc" id="L208"> try (Connection c=HmiDb.open(); PreparedStatement p=c.prepareStatement(</span>
|
||||
<span class="nc" id="L209"> "INSERT INTO "+schema()+"HMI_APP_PAGE(ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED) VALUES (?,?,?,?,?,1)")) {</span>
|
||||
<span class="nc" id="L210"> p.setString(1,pageId);p.setString(2,key);p.setString(3,title);p.setString(4,menu);p.setInt(5,order);p.executeUpdate();</span>
|
||||
<span class="nc" id="L211"> audit(c,"page.created",pageId,key);</span>
|
||||
}
|
||||
<span class="nc" id="L213"> return json(new JSONObject().put("id",pageId).put("key",key));</span>
|
||||
<span class="nc" id="L214"> } catch (JSONException e) { return err(400,e.getMessage()); } catch (SQLException e) { return dbError(e); }</span>
|
||||
}
|
||||
@PUT @Path("pages/{pageId}") @Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response updatePage(@PathParam("pageId") String pageId, String body) {
|
||||
<span class="nc bnc" id="L218" title="All 2 branches missed."> Response r=writeAllowed(designer()); if(r!=null)return r;</span>
|
||||
try {
|
||||
<span class="nc" id="L220"> JSONObject in=parse(body);String title=need(in,"title",255), menu=in.optString("menuGroup","HMI");</span>
|
||||
<span class="nc bnc" id="L221" title="All 2 branches missed."> if(menu.length()>120)return err(400,"Menu too long");</span>
|
||||
<span class="nc" id="L222"> try (Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement("UPDATE "+schema()+"HMI_APP_PAGE SET TITLE=?,MENU_GROUP=?,SORT_ORDER=?,ENABLED=? WHERE ID=?")) {</span>
|
||||
<span class="nc" id="L223"> p.setString(1,title);p.setString(2,menu);p.setInt(3,bounded(in,"order",0,100000,0));</span>
|
||||
<span class="nc bnc" id="L224" title="All 2 branches missed."> p.setInt(4,in.optBoolean("enabled",true)?1:0);p.setString(5,pageId);</span>
|
||||
<span class="nc bnc" id="L225" title="All 2 branches missed."> if(p.executeUpdate()==0)return err(404,"Page not found"); audit(c,"page.updated",pageId,title);</span>
|
||||
<span class="nc bnc" id="L226" title="All 4 branches missed."> }</span>
|
||||
<span class="nc" id="L227"> return json(new JSONObject().put("ok",true));</span>
|
||||
<span class="nc" id="L228"> } 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="L232" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
|
||||
<span class="nc" id="L233"> JSONArray a=new JSONArray();</span>
|
||||
<span class="nc" id="L234"> 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="L235" 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="L236"> return json(a);</span>
|
||||
<span class="nc" id="L237"> }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="L241" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
|
||||
try {
|
||||
<span class="nc" id="L243"> JSONObject layout=validateTemplate(parse(body));</span>
|
||||
<span class="nc" id="L244"> try(Connection c=HmiDb.open()) {</span>
|
||||
<span class="nc bnc" id="L245" title="All 2 branches missed."> if(!pageExists(c,pageId))return err(404,"Page not found");</span>
|
||||
<span class="nc" id="L246"> int version=1;</span>
|
||||
<span class="nc" id="L247"> try(PreparedStatement p=c.prepareStatement("SELECT MAX(VERSION_NO) FROM "+schema()+"HMI_APP_TEMPLATE WHERE PAGE_ID=?")){</span>
|
||||
<span class="nc bnc" id="L248" 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="L250"> String templateId=id();</span>
|
||||
<span class="nc" id="L251"> 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="L252"> 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="L253"> p.setString(6,user());p.setTimestamp(7,Timestamp.from(Instant.now()));p.executeUpdate();</span>
|
||||
}
|
||||
<span class="nc" id="L255"> audit(c,"template.created",templateId,pageId);</span>
|
||||
<span class="nc" id="L256"> return json(new JSONObject().put("id",templateId).put("version",version).put("status","DRAFT"));</span>
|
||||
<span class="nc bnc" id="L257" title="All 2 branches missed."> }</span>
|
||||
<span class="nc" id="L258"> } 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="L262" title="All 2 branches missed."> Response r=writeAllowed(designer());if(r!=null)return r;</span>
|
||||
try {
|
||||
<span class="nc" id="L264"> JSONObject layout=validateTemplate(parse(body));</span>
|
||||
<span class="nc" id="L265"> try(Connection c=HmiDb.open();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="L266"> 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="L267" title="All 2 branches missed."> if(p.executeUpdate()==0)return err(409,"Template not a draft or not found");audit(c,"template.saved",templateId,"");</span>
|
||||
<span class="nc bnc" id="L268" title="All 4 branches missed."> }</span>
|
||||
<span class="nc" id="L269"> return json(new JSONObject().put("ok",true));</span>
|
||||
<span class="nc" id="L270"> }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="L274" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
|
||||
<span class="nc" id="L275"> 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="L276"> p.setString(1,templateId);try(ResultSet rs=p.executeQuery()){</span>
|
||||
<span class="nc bnc" id="L277" title="All 2 branches missed."> if(!rs.next())return err(404,"Template not found");</span>
|
||||
<span class="nc" id="L278"> return json(new JSONObject().put("id",rs.getString(1)).put("pageId",rs.getString(2))</span>
|
||||
<span class="nc" id="L279"> .put("version",rs.getInt(3)).put("status",rs.getString(4))</span>
|
||||
<span class="nc" id="L280"> .put("layout",objectOrEmpty(clob(rs,"LAYOUT_JSON"))));</span>
|
||||
<span class="nc bnc" id="L281" title="All 2 branches missed."> }</span>
|
||||
<span class="nc bnc" id="L282" 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="L286" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
|
||||
<span class="nc" id="L287"> try(Connection c=HmiDb.open()) {</span>
|
||||
<span class="nc" id="L288"> try(PreparedStatement chk=c.prepareStatement("SELECT ID FROM "+schema()+"HMI_APP_ASSIGNMENT WHERE TEMPLATE_ID=?")) {</span>
|
||||
<span class="nc bnc" id="L289" 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="L290" title="All 2 branches missed."> }</span>
|
||||
<span class="nc" id="L291"> try(PreparedStatement p=c.prepareStatement("UPDATE "+schema()+"HMI_APP_TEMPLATE SET STATUS='ARCHIVED' WHERE ID=? AND STATUS='PUBLISHED'")) {</span>
|
||||
<span class="nc bnc" id="L292" 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="L293" title="All 2 branches missed."> }</span>
|
||||
<span class="nc" id="L294"> audit(c,"template.archived",templateId,"");return json(new JSONObject().put("ok",true));</span>
|
||||
<span class="nc bnc" id="L295" 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="L299" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
|
||||
<span class="nc" id="L300"> try(Connection c=HmiDb.open()) {</span>
|
||||
<span class="nc" id="L301"> c.setAutoCommit(false);</span>
|
||||
try {
|
||||
String pageId;
|
||||
<span class="nc" id="L304"> try(PreparedStatement p=c.prepareStatement("SELECT PAGE_ID,STATUS FROM "+schema()+"HMI_APP_TEMPLATE WHERE ID=?")) {</span>
|
||||
<span class="nc" id="L305"> p.setString(1,templateId);</span>
|
||||
<span class="nc" id="L306"> try(ResultSet rs=p.executeQuery()){</span>
|
||||
<span class="nc bnc" id="L307" title="All 2 branches missed."> if(!rs.next()){c.rollback();return err(404,"Template not found");}</span>
|
||||
<span class="nc" id="L308"> pageId=rs.getString(1);</span>
|
||||
<span class="nc bnc" id="L309" 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="L310" title="All 4 branches missed."> }</span>
|
||||
<span class="nc bnc" id="L311" 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="L314"> try(PreparedStatement p=c.prepareStatement("UPDATE "+schema()+"HMI_APP_TEMPLATE SET STATUS='PUBLISHED' WHERE ID=? AND STATUS='DRAFT'")){</span>
|
||||
<span class="nc bnc" id="L315" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)throw new SQLException("Concurrent template publication");</span>
|
||||
}
|
||||
<span class="nc" id="L317"> audit(c,"template.published",templateId,pageId);c.commit();</span>
|
||||
<span class="nc" id="L318"> return json(new JSONObject().put("ok",true));</span>
|
||||
<span class="nc" id="L319"> }catch(Exception ex){c.rollback();throw ex;}</span>
|
||||
<span class="nc bnc" id="L320" 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="L324" title="All 2 branches missed."> Response r=denied(true);if(r!=null)return r;</span>
|
||||
<span class="nc" id="L325"> try(Connection c=HmiDb.open()){</span>
|
||||
<span class="nc" id="L326"> JSONObject page=fetchPage(c,pageId);</span>
|
||||
<span class="nc bnc" id="L327" title="All 6 branches missed."> if(page==null||(!page.getBoolean("enabled")&&!designer()))return err(404,"Page not found");</span>
|
||||
<span class="nc" id="L328"> List<String> groups=groups(c,user());</span>
|
||||
<span class="nc" id="L329"> JSONObject template=resolveTemplate(c,pageId,station,groups);</span>
|
||||
<span class="nc bnc" id="L330" title="All 2 branches missed."> if(template==null)return err(404,"No published template assigned");</span>
|
||||
<span class="nc" id="L331"> JSONObject prefs=new JSONObject().put("fontScale",1).put("theme","ios").put("contrast","normal").put("accent","#a40640");</span>
|
||||
<span class="nc" id="L332"> merge(prefs,preference(c,"DEFAULT","*"));</span>
|
||||
<span class="nc bnc" id="L333" title="All 2 branches missed."> for(String g:groups)merge(prefs,preference(c,"GROUP",g));</span>
|
||||
<span class="nc bnc" id="L334" title="All 4 branches missed."> if(station!=null&&!station.isEmpty())merge(prefs,preference(c,"STATION",station));</span>
|
||||
<span class="nc" id="L335"> merge(prefs,preference(c,"USER",user()));</span>
|
||||
<span class="nc" id="L336"> JSONObject overrides=new JSONObject();String baseId="";</span>
|
||||
<span class="nc" id="L337"> 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="L338"> p.setString(1,user());p.setString(2,pageId);p.setString(3, stationKey(station));</span>
|
||||
<span class="nc bnc" id="L339" 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="L341"> JSONObject layout=template.getJSONObject("layout");</span>
|
||||
<span class="nc" id="L342"> JSONArray effectiveWidgets=new JSONArray();</span>
|
||||
<span class="nc bnc" id="L343" title="All 2 branches missed."> for(Object val:layout.getJSONArray("widgets")) {</span>
|
||||
<span class="nc" id="L344"> JSONObject widget=new JSONObject(val.toString());String widgetId=widget.getString("id");</span>
|
||||
<span class="nc bnc" id="L345" title="All 4 branches missed."> if(widget.optBoolean("personalizable",true)&&overrides.has(widgetId)){</span>
|
||||
<span class="nc" id="L346"> JSONObject o=overrides.getJSONObject(widgetId);</span>
|
||||
<span class="nc bnc" id="L347" 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="L348" title="All 4 branches missed."> if(!widget.optBoolean("mandatory",false)&&o.optBoolean("hidden",false))widget.put("hidden",true);</span>
|
||||
<span class="nc bnc" id="L349" title="All 2 branches missed."> if(widget.getInt("x")+widget.getInt("w")>12)widget.put("x",0);</span>
|
||||
}
|
||||
<span class="nc" id="L351"> effectiveWidgets.put(widget);</span>
|
||||
<span class="nc" id="L352"> }</span>
|
||||
<span class="nc" id="L353"> return json(new JSONObject().put("page",page).put("templateId",template.getString("id"))</span>
|
||||
<span class="nc" id="L354"> .put("version",template.getInt("version")).put("originalTemplateId",baseId)</span>
|
||||
<span class="nc" id="L355"> .put("layout",new JSONObject().put("widgets",effectiveWidgets)).put("preferences",prefs));</span>
|
||||
<span class="nc bnc" id="L356" title="All 4 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="L360" title="All 2 branches missed."> Response r=writeAllowed(true);if(r!=null)return r;</span>
|
||||
try {
|
||||
<span class="nc" id="L362"> JSONObject in=parse(body);</span>
|
||||
<span class="nc" id="L363"> JSONObject o=validateOverrides(in.getJSONObject("overrides"));</span>
|
||||
<span class="nc" id="L364"> try(Connection c=HmiDb.open()){</span>
|
||||
<span class="nc bnc" id="L365" 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="L367"> JSONObject template=resolveTemplate(c,pageId,in.optString("station",""),groups(c,user()));</span>
|
||||
<span class="nc bnc" id="L368" title="All 2 branches missed."> if(template==null)return err(409,"Page has no published template");</span>
|
||||
<span class="nc" id="L369"> JSONObject allowed=new JSONObject();</span>
|
||||
<span class="nc bnc" id="L370" title="All 2 branches missed."> for(Object obj:template.getJSONObject("layout").getJSONArray("widgets")) {</span>
|
||||
<span class="nc" id="L371"> JSONObject w=(JSONObject)obj;String widgetId=w.getString("id");</span>
|
||||
<span class="nc bnc" id="L372" title="All 4 branches missed."> if(!o.has(widgetId) || !w.optBoolean("personalizable",true))continue;</span>
|
||||
<span class="nc" id="L373"> JSONObject override=o.getJSONObject(widgetId);</span>
|
||||
<span class="nc bnc" id="L374" 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="L375"> allowed.put(widgetId,override);</span>
|
||||
<span class="nc" id="L376"> }</span>
|
||||
int count;
|
||||
<span class="nc" id="L378"> 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="L379"> 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="L381" 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="L382"> 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="L384"> audit(c,"user.layout",pageId,"Saved overrides");</span>
|
||||
<span class="nc" id="L385"> return json(new JSONObject().put("ok",true).put("saved",allowed.length()));</span>
|
||||
<span class="nc bnc" id="L386" title="All 6 branches missed."> }</span>
|
||||
<span class="nc" id="L387"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
|
||||
}
|
||||
@GET @Path("preferences")
|
||||
<span class="nc bnc" id="L390" title="All 2 branches missed."> public Response myPreferences(){Response r=denied(true);if(r!=null)return r;</span>
|
||||
<span class="nc" id="L391"> 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="L393" title="All 2 branches missed."> public Response myPreferencesSave(String body){Response r=writeAllowed(true);if(r!=null)return r;</span>
|
||||
<span class="nc" id="L394"> 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="L396" 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="L397" title="All 2 branches missed."> if(!legalKey(group))return err(400,"Invalid group");</span>
|
||||
<span class="nc" id="L398"> 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="L401" title="All 2 branches missed."> Response r=writeAllowed(admin());if(r!=null)return r;</span>
|
||||
<span class="nc bnc" id="L402" 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="L403"> try(Connection c=HmiDb.open()){</span>
|
||||
<span class="nc" id="L404"> 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="L405" 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="L406" title="All 2 branches missed."> }</span>
|
||||
<span class="nc" id="L407"> try(PreparedStatement p=c.prepareStatement("INSERT INTO "+schema()+"HMI_APP_GROUP_MEMBER(USER_ID,GROUP_ID) VALUES (?,?)")){</span>
|
||||
<span class="nc" id="L408"> p.setString(1,member);p.setString(2,group);p.executeUpdate();</span>
|
||||
}
|
||||
<span class="nc" id="L410"> audit(c,"group.member.added",group,member);return json(new JSONObject().put("ok",true));</span>
|
||||
<span class="nc bnc" id="L411" title="All 2 branches missed."> }catch(SQLException e){return dbError(e);}</span>
|
||||
}
|
||||
@POST @Path("assignments") @Consumes(MediaType.APPLICATION_JSON)
|
||||
<span class="nc bnc" id="L414" 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="L416"> 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="L417" 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="L418"> try(Connection c=HmiDb.open()){</span>
|
||||
<span class="nc" id="L419"> try(PreparedStatement check=c.prepareStatement("SELECT STATUS FROM "+schema()+"HMI_APP_TEMPLATE WHERE ID=? AND PAGE_ID=?")){</span>
|
||||
<span class="nc" id="L420"> check.setString(1,templateId);check.setString(2,pageId);</span>
|
||||
<span class="nc bnc" id="L421" 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="L422" title="All 2 branches missed."> }</span>
|
||||
<span class="nc" id="L423"> 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="L424"> 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="L426"> audit(c,"template.assigned",templateId,type+":"+scope);return json(new JSONObject().put("ok",true));</span>
|
||||
<span class="nc bnc" id="L427" title="All 2 branches missed."> }</span>
|
||||
<span class="nc" id="L428"> }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="L431"> JSONObject prefs=validatePreferences(input);</span>
|
||||
<span class="nc" id="L432"> try(Connection c=HmiDb.open()){</span>
|
||||
int count;
|
||||
<span class="nc" id="L434"> 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="L435"> 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="L437" 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="L438"> 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="L440"> audit(c,"preferences.saved",type+":"+identity,"");return json(prefs);</span>
|
||||
<span class="nc" id="L441"> }catch(SQLException e){return dbError(e);}</span>
|
||||
}
|
||||
private JSONObject preference(Connection c,String type,String identity)throws SQLException{
|
||||
<span class="nc" id="L444"> try(PreparedStatement p=c.prepareStatement("SELECT PREFS_JSON FROM "+schema()+"HMI_APP_PREF WHERE SCOPE_TYPE=? AND SCOPE_ID=?")){</span>
|
||||
<span class="nc" id="L445"> p.setString(1,type);p.setString(2,identity);</span>
|
||||
<span class="nc bnc" id="L446" 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="L449" 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="L451"> List<String> a=new ArrayList<>();</span>
|
||||
<span class="nc" id="L452"> 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="L453" 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="L455"> return a;</span>
|
||||
}
|
||||
private JSONObject fetchPage(Connection c,String pageId)throws SQLException{
|
||||
<span class="nc" id="L458"> try(PreparedStatement p=c.prepareStatement("SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED FROM "+schema()+"HMI_APP_PAGE WHERE ID=?")){</span>
|
||||
<span class="nc" id="L459"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
|
||||
<span class="nc bnc" id="L460" 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="L461" title="All 2 branches missed."> .put("menuGroup",rs.getString(4)).put("order",rs.getInt(5)).put("enabled",rs.getInt(6)!=0):null;</span>
|
||||
}
|
||||
}
|
||||
}
|
||||
<span class="nc bnc" id="L465" 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="L467"> List<String> ids=new ArrayList<>();</span>
|
||||
<span class="nc bnc" id="L468" title="All 4 branches missed."> if(station!=null&&!station.isEmpty())ids.add("STATION:"+station);</span>
|
||||
<span class="nc bnc" id="L469" title="All 2 branches missed."> for(String g:groups)ids.add("GROUP:"+g);</span>
|
||||
<span class="nc" id="L470"> ids.add("DEFAULT:*");</span>
|
||||
// Distinct selected precedence STATION > GROUP > DEFAULT; highest priority within a scope.
|
||||
<span class="nc bnc" id="L472" title="All 2 branches missed."> for(String selection:ids){</span>
|
||||
<span class="nc" id="L473"> String[] parts=selection.split(":",2);</span>
|
||||
<span class="nc" id="L474"> 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="L475"> p.setString(1,pageId);p.setString(2,parts[0]);p.setString(3,parts[1]);</span>
|
||||
<span class="nc bnc" id="L476" 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="L477" title="All 2 branches missed."> }</span>
|
||||
<span class="nc" id="L478"> }</span>
|
||||
<span class="nc" id="L479"> 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="L480"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
|
||||
<span class="nc bnc" id="L481" 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="L486"> 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="L487"> p.setString(1,id());p.setString(2,user());p.setString(3,action);p.setString(4,entity);</span>
|
||||
<span class="nc bnc" id="L488" 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="L490"> }</span>
|
||||
private Response dbError(Exception ex){
|
||||
<span class="nc" id="L492"> java.util.logging.Logger.getLogger(HmiApiResource.class.getName()).log(java.util.logging.Level.SEVERE,"HMI DB operation failed",ex);</span>
|
||||
<span class="nc" id="L493"> 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.4.201905082037</span></div></body></html>
|
||||
Reference in New Issue
Block a user