mirror of
https://github.com/svek95/WST_Labs.git
synced 2026-05-17 18:44:02 +03:00
Add Lab4
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
package Lab4;
|
||||||
|
|
||||||
|
import Lab4.common.CarResource;
|
||||||
|
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
|
||||||
|
import com.sun.jersey.api.core.ClassNamesResourceConfig;
|
||||||
|
import com.sun.jersey.api.core.ResourceConfig;
|
||||||
|
import org.glassfish.grizzly.http.server.HttpServer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
private static final URI BASE_URI = URI.create("http://localhost:8081/rest/");
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
HttpServer server = null;
|
||||||
|
try {
|
||||||
|
ResourceConfig resourceConfig = new ClassNamesResourceConfig(CarResource.class);
|
||||||
|
server = GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
|
||||||
|
server.start();
|
||||||
|
System.in.read();
|
||||||
|
stopServer(server);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
stopServer(server);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void stopServer(HttpServer server) {
|
||||||
|
if (server != null)
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package Lab4.common;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
|
||||||
|
@XmlRootElement
|
||||||
|
public class Car {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private Date dateOfSales;
|
||||||
|
private String country;
|
||||||
|
private double power;
|
||||||
|
private String model;
|
||||||
|
|
||||||
|
public Car(String name, Date date, String country, double power, String model) {
|
||||||
|
this.name = name;
|
||||||
|
this.dateOfSales = date;
|
||||||
|
this.country = country;
|
||||||
|
this.power = power;
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getdateOfSales() {
|
||||||
|
return dateOfSales;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setdateOfSales(Date dateOfSales) {
|
||||||
|
this.dateOfSales = dateOfSales;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCountry() {
|
||||||
|
return country;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCountry(String country) {
|
||||||
|
this.country = country;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getpower() {
|
||||||
|
return power;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setpower(double power) {
|
||||||
|
this.power = power;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getmodel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setmodel(String model) {
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package Lab4.common;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public abstract class CarDAO {
|
||||||
|
|
||||||
|
private Boolean CheckIN(Object value, String fieldName, Boolean isNotFirst, StringBuilder query) {
|
||||||
|
if (value != null) {
|
||||||
|
if (isNotFirst) {
|
||||||
|
query.append(" and ");
|
||||||
|
}
|
||||||
|
if (value instanceof String) {
|
||||||
|
query.append(fieldName).append(" = '").append(value).append("'");
|
||||||
|
} else {
|
||||||
|
query.append(fieldName).append(" = ").append(value);
|
||||||
|
}
|
||||||
|
if (!isNotFirst) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isNotFirst;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract Connection getConnection();
|
||||||
|
|
||||||
|
public List<Car> getDataByFields(String name, String model, String country, Date dateOfSales, Integer power) {
|
||||||
|
List<Car> cars = Collections.emptyList();
|
||||||
|
try (Connection connection = getConnection()) {
|
||||||
|
StringBuilder query = new StringBuilder("select * from cars");
|
||||||
|
boolean notFirstField = false;
|
||||||
|
if (name != null || model != null || country != null && dateOfSales != null || power != 0) {
|
||||||
|
query.append(" where ");
|
||||||
|
}
|
||||||
|
notFirstField = CheckIN(name, "name", notFirstField, query);
|
||||||
|
notFirstField = CheckIN(model, "model", notFirstField, query);
|
||||||
|
notFirstField = CheckIN(country, "country", notFirstField, query);
|
||||||
|
notFirstField = CheckIN(dateOfSales, "dateOfSales", notFirstField, query);
|
||||||
|
CheckIN(power, "power", notFirstField, query);
|
||||||
|
|
||||||
|
PreparedStatement stmt = connection.prepareStatement(query.toString());
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
cars = extractCarsFromResult(rs);
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
Logger.getLogger(CarDAO.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
return cars;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Car> extractCarsFromResult(ResultSet rs) throws SQLException{
|
||||||
|
List<Car> cars = new ArrayList<Car>();
|
||||||
|
while (rs.next()) {
|
||||||
|
String name = rs.getString("name");
|
||||||
|
Date date = rs.getDate("dateOfSales");
|
||||||
|
String model = rs.getString("model");
|
||||||
|
String country = rs.getString("country");
|
||||||
|
double power = rs.getDouble("power");
|
||||||
|
|
||||||
|
Car car = new Car(name, date, country, power, model);
|
||||||
|
cars.add(car);
|
||||||
|
} return cars;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package Lab4.common;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.QueryParam;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Path("/cars")
|
||||||
|
@Produces({MediaType.APPLICATION_JSON})
|
||||||
|
public class CarResource {
|
||||||
|
|
||||||
|
private final static CarDAO carDao = new StandaloneCarDAO();
|
||||||
|
|
||||||
|
@GET
|
||||||
|
public List<Car> findCars(@QueryParam("Name") String name,
|
||||||
|
@QueryParam("model") String model,
|
||||||
|
@QueryParam("country") String country,
|
||||||
|
@QueryParam("dateOfSales") Date dateOfSales,
|
||||||
|
@QueryParam("power") Integer power) {
|
||||||
|
return carDao.getDataByFields(name, model, country, dateOfSales, power);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package Lab4.common;
|
||||||
|
|
||||||
|
//import Lab1.CarDAO;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class ConnectionUtil {
|
||||||
|
/// private static final String JDBC_URL = "jdbc:postgresql://192.168.1.119:5432/";
|
||||||
|
private static final String JDBC_URL = "jdbc:postgresql://5.19.136.134:5432/";
|
||||||
|
private static final String JDBC_USER = "postgres";
|
||||||
|
private static final String JDBC_PASSWORD = "qwerty";
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
Class.forName("org.postgresql.Driver");
|
||||||
|
} catch (ClassNotFoundException ex) {
|
||||||
|
Logger.getLogger(CarDAO.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static Connection getConnection() {
|
||||||
|
Connection connection = null;
|
||||||
|
try {
|
||||||
|
connection = DriverManager.getConnection(JDBC_URL, JDBC_USER,
|
||||||
|
JDBC_PASSWORD);
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
Logger.getLogger(ConnectionUtil.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package Lab4.common;
|
||||||
|
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
|
||||||
|
public class StandaloneCarDAO extends CarDAO {
|
||||||
|
@Override
|
||||||
|
public Connection getConnection() {
|
||||||
|
return ConnectionUtil.getConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user