241 lines
12 KiB
HTML
241 lines
12 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="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>GetStationsApi.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</a> > <span class="el_source">GetStationsApi.java</span></div><h1>GetStationsApi.java</h1><pre class="source lang-java linenums">package com.workbenchclassic;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import javax.ws.rs.Consumes;
|
|
import javax.ws.rs.POST;
|
|
import javax.ws.rs.Path;
|
|
import javax.ws.rs.Produces;
|
|
import javax.ws.rs.core.MediaType;
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
/**
|
|
* REST endpoint that fetches station data from DB and returns jsTree JSON.
|
|
*/
|
|
|
|
@Path("/getStations")
|
|
<span class="nc" id="L30">public class GetStationsApi {</span>
|
|
|
|
<span class="nc" id="L32"> private static final DBService DB_SERVICE = new DBService("dsMesMiiNJTA");</span>
|
|
<span class="nc" id="L33"> private static final ObjectMapper MAPPER = new ObjectMapper();</span>
|
|
|
|
/**
|
|
* Fetches station data via dbConnectAndGetRows, builds a hierarchical
|
|
* structure, and returns as jsTree-friendly JSON.
|
|
*
|
|
* @return JAX-RS Response with JSON array
|
|
*/
|
|
@POST
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
public Response getStations(String jsonBody) {
|
|
<span class="nc" id="L45"> JSONObject json = new JSONObject(jsonBody);</span>
|
|
<span class="nc" id="L46"> String station = json.getString("station");</span>
|
|
|
|
<span class="nc" id="L48"> String query = "SELECT mk.kap_nr as kap_nr, kap_bez as kap_bez, "</span>
|
|
+ "mg.ma_grp_nr as ma_grp_nr, mg.ma_grp_bez as ma_grp_bez, "
|
|
+ "li.line_nr as line_nr, li.line_bez as line_bez "
|
|
+ "FROM bde.kast mk "
|
|
+ "JOIN bde.ma_grp mg ON (mg.ma_grp_id = mk.ma_grp_id) "
|
|
+ "JOIN bde.line_ma_grp lm ON (lm.ma_grp_id = mk.ma_grp_id) "
|
|
+ "JOIN bde.line li ON (li.line_id = lm.line_id) "
|
|
+ "JOIN bde.werk we ON (li.werk_id = we.werk_id) "
|
|
+ "WHERE Line_typ ='T' "
|
|
+ "and mk.werk_id IN (SELECT werk_id FROM bde.kast WHERE kap_nr = ? )";
|
|
|
|
List<Map<String, Object>> rows;
|
|
<span class="nc" id="L60"> try (Connection conn = DB_SERVICE.getConnection();</span>
|
|
<span class="nc" id="L61"> PreparedStatement ps = conn.prepareStatement(query)) {</span>
|
|
<span class="nc" id="L62"> ps.setString(1, station);</span>
|
|
|
|
<span class="nc" id="L64"> rows = DB_SERVICE.preparedDbConnectAndGetRows(ps);</span>
|
|
<span class="nc" id="L65"> } catch (SQLException e) {</span>
|
|
<span class="nc" id="L66"> String errJson = String.format("{\"error\":\"DB error: %s\"}", e.getMessage());</span>
|
|
<span class="nc" id="L67"> return Response.status(Response.Status.INTERNAL_SERVER_ERROR)</span>
|
|
<span class="nc" id="L68"> .entity(errJson)</span>
|
|
<span class="nc" id="L69"> .build();</span>
|
|
<span class="nc" id="L70"> }</span>
|
|
|
|
<span class="nc" id="L72"> List<JsTreeNode> jsTreeData = buildJsTree(rows);</span>
|
|
|
|
// Convert to JSON
|
|
String jsonResult;
|
|
try {
|
|
<span class="nc" id="L77"> jsonResult = MAPPER.writeValueAsString(jsTreeData);</span>
|
|
<span class="nc" id="L78"> } catch (JsonProcessingException e) {</span>
|
|
<span class="nc" id="L79"> return Response.status(Response.Status.INTERNAL_SERVER_ERROR)</span>
|
|
<span class="nc" id="L80"> .entity("{\"error\":\"JSON parsing failed\"}")</span>
|
|
<span class="nc" id="L81"> .build();</span>
|
|
<span class="nc" id="L82"> }</span>
|
|
|
|
<span class="nc" id="L84"> return Response.ok(jsonResult).build();</span>
|
|
}
|
|
|
|
/**
|
|
* Converts database rows into a jsTree-compatible structure.
|
|
*
|
|
* @param rows List of maps representing DB rows.
|
|
* @return List of jsTree nodes.
|
|
*/
|
|
private List<JsTreeNode> buildJsTree(List<Map<String, Object>> rows) {
|
|
<span class="nc" id="L94"> Map<String, LineNode> lineMap = new HashMap<>();</span>
|
|
|
|
<span class="nc bnc" id="L96" title="All 2 branches missed."> for (Map<String, Object> row : rows) {</span>
|
|
<span class="nc" id="L97"> String lineNr = String.valueOf(row.get("line_nr"));</span>
|
|
<span class="nc" id="L98"> String lineBez = String.valueOf(row.get("line_bez"));</span>
|
|
<span class="nc" id="L99"> String maGrpNr = String.valueOf(row.get("ma_grp_nr"));</span>
|
|
<span class="nc" id="L100"> String maGrpBez = String.valueOf(row.get("ma_grp_bez"));</span>
|
|
<span class="nc" id="L101"> String kapNr = String.valueOf(row.get("kap_nr"));</span>
|
|
<span class="nc" id="L102"> String kapBez = String.valueOf(row.get("kap_bez"));</span>
|
|
|
|
// 1) Get or create line node
|
|
<span class="nc" id="L105"> LineNode lineNode = lineMap.computeIfAbsent(lineNr,</span>
|
|
<span class="nc" id="L106"> ln -> new LineNode("" + lineNr, lineBez));</span>
|
|
|
|
// 2) Get or create ma group node
|
|
<span class="nc" id="L109"> MaGrpNode maNode = lineNode.getMaGroups().computeIfAbsent(maGrpNr,</span>
|
|
<span class="nc" id="L110"> mg -> new MaGrpNode("" + maGrpNr, maGrpBez));</span>
|
|
|
|
// 3) Add kap child
|
|
<span class="nc" id="L113"> KapNode kapNode = new KapNode("" + kapNr, kapBez);</span>
|
|
<span class="nc" id="L114"> maNode.getKaps().add(kapNode);</span>
|
|
<span class="nc" id="L115"> }</span>
|
|
|
|
<span class="nc" id="L117"> return transformToJsTree(lineMap);</span>
|
|
}
|
|
|
|
/**
|
|
* Transforms the hierarchical structure into jsTree format.
|
|
*
|
|
* @param lineMap lines mapped by their lineNr
|
|
* @return a list of top-level jsTree nodes
|
|
*/
|
|
private List<JsTreeNode> transformToJsTree(Map<String, LineNode> lineMap) {
|
|
<span class="nc" id="L127"> List<JsTreeNode> result = new ArrayList<>();</span>
|
|
<span class="nc bnc" id="L128" title="All 2 branches missed."> for (LineNode line : lineMap.values()) {</span>
|
|
<span class="nc" id="L129"> JsTreeNode lineJs = new JsTreeNode(line.getId(), line.getText());</span>
|
|
<span class="nc" id="L130"> lineJs.setChildren(new ArrayList<>());</span>
|
|
|
|
<span class="nc bnc" id="L132" title="All 2 branches missed."> for (MaGrpNode ma : line.getMaGroups().values()) {</span>
|
|
<span class="nc" id="L133"> JsTreeNode maJs = new JsTreeNode(ma.getId(), ma.getText());</span>
|
|
<span class="nc" id="L134"> maJs.setChildren(new ArrayList<>());</span>
|
|
|
|
<span class="nc bnc" id="L136" title="All 2 branches missed."> for (KapNode kap : ma.getKaps()) {</span>
|
|
<span class="nc" id="L137"> JsTreeNode kapJs = new JsTreeNode(kap.getId(), kap.getText());</span>
|
|
<span class="nc" id="L138"> kapJs.setChildren(Collections.emptyList());</span>
|
|
<span class="nc" id="L139"> maJs.getChildren().add(kapJs);</span>
|
|
<span class="nc" id="L140"> }</span>
|
|
<span class="nc" id="L141"> lineJs.getChildren().add(maJs);</span>
|
|
<span class="nc" id="L142"> }</span>
|
|
<span class="nc" id="L143"> result.add(lineJs);</span>
|
|
<span class="nc" id="L144"> }</span>
|
|
<span class="nc" id="L145"> return result;</span>
|
|
}
|
|
|
|
// -------------------- Model Classes --------------------
|
|
|
|
private static class LineNode {
|
|
private final String id;
|
|
private final String text;
|
|
<span class="nc" id="L153"> private final Map<String, MaGrpNode> maGroups = new LinkedHashMap<>();</span>
|
|
|
|
<span class="nc" id="L155"> LineNode(String id, String text) {</span>
|
|
<span class="nc" id="L156"> this.id = id;</span>
|
|
<span class="nc" id="L157"> this.text = text;</span>
|
|
<span class="nc" id="L158"> }</span>
|
|
|
|
public String getId() {
|
|
<span class="nc" id="L161"> return id;</span>
|
|
}
|
|
|
|
public String getText() {
|
|
<span class="nc" id="L165"> return text;</span>
|
|
}
|
|
|
|
public Map<String, MaGrpNode> getMaGroups() {
|
|
<span class="nc" id="L169"> return maGroups;</span>
|
|
}
|
|
}
|
|
|
|
private static class MaGrpNode {
|
|
private final String id;
|
|
private final String text;
|
|
<span class="nc" id="L176"> private final List<KapNode> kaps = new ArrayList<>();</span>
|
|
|
|
<span class="nc" id="L178"> MaGrpNode(String id, String text) {</span>
|
|
<span class="nc" id="L179"> this.id = id;</span>
|
|
<span class="nc" id="L180"> this.text = text;</span>
|
|
<span class="nc" id="L181"> }</span>
|
|
|
|
public String getId() {
|
|
<span class="nc" id="L184"> return id;</span>
|
|
}
|
|
|
|
public String getText() {
|
|
<span class="nc" id="L188"> return text;</span>
|
|
}
|
|
|
|
public List<KapNode> getKaps() {
|
|
<span class="nc" id="L192"> return kaps;</span>
|
|
}
|
|
}
|
|
|
|
private static class KapNode {
|
|
private final String id;
|
|
private final String text;
|
|
|
|
<span class="nc" id="L200"> KapNode(String id, String text) {</span>
|
|
<span class="nc" id="L201"> this.id = id;</span>
|
|
<span class="nc" id="L202"> this.text = text;</span>
|
|
<span class="nc" id="L203"> }</span>
|
|
|
|
public String getId() {
|
|
<span class="nc" id="L206"> return id;</span>
|
|
}
|
|
|
|
public String getText() {
|
|
<span class="nc" id="L210"> return text;</span>
|
|
}
|
|
}
|
|
|
|
private static class JsTreeNode {
|
|
private final String id;
|
|
private final String text;
|
|
private List<JsTreeNode> children;
|
|
|
|
<span class="nc" id="L219"> JsTreeNode(String id, String text) {</span>
|
|
<span class="nc" id="L220"> this.id = id;</span>
|
|
<span class="nc" id="L221"> this.text = text;</span>
|
|
<span class="nc" id="L222"> }</span>
|
|
|
|
public String getId() {
|
|
<span class="nc" id="L225"> return id;</span>
|
|
}
|
|
|
|
public String getText() {
|
|
<span class="nc" id="L229"> return text;</span>
|
|
}
|
|
|
|
public List<JsTreeNode> getChildren() {
|
|
<span class="nc" id="L233"> return children;</span>
|
|
}
|
|
|
|
public void setChildren(List<JsTreeNode> children) {
|
|
<span class="nc" id="L237"> this.children = children;</span>
|
|
<span class="nc" id="L238"> }</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> |