155 lines
11 KiB
HTML
155 lines
11 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>GetMetadata.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">GetMetadata.java</span></div><h1>GetMetadata.java</h1><pre class="source lang-java linenums">package com.workbenchclassic;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
/**
|
|
* Example refactoring: return a JSON string instead of JAX-RS Response.
|
|
*/
|
|
<span class="nc" id="L16">public class GetMetadata {</span>
|
|
|
|
<span class="nc" id="L18"> private static final DBService DB_SERVICE = new DBService("dsMesMiiNJTA");</span>
|
|
<span class="nc" id="L19"> private static final Logger LOGGER = Logger.getLogger(GetMetadata.class.getName());</span>
|
|
|
|
public String getExtPlantJson(String station) throws Exception {
|
|
<span class="nc" id="L22"> Connection conn = null;</span>
|
|
<span class="nc" id="L23"> PreparedStatement pstmt1 = null;</span>
|
|
<span class="nc" id="L24"> ResultSet rs1 = null;</span>
|
|
<span class="nc" id="L25"> PreparedStatement pstmt2 = null;</span>
|
|
<span class="nc" id="L26"> ResultSet rs2 = null;</span>
|
|
|
|
try {
|
|
<span class="nc" id="L29"> conn = DB_SERVICE.getConnection();</span>
|
|
|
|
// 1) Query #1
|
|
<span class="nc" id="L32"> String werkQuery = "select bw.ext_werk_nr , bw.werk_nr, b.ext_company_nr, b.client_id from bde.werk bw join BDE.buchkreis b on bw.company_id = b.company_id JOIN bde.kast k ON bw.werk_id = k.werk_id where k.KAP_TYP = 'T' and "</span>
|
|
+
|
|
" k.kap_nr = ?";
|
|
<span class="nc" id="L35"> pstmt1 = conn.prepareStatement(werkQuery);</span>
|
|
<span class="nc" id="L36"> pstmt1.setString(1, station);</span>
|
|
<span class="nc" id="L37"> rs1 = pstmt1.executeQuery();</span>
|
|
|
|
<span class="nc" id="L39"> JSONArray dataArray1 = new JSONArray();</span>
|
|
<span class="nc" id="L40"> String werkNr = null;</span>
|
|
<span class="nc" id="L41"> String ext_company_nr = null;</span>
|
|
<span class="nc" id="L42"> String clientID = null;</span>
|
|
|
|
<span class="nc bnc" id="L44" title="All 2 branches missed."> if (rs1.next()) {</span>
|
|
<span class="nc" id="L45"> ext_company_nr = rs1.getString("ext_company_nr");</span>
|
|
<span class="nc" id="L46"> werkNr = rs1.getString("ext_werk_nr");</span>
|
|
<span class="nc" id="L47"> clientID = rs1.getString("client_id");</span>
|
|
<span class="nc" id="L48"> JSONObject row = new JSONObject();</span>
|
|
<span class="nc" id="L49"> row.put("ext_company_nr", ext_company_nr);</span>
|
|
<span class="nc" id="L50"> row.put("ext_werk_nr", werkNr);</span>
|
|
<span class="nc" id="L51"> row.put("client_id", clientID);</span>
|
|
<span class="nc" id="L52"> dataArray1.put(row);</span>
|
|
}
|
|
|
|
<span class="nc bnc" id="L55" title="All 2 branches missed."> if (dataArray1.length() == 0) {</span>
|
|
// No data for that station
|
|
<span class="nc" id="L57"> JSONObject noDataResponse = new JSONObject();</span>
|
|
<span class="nc" id="L58"> noDataResponse.put("errorCode", "1");</span>
|
|
<span class="nc" id="L59"> noDataResponse.put("errorMessage", "No werk found for station: " + station);</span>
|
|
<span class="nc" id="L60"> noDataResponse.put("data", "[]");</span>
|
|
<span class="nc" id="L61"> return noDataResponse.toString(); // Return the JSON string</span>
|
|
}
|
|
|
|
// Assume the FIRST row is enough
|
|
<span class="nc" id="L65"> JSONObject firstRow = dataArray1.getJSONObject(0);</span>
|
|
<span class="nc" id="L66"> werkNr = firstRow.optString("ext_werk_nr", null);</span>
|
|
<span class="nc" id="L67"> ext_company_nr = firstRow.optString("ext_company_nr", null);</span>
|
|
|
|
<span class="nc bnc" id="L69" title="All 2 branches missed."> if (werkNr == null) {</span>
|
|
<span class="nc" id="L70"> JSONObject missingWerkNr = new JSONObject();</span>
|
|
<span class="nc" id="L71"> missingWerkNr.put("errorCode", "1");</span>
|
|
<span class="nc" id="L72"> missingWerkNr.put("errorMessage", "werk_nr not found for station: " + station);</span>
|
|
<span class="nc" id="L73"> missingWerkNr.put("data", "[]");</span>
|
|
<span class="nc" id="L74"> return missingWerkNr.toString();</span>
|
|
}
|
|
|
|
// 2) Query #2 for clientNr
|
|
<span class="nc" id="L78"> String clientNrQuery = "SELECT CLIENT_NR AS werknummer " +</span>
|
|
"FROM bde.mandant m " +
|
|
"WHERE m.client_id = ?";
|
|
<span class="nc" id="L81"> pstmt2 = conn.prepareStatement(clientNrQuery);</span>
|
|
<span class="nc" id="L82"> pstmt2.setString(1, clientID);</span>
|
|
<span class="nc" id="L83"> rs2 = pstmt2.executeQuery();</span>
|
|
|
|
<span class="nc" id="L85"> String clientNr = null;</span>
|
|
<span class="nc bnc" id="L86" title="All 2 branches missed."> if (rs2.next()) {</span>
|
|
<span class="nc" id="L87"> clientNr = rs2.getString("werknummer");</span>
|
|
}
|
|
|
|
// 3) Combine results into final JSON
|
|
<span class="nc" id="L91"> JSONObject combinedRow = new JSONObject();</span>
|
|
<span class="nc" id="L92"> combinedRow.put("ext_company_nr", ext_company_nr);</span>
|
|
<span class="nc" id="L93"> combinedRow.put("werk_nr", werkNr);</span>
|
|
<span class="nc bnc" id="L94" title="All 2 branches missed."> combinedRow.put("client_nr", clientNr != null ? clientNr : JSONObject.NULL);</span>
|
|
|
|
<span class="nc" id="L96"> JSONArray finalDataArray = new JSONArray();</span>
|
|
<span class="nc" id="L97"> finalDataArray.put(combinedRow);</span>
|
|
|
|
<span class="nc" id="L99"> JSONObject finalResult = new JSONObject();</span>
|
|
<span class="nc" id="L100"> finalResult.put("errorCode", "0");</span>
|
|
<span class="nc" id="L101"> finalResult.put("errorMessage", "Done");</span>
|
|
<span class="nc" id="L102"> finalResult.put("data", finalDataArray.toString());</span>
|
|
|
|
<span class="nc" id="L104"> return finalResult.toString();</span>
|
|
|
|
<span class="nc" id="L106"> } catch (SQLException e) {</span>
|
|
<span class="nc" id="L107"> LOGGER.log(Level.SEVERE, "SQL error in getExtPlant", e);</span>
|
|
<span class="nc" id="L108"> JSONObject errorJson = new JSONObject();</span>
|
|
<span class="nc" id="L109"> errorJson.put("errorCode", "99");</span>
|
|
<span class="nc" id="L110"> errorJson.put("errorMessage", "Database Error: " + e.getMessage());</span>
|
|
<span class="nc" id="L111"> errorJson.put("data", "[]");</span>
|
|
<span class="nc" id="L112"> return errorJson.toString();</span>
|
|
<span class="nc" id="L113"> } catch (Exception e) {</span>
|
|
<span class="nc" id="L114"> LOGGER.log(Level.SEVERE, "General error in getExtPlant", e);</span>
|
|
<span class="nc" id="L115"> JSONObject errorJson = new JSONObject();</span>
|
|
<span class="nc" id="L116"> errorJson.put("errorCode", "99");</span>
|
|
<span class="nc" id="L117"> errorJson.put("errorMessage", "General Error: " + e.getMessage());</span>
|
|
<span class="nc" id="L118"> errorJson.put("data", "[]");</span>
|
|
<span class="nc" id="L119"> return errorJson.toString();</span>
|
|
} finally {
|
|
// close resources
|
|
<span class="nc bnc" id="L122" title="All 2 branches missed."> if (rs1 != null)</span>
|
|
try {
|
|
<span class="nc" id="L124"> rs1.close();</span>
|
|
<span class="nc" id="L125"> } catch (SQLException e) {</span>
|
|
<span class="nc" id="L126"> e.printStackTrace();</span>
|
|
<span class="nc" id="L127"> }</span>
|
|
<span class="nc bnc" id="L128" title="All 2 branches missed."> if (pstmt1 != null)</span>
|
|
try {
|
|
<span class="nc" id="L130"> pstmt1.close();</span>
|
|
<span class="nc" id="L131"> } catch (SQLException e) {</span>
|
|
<span class="nc" id="L132"> e.printStackTrace();</span>
|
|
<span class="nc" id="L133"> }</span>
|
|
<span class="nc bnc" id="L134" title="All 2 branches missed."> if (rs2 != null)</span>
|
|
try {
|
|
<span class="nc" id="L136"> rs2.close();</span>
|
|
<span class="nc" id="L137"> } catch (SQLException e) {</span>
|
|
<span class="nc" id="L138"> e.printStackTrace();</span>
|
|
<span class="nc" id="L139"> }</span>
|
|
<span class="nc bnc" id="L140" title="All 2 branches missed."> if (pstmt2 != null)</span>
|
|
try {
|
|
<span class="nc" id="L142"> pstmt2.close();</span>
|
|
<span class="nc" id="L143"> } catch (SQLException e) {</span>
|
|
<span class="nc" id="L144"> e.printStackTrace();</span>
|
|
<span class="nc" id="L145"> }</span>
|
|
<span class="nc bnc" id="L146" title="All 2 branches missed."> if (conn != null)</span>
|
|
try {
|
|
<span class="nc" id="L148"> conn.close();</span>
|
|
<span class="nc" id="L149"> } catch (SQLException e) {</span>
|
|
<span class="nc" id="L150"> e.printStackTrace();</span>
|
|
<span class="nc" id="L151"> }</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> |