200 lines
14 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>SetStationConfig.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">backend</a> &gt; <a href="index.source.html" class="el_package">com.workbenchclassic</a> &gt; <span class="el_source">SetStationConfig.java</span></div><h1>SetStationConfig.java</h1><pre class="source lang-java linenums">package com.workbenchclassic;
import org.json.JSONArray;
import org.json.JSONObject;
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 java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Path(&quot;/setStationConfig&quot;)
<span class="nc" id="L22">public class SetStationConfig {</span>
// Nutzen Sie eine einheitliche Instanz von DBService
<span class="nc" id="L25"> private final DBService dbService = new DBService(&quot;dsMesMiiNJTA&quot;);</span>
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response doUpdate(String jsonBody) {
<span class="nc" id="L31"> Connection conn = null;</span>
try {
// 1) JSON einlesen
<span class="nc" id="L34"> JSONObject json = new JSONObject(jsonBody);</span>
<span class="nc" id="L35"> String station = json.getString(&quot;station&quot;);</span>
<span class="nc" id="L36"> String persIdStr = json.getString(&quot;persID&quot;); // z. B. &quot;1234&quot;</span>
<span class="nc" id="L37"> int persID = Integer.parseInt(persIdStr);</span>
<span class="nc" id="L38"> JSONArray dataArray = json.getJSONArray(&quot;data&quot;);</span>
// 2) Connection holen
<span class="nc" id="L41"> conn = dbService.getConnection();</span>
<span class="nc" id="L42"> conn.setAutoCommit(false); // Transaktion starten</span>
// 3) DB-Daten ermitteln (nur Code-NR + Code-Wert)
<span class="nc" id="L45"> String query = &quot;SELECT code_nr, code_wert, stamp &quot;</span>
+ &quot; FROM bde.txt_konfig &quot;
+ &quot; WHERE code IN (SELECT kap_id FROM bde.kast WHERE KAP_NR = ? )&quot;;
// In diesem Beispiel nutzen wir direkt PreparedStatement + das gleiche conn
<span class="nc" id="L50"> Map&lt;Integer, String&gt; dbCodeMap = new HashMap&lt;&gt;();</span>
<span class="nc" id="L51"> Set&lt;Integer&gt; dbCodeNrSet = new HashSet&lt;&gt;();</span>
<span class="nc" id="L53"> try (PreparedStatement ps = conn.prepareStatement(query)) {</span>
<span class="nc" id="L54"> ps.setString(1, station);</span>
<span class="nc" id="L55"> try (ResultSet rs = ps.executeQuery()) {</span>
<span class="nc bnc" id="L56" title="All 2 branches missed."> while (rs.next()) {</span>
<span class="nc" id="L57"> int codeNr = rs.getInt(&quot;code_nr&quot;);</span>
<span class="nc" id="L58"> String codeWert = rs.getString(&quot;code_wert&quot;);</span>
<span class="nc" id="L59"> dbCodeMap.put(codeNr, codeWert);</span>
<span class="nc" id="L60"> dbCodeNrSet.add(codeNr);</span>
<span class="nc" id="L61"> }</span>
}
}
// 4) SQL-Statements für Update, Insert und Delete vorbereiten
<span class="nc" id="L66"> String updateSql = &quot;UPDATE bde.txt_konfig &quot;</span>
+ &quot; SET code_wert = ?, user_id = ? &quot;
+ &quot; WHERE code_nr = ? &quot;
+ &quot; AND code IN (SELECT DISTINCT kap_id FROM bde.kast WHERE KAP_NR = ? AND KAP_TYP = 'T')&quot;;
<span class="nc" id="L71"> String insertSql = &quot;INSERT INTO bde.txt_konfig (code, code_nr, code_bez, code_wert, created, stamp, user_ID) &quot;</span>
+ &quot;VALUES ( (SELECT kap_id FROM bde.kast WHERE KAP_NR = ? AND KAP_TYP = 'T'), ?, ?, ?, ?, ?, ? )&quot;;
<span class="nc" id="L74"> String deleteSql = &quot;DELETE FROM bde.txt_konfig &quot;</span>
+ &quot; WHERE code_nr = ? &quot;
+ &quot; AND code IN (SELECT kap_id FROM bde.kast WHERE KAP_NR = ? and KAP_TYP = 'T')&quot;;
// Für das Nachhalten aller &quot;code_nr&quot; aus dem JSON
<span class="nc" id="L79"> Set&lt;Integer&gt; jsonCodeNrSet = new HashSet&lt;&gt;();</span>
<span class="nc" id="L80"> LocalDateTime jetzt = LocalDateTime.now();</span>
<span class="nc" id="L81"> Timestamp timestamp = Timestamp.valueOf(jetzt);</span>
<span class="nc" id="L82"> DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd HH:mm:ss&quot;);</span>
<span class="nc" id="L84"> try (PreparedStatement pstmtUpdate = conn.prepareStatement(updateSql);</span>
<span class="nc" id="L85"> PreparedStatement pstmtInsert = conn.prepareStatement(insertSql);</span>
<span class="nc" id="L86"> PreparedStatement pstmtDelete = conn.prepareStatement(deleteSql)) {</span>
// 4a) Über JSON-Array iterieren und ggf. Updaten/Einfügen
<span class="nc bnc" id="L89" title="All 2 branches missed."> for (int i = 0; i &lt; dataArray.length(); i++) {</span>
<span class="nc" id="L90"> JSONObject obj = dataArray.getJSONObject(i);</span>
// Beispiel: JSON hat &quot;code_nr&quot; und &quot;code_wert&quot;
<span class="nc bnc" id="L93" title="All 4 branches missed."> if (obj.has(&quot;code_nr&quot;) &amp;&amp; !obj.isNull(&quot;code_nr&quot;)</span>
<span class="nc bnc" id="L94" title="All 4 branches missed."> &amp;&amp; obj.has(&quot;code_wert&quot;) &amp;&amp; !obj.isNull(&quot;code_wert&quot;)) {</span>
<span class="nc" id="L95"> Integer jsonCodeNr = obj.getInt(&quot;code_nr&quot;);</span>
<span class="nc" id="L96"> String jsonCodeWert = obj.getString(&quot;code_wert&quot;).trim();</span>
<span class="nc" id="L97"> String jsonCodeBez = obj.getString(&quot;description&quot;).trim();</span>
<span class="nc" id="L98"> String jsoncreated = obj.getString(&quot;timestamp&quot;).trim();</span>
<span class="nc" id="L99"> jsonCodeNrSet.add(jsonCodeNr);</span>
// Fall A: code_nr ist in DB -&gt; Update (nur wenn sich der Wert ändert)
<span class="nc bnc" id="L102" title="All 2 branches missed."> if (dbCodeMap.containsKey(jsonCodeNr)) {</span>
<span class="nc" id="L103"> String dbCodeWert = dbCodeMap.get(jsonCodeNr);</span>
<span class="nc bnc" id="L104" title="All 2 branches missed."> if (!dbCodeWert.equals(jsonCodeWert)) {</span>
<span class="nc" id="L105"> pstmtUpdate.setString(1, jsonCodeWert);</span>
<span class="nc" id="L106"> pstmtUpdate.setInt(2, persID); // Set the user_id parameter</span>
<span class="nc" id="L107"> pstmtUpdate.setInt(3, jsonCodeNr);</span>
<span class="nc" id="L108"> pstmtUpdate.setString(4, station);</span>
<span class="nc" id="L109"> pstmtUpdate.executeUpdate();</span>
}
<span class="nc" id="L111"> }</span>
// Fall B: code_nr noch nicht in DB -&gt; Insert
else {
<span class="nc" id="L114"> LocalDateTime localDateTime = LocalDateTime.parse(jsoncreated, formatter);</span>
<span class="nc" id="L115"> Timestamp timestampcreated = Timestamp.valueOf(localDateTime);</span>
<span class="nc" id="L116"> pstmtInsert.setString(1, station);</span>
<span class="nc" id="L117"> pstmtInsert.setInt(2, jsonCodeNr);</span>
<span class="nc" id="L118"> pstmtInsert.setString(3, jsonCodeBez);</span>
<span class="nc" id="L119"> pstmtInsert.setString(4, jsonCodeWert);</span>
<span class="nc" id="L120"> pstmtInsert.setTimestamp(5, timestampcreated);</span>
<span class="nc" id="L121"> pstmtInsert.setTimestamp(6, timestamp);</span>
<span class="nc" id="L122"> pstmtInsert.setInt(7, persID);</span>
<span class="nc" id="L123"> pstmtInsert.executeUpdate();</span>
}
}
}
// 4b) Löschen der Einträge, die nicht mehr im JSON vorhanden sind
<span class="nc bnc" id="L129" title="All 2 branches missed."> for (Integer dbCodeNr : dbCodeNrSet) {</span>
<span class="nc bnc" id="L130" title="All 2 branches missed."> if (!jsonCodeNrSet.contains(dbCodeNr)) {</span>
<span class="nc" id="L131"> pstmtDelete.setInt(1, dbCodeNr);</span>
<span class="nc" id="L132"> pstmtDelete.setString(2, station);</span>
<span class="nc" id="L133"> pstmtDelete.executeUpdate();</span>
}
<span class="nc" id="L135"> }</span>
}
// 5) Alles erfolgreich -&gt; commit
<span class="nc" id="L139"> conn.commit();</span>
// Beispiel: Werk-Daten könnten Sie ggf. noch laden
<span class="nc" id="L142"> JSONObject werkData = getWerkData(conn, station);</span>
// 6) Erfolgs-Response
<span class="nc" id="L145"> JSONObject response = new JSONObject();</span>
<span class="nc" id="L146"> response.put(&quot;status&quot;, &quot;ok&quot;);</span>
<span class="nc" id="L147"> response.put(&quot;message&quot;, &quot;Daten wurden erfolgreich verarbeitet.&quot;);</span>
<span class="nc" id="L148"> response.put(&quot;werk_data&quot;, werkData); // falls Sie die Werkdaten zurückgeben möchten</span>
<span class="nc" id="L149"> return Response.ok(response.toString()).build();</span>
<span class="nc" id="L151"> } catch (Exception e) {</span>
// Fehlerfall -&gt; Rollback, JSON-Fehlerantwort
<span class="nc bnc" id="L153" title="All 2 branches missed."> if (conn != null) {</span>
try {
<span class="nc" id="L155"> conn.rollback();</span>
<span class="nc" id="L156"> } catch (SQLException ex) {</span>
<span class="nc" id="L157"> ex.printStackTrace();</span>
<span class="nc" id="L158"> }</span>
}
<span class="nc" id="L160"> e.printStackTrace();</span>
<span class="nc" id="L161"> JSONObject error = new JSONObject();</span>
<span class="nc" id="L162"> error.put(&quot;status&quot;, &quot;error&quot;);</span>
<span class="nc" id="L163"> error.put(&quot;message&quot;, e.getMessage());</span>
<span class="nc" id="L164"> return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error.toString()).build();</span>
} finally {
// Verbindung sauber schließen
<span class="nc bnc" id="L167" title="All 2 branches missed."> if (conn != null) {</span>
try {
<span class="nc" id="L169"> conn.setAutoCommit(true);</span>
<span class="nc" id="L170"> conn.close();</span>
<span class="nc" id="L171"> } catch (SQLException e) {</span>
<span class="nc" id="L172"> e.printStackTrace();</span>
<span class="nc" id="L173"> }</span>
}
}
}
/**
* Beispielmethode: Lädt aus bde.werk die Werkdaten für eine Station.
*/
private JSONObject getWerkData(Connection conn, String station) throws SQLException {
<span class="nc" id="L182"> String query = &quot;SELECT ext_werk_nr, werk_nr &quot;</span>
+ &quot; FROM bde.werk &quot;
+ &quot; WHERE werk_id IN (SELECT werk_id FROM bde.kast WHERE kap_nr = ?)&quot;;
<span class="nc" id="L186"> try (PreparedStatement ps = conn.prepareStatement(query)) {</span>
<span class="nc" id="L187"> ps.setString(1, station);</span>
<span class="nc" id="L188"> try (ResultSet rs = ps.executeQuery()) {</span>
<span class="nc bnc" id="L189" title="All 2 branches missed."> if (rs.next()) {</span>
<span class="nc" id="L190"> JSONObject result = new JSONObject();</span>
<span class="nc" id="L191"> result.put(&quot;ext_werk_nr&quot;, rs.getString(&quot;ext_werk_nr&quot;));</span>
<span class="nc" id="L192"> result.put(&quot;werk_nr&quot;, rs.getString(&quot;werk_nr&quot;));</span>
<span class="nc" id="L193"> return result;</span>
}
<span class="nc bnc" id="L195" title="All 2 branches missed."> }</span>
<span class="nc bnc" id="L196" title="All 2 branches missed."> }</span>
<span class="nc" id="L197"> return new JSONObject();</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>