|
@@ -29,29 +29,26 @@ public class ManagementUtil extends DBUtil {
|
|
|
|
|
|
|
|
if (payload.containsKey("sensors")) {
|
|
if (payload.containsKey("sensors")) {
|
|
|
JSONArray sensorsJsonArray = payload.getJSONArray("sensors");
|
|
JSONArray sensorsJsonArray = payload.getJSONArray("sensors");
|
|
|
- List<Sensor> sensors = new ArrayList<>(sensorsJsonArray.size());
|
|
|
|
|
|
|
+ long[] sensorIds = new long[sensorsJsonArray.size()];
|
|
|
for (int i = 0; i < sensorsJsonArray.size(); i++) {
|
|
for (int i = 0; i < sensorsJsonArray.size(); i++) {
|
|
|
JSONObject sensorJson = sensorsJsonArray.getJSONObject(i);
|
|
JSONObject sensorJson = sensorsJsonArray.getJSONObject(i);
|
|
|
if (!sensorJson.containsKey("sensor_id")) {
|
|
if (!sensorJson.containsKey("sensor_id")) {
|
|
|
throw new NoItemFoundException("Attribute 'sensor_id' is required.");
|
|
throw new NoItemFoundException("Attribute 'sensor_id' is required.");
|
|
|
}
|
|
}
|
|
|
- long sensorId = sensorJson.getLong("sensor_id");
|
|
|
|
|
- sensors.add(new Sensor(sensorId, null, null, null));
|
|
|
|
|
|
|
+ sensorIds[i] = sensorJson.getLong("sensor_id");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- for (Sensor sensor : sensors) {
|
|
|
|
|
- long sensorId = sensor.getSensorId();
|
|
|
|
|
- String del = "DELETE FROM units_to_sensors WHERE sensor_id = "+sensorId + " AND unit_id = " + unitId;
|
|
|
|
|
- int result = SQLExecutor.executeUpdate(del);
|
|
|
|
|
-
|
|
|
|
|
- // TODO delete sensor in the database
|
|
|
|
|
|
|
+ List<Sensor> sensors = new ArrayList<>(sensorIds.length);
|
|
|
|
|
+ for (long sensorId : sensorIds) {
|
|
|
|
|
+ String sql = String.format("DELETE FROM units_to_sensors WHERE sensor_id = %s AND unit_id = %s", sensorId, unitId);
|
|
|
|
|
+ int result = SQLExecutor.executeUpdate(sql);
|
|
|
//- delete sensor pokud je pouze u teto jednotky
|
|
//- delete sensor pokud je pouze u teto jednotky
|
|
|
//- delete pouze units_to_sensors pokud je u vice jednotek - typ senzoru - OK
|
|
//- delete pouze units_to_sensors pokud je u vice jednotek - typ senzoru - OK
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if (result > 0) {
|
|
|
|
|
+ sensors.add(new Sensor(sensorId, null, null, null));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
return new UnitInsert(unitId, null, sensors);
|
|
return new UnitInsert(unitId, null, sensors);
|
|
|
-
|
|
|
|
|
} else {
|
|
} else {
|
|
|
throw new NoItemFoundException("Attribute 'sensors' is required.");
|
|
throw new NoItemFoundException("Attribute 'sensors' is required.");
|
|
|
}
|
|
}
|
|
@@ -69,7 +66,7 @@ public class ManagementUtil extends DBUtil {
|
|
|
long unitId = unitJson.getLong("unit_id");
|
|
long unitId = unitJson.getLong("unit_id");
|
|
|
final UnitUtil unitUtil = new UnitUtil();
|
|
final UnitUtil unitUtil = new UnitUtil();
|
|
|
int result = unitUtil.deleteUnit(unitId);
|
|
int result = unitUtil.deleteUnit(unitId);
|
|
|
- return result != 0 ? unitId : null;
|
|
|
|
|
|
|
+ return result > 0 ? unitId : null;
|
|
|
} else {
|
|
} else {
|
|
|
throw new NoItemFoundException("Attribute 'unit' is required.");
|
|
throw new NoItemFoundException("Attribute 'unit' is required.");
|
|
|
}
|
|
}
|
|
@@ -139,13 +136,10 @@ public class ManagementUtil extends DBUtil {
|
|
|
throw new NoItemFoundException("Attribute 'unit_id' is required.");
|
|
throw new NoItemFoundException("Attribute 'unit_id' is required.");
|
|
|
}
|
|
}
|
|
|
long unitId = unitJson.getLong("unit_id");
|
|
long unitId = unitJson.getLong("unit_id");
|
|
|
- String updUnit = "UPDATE units SET description = '"+unitJson.getString("description")+"' WHERE unit_id = "+unitId;
|
|
|
|
|
- int result = SQLExecutor.executeUpdate(updUnit);
|
|
|
|
|
-
|
|
|
|
|
- // TODO UPDATE unit attributes in the database
|
|
|
|
|
-
|
|
|
|
|
- return new UnitInsert(unitId, null, null);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ String unitDsc = unitJson.getString("description");
|
|
|
|
|
+ String sql = String.format("UPDATE units SET description = '%s' WHERE unit_id = %s", unitDsc, unitId);
|
|
|
|
|
+ int result = SQLExecutor.executeUpdate(sql);
|
|
|
|
|
+ return result > 0 ? new UnitInsert(unitId, unitDsc, null) : null;
|
|
|
} else {
|
|
} else {
|
|
|
throw new NoItemFoundException("Attribute 'unit' is required.");
|
|
throw new NoItemFoundException("Attribute 'unit' is required.");
|
|
|
}
|
|
}
|