Workplace Widget 5.10.6

This commit is contained in:
chris
2026-10-29 20:55:59 +00:00
parent 753019771c
commit 513550f58d
48 changed files with 1309 additions and 403 deletions

View File

@@ -45,7 +45,7 @@ import org.json.JSONObject;
static {
<span class="nc" id="L46"> Collections.addAll(WIDGET_TYPES, &quot;ui.text&quot;, &quot;ui.table&quot;, &quot;ui.button&quot;, &quot;ui.header&quot;, &quot;ui.image&quot;, &quot;ui.script&quot;, &quot;ui.curved-header&quot;, &quot;ui.neon-actions&quot;, &quot;ui.metal-panel&quot;, &quot;ui.cheerpj&quot;, &quot;ui.cheerpj-workplace&quot;, &quot;mes.active-order&quot;,</span>
&quot;mes.serial-scanner&quot;, &quot;mes.station-status&quot;, &quot;mes.booking-table&quot;, &quot;mes.workplan&quot;,
&quot;wb.station-tree&quot;, &quot;wb.station-config&quot;, &quot;wb.workorders&quot;, &quot;wb.history&quot;,
&quot;wb.station-tree&quot;, &quot;wb.production-overview&quot;, &quot;wb.station-config&quot;, &quot;wb.workorders&quot;, &quot;wb.history&quot;,
&quot;wb.activities&quot;, &quot;wb.activity-designer&quot;, &quot;wb.contacts&quot;, &quot;wb.erp-groups&quot;,
&quot;wb.users&quot;, &quot;wb.bom-import&quot;, &quot;wb.part-import&quot;, &quot;wb.station-import&quot;,
&quot;wb.erp-import&quot;, &quot;wb.contact-import&quot;, &quot;wb.user-import&quot;);
@@ -115,7 +115,7 @@ import org.json.JSONObject;
<span class="nc" id="L115"> return out;</span>
}
<span class="nc bnc" id="L117" title="All 6 branches missed."> private static double boundedScale(double x) { if (!Double.isFinite(x) || x &lt; 0.75 || x &gt; 2.0) throw new JSONException(&quot;fontScale must be 0.75 to 2.0&quot;); return x; }</span>
private static JSONObject validateTemplate(JSONObject layout) {
static JSONObject validateTemplate(JSONObject layout) {
<span class="nc" id="L119"> JSONArray widgets = layout.getJSONArray(&quot;widgets&quot;);</span>
<span class="nc bnc" id="L120" title="All 2 branches missed."> if (widgets.length() &gt; 80) throw new JSONException(&quot;Maximum 80 widgets&quot;);</span>
<span class="nc" id="L121"> Set&lt;String&gt; ids = new HashSet&lt;&gt;();</span>
@@ -196,7 +196,7 @@ import org.json.JSONObject;
}
<span class="nc" id="L197"> return map;</span>
}
private static boolean scriptChanges(JSONObject previous, JSONObject proposed) {
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>
}
@@ -514,265 +514,279 @@ import org.json.JSONObject;
<span class="nc bnc" id="L514" title="All 4 branches missed."> if (!scriptEditor() &amp;&amp; scriptChanges(objectOrEmpty(current),layout)) {</span>
<span class="nc" id="L515"> c.rollback();return err(403,&quot;Code- oder JNLP-Änderungen erfordern die Rolle hmi-script-editor&quot;);</span>
}
<span class="nc" id="L517"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET LAYOUT_JSON=?,UPDATED_BY=?,UPDATED_AT=? WHERE ID=? AND STATUS='DRAFT'&quot;)) {</span>
<span class="nc" id="L518"> 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="L519" title="All 2 branches missed."> if(p.executeUpdate()==0) {c.rollback();return err(409,&quot;Template not a draft or not found&quot;);}</span>
<span class="nc bnc" id="L520" title="All 2 branches missed."> }</span>
<span class="nc" id="L521"> audit(c,&quot;template.saved&quot;,templateId,&quot;&quot;);</span>
<span class="nc bnc" id="L522" title="All 2 branches missed."> if(scriptChanges(objectOrEmpty(current),layout))audit(c,&quot;widget.script.edited&quot;,templateId,&quot;Code or JNLP configuration updated&quot;);</span>
<span class="nc" id="L523"> c.commit();</span>
<span class="nc" id="L524"> } catch(SQLException | RuntimeException ex) {c.rollback();throw ex;}</span>
<span class="nc bnc" id="L525" title="All 6 branches missed."> }</span>
<span class="nc" id="L526"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L527"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
// Widgets with revision history may only change through a valid checkout/checkin.
<span class="nc" id="L518"> JSONObject savedLayout=objectOrEmpty(current);</span>
<span class="nc" id="L519"> JSONArray incomingWidgets=layout.getJSONArray(&quot;widgets&quot;);</span>
<span class="nc" id="L520"> JSONArray savedWidgets=savedLayout.optJSONArray(&quot;widgets&quot;);</span>
<span class="nc bnc" id="L521" title="All 4 branches missed."> if(savedWidgets!=null)for(int i=0;i&lt;savedWidgets.length();i++){</span>
<span class="nc" id="L522"> JSONObject oldWidget=savedWidgets.getJSONObject(i);</span>
<span class="nc" id="L523"> JSONObject candidate=null;</span>
<span class="nc bnc" id="L524" title="All 4 branches missed."> for(int j=0;j&lt;incomingWidgets.length();j++)if(oldWidget.getString(&quot;id&quot;).equals(incomingWidgets.getJSONObject(j).getString(&quot;id&quot;))){candidate=incomingWidgets.getJSONObject(j);break;}</span>
<span class="nc bnc" id="L525" title="All 4 branches missed."> if(candidate==null||oldWidget.similar(candidate))continue;</span>
<span class="nc" id="L526"> try(PreparedStatement versioned=c.prepareStatement(&quot;SELECT COUNT(*) FROM &quot;+schema()+&quot;HMI_APP_WIDGET_REVISION WHERE PAGE_ID=(SELECT PAGE_ID FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?) AND WIDGET_ID=?&quot;)){</span>
<span class="nc" id="L527"> versioned.setString(1,templateId);versioned.setString(2,oldWidget.getString(&quot;id&quot;));</span>
<span class="nc bnc" id="L528" title="All 6 branches missed."> try(ResultSet rs=versioned.executeQuery()){if(rs.next()&amp;&amp;rs.getInt(1)&gt;0){c.rollback();return err(409,&quot;Versioniertes Widget &quot;+oldWidget.getString(&quot;id&quot;)+&quot; zuerst auschecken und einchecken&quot;);}}</span>
<span class="nc bnc" id="L529" title="All 2 branches missed."> }</span>
}
<span class="nc" id="L531"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET LAYOUT_JSON=?,UPDATED_BY=?,UPDATED_AT=? WHERE ID=? AND STATUS='DRAFT'&quot;)) {</span>
<span class="nc" id="L532"> clob(p,1,layout.toString());p.setString(2,user());p.setTimestamp(3,Timestamp.from(Instant.now()));p.setString(4,templateId);</span>
<span class="nc bnc" id="L533" title="All 2 branches missed."> if(p.executeUpdate()==0) {c.rollback();return err(409,&quot;Template not a draft or not found&quot;);}</span>
<span class="nc bnc" id="L534" title="All 2 branches missed."> }</span>
<span class="nc" id="L535"> audit(c,&quot;template.saved&quot;,templateId,&quot;&quot;);</span>
<span class="nc bnc" id="L536" title="All 2 branches missed."> if(scriptChanges(objectOrEmpty(current),layout))audit(c,&quot;widget.script.edited&quot;,templateId,&quot;Code or JNLP configuration updated&quot;);</span>
<span class="nc" id="L537"> c.commit();</span>
<span class="nc" id="L538"> } catch(SQLException | RuntimeException ex) {c.rollback();throw ex;}</span>
<span class="nc bnc" id="L539" title="All 8 branches missed."> }</span>
<span class="nc" id="L540"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L541"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
}
@GET @Path(&quot;templates/{templateId}&quot;)
public Response getTemplate(@PathParam(&quot;templateId&quot;)String templateId) {
<span class="nc bnc" id="L531" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
<span class="nc" id="L532"> try(Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(&quot;SELECT ID,PAGE_ID,VERSION_NO,STATUS,LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?&quot;)){</span>
<span class="nc" id="L533"> p.setString(1,templateId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L534" title="All 2 branches missed."> if(!rs.next())return err(404,&quot;Template not found&quot;);</span>
<span class="nc" id="L535"> return json(new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;pageId&quot;,rs.getString(2))</span>
<span class="nc" id="L536"> .put(&quot;version&quot;,rs.getInt(3)).put(&quot;status&quot;,rs.getString(4))</span>
<span class="nc" id="L537"> .put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;))));</span>
<span class="nc bnc" id="L538" title="All 2 branches missed."> }</span>
<span class="nc bnc" id="L539" title="All 4 branches missed."> }catch(SQLException e){return dbError(e);}</span>
<span class="nc bnc" id="L545" title="All 2 branches missed."> Response r=denied(designer());if(r!=null)return r;</span>
<span class="nc" id="L546"> try(Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(&quot;SELECT ID,PAGE_ID,VERSION_NO,STATUS,LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?&quot;)){</span>
<span class="nc" id="L547"> p.setString(1,templateId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L548" title="All 2 branches missed."> if(!rs.next())return err(404,&quot;Template not found&quot;);</span>
<span class="nc" id="L549"> return json(new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;pageId&quot;,rs.getString(2))</span>
<span class="nc" id="L550"> .put(&quot;version&quot;,rs.getInt(3)).put(&quot;status&quot;,rs.getString(4))</span>
<span class="nc" id="L551"> .put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;))));</span>
<span class="nc bnc" id="L552" title="All 2 branches missed."> }</span>
<span class="nc bnc" id="L553" title="All 4 branches missed."> }catch(SQLException e){return dbError(e);}</span>
}
@POST @Path(&quot;templates/{templateId}/archive&quot;)
public Response archive(@PathParam(&quot;templateId&quot;)String templateId) {
<span class="nc bnc" id="L543" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc" id="L544"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L545"> try(PreparedStatement chk=c.prepareStatement(&quot;SELECT ID FROM &quot;+schema()+&quot;HMI_APP_ASSIGNMENT WHERE TEMPLATE_ID=?&quot;)) {</span>
<span class="nc bnc" id="L546" title="All 4 branches missed."> chk.setString(1,templateId);try(ResultSet rs=chk.executeQuery()){if(rs.next())return err(409,&quot;Template is assigned; update assignments before archiving&quot;);}</span>
<span class="nc bnc" id="L547" title="All 2 branches missed."> }</span>
<span class="nc" id="L548"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET STATUS='ARCHIVED' WHERE ID=? AND STATUS='PUBLISHED'&quot;)) {</span>
<span class="nc bnc" id="L549" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)return err(409,&quot;Not published or not found&quot;);</span>
<span class="nc bnc" id="L550" title="All 2 branches missed."> }</span>
<span class="nc" id="L551"> audit(c,&quot;template.archived&quot;,templateId,&quot;&quot;);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L552" title="All 4 branches missed."> }catch(SQLException e){return dbError(e);}</span>
<span class="nc bnc" id="L557" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc" id="L558"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L559"> try(PreparedStatement chk=c.prepareStatement(&quot;SELECT ID FROM &quot;+schema()+&quot;HMI_APP_ASSIGNMENT WHERE TEMPLATE_ID=?&quot;)) {</span>
<span class="nc bnc" id="L560" title="All 4 branches missed."> chk.setString(1,templateId);try(ResultSet rs=chk.executeQuery()){if(rs.next())return err(409,&quot;Template is assigned; update assignments before archiving&quot;);}</span>
<span class="nc bnc" id="L561" title="All 2 branches missed."> }</span>
<span class="nc" id="L562"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET STATUS='ARCHIVED' WHERE ID=? AND STATUS='PUBLISHED'&quot;)) {</span>
<span class="nc bnc" id="L563" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)return err(409,&quot;Not published or not found&quot;);</span>
<span class="nc bnc" id="L564" title="All 2 branches missed."> }</span>
<span class="nc" id="L565"> audit(c,&quot;template.archived&quot;,templateId,&quot;&quot;);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L566" title="All 4 branches missed."> }catch(SQLException e){return dbError(e);}</span>
}
@POST @Path(&quot;templates/{templateId}/publish&quot;)
public Response publish(@PathParam(&quot;templateId&quot;)String templateId) {
<span class="nc bnc" id="L556" title="All 2 branches missed."> Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc" id="L557"> try(Connection c=HmiDb.open()) {</span>
<span class="nc" id="L558"> c.setAutoCommit(false);</span>
<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="L561"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT PAGE_ID,STATUS FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?&quot;)) {</span>
<span class="nc" id="L562"> p.setString(1,templateId);</span>
<span class="nc" id="L563"> try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L564" title="All 2 branches missed."> if(!rs.next()){c.rollback();return err(404,&quot;Template not found&quot;);}</span>
<span class="nc" id="L565"> pageId=rs.getString(1);</span>
<span class="nc bnc" id="L566" title="All 2 branches missed."> if(!&quot;DRAFT&quot;.equals(rs.getString(2))){c.rollback();return err(409,&quot;Only drafts can be published&quot;);}</span>
<span class="nc bnc" id="L567" title="All 4 branches missed."> }</span>
<span class="nc bnc" id="L568" title="All 4 branches missed."> }</span>
<span class="nc" id="L575"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT PAGE_ID,STATUS FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=?&quot;)) {</span>
<span class="nc" id="L576"> p.setString(1,templateId);</span>
<span class="nc" id="L577"> try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L578" title="All 2 branches missed."> if(!rs.next()){c.rollback();return err(404,&quot;Template not found&quot;);}</span>
<span class="nc" id="L579"> pageId=rs.getString(1);</span>
<span class="nc bnc" id="L580" title="All 2 branches missed."> if(!&quot;DRAFT&quot;.equals(rs.getString(2))){c.rollback();return err(409,&quot;Only drafts can be published&quot;);}</span>
<span class="nc bnc" id="L581" title="All 4 branches missed."> }</span>
<span class="nc bnc" id="L582" title="All 4 branches missed."> }</span>
// Keep other published templates available for group/station assignments.
// Default page resolves highest published version; explicit assignment stays pinned.
<span class="nc" id="L571"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET STATUS='PUBLISHED' WHERE ID=? AND STATUS='DRAFT'&quot;)){</span>
<span class="nc bnc" id="L572" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)throw new SQLException(&quot;Concurrent template publication&quot;);</span>
<span class="nc" id="L585"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_TEMPLATE SET STATUS='PUBLISHED' WHERE ID=? AND STATUS='DRAFT'&quot;)){</span>
<span class="nc bnc" id="L586" title="All 2 branches missed."> p.setString(1,templateId);if(p.executeUpdate()!=1)throw new SQLException(&quot;Concurrent template publication&quot;);</span>
}
<span class="nc" id="L574"> audit(c,&quot;template.published&quot;,templateId,pageId);c.commit();</span>
<span class="nc" id="L575"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L576"> }catch(Exception ex){c.rollback();throw ex;}</span>
<span class="nc bnc" id="L577" title="All 4 branches missed."> }catch(Exception e){return dbError(e);}</span>
<span class="nc" id="L588"> audit(c,&quot;template.published&quot;,templateId,pageId);c.commit();</span>
<span class="nc" id="L589"> return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc" id="L590"> }catch(Exception ex){c.rollback();throw ex;}</span>
<span class="nc bnc" id="L591" title="All 4 branches missed."> }catch(Exception e){return dbError(e);}</span>
}
@GET @Path(&quot;pages/{pageId}/effective&quot;)
public Response effective(@PathParam(&quot;pageId&quot;)String pageId,@QueryParam(&quot;station&quot;)String station){
<span class="nc bnc" id="L581" title="All 2 branches missed."> Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L582"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L583"> JSONObject page=fetchPage(c,pageId);</span>
<span class="nc bnc" id="L584" title="All 2 branches missed."> if(page==null)return err(404,&quot;Page not found&quot;);</span>
<span class="nc bnc" id="L585" title="All 2 branches missed."> if(!designer()){</span>
<span class="nc" id="L586"> java.util.Set&lt;String&gt; seen=new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L587"> JSONObject ancestor=page;</span>
<span class="nc bnc" id="L588" title="All 2 branches missed."> while(ancestor!=null){</span>
<span class="nc bnc" id="L589" title="All 4 branches missed."> if(!seen.add(ancestor.getString(&quot;id&quot;))||!ancestor.getBoolean(&quot;enabled&quot;))return err(404,&quot;Page not found&quot;);</span>
<span class="nc bnc" id="L590" title="All 2 branches missed."> String parent=ancestor.isNull(&quot;parentPageId&quot;)?null:ancestor.optString(&quot;parentPageId&quot;,null);</span>
<span class="nc bnc" id="L591" title="All 2 branches missed."> ancestor=parent==null?null:fetchPage(c,parent);</span>
<span class="nc bnc" id="L592" title="All 4 branches missed."> if(parent!=null&amp;&amp;ancestor==null)return err(404,&quot;Page not found&quot;);</span>
<span class="nc" id="L593"> }</span>
<span class="nc bnc" id="L595" title="All 2 branches missed."> Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L596"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L597"> JSONObject page=fetchPage(c,pageId);</span>
<span class="nc bnc" id="L598" title="All 2 branches missed."> if(page==null)return err(404,&quot;Page not found&quot;);</span>
<span class="nc bnc" id="L599" title="All 2 branches missed."> if(!designer()){</span>
<span class="nc" id="L600"> java.util.Set&lt;String&gt; seen=new java.util.HashSet&lt;&gt;();</span>
<span class="nc" id="L601"> JSONObject ancestor=page;</span>
<span class="nc bnc" id="L602" title="All 2 branches missed."> while(ancestor!=null){</span>
<span class="nc bnc" id="L603" title="All 4 branches missed."> if(!seen.add(ancestor.getString(&quot;id&quot;))||!ancestor.getBoolean(&quot;enabled&quot;))return err(404,&quot;Page not found&quot;);</span>
<span class="nc bnc" id="L604" title="All 2 branches missed."> String parent=ancestor.isNull(&quot;parentPageId&quot;)?null:ancestor.optString(&quot;parentPageId&quot;,null);</span>
<span class="nc bnc" id="L605" title="All 2 branches missed."> ancestor=parent==null?null:fetchPage(c,parent);</span>
<span class="nc bnc" id="L606" title="All 4 branches missed."> if(parent!=null&amp;&amp;ancestor==null)return err(404,&quot;Page not found&quot;);</span>
<span class="nc" id="L607"> }</span>
}
<span class="nc" id="L595"> List&lt;String&gt; groups=groups(c,user());</span>
<span class="nc" id="L596"> JSONObject template=resolveTemplate(c,pageId,station,groups);</span>
<span class="nc bnc" id="L597" title="All 2 branches missed."> if(template==null)return err(404,&quot;No published template assigned&quot;);</span>
<span class="nc" id="L598"> JSONObject prefs=new JSONObject().put(&quot;fontScale&quot;,1).put(&quot;theme&quot;,&quot;ios&quot;).put(&quot;contrast&quot;,&quot;normal&quot;).put(&quot;accent&quot;,&quot;#a40640&quot;);</span>
<span class="nc" id="L599"> merge(prefs,preference(c,&quot;DEFAULT&quot;,&quot;*&quot;));</span>
<span class="nc bnc" id="L600" title="All 2 branches missed."> for(String g:groups)merge(prefs,preference(c,&quot;GROUP&quot;,g));</span>
<span class="nc bnc" id="L601" title="All 4 branches missed."> if(station!=null&amp;&amp;!station.isEmpty())merge(prefs,preference(c,&quot;STATION&quot;,station));</span>
<span class="nc" id="L602"> merge(prefs,preference(c,&quot;USER&quot;,user()));</span>
<span class="nc" id="L603"> JSONObject overrides=new JSONObject();String baseId=&quot;&quot;;</span>
<span class="nc" id="L604"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT BASE_TEMPLATE_ID,OVERRIDES_JSON FROM &quot;+schema()+&quot;HMI_APP_LAYOUT WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?&quot;)){</span>
<span class="nc" id="L605"> p.setString(1,user());p.setString(2,pageId);p.setString(3, stationKey(station));</span>
<span class="nc bnc" id="L606" title="All 2 branches missed."> try(ResultSet rs=p.executeQuery()) {if(rs.next()){baseId=rs.getString(1);overrides=objectOrEmpty(clob(rs,&quot;OVERRIDES_JSON&quot;));}}</span>
<span class="nc" id="L609"> List&lt;String&gt; groups=groups(c,user());</span>
<span class="nc" id="L610"> JSONObject template=resolveTemplate(c,pageId,station,groups);</span>
<span class="nc bnc" id="L611" title="All 2 branches missed."> if(template==null)return err(404,&quot;No published template assigned&quot;);</span>
<span class="nc" id="L612"> JSONObject prefs=new JSONObject().put(&quot;fontScale&quot;,1).put(&quot;theme&quot;,&quot;ios&quot;).put(&quot;contrast&quot;,&quot;normal&quot;).put(&quot;accent&quot;,&quot;#a40640&quot;);</span>
<span class="nc" id="L613"> merge(prefs,preference(c,&quot;DEFAULT&quot;,&quot;*&quot;));</span>
<span class="nc bnc" id="L614" title="All 2 branches missed."> for(String g:groups)merge(prefs,preference(c,&quot;GROUP&quot;,g));</span>
<span class="nc bnc" id="L615" title="All 4 branches missed."> if(station!=null&amp;&amp;!station.isEmpty())merge(prefs,preference(c,&quot;STATION&quot;,station));</span>
<span class="nc" id="L616"> merge(prefs,preference(c,&quot;USER&quot;,user()));</span>
<span class="nc" id="L617"> JSONObject overrides=new JSONObject();String baseId=&quot;&quot;;</span>
<span class="nc" id="L618"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT BASE_TEMPLATE_ID,OVERRIDES_JSON FROM &quot;+schema()+&quot;HMI_APP_LAYOUT WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?&quot;)){</span>
<span class="nc" id="L619"> p.setString(1,user());p.setString(2,pageId);p.setString(3, stationKey(station));</span>
<span class="nc bnc" id="L620" title="All 2 branches missed."> try(ResultSet rs=p.executeQuery()) {if(rs.next()){baseId=rs.getString(1);overrides=objectOrEmpty(clob(rs,&quot;OVERRIDES_JSON&quot;));}}</span>
}
<span class="nc" id="L608"> JSONObject layout=template.getJSONObject(&quot;layout&quot;);</span>
<span class="nc" id="L609"> JSONArray effectiveWidgets=new JSONArray();</span>
<span class="nc" id="L610"> java.util.Set&lt;String&gt; granted = HmiAuth.roles(request);</span>
<span class="nc bnc" id="L611" title="All 2 branches missed."> for(Object val:layout.getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L612"> JSONObject widget=new JSONObject(val.toString());String widgetId=widget.getString(&quot;id&quot;);</span>
<span class="nc" id="L622"> JSONObject layout=template.getJSONObject(&quot;layout&quot;);</span>
<span class="nc" id="L623"> JSONArray effectiveWidgets=new JSONArray();</span>
<span class="nc" id="L624"> java.util.Set&lt;String&gt; granted = HmiAuth.roles(request);</span>
<span class="nc bnc" id="L625" title="All 2 branches missed."> for(Object val:layout.getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L626"> JSONObject widget=new JSONObject(val.toString());String widgetId=widget.getString(&quot;id&quot;);</span>
// Security: role-restricted widgets must never be delivered in the effective payload.
<span class="nc" id="L614"> JSONArray required = widget.optJSONArray(&quot;visibleToRoles&quot;);</span>
<span class="nc bnc" id="L615" title="All 6 branches missed."> if (required!=null &amp;&amp; required.length()&gt;0 &amp;&amp; !granted.contains(&quot;hmi-admin&quot;)) {</span>
<span class="nc" id="L616"> boolean allowed=false;</span>
<span class="nc bnc" id="L617" title="All 4 branches missed."> for(int i=0;i&lt;required.length();i++) if(granted.contains(required.optString(i))) {allowed=true;break;}</span>
<span class="nc bnc" id="L618" title="All 2 branches missed."> if(!allowed)continue;</span>
<span class="nc" id="L628"> JSONArray required = widget.optJSONArray(&quot;visibleToRoles&quot;);</span>
<span class="nc bnc" id="L629" title="All 6 branches missed."> if (required!=null &amp;&amp; required.length()&gt;0 &amp;&amp; !granted.contains(&quot;hmi-admin&quot;)) {</span>
<span class="nc" id="L630"> boolean allowed=false;</span>
<span class="nc bnc" id="L631" title="All 4 branches missed."> for(int i=0;i&lt;required.length();i++) if(granted.contains(required.optString(i))) {allowed=true;break;}</span>
<span class="nc bnc" id="L632" title="All 2 branches missed."> if(!allowed)continue;</span>
}
<span class="nc bnc" id="L620" title="All 4 branches missed."> if(widget.optBoolean(&quot;personalizable&quot;,true)&amp;&amp;overrides.has(widgetId)){</span>
<span class="nc" id="L621"> JSONObject o=overrides.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L622" title="All 4 branches missed."> for(String key:new String[]{&quot;x&quot;,&quot;y&quot;,&quot;w&quot;,&quot;h&quot;})if(o.has(key))widget.put(key,o.getInt(key));</span>
<span class="nc bnc" id="L623" title="All 4 branches missed."> if(!widget.optBoolean(&quot;mandatory&quot;,false)&amp;&amp;o.optBoolean(&quot;hidden&quot;,false))widget.put(&quot;hidden&quot;,true);</span>
<span class="nc bnc" id="L624" title="All 2 branches missed."> if(widget.getInt(&quot;x&quot;)+widget.getInt(&quot;w&quot;)&gt;12)widget.put(&quot;x&quot;,0);</span>
<span class="nc bnc" id="L634" title="All 4 branches missed."> if(widget.optBoolean(&quot;personalizable&quot;,true)&amp;&amp;overrides.has(widgetId)){</span>
<span class="nc" id="L635"> JSONObject o=overrides.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L636" title="All 4 branches missed."> for(String key:new String[]{&quot;x&quot;,&quot;y&quot;,&quot;w&quot;,&quot;h&quot;})if(o.has(key))widget.put(key,o.getInt(key));</span>
<span class="nc bnc" id="L637" title="All 4 branches missed."> if(!widget.optBoolean(&quot;mandatory&quot;,false)&amp;&amp;o.optBoolean(&quot;hidden&quot;,false))widget.put(&quot;hidden&quot;,true);</span>
<span class="nc bnc" id="L638" title="All 2 branches missed."> if(widget.getInt(&quot;x&quot;)+widget.getInt(&quot;w&quot;)&gt;12)widget.put(&quot;x&quot;,0);</span>
}
<span class="nc" id="L626"> effectiveWidgets.put(widget);</span>
<span class="nc" id="L627"> }</span>
<span class="nc" id="L628"> return json(new JSONObject().put(&quot;page&quot;,page).put(&quot;templateId&quot;,template.getString(&quot;id&quot;))</span>
<span class="nc" id="L629"> .put(&quot;version&quot;,template.getInt(&quot;version&quot;)).put(&quot;originalTemplateId&quot;,baseId)</span>
<span class="nc" id="L630"> .put(&quot;layout&quot;,new JSONObject().put(&quot;widgets&quot;,effectiveWidgets)).put(&quot;preferences&quot;,prefs));</span>
<span class="nc bnc" id="L631" title="All 8 branches missed."> }catch(Exception e){return dbError(e);}</span>
<span class="nc" id="L640"> effectiveWidgets.put(widget);</span>
<span class="nc" id="L641"> }</span>
<span class="nc" id="L642"> return json(new JSONObject().put(&quot;page&quot;,page).put(&quot;templateId&quot;,template.getString(&quot;id&quot;))</span>
<span class="nc" id="L643"> .put(&quot;version&quot;,template.getInt(&quot;version&quot;)).put(&quot;originalTemplateId&quot;,baseId)</span>
<span class="nc" id="L644"> .put(&quot;layout&quot;,new JSONObject().put(&quot;widgets&quot;,effectiveWidgets)).put(&quot;preferences&quot;,prefs));</span>
<span class="nc bnc" id="L645" title="All 8 branches missed."> }catch(Exception e){return dbError(e);}</span>
}
@PUT @Path(&quot;pages/{pageId}/layout&quot;) @Consumes(MediaType.APPLICATION_JSON)
public Response saveLayout(@PathParam(&quot;pageId&quot;)String pageId,String body){
<span class="nc bnc" id="L635" title="All 2 branches missed."> Response r=writeAllowed(true);if(r!=null)return r;</span>
<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="L637"> JSONObject in=parse(body);</span>
<span class="nc" id="L638"> JSONObject o=validateOverrides(in.getJSONObject(&quot;overrides&quot;));</span>
<span class="nc" id="L639"> try(Connection c=HmiDb.open()){</span>
<span class="nc bnc" id="L640" title="All 2 branches missed."> if(!pageExists(c,pageId))return err(404,&quot;Page not found&quot;);</span>
<span class="nc" id="L651"> JSONObject in=parse(body);</span>
<span class="nc" id="L652"> JSONObject o=validateOverrides(in.getJSONObject(&quot;overrides&quot;));</span>
<span class="nc" id="L653"> try(Connection c=HmiDb.open()){</span>
<span class="nc bnc" id="L654" title="All 2 branches missed."> if(!pageExists(c,pageId))return err(404,&quot;Page not found&quot;);</span>
// Server enforcement: only published, personalizable widget IDs can be overridden.
<span class="nc" id="L642"> JSONObject template=resolveTemplate(c,pageId,in.optString(&quot;station&quot;,&quot;&quot;),groups(c,user()));</span>
<span class="nc bnc" id="L643" title="All 2 branches missed."> if(template==null)return err(409,&quot;Page has no published template&quot;);</span>
<span class="nc" id="L644"> JSONObject allowed=new JSONObject();</span>
<span class="nc bnc" id="L645" title="All 2 branches missed."> for(Object obj:template.getJSONObject(&quot;layout&quot;).getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L646"> JSONObject w=(JSONObject)obj;String widgetId=w.getString(&quot;id&quot;);</span>
<span class="nc bnc" id="L647" title="All 4 branches missed."> if(!o.has(widgetId) || !w.optBoolean(&quot;personalizable&quot;,true))continue;</span>
<span class="nc" id="L648"> JSONObject override=o.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L649" title="All 4 branches missed."> if(w.optBoolean(&quot;mandatory&quot;,false)&amp;&amp;override.optBoolean(&quot;hidden&quot;,false))return err(400,&quot;Cannot hide mandatory widget&quot;);</span>
<span class="nc" id="L650"> allowed.put(widgetId,override);</span>
<span class="nc" id="L651"> }</span>
<span class="nc" id="L656"> JSONObject template=resolveTemplate(c,pageId,in.optString(&quot;station&quot;,&quot;&quot;),groups(c,user()));</span>
<span class="nc bnc" id="L657" title="All 2 branches missed."> if(template==null)return err(409,&quot;Page has no published template&quot;);</span>
<span class="nc" id="L658"> JSONObject allowed=new JSONObject();</span>
<span class="nc bnc" id="L659" title="All 2 branches missed."> for(Object obj:template.getJSONObject(&quot;layout&quot;).getJSONArray(&quot;widgets&quot;)) {</span>
<span class="nc" id="L660"> JSONObject w=(JSONObject)obj;String widgetId=w.getString(&quot;id&quot;);</span>
<span class="nc bnc" id="L661" title="All 4 branches missed."> if(!o.has(widgetId) || !w.optBoolean(&quot;personalizable&quot;,true))continue;</span>
<span class="nc" id="L662"> JSONObject override=o.getJSONObject(widgetId);</span>
<span class="nc bnc" id="L663" title="All 4 branches missed."> if(w.optBoolean(&quot;mandatory&quot;,false)&amp;&amp;override.optBoolean(&quot;hidden&quot;,false))return err(400,&quot;Cannot hide mandatory widget&quot;);</span>
<span class="nc" id="L664"> allowed.put(widgetId,override);</span>
<span class="nc" id="L665"> }</span>
int count;
<span class="nc" id="L653"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_LAYOUT SET OVERRIDES_JSON=?,BASE_TEMPLATE_ID=?,UPDATED_AT=? WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?&quot;)){</span>
<span class="nc" id="L654"> clob(p,1,allowed.toString());p.setString(2,template.getString(&quot;id&quot;));p.setTimestamp(3,Timestamp.from(Instant.now()));p.setString(4,user());p.setString(5,pageId);p.setString(6,stationKey(in.optString(&quot;station&quot;, &quot;&quot;)));count=p.executeUpdate();</span>
<span class="nc" id="L667"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_LAYOUT SET OVERRIDES_JSON=?,BASE_TEMPLATE_ID=?,UPDATED_AT=? WHERE USER_ID=? AND PAGE_ID=? AND STATION_ID=?&quot;)){</span>
<span class="nc" id="L668"> clob(p,1,allowed.toString());p.setString(2,template.getString(&quot;id&quot;));p.setTimestamp(3,Timestamp.from(Instant.now()));p.setString(4,user());p.setString(5,pageId);p.setString(6,stationKey(in.optString(&quot;station&quot;, &quot;&quot;)));count=p.executeUpdate();</span>
}
<span class="nc bnc" id="L656" title="All 2 branches missed."> if(count==0){try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_LAYOUT(USER_ID,PAGE_ID,STATION_ID,BASE_TEMPLATE_ID,OVERRIDES_JSON,UPDATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L657"> p.setString(1,user());p.setString(2,pageId);p.setString(3,stationKey(in.optString(&quot;station&quot;, &quot;&quot;)));p.setString(4,template.getString(&quot;id&quot;));clob(p,5,allowed.toString());p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();</span>
<span class="nc bnc" id="L670" title="All 2 branches missed."> if(count==0){try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_LAYOUT(USER_ID,PAGE_ID,STATION_ID,BASE_TEMPLATE_ID,OVERRIDES_JSON,UPDATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L671"> p.setString(1,user());p.setString(2,pageId);p.setString(3,stationKey(in.optString(&quot;station&quot;, &quot;&quot;)));p.setString(4,template.getString(&quot;id&quot;));clob(p,5,allowed.toString());p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();</span>
}}
<span class="nc" id="L659"> audit(c,&quot;user.layout&quot;,pageId,&quot;Saved overrides&quot;);</span>
<span class="nc" id="L660"> return json(new JSONObject().put(&quot;ok&quot;,true).put(&quot;saved&quot;,allowed.length()));</span>
<span class="nc bnc" id="L661" title="All 6 branches missed."> }</span>
<span class="nc" id="L662"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L673"> audit(c,&quot;user.layout&quot;,pageId,&quot;Saved overrides&quot;);</span>
<span class="nc" id="L674"> return json(new JSONObject().put(&quot;ok&quot;,true).put(&quot;saved&quot;,allowed.length()));</span>
<span class="nc bnc" id="L675" title="All 6 branches missed."> }</span>
<span class="nc" id="L676"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
}
@GET @Path(&quot;preferences&quot;)
<span class="nc bnc" id="L665" title="All 2 branches missed."> public Response myPreferences(){Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L666"> try(Connection c=HmiDb.open()){return json(preference(c,&quot;USER&quot;,user()));}catch(SQLException e){return dbError(e);}}</span>
<span class="nc bnc" id="L679" title="All 2 branches missed."> public Response myPreferences(){Response r=denied(true);if(r!=null)return r;</span>
<span class="nc" id="L680"> try(Connection c=HmiDb.open()){return json(preference(c,&quot;USER&quot;,user()));}catch(SQLException e){return dbError(e);}}</span>
@PUT @Path(&quot;preferences&quot;) @Consumes(MediaType.APPLICATION_JSON)
<span class="nc bnc" id="L668" title="All 2 branches missed."> public Response myPreferencesSave(String body){Response r=writeAllowed(true);if(r!=null)return r;</span>
<span class="nc" id="L669"> try{return storePreferences(&quot;USER&quot;,user(),parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
<span class="nc bnc" id="L682" title="All 2 branches missed."> public Response myPreferencesSave(String body){Response r=writeAllowed(true);if(r!=null)return r;</span>
<span class="nc" id="L683"> try{return storePreferences(&quot;USER&quot;,user(),parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
@PUT @Path(&quot;groups/{group}/preferences&quot;) @Consumes(MediaType.APPLICATION_JSON)
<span class="nc bnc" id="L671" title="All 2 branches missed."> public Response groupPreferences(@PathParam(&quot;group&quot;)String group,String body){Response r=writeAllowed(admin());if(r!=null)return r;</span>
<span class="nc bnc" id="L672" title="All 2 branches missed."> if(!legalKey(group))return err(400,&quot;Invalid group&quot;);</span>
<span class="nc" id="L673"> try{return storePreferences(&quot;GROUP&quot;,group,parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
<span class="nc bnc" id="L685" title="All 2 branches missed."> public Response groupPreferences(@PathParam(&quot;group&quot;)String group,String body){Response r=writeAllowed(admin());if(r!=null)return r;</span>
<span class="nc bnc" id="L686" title="All 2 branches missed."> if(!legalKey(group))return err(400,&quot;Invalid group&quot;);</span>
<span class="nc" id="L687"> try{return storePreferences(&quot;GROUP&quot;,group,parse(body));}catch(JSONException e){return err(400,e.getMessage());}}</span>
@PUT @Path(&quot;groups/{group}/members/{member}&quot;)
public Response groupMember(@PathParam(&quot;group&quot;)String group,@PathParam(&quot;member&quot;)String member){
<span class="nc bnc" id="L676" title="All 2 branches missed."> Response r=writeAllowed(admin());if(r!=null)return r;</span>
<span class="nc bnc" id="L677" title="All 6 branches missed."> if(!legalKey(group)||member.length()&gt;255||member.isEmpty())return err(400,&quot;Invalid group or member&quot;);</span>
<span class="nc" id="L678"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L679"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT USER_ID FROM &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER WHERE GROUP_ID=? AND USER_ID=?&quot;)){</span>
<span class="nc bnc" id="L680" title="All 4 branches missed."> p.setString(1,group);p.setString(2,member);try(ResultSet rs=p.executeQuery()){if(rs.next())return json(new JSONObject().put(&quot;ok&quot;,true));}</span>
<span class="nc bnc" id="L681" title="All 2 branches missed."> }</span>
<span class="nc" id="L682"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER(USER_ID,GROUP_ID) VALUES (?,?)&quot;)){</span>
<span class="nc" id="L683"> p.setString(1,member);p.setString(2,group);p.executeUpdate();</span>
<span class="nc bnc" id="L690" title="All 2 branches missed."> Response r=writeAllowed(admin());if(r!=null)return r;</span>
<span class="nc bnc" id="L691" title="All 6 branches missed."> if(!legalKey(group)||member.length()&gt;255||member.isEmpty())return err(400,&quot;Invalid group or member&quot;);</span>
<span class="nc" id="L692"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L693"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT USER_ID FROM &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER WHERE GROUP_ID=? AND USER_ID=?&quot;)){</span>
<span class="nc bnc" id="L694" title="All 4 branches missed."> p.setString(1,group);p.setString(2,member);try(ResultSet rs=p.executeQuery()){if(rs.next())return json(new JSONObject().put(&quot;ok&quot;,true));}</span>
<span class="nc bnc" id="L695" title="All 2 branches missed."> }</span>
<span class="nc" id="L696"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER(USER_ID,GROUP_ID) VALUES (?,?)&quot;)){</span>
<span class="nc" id="L697"> p.setString(1,member);p.setString(2,group);p.executeUpdate();</span>
}
<span class="nc" id="L685"> audit(c,&quot;group.member.added&quot;,group,member);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L686" title="All 2 branches missed."> }catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L699"> audit(c,&quot;group.member.added&quot;,group,member);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L700" title="All 2 branches missed."> }catch(SQLException e){return dbError(e);}</span>
}
@POST @Path(&quot;assignments&quot;) @Consumes(MediaType.APPLICATION_JSON)
<span class="nc bnc" id="L689" title="All 2 branches missed."> public Response assign(String body){Response r=writeAllowed(publisher());if(r!=null)return r;</span>
<span class="nc bnc" id="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="L691"> JSONObject in=parse(body);String pageId=need(in,&quot;pageId&quot;,64), templateId=need(in,&quot;templateId&quot;,64), type=need(in,&quot;scopeType&quot;,20), scope=need(in,&quot;scopeId&quot;,255);</span>
<span class="nc bnc" id="L692" title="All 6 branches missed."> if(!type.equals(&quot;GROUP&quot;)&amp;&amp;!type.equals(&quot;STATION&quot;)&amp;&amp;!type.equals(&quot;DEFAULT&quot;))return err(400,&quot;Invalid scope&quot;);</span>
<span class="nc" id="L693"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L694"> try(PreparedStatement check=c.prepareStatement(&quot;SELECT STATUS FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=? AND PAGE_ID=?&quot;)){</span>
<span class="nc" id="L695"> check.setString(1,templateId);check.setString(2,pageId);</span>
<span class="nc bnc" id="L696" title="All 6 branches missed."> try(ResultSet rs=check.executeQuery()){if(!rs.next()||!&quot;PUBLISHED&quot;.equals(rs.getString(1)))return err(400,&quot;Only published templates can be assigned&quot;);}</span>
<span class="nc bnc" id="L697" title="All 2 branches missed."> }</span>
<span class="nc" id="L698"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_ASSIGNMENT(ID,PAGE_ID,SCOPE_TYPE,SCOPE_ID,TEMPLATE_ID,PRIORITY) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L699"> p.setString(1,id());p.setString(2,pageId);p.setString(3,type);p.setString(4,scope);p.setString(5,templateId);p.setInt(6,bounded(in,&quot;priority&quot;,0,1000,0));p.executeUpdate();</span>
<span class="nc" id="L705"> JSONObject in=parse(body);String pageId=need(in,&quot;pageId&quot;,64), templateId=need(in,&quot;templateId&quot;,64), type=need(in,&quot;scopeType&quot;,20), scope=need(in,&quot;scopeId&quot;,255);</span>
<span class="nc bnc" id="L706" title="All 6 branches missed."> if(!type.equals(&quot;GROUP&quot;)&amp;&amp;!type.equals(&quot;STATION&quot;)&amp;&amp;!type.equals(&quot;DEFAULT&quot;))return err(400,&quot;Invalid scope&quot;);</span>
<span class="nc" id="L707"> try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L708"> try(PreparedStatement check=c.prepareStatement(&quot;SELECT STATUS FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE ID=? AND PAGE_ID=?&quot;)){</span>
<span class="nc" id="L709"> check.setString(1,templateId);check.setString(2,pageId);</span>
<span class="nc bnc" id="L710" title="All 6 branches missed."> try(ResultSet rs=check.executeQuery()){if(!rs.next()||!&quot;PUBLISHED&quot;.equals(rs.getString(1)))return err(400,&quot;Only published templates can be assigned&quot;);}</span>
<span class="nc bnc" id="L711" title="All 2 branches missed."> }</span>
<span class="nc" id="L712"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_ASSIGNMENT(ID,PAGE_ID,SCOPE_TYPE,SCOPE_ID,TEMPLATE_ID,PRIORITY) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L713"> p.setString(1,id());p.setString(2,pageId);p.setString(3,type);p.setString(4,scope);p.setString(5,templateId);p.setInt(6,bounded(in,&quot;priority&quot;,0,1000,0));p.executeUpdate();</span>
}
<span class="nc" id="L701"> audit(c,&quot;template.assigned&quot;,templateId,type+&quot;:&quot;+scope);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L702" title="All 2 branches missed."> }</span>
<span class="nc" id="L703"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L715"> audit(c,&quot;template.assigned&quot;,templateId,type+&quot;:&quot;+scope);return json(new JSONObject().put(&quot;ok&quot;,true));</span>
<span class="nc bnc" id="L716" title="All 2 branches missed."> }</span>
<span class="nc" id="L717"> }catch(JSONException e){return err(400,e.getMessage());}catch(SQLException e){return dbError(e);}</span>
}
private Response storePreferences(String type,String identity,JSONObject input){
<span class="nc" id="L706"> JSONObject prefs=validatePreferences(input);</span>
<span class="nc" id="L707"> try(Connection c=HmiDb.open()){</span>
<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="L709"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_PREF SET PREFS_JSON=?,UPDATED_AT=? WHERE SCOPE_TYPE=? AND SCOPE_ID=?&quot;)){</span>
<span class="nc" id="L710"> clob(p,1,prefs.toString());p.setTimestamp(2,Timestamp.from(Instant.now()));p.setString(3,type);p.setString(4,identity);count=p.executeUpdate();</span>
<span class="nc" id="L723"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+schema()+&quot;HMI_APP_PREF SET PREFS_JSON=?,UPDATED_AT=? WHERE SCOPE_TYPE=? AND SCOPE_ID=?&quot;)){</span>
<span class="nc" id="L724"> clob(p,1,prefs.toString());p.setTimestamp(2,Timestamp.from(Instant.now()));p.setString(3,type);p.setString(4,identity);count=p.executeUpdate();</span>
}
<span class="nc bnc" id="L712" title="All 2 branches missed."> if(count==0){try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_PREF(SCOPE_TYPE,SCOPE_ID,PREFS_JSON,UPDATED_AT) VALUES (?,?,?,?)&quot;)){</span>
<span class="nc" id="L713"> p.setString(1,type);p.setString(2,identity);clob(p,3,prefs.toString());p.setTimestamp(4,Timestamp.from(Instant.now()));p.executeUpdate();</span>
<span class="nc bnc" id="L726" title="All 2 branches missed."> if(count==0){try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_PREF(SCOPE_TYPE,SCOPE_ID,PREFS_JSON,UPDATED_AT) VALUES (?,?,?,?)&quot;)){</span>
<span class="nc" id="L727"> p.setString(1,type);p.setString(2,identity);clob(p,3,prefs.toString());p.setTimestamp(4,Timestamp.from(Instant.now()));p.executeUpdate();</span>
}}
<span class="nc" id="L715"> audit(c,&quot;preferences.saved&quot;,type+&quot;:&quot;+identity,&quot;&quot;);return json(prefs);</span>
<span class="nc" id="L716"> }catch(SQLException e){return dbError(e);}</span>
<span class="nc" id="L729"> audit(c,&quot;preferences.saved&quot;,type+&quot;:&quot;+identity,&quot;&quot;);return json(prefs);</span>
<span class="nc" id="L730"> }catch(SQLException e){return dbError(e);}</span>
}
private JSONObject preference(Connection c,String type,String identity)throws SQLException{
<span class="nc" id="L719"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT PREFS_JSON FROM &quot;+schema()+&quot;HMI_APP_PREF WHERE SCOPE_TYPE=? AND SCOPE_ID=?&quot;)){</span>
<span class="nc" id="L720"> p.setString(1,type);p.setString(2,identity);</span>
<span class="nc bnc" id="L721" title="All 2 branches missed."> try(ResultSet rs=p.executeQuery()){return rs.next()?objectOrEmpty(clob(rs,&quot;PREFS_JSON&quot;)):new JSONObject();}</span>
<span class="nc" id="L733"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT PREFS_JSON FROM &quot;+schema()+&quot;HMI_APP_PREF WHERE SCOPE_TYPE=? AND SCOPE_ID=?&quot;)){</span>
<span class="nc" id="L734"> p.setString(1,type);p.setString(2,identity);</span>
<span class="nc bnc" id="L735" title="All 2 branches missed."> try(ResultSet rs=p.executeQuery()){return rs.next()?objectOrEmpty(clob(rs,&quot;PREFS_JSON&quot;)):new JSONObject();}</span>
}
}
<span class="nc bnc" id="L724" title="All 2 branches missed."> private static void merge(JSONObject target,JSONObject changes){for(String k:changes.keySet())target.put(k,changes.get(k));}</span>
<span class="nc bnc" id="L738" title="All 2 branches missed."> private static void merge(JSONObject target,JSONObject changes){for(String k:changes.keySet())target.put(k,changes.get(k));}</span>
private List&lt;String&gt; groups(Connection c,String userId)throws SQLException{
<span class="nc" id="L726"> List&lt;String&gt; a=new ArrayList&lt;&gt;();</span>
<span class="nc" id="L727"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT GROUP_ID FROM &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER WHERE USER_ID=? ORDER BY GROUP_ID&quot;)){</span>
<span class="nc bnc" id="L728" 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="L740"> List&lt;String&gt; a=new ArrayList&lt;&gt;();</span>
<span class="nc" id="L741"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT GROUP_ID FROM &quot;+schema()+&quot;HMI_APP_GROUP_MEMBER WHERE USER_ID=? ORDER BY GROUP_ID&quot;)){</span>
<span class="nc bnc" id="L742" title="All 2 branches missed."> p.setString(1,userId);try(ResultSet rs=p.executeQuery()){while(rs.next())a.add(rs.getString(1));}</span>
}
<span class="nc" id="L730"> String pers=HmiAuth.persId(c,userId);</span>
<span class="nc bnc" id="L731" title="All 2 branches missed."> if(pers!=null)try(PreparedStatement p=c.prepareStatement(</span>
<span class="nc" id="L732"> &quot;SELECT GROUP_KEY FROM &quot;+schema()+&quot;HMI_APP_PERSON_GROUP WHERE PERS_ID=? ORDER BY GROUP_KEY&quot;)) {</span>
<span class="nc" id="L733"> p.setString(1,pers);</span>
<span class="nc bnc" id="L734" 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="L744"> String pers=HmiAuth.persId(c,userId);</span>
<span class="nc bnc" id="L745" title="All 2 branches missed."> if(pers!=null)try(PreparedStatement p=c.prepareStatement(</span>
<span class="nc" id="L746"> &quot;SELECT GROUP_KEY FROM &quot;+schema()+&quot;HMI_APP_PERSON_GROUP WHERE PERS_ID=? ORDER BY GROUP_KEY&quot;)) {</span>
<span class="nc" id="L747"> p.setString(1,pers);</span>
<span class="nc bnc" id="L748" title="All 4 branches missed."> try(ResultSet rs=p.executeQuery()) { while(rs.next())if(!a.contains(rs.getString(1)))a.add(rs.getString(1)); }</span>
}
<span class="nc" id="L736"> return a;</span>
<span class="nc" id="L750"> return a;</span>
}
private JSONObject fetchPage(Connection c,String pageId)throws SQLException{
<span class="nc" id="L739"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID FROM &quot;+schema()+&quot;HMI_APP_PAGE WHERE ID=?&quot;)){</span>
<span class="nc" id="L740"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L741" title="All 2 branches missed."> return rs.next()?new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;key&quot;,rs.getString(2)).put(&quot;title&quot;,rs.getString(3))</span>
<span class="nc bnc" id="L742" title="All 2 branches missed."> .put(&quot;menuGroup&quot;,rs.getString(4)).put(&quot;order&quot;,rs.getInt(5)).put(&quot;enabled&quot;,rs.getInt(6)!=0)</span>
<span class="nc bnc" id="L743" title="All 2 branches missed."> .put(&quot;parentPageId&quot;,rs.getString(7)==null?JSONObject.NULL:rs.getString(7)):null;</span>
<span class="nc" id="L753"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID,PAGE_KEY,TITLE,MENU_GROUP,SORT_ORDER,ENABLED,PARENT_PAGE_ID FROM &quot;+schema()+&quot;HMI_APP_PAGE WHERE ID=?&quot;)){</span>
<span class="nc" id="L754"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L755" title="All 2 branches missed."> return rs.next()?new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;key&quot;,rs.getString(2)).put(&quot;title&quot;,rs.getString(3))</span>
<span class="nc bnc" id="L756" title="All 2 branches missed."> .put(&quot;menuGroup&quot;,rs.getString(4)).put(&quot;order&quot;,rs.getInt(5)).put(&quot;enabled&quot;,rs.getInt(6)!=0)</span>
<span class="nc bnc" id="L757" title="All 2 branches missed."> .put(&quot;parentPageId&quot;,rs.getString(7)==null?JSONObject.NULL:rs.getString(7)):null;</span>
}
}
}
<span class="nc bnc" id="L747" title="All 2 branches missed."> private boolean pageExists(Connection c,String pageId)throws SQLException{return fetchPage(c,pageId)!=null;}</span>
<span class="nc bnc" id="L761" title="All 2 branches missed."> private boolean pageExists(Connection c,String pageId)throws SQLException{return fetchPage(c,pageId)!=null;}</span>
private JSONObject resolveTemplate(Connection c,String pageId,String station,List&lt;String&gt; groups)throws SQLException{
<span class="nc" id="L749"> List&lt;String&gt; ids=new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L750" title="All 4 branches missed."> if(station!=null&amp;&amp;!station.isEmpty())ids.add(&quot;STATION:&quot;+station);</span>
<span class="nc bnc" id="L751" title="All 2 branches missed."> for(String g:groups)ids.add(&quot;GROUP:&quot;+g);</span>
<span class="nc" id="L752"> ids.add(&quot;DEFAULT:*&quot;);</span>
<span class="nc" id="L763"> List&lt;String&gt; ids=new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L764" title="All 4 branches missed."> if(station!=null&amp;&amp;!station.isEmpty())ids.add(&quot;STATION:&quot;+station);</span>
<span class="nc bnc" id="L765" title="All 2 branches missed."> for(String g:groups)ids.add(&quot;GROUP:&quot;+g);</span>
<span class="nc" id="L766"> ids.add(&quot;DEFAULT:*&quot;);</span>
// Distinct selected precedence STATION &gt; GROUP &gt; DEFAULT; highest priority within a scope.
<span class="nc bnc" id="L754" title="All 2 branches missed."> for(String selection:ids){</span>
<span class="nc" id="L755"> String[] parts=selection.split(&quot;:&quot;,2);</span>
<span class="nc" id="L756"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT T.ID,T.VERSION_NO,T.LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_ASSIGNMENT A JOIN &quot;+schema()+&quot;HMI_APP_TEMPLATE T ON A.TEMPLATE_ID=T.ID WHERE A.PAGE_ID=? AND A.SCOPE_TYPE=? AND A.SCOPE_ID=? AND T.STATUS='PUBLISHED' ORDER BY A.PRIORITY DESC,T.VERSION_NO DESC&quot;)){</span>
<span class="nc" id="L757"> p.setString(1,pageId);p.setString(2,parts[0]);p.setString(3,parts[1]);</span>
<span class="nc bnc" id="L758" title="All 4 branches missed."> try(ResultSet rs=p.executeQuery()){if(rs.next())return new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;)));}</span>
<span class="nc bnc" id="L759" title="All 2 branches missed."> }</span>
<span class="nc" id="L760"> }</span>
<span class="nc" id="L761"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID,VERSION_NO,LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE PAGE_ID=? AND STATUS='PUBLISHED' ORDER BY VERSION_NO DESC&quot;)){</span>
<span class="nc" id="L762"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L763" title="All 2 branches missed."> return rs.next()?new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;))):null;</span>
<span class="nc bnc" id="L768" title="All 2 branches missed."> for(String selection:ids){</span>
<span class="nc" id="L769"> String[] parts=selection.split(&quot;:&quot;,2);</span>
<span class="nc" id="L770"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT T.ID,T.VERSION_NO,T.LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_ASSIGNMENT A JOIN &quot;+schema()+&quot;HMI_APP_TEMPLATE T ON A.TEMPLATE_ID=T.ID WHERE A.PAGE_ID=? AND A.SCOPE_TYPE=? AND A.SCOPE_ID=? AND T.STATUS='PUBLISHED' ORDER BY A.PRIORITY DESC,T.VERSION_NO DESC&quot;)){</span>
<span class="nc" id="L771"> p.setString(1,pageId);p.setString(2,parts[0]);p.setString(3,parts[1]);</span>
<span class="nc bnc" id="L772" title="All 4 branches missed."> try(ResultSet rs=p.executeQuery()){if(rs.next())return new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;)));}</span>
<span class="nc bnc" id="L773" title="All 2 branches missed."> }</span>
<span class="nc" id="L774"> }</span>
<span class="nc" id="L775"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID,VERSION_NO,LAYOUT_JSON FROM &quot;+schema()+&quot;HMI_APP_TEMPLATE WHERE PAGE_ID=? AND STATUS='PUBLISHED' ORDER BY VERSION_NO DESC&quot;)){</span>
<span class="nc" id="L776"> p.setString(1,pageId);try(ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L777" title="All 2 branches missed."> return rs.next()?new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;layout&quot;,objectOrEmpty(clob(rs,&quot;LAYOUT_JSON&quot;))):null;</span>
}
}
}
private void audit(Connection c,String action,String entity,String details)throws SQLException{
<span class="nc" id="L768"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_AUDIT(ID,ACTOR,ACTION_NAME,ENTITY_ID,DETAILS,CREATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L769"> p.setString(1,id());p.setString(2,user());p.setString(3,action);p.setString(4,entity);</span>
<span class="nc bnc" id="L770" title="All 2 branches missed."> p.setString(5,details.length()&gt;1000?details.substring(0,1000):details);p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();</span>
<span class="nc" id="L782"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+schema()+&quot;HMI_APP_AUDIT(ID,ACTOR,ACTION_NAME,ENTITY_ID,DETAILS,CREATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L783"> p.setString(1,id());p.setString(2,user());p.setString(3,action);p.setString(4,entity);</span>
<span class="nc bnc" id="L784" title="All 2 branches missed."> p.setString(5,details.length()&gt;1000?details.substring(0,1000):details);p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();</span>
}
<span class="nc" id="L772"> }</span>
<span class="nc" id="L786"> }</span>
private Response dbError(Exception ex){
<span class="nc" id="L774"> java.util.logging.Logger.getLogger(HmiApiResource.class.getName()).log(java.util.logging.Level.SEVERE,&quot;HMI DB operation failed&quot;,ex);</span>
<span class="nc" id="L775"> return err(500,&quot;Database operation failed; check server logs&quot;);</span>
<span class="nc" id="L788"> java.util.logging.Logger.getLogger(HmiApiResource.class.getName()).log(java.util.logging.Level.SEVERE,&quot;HMI DB operation failed&quot;,ex);</span>
<span class="nc" id="L789"> return err(500,&quot;Database operation failed; check server logs&quot;);</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.4.201905082037</span></div></body></html>