183 lines
15 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>UpdateStation.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">UpdateStation.java</span></div><h1>UpdateStation.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;
/**
* UpdateStation performs database insert depending on DB vendor (MSSQL or
* Oracle).
*/
@Path(&quot;/updateStations&quot;)
<span class="nc" id="L18">public class UpdateStation {</span>
/** Our DB service for queries (JNDI, etc.). */
<span class="nc" id="L21"> private final DBService dbService = new DBService(&quot;dsTranNJTA&quot;);</span>
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response doUpdate(String jsonBody) {
try {
// 1) JSON parsen
<span class="nc" id="L29"> JSONObject json = new JSONObject(jsonBody);</span>
<span class="nc" id="L30"> String station = json.getString(&quot;station&quot;); // z.B. &quot;TEST&quot;</span>
<span class="nc" id="L31"> JSONArray dataArray = json.getJSONArray(&quot;data&quot;); // Liste mit Objekten</span>
// 2) DB-Vendor abfragen
<span class="nc" id="L34"> String vendor = dbService.getDatabaseProductName();</span>
<span class="nc" id="L35"> System.out.println(&quot;Aktueller DB-Vendor: &quot; + vendor);</span>
<span class="nc" id="L36"> GetMetadata metadata = new GetMetadata();</span>
<span class="nc" id="L37"> String extPlantResponseString = metadata.getExtPlantJson(station);</span>
<span class="nc" id="L38"> JSONObject extPlantResponse = new JSONObject(extPlantResponseString);</span>
<span class="nc" id="L40"> String errorCode = extPlantResponse.optString(&quot;errorCode&quot;, &quot;0&quot;);</span>
<span class="nc bnc" id="L41" title="All 2 branches missed."> if (!&quot;0&quot;.equals(errorCode)) {</span>
// Return an HTTP 400 or 500 with that JSON
<span class="nc" id="L43"> return Response.status(Response.Status.BAD_REQUEST).entity(extPlantResponse.toString()).build();</span>
}
// 3) werknummer ermitteln
// werkResultJson könnte z.B. so aussehen:
// {&quot;errorCode&quot;:&quot;0&quot;,&quot;errorMessage&quot;:&quot;Done&quot;,&quot;data&quot;:&quot;[{\&quot;werknummer\&quot;:\&quot;ABC\&quot;}]&quot;}
<span class="nc" id="L50"> String werkData = extPlantResponse.optString(&quot;data&quot;, &quot;[]&quot;);</span>
<span class="nc" id="L51"> JSONArray werkArr = new JSONArray(werkData);</span>
<span class="nc" id="L53"> String WERK_NR = null;</span>
<span class="nc" id="L54"> String COMPANY_NO = null;</span>
<span class="nc" id="L55"> String CLIENT_NO = null;</span>
<span class="nc bnc" id="L56" title="All 2 branches missed."> if (werkArr.length() &gt; 0) {</span>
<span class="nc" id="L57"> JSONObject row = werkArr.getJSONObject(0);</span>
<span class="nc" id="L58"> WERK_NR = row.optString(&quot;ext_company_nr&quot;, null);</span>
<span class="nc" id="L59"> COMPANY_NO = row.optString(&quot;werk_nr&quot;, null);</span>
<span class="nc" id="L60"> CLIENT_NO = row.optString(&quot;client_nr&quot;, null);</span>
}
<span class="nc" id="L63"> boolean isMSSQL = &quot;Microsoft SQL Server&quot;.equals(vendor);</span>
<span class="nc" id="L64"> boolean isOracle = &quot;Oracle&quot;.equals(vendor);</span>
// 4) ZUERST den tran_idocstatus-Eintrag anlegen
<span class="nc bnc" id="L67" title="All 2 branches missed."> if (isMSSQL) {</span>
<span class="nc" id="L68"> String insertIdocstatusMSSQL = &quot;INSERT INTO xtran.tran_idocstatus &quot;</span>
+ &quot;(ID, DATE_CREATION, ewstatus, errorcode, content_type, source) &quot;
+ &quot;VALUES ( (NEXT VALUE FOR xtran.seq_tranidocstatus), getdate(), 1, 0, 60, 0)&quot;;
<span class="nc" id="L71"> dbService.dbConnect(insertIdocstatusMSSQL);</span>
<span class="nc bnc" id="L73" title="All 2 branches missed."> } else if (isOracle) {</span>
<span class="nc" id="L74"> String insertIdocstatusOracle = &quot;INSERT INTO tran.tran_idocstatus &quot;</span>
+ &quot;(ID, DATE_CREATION, ewstatus, errorcode, content_type, source) &quot;
+ &quot;VALUES ( TRAN.SEQ_TRANIDOCSTATUS.nextval, sysdate, 1, 0, 60, 0)&quot;;
<span class="nc" id="L77"> dbService.dbConnect(insertIdocstatusOracle);</span>
<span class="nc" id="L79"> } else {</span>
<span class="nc" id="L80"> System.out.println(&quot;DB-Vendor nicht implementiert: &quot; + vendor);</span>
}
// 5) Für JEDES Element in &quot;data&quot; =&gt; Insert in cell, machine_group, station
<span class="nc bnc" id="L84" title="All 2 branches missed."> for (int i = 0; i &lt; dataArray.length(); i++) {</span>
<span class="nc" id="L85"> JSONObject obj = dataArray.getJSONObject(i);</span>
<span class="nc" id="L87"> String lineID = obj.getString(&quot;line_nr&quot;);</span>
<span class="nc" id="L88"> String lineBez = obj.getString(&quot;line_bez&quot;);</span>
<span class="nc" id="L89"> String maGroupID = obj.getString(&quot;ma_grp_nr&quot;);</span>
<span class="nc" id="L90"> String maGroupBez = obj.getString(&quot;ma_grp_bez&quot;);</span>
<span class="nc" id="L91"> String kapID = obj.getString(&quot;kap_nr&quot;);</span>
<span class="nc" id="L92"> String kapBez = obj.getString(&quot;kap_bez&quot;);</span>
<span class="nc bnc" id="L94" title="All 2 branches missed."> if (isMSSQL) {</span>
// tran_cell
String insertCellMSSQL = &quot;INSERT INTO xtran.tran_cell &quot;
+ &quot;(IDOC_ID, TRAN_ID, SOURCE, STATUS, CREATED, STAMP, CLIENT_NO, COMPANY_NO, PLANT_NO, CELL_NUMBER, CELL_DESC) &quot;
+ &quot;VALUES ( (SELECT MAX(id) FROM xtran.tran_idocstatus),&quot;
+ &quot; (NEXT VALUE FOR xtran.SEQ_TRAN_CELL),&quot;
+ &quot; 0,0, getdate(), getdate(),&quot;
<span class="nc bnc" id="L101" title="All 4 branches missed."> + &quot; '&quot; + (CLIENT_NO != null ? CLIENT_NO : &quot;&quot;) + &quot;','&quot; + (WERK_NR != null ? WERK_NR : &quot;&quot;)</span>
<span class="nc bnc" id="L102" title="All 2 branches missed."> + &quot;', '&quot; + (COMPANY_NO != null ? COMPANY_NO : &quot;&quot;) + &quot;',&quot;</span>
+ &quot; '&quot; + lineID + &quot;', '&quot; + lineBez + &quot;')&quot;;
<span class="nc" id="L104"> dbService.dbConnect(insertCellMSSQL);</span>
// tran_machine_group
String insertMachineGroupMSSQL = &quot;INSERT INTO xtran.tran_machine_group &quot;
+ &quot;(IDOC_ID, TRAN_ID, SOURCE, STATUS, CREATED, STAMP, CLIENT_NO, COMPANY_NO, PLANT_NO, &quot;
+ &quot; PARENT_CELL_NUMBER, MACHINE_GROUP_NUMBER, MACHINE_GROUP_DESC) &quot;
+ &quot;VALUES ( (SELECT MAX(id) FROM xtran.tran_idocstatus),&quot;
+ &quot; (NEXT VALUE FOR xtran.SEQ_TRAN_MACHINE_GROUP),&quot;
+ &quot; 0,0, getdate(), getdate(),&quot;
<span class="nc bnc" id="L113" title="All 4 branches missed."> + &quot; '&quot; + (CLIENT_NO != null ? CLIENT_NO : &quot;&quot;) + &quot;','&quot; + (WERK_NR != null ? WERK_NR : &quot;&quot;)</span>
<span class="nc bnc" id="L114" title="All 2 branches missed."> + &quot;', '&quot; + (COMPANY_NO != null ? COMPANY_NO : &quot;&quot;) + &quot;',&quot;</span>
+ &quot; '&quot; + lineID + &quot;', '&quot; + maGroupID + &quot;', '&quot; + maGroupBez + &quot;')&quot;;
<span class="nc" id="L116"> dbService.dbConnect(insertMachineGroupMSSQL);</span>
// tran_station
String insertStationMSSQL = &quot;INSERT INTO xtran.tran_station &quot;
+ &quot;(IDOC_ID, TRAN_ID, SOURCE, STATUS, CREATED, STAMP, CLIENT_NO, COMPANY_NO, PLANT_NO, &quot;
+ &quot; PARENT_MACHINE_GROUP_NUMBER, STATION_NUMBER, STATION_DESC, STATION_TYPE) &quot;
+ &quot;VALUES ( (SELECT MAX(id) FROM xtran.tran_idocstatus),&quot;
+ &quot; (NEXT VALUE FOR xtran.SEQ_TRAN_STATION),&quot;
+ &quot; 0,0, getdate(), getdate(),&quot;
<span class="nc bnc" id="L125" title="All 4 branches missed."> + &quot; '&quot; + (CLIENT_NO != null ? CLIENT_NO : &quot;&quot;) + &quot;','&quot; + (WERK_NR != null ? WERK_NR : &quot;&quot;)</span>
<span class="nc bnc" id="L126" title="All 2 branches missed."> + &quot;', '&quot; + (COMPANY_NO != null ? COMPANY_NO : &quot;&quot;) + &quot;',&quot;</span>
+ &quot; '&quot; + maGroupID + &quot;', '&quot; + kapID + &quot;', '&quot; + kapBez + &quot;', 'B')&quot;;
<span class="nc" id="L128"> dbService.dbConnect(insertStationMSSQL);</span>
<span class="nc bnc" id="L130" title="All 2 branches missed."> } else if (isOracle) {</span>
// tran_cell
String insertCellOracle = &quot;INSERT INTO tran.tran_cell &quot;
+ &quot;(IDOC_ID, TRAN_ID, SOURCE, STATUS, CREATED, STAMP, CLIENT_NO, COMPANY_NO, PLANT_NO, CELL_NUMBER, CELL_DESC) &quot;
+ &quot;VALUES ( (SELECT MAX(id) FROM tran.tran_idocstatus), &quot;
+ &quot; tran.seq_tran_cell.nextval, 0,0, sysdate, sysdate, &quot;
<span class="nc bnc" id="L136" title="All 4 branches missed."> + &quot; '&quot; + (CLIENT_NO != null ? CLIENT_NO : &quot;&quot;) + &quot;','&quot; + (WERK_NR != null ? WERK_NR : &quot;&quot;)</span>
<span class="nc bnc" id="L137" title="All 2 branches missed."> + &quot;', '&quot; + (COMPANY_NO != null ? COMPANY_NO : &quot;&quot;) + &quot;',&quot;</span>
+ &quot; '&quot; + lineID + &quot;', '&quot; + lineBez + &quot;')&quot;;
<span class="nc" id="L139"> dbService.dbConnect(insertCellOracle);</span>
// tran_machine_group
String insertMachineGroupOracle = &quot;INSERT INTO tran.tran_machine_group &quot;
+ &quot;(IDOC_ID, TRAN_ID, SOURCE, STATUS, CREATED, STAMP, CLIENT_NO, COMPANY_NO, PLANT_NO, &quot;
+ &quot; PARENT_CELL_NUMBER, MACHINE_GROUP_NUMBER, MACHINE_GROUP_DESC) &quot;
+ &quot;VALUES ( (SELECT MAX(id) FROM tran.tran_idocstatus), &quot;
+ &quot; tran.seq_tran_machine_group.nextval, 0,0, sysdate, sysdate, &quot;
<span class="nc bnc" id="L147" title="All 4 branches missed."> + &quot; '&quot; + (CLIENT_NO != null ? CLIENT_NO : &quot;&quot;) + &quot;','&quot; + (WERK_NR != null ? WERK_NR : &quot;&quot;)</span>
<span class="nc bnc" id="L148" title="All 2 branches missed."> + &quot;', '&quot; + (COMPANY_NO != null ? COMPANY_NO : &quot;&quot;) + &quot;',&quot;</span>
+ &quot; '&quot; + lineID + &quot;', '&quot; + maGroupID + &quot;', '&quot; + maGroupBez + &quot;')&quot;;
<span class="nc" id="L150"> dbService.dbConnect(insertMachineGroupOracle);</span>
// tran_station
String insertStationOracle = &quot;INSERT INTO tran.tran_station &quot;
+ &quot;(IDOC_ID, TRAN_ID, SOURCE, STATUS, CREATED, STAMP, CLIENT_NO, COMPANY_NO, PLANT_NO, &quot;
+ &quot; PARENT_MACHINE_GROUP_NUMBER, STATION_NUMBER, STATION_DESC, STATION_TYPE) &quot;
+ &quot;VALUES ( (SELECT MAX(id) FROM tran.tran_idocstatus), &quot;
+ &quot; tran.seq_tran_station.nextval, 0,0, sysdate, sysdate, &quot;
<span class="nc bnc" id="L158" title="All 4 branches missed."> + &quot; '&quot; + (CLIENT_NO != null ? CLIENT_NO : &quot;&quot;) + &quot;','&quot; + (WERK_NR != null ? WERK_NR : &quot;&quot;)</span>
<span class="nc bnc" id="L159" title="All 2 branches missed."> + &quot;', '&quot; + (COMPANY_NO != null ? COMPANY_NO : &quot;&quot;) + &quot;',&quot;</span>
+ &quot; '&quot; + maGroupID + &quot;', '&quot; + kapID + &quot;', '&quot; + kapBez + &quot;', 'B')&quot;;
<span class="nc" id="L161"> dbService.dbConnect(insertStationOracle);</span>
<span class="nc" id="L163"> } else {</span>
<span class="nc" id="L164"> System.out.println(&quot;Anderer/nicht implementierter DB-Vendor: &quot; + vendor);</span>
}
}
// 6) Erfolgreich fertig
<span class="nc" id="L169"> JSONObject response = new JSONObject();</span>
<span class="nc" id="L170"> response.put(&quot;status&quot;, &quot;ok&quot;);</span>
<span class="nc" id="L171"> response.put(&quot;message&quot;, &quot;Daten wurden erfolgreich verarbeitet.&quot;);</span>
<span class="nc" id="L172"> return Response.ok(response.toString()).build();</span>
<span class="nc" id="L174"> } catch (Exception e) {</span>
<span class="nc" id="L175"> e.printStackTrace();</span>
<span class="nc" id="L176"> JSONObject error = new JSONObject();</span>
<span class="nc" id="L177"> error.put(&quot;status&quot;, &quot;error&quot;);</span>
<span class="nc" id="L178"> error.put(&quot;message&quot;, e.getMessage());</span>
<span class="nc" id="L179"> return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error.toString()).build();</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>