backend/src/main/java/com/workbenchclassic/GetMetadata.java aktualisiert
This commit is contained in:
@@ -1,154 +1,154 @@
|
|||||||
package com.workbenchclassic;
|
package com.workbenchclassic;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example refactoring: return a JSON string instead of JAX-RS Response.
|
* Example refactoring: return a JSON string instead of JAX-RS Response.
|
||||||
*/
|
*/
|
||||||
public class GetMetadata {
|
public class GetMetadata {
|
||||||
|
|
||||||
private static final DBService DB_SERVICE = new DBService("dsMesMiiNJTA");
|
private static final DBService DB_SERVICE = new DBService("dsMesMiiNJTA");
|
||||||
private static final Logger LOGGER = Logger.getLogger(GetMetadata.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(GetMetadata.class.getName());
|
||||||
|
|
||||||
public String getExtPlantJson(String station) throws Exception {
|
public String getExtPlantJson(String station) throws Exception {
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
PreparedStatement pstmt1 = null;
|
PreparedStatement pstmt1 = null;
|
||||||
ResultSet rs1 = null;
|
ResultSet rs1 = null;
|
||||||
PreparedStatement pstmt2 = null;
|
PreparedStatement pstmt2 = null;
|
||||||
ResultSet rs2 = null;
|
ResultSet rs2 = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
conn = DB_SERVICE.getConnection();
|
conn = DB_SERVICE.getConnection();
|
||||||
|
|
||||||
// 1) Query #1
|
// 1) Query #1
|
||||||
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 "
|
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 "
|
||||||
+
|
+
|
||||||
" k.kap_nr = ?";
|
" k.kap_nr = ?";
|
||||||
pstmt1 = conn.prepareStatement(werkQuery);
|
pstmt1 = conn.prepareStatement(werkQuery);
|
||||||
pstmt1.setString(1, station);
|
pstmt1.setString(1, station);
|
||||||
rs1 = pstmt1.executeQuery();
|
rs1 = pstmt1.executeQuery();
|
||||||
|
|
||||||
JSONArray dataArray1 = new JSONArray();
|
JSONArray dataArray1 = new JSONArray();
|
||||||
String werkNr = null;
|
String werkNr = null;
|
||||||
String ext_company_nr = null;
|
String ext_company_nr = null;
|
||||||
String clientID = null;
|
String clientID = null;
|
||||||
|
|
||||||
if (rs1.next()) {
|
if (rs1.next()) {
|
||||||
ext_company_nr = rs1.getString("ext_company_nr");
|
ext_company_nr = rs1.getString("ext_werk_nr");
|
||||||
werkNr = rs1.getString("ext_werk_nr");
|
werkNr = rs1.getString("ext_company_nr");
|
||||||
clientID = rs1.getString("client_id");
|
clientID = rs1.getString("client_id");
|
||||||
JSONObject row = new JSONObject();
|
JSONObject row = new JSONObject();
|
||||||
row.put("ext_company_nr", ext_company_nr);
|
row.put("ext_company_nr", ext_company_nr);
|
||||||
row.put("ext_werk_nr", werkNr);
|
row.put("ext_werk_nr", werkNr);
|
||||||
row.put("client_id", clientID);
|
row.put("client_id", clientID);
|
||||||
dataArray1.put(row);
|
dataArray1.put(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataArray1.length() == 0) {
|
if (dataArray1.length() == 0) {
|
||||||
// No data for that station
|
// No data for that station
|
||||||
JSONObject noDataResponse = new JSONObject();
|
JSONObject noDataResponse = new JSONObject();
|
||||||
noDataResponse.put("errorCode", "1");
|
noDataResponse.put("errorCode", "1");
|
||||||
noDataResponse.put("errorMessage", "No werk found for station: " + station);
|
noDataResponse.put("errorMessage", "No werk found for station: " + station);
|
||||||
noDataResponse.put("data", "[]");
|
noDataResponse.put("data", "[]");
|
||||||
return noDataResponse.toString(); // Return the JSON string
|
return noDataResponse.toString(); // Return the JSON string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assume the FIRST row is enough
|
// Assume the FIRST row is enough
|
||||||
JSONObject firstRow = dataArray1.getJSONObject(0);
|
JSONObject firstRow = dataArray1.getJSONObject(0);
|
||||||
werkNr = firstRow.optString("ext_werk_nr", null);
|
werkNr = firstRow.optString("ext_werk_nr", null);
|
||||||
ext_company_nr = firstRow.optString("ext_company_nr", null);
|
ext_company_nr = firstRow.optString("ext_company_nr", null);
|
||||||
|
|
||||||
if (werkNr == null) {
|
if (werkNr == null) {
|
||||||
JSONObject missingWerkNr = new JSONObject();
|
JSONObject missingWerkNr = new JSONObject();
|
||||||
missingWerkNr.put("errorCode", "1");
|
missingWerkNr.put("errorCode", "1");
|
||||||
missingWerkNr.put("errorMessage", "werk_nr not found for station: " + station);
|
missingWerkNr.put("errorMessage", "werk_nr not found for station: " + station);
|
||||||
missingWerkNr.put("data", "[]");
|
missingWerkNr.put("data", "[]");
|
||||||
return missingWerkNr.toString();
|
return missingWerkNr.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2) Query #2 for clientNr
|
// 2) Query #2 for clientNr
|
||||||
String clientNrQuery = "SELECT CLIENT_NR AS werknummer " +
|
String clientNrQuery = "SELECT CLIENT_NR AS werknummer " +
|
||||||
"FROM bde.mandant m " +
|
"FROM bde.mandant m " +
|
||||||
"WHERE m.client_id = ?";
|
"WHERE m.client_id = ?";
|
||||||
pstmt2 = conn.prepareStatement(clientNrQuery);
|
pstmt2 = conn.prepareStatement(clientNrQuery);
|
||||||
pstmt2.setString(1, clientID);
|
pstmt2.setString(1, clientID);
|
||||||
rs2 = pstmt2.executeQuery();
|
rs2 = pstmt2.executeQuery();
|
||||||
|
|
||||||
String clientNr = null;
|
String clientNr = null;
|
||||||
if (rs2.next()) {
|
if (rs2.next()) {
|
||||||
clientNr = rs2.getString("werknummer");
|
clientNr = rs2.getString("werknummer");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3) Combine results into final JSON
|
// 3) Combine results into final JSON
|
||||||
JSONObject combinedRow = new JSONObject();
|
JSONObject combinedRow = new JSONObject();
|
||||||
combinedRow.put("ext_company_nr", ext_company_nr);
|
combinedRow.put("ext_company_nr", ext_company_nr);
|
||||||
combinedRow.put("werk_nr", werkNr);
|
combinedRow.put("werk_nr", werkNr);
|
||||||
combinedRow.put("client_nr", clientNr != null ? clientNr : JSONObject.NULL);
|
combinedRow.put("client_nr", clientNr != null ? clientNr : JSONObject.NULL);
|
||||||
|
|
||||||
JSONArray finalDataArray = new JSONArray();
|
JSONArray finalDataArray = new JSONArray();
|
||||||
finalDataArray.put(combinedRow);
|
finalDataArray.put(combinedRow);
|
||||||
|
|
||||||
JSONObject finalResult = new JSONObject();
|
JSONObject finalResult = new JSONObject();
|
||||||
finalResult.put("errorCode", "0");
|
finalResult.put("errorCode", "0");
|
||||||
finalResult.put("errorMessage", "Done");
|
finalResult.put("errorMessage", "Done");
|
||||||
finalResult.put("data", finalDataArray.toString());
|
finalResult.put("data", finalDataArray.toString());
|
||||||
|
|
||||||
return finalResult.toString();
|
return finalResult.toString();
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.log(Level.SEVERE, "SQL error in getExtPlant", e);
|
LOGGER.log(Level.SEVERE, "SQL error in getExtPlant", e);
|
||||||
JSONObject errorJson = new JSONObject();
|
JSONObject errorJson = new JSONObject();
|
||||||
errorJson.put("errorCode", "99");
|
errorJson.put("errorCode", "99");
|
||||||
errorJson.put("errorMessage", "Database Error: " + e.getMessage());
|
errorJson.put("errorMessage", "Database Error: " + e.getMessage());
|
||||||
errorJson.put("data", "[]");
|
errorJson.put("data", "[]");
|
||||||
return errorJson.toString();
|
return errorJson.toString();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.log(Level.SEVERE, "General error in getExtPlant", e);
|
LOGGER.log(Level.SEVERE, "General error in getExtPlant", e);
|
||||||
JSONObject errorJson = new JSONObject();
|
JSONObject errorJson = new JSONObject();
|
||||||
errorJson.put("errorCode", "99");
|
errorJson.put("errorCode", "99");
|
||||||
errorJson.put("errorMessage", "General Error: " + e.getMessage());
|
errorJson.put("errorMessage", "General Error: " + e.getMessage());
|
||||||
errorJson.put("data", "[]");
|
errorJson.put("data", "[]");
|
||||||
return errorJson.toString();
|
return errorJson.toString();
|
||||||
} finally {
|
} finally {
|
||||||
// close resources
|
// close resources
|
||||||
if (rs1 != null)
|
if (rs1 != null)
|
||||||
try {
|
try {
|
||||||
rs1.close();
|
rs1.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
if (pstmt1 != null)
|
if (pstmt1 != null)
|
||||||
try {
|
try {
|
||||||
pstmt1.close();
|
pstmt1.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
if (rs2 != null)
|
if (rs2 != null)
|
||||||
try {
|
try {
|
||||||
rs2.close();
|
rs2.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
if (pstmt2 != null)
|
if (pstmt2 != null)
|
||||||
try {
|
try {
|
||||||
pstmt2.close();
|
pstmt2.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
if (conn != null)
|
if (conn != null)
|
||||||
try {
|
try {
|
||||||
conn.close();
|
conn.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user