Finished Lab3

This commit is contained in:
2019-04-15 22:05:59 +02:00
parent d530e0bcb6
commit 5947a358a3
4 changed files with 99 additions and 3 deletions
+34
View File
@@ -0,0 +1,34 @@
package Lab3;
import Lab1.CarDAO;
import Lab1.ConnectionUtil;
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;
public class CarDao extends Lab2.CarsDao{
public boolean FindID(Integer id) {
boolean isIdPresent = false;
String query = "select count(*) from Cars where id = ?";
try (Connection connection = ConnectionUtil.getConnection()) {
try (PreparedStatement ps = connection.prepareStatement(query)) {
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
isIdPresent = rs.getInt(1) == 1;
}
}
} catch (SQLException ex) {
Logger.getLogger(CarDAO.class.getName()).log(Level.SEVERE, null, ex);
} finally {
return isIdPresent;
}
}
}
+63 -1
View File
@@ -1,6 +1,68 @@
package Lab3; package Lab3;
import Lab1.CarWebService; import Lab1.CarWebService;
import Lab3.Exception.*;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlElement;
import java.util.Date;
@WebService(name = "CarService")
public class CarWebExcept extends CarWebService {
private final static CarDao carDao = new CarDao();
@WebMethod
public Integer insertCar(
// @WebParam(name = "CarID") Integer id,
@WebParam(name = "Name") String name,
@WebParam(name = "model") String model,
@WebParam(name = "country") String country,
@WebParam(name = "dateOfSales") Date dateOfSales,
@WebParam(name = "power") int power) throws WrongValueExcept {
if (power < 1 || power > 2000) {
CarServiceFault fault = new CarServiceFault("insert", "ERROR insert, power not value");
throw new WrongValueExcept("insert", "Power must be between 1 PS and 2000 PS", fault);
} else {
return carDao.insertDB(name, model, country, dateOfSales, power);
}
}
@WebMethod
public String updateCar(@XmlElement(required = true)
@WebParam(name = "CarID") Integer id,
@WebParam(name = "Name") String name,
@WebParam(name = "model") String model,
@WebParam(name = "country") String country,
@WebParam(name = "dateOfSales") Date dateOfSales,
@WebParam(name = "power") int power) throws IDExcept, WrongValueExcept {
if (carDao.FindID(id)) {
if (power < 1 || power > 2000) {
CarServiceFault fault = new CarServiceFault("insert", "ERROR insert, power not value");
throw new WrongValueExcept("insert", "Power must be between 1 PS and 2000 PS", fault);
} else {
return carDao.updateDB(id, name, model, country, dateOfSales, power).getStringValue();
}
} else {
CarServiceFault fault = new CarServiceFault("update", "Row update");
throw new IDExcept(id, "update", fault);
}
}
@WebMethod
public String deleteCar(@XmlElement(required = true)
@WebParam(name = "CarID") Integer id) throws IDExcept {
if (carDao.FindID(id)) {
return carDao.deleteDB(id).getStringValue();
} else {
CarServiceFault fault = new CarServiceFault("delete", "Row delete must");
throw new IDExcept(id, "delete", fault);
}
}
public class CarWebExcept {
} }
@@ -2,7 +2,7 @@ package Lab3.Exception;
public class CarServiceFault { public class CarServiceFault {
private String message; private String message;
private final static String TEMPLATE_MESSAGE = "Problem during %s operation. %s"; private final static String TEMPLATE_MESSAGE = "Problems with %s operation. %s";
public String getMessage() { public String getMessage() {
return message; return message;
+1 -1
View File
@@ -7,7 +7,7 @@ public class IDExcept extends Exception {
private CarServiceFault _CarServiceFault; private CarServiceFault _CarServiceFault;
private final static String TEMPLATE_OF_MESSAGE = "Error during %s operation. " + private final static String TEMPLATE_OF_MESSAGE = "Error during %s operation. " +
"Film with id %d is not found"; "ERROR: Car with ID %d is not found";
public IDExcept(Integer id, String command, CarServiceFault fault) { public IDExcept(Integer id, String command, CarServiceFault fault) {
super(String.format(TEMPLATE_OF_MESSAGE, command, id)); super(String.format(TEMPLATE_OF_MESSAGE, command, id));