GetStationConfig.java
package com.workbenchclassic;
import java.sql.SQLException;
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.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.reflect.Method;
@Path("/getStationConfig")
public class GetStationConfig { // Klassenname geƤndert
private static final DBService DB_SERVICE = new DBService("dsMesMiiNJTA");
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final Logger LOGGER = Logger.getLogger(DBService.class.getName());
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getUserResponse(String jsonBody) {
String station;
try {
JSONObject json = new JSONObject(jsonBody);
station = json.getString("station");
} catch (JSONException e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("{\"error\":\"Missing or invalid 'station' parameter\"}")
.build();
}
String query = "select code_nr , code_wert, btk.stamp as timestamp, bde_userid as changedBy from bde.txt_konfig btk join bde.pers_stamm bps on bps.pers_id=btk.user_id where code in (select kap_id from bde.kast where KAP_NR = '"
+ station + "')";
// LOGGER.log(Level.INFO, "Executing SQL query: {0}", query);
try {
List<Map<String, Object>> rows = DB_SERVICE.dbConnectAndGetRows(query);
for (Map<String, Object> row : rows) {
Object tsVal = row.get("timestamp");
if (tsVal != null) {
row.put("timestamp", convertTimestamp(tsVal));
}
}
// Vorbereitetes Statement nutzen
//
return Response.ok(MAPPER.writeValueAsString(rows)).build();
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Database error", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(String.format("{\"error\":\"DB error: %s\"}", e.getMessage()))
.build();
} catch (JsonProcessingException e) {
LOGGER.log(Level.SEVERE, "JSON processing error", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(String.format("{\"error\":\"JSON processing error: %s\"}", e.getMessage()))
.build();
}
}
private Object convertTimestamp(Object val) {
if (val != null && "oracle.sql.TIMESTAMP".equals(val.getClass().getName())) {
try {
Method m = val.getClass().getMethod("timestampValue");
return m.invoke(val); // returns a java.sql.Timestamp
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Error converting oracle.sql.TIMESTAMP via reflection", e);
}
} else if (val instanceof java.sql.Timestamp) {
return val;
} else {
return val;
}
}
}