Commit f35f4c1c by Patryk Czarnik

Stary projekt wielomodułowy

parent 4cd727ee
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>P40-OgloszeniaParent</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding/<project>=UTF-8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-api</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-api">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="1.8"/>
<installed facet="jst.utility" version="1.0"/>
</faceted-project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-api</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
package ogloszenia.rest;
import java.math.BigDecimal;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
@Path("/ogloszenia")
public interface IOgloszenia {
@GET
@Produces("application/xml")
public ListaOgloszen wszystkieOgloszenia() throws BladBazyDanych;
@POST
@Consumes("application/xml")
@Produces("application/xml")
public OgloszenieSamochodowe zapiszOgloszenie(OgloszenieSamochodowe ogloszenie) throws BladBazyDanych;
@Path("/{id}")
@GET
@Produces("application/xml")
public OgloszenieSamochodowe jednoOgloszenie(
@PathParam("id") int idOgloszenia) throws BladBazyDanych, NieznanyRekord;
@Path("/{id}/cena")
@GET
@Produces("text/plain")
public BigDecimal getCena(@PathParam("id") int idOgloszenia) throws BladBazyDanych, NieznanyRekord;
@Path("/{id}/cena")
@PUT
@Consumes("text/plain")
public void setCena(@PathParam("id") int idOgloszenia,
BigDecimal nowaCena) throws BladBazyDanych, NieznanyRekord;
@Path("/{id}")
@DELETE
public void usun(@PathParam("id") int idOgloszenia) throws BladBazyDanych, NieznanyRekord;
@Path("/{id}/foto")
@GET
@Produces("image/jpeg")
public byte[] foto(@PathParam("id") int idOgloszenia) throws NieznanyRekord;
}
package ogloszenia.soap;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.OgloszenieSamochodowe;
@WebService(targetNamespace=Nazwy.NAMESPACE,
name=Nazwy.PORT_TYPE_NAME)
public interface ISerwisOgloszeniowy {
@WebResult(name="ogloszenie")
public List<OgloszenieSamochodowe> wszystkieOgloszenia() throws BladBazyDanych;
@WebResult(name="ogloszenie")
public OgloszenieSamochodowe jednoOgloszenie(@WebParam(name="id-ogloszenia") int idOgloszenia)
throws BladBazyDanych, NieznanyRekord;
public void zapiszOgloszenia(@WebParam(name="ogloszenie") OgloszenieSamochodowe ogloszenie)
throws BladBazyDanych;
@WebResult(name="foto")
public byte[] foto(@WebParam(name="id-ogloszenia") int idOgloszenia) throws NieznanyRekord;
}
package ogloszenia.soap;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
/** Klasa definiująca stałe: nazwy i adresy używane w usłudze SOAP. */
public final class Nazwy {
private Nazwy() {}
public static final String NAMESPACE = "http://vavatech.pl/jws/SerwisOgloszeniowy";
public static final String SERVICE_NAME = "SerwisOgloszeniowy";
public static final String PORT_NAME = "SerwisOgloszeniowyPort";
public static final String PORT_TYPE_NAME = "SerwisOgloszeniowy";
public static final QName SERVICE_QNAME = new QName(NAMESPACE, SERVICE_NAME);
public static final QName PORT_QNAME = new QName(NAMESPACE, PORT_NAME);
/** Spodziewany adres WSDL na serwerze. */
public static final String WSDL = "http://localhost:8080/ogloszenia-soap/SerwisOgloszeniowy?wsdl";
public static final URL WSDL_URL = generateUrl(WSDL);
private static URL generateUrl(String address) {
try {
return new URL(address);
} catch(MalformedURLException e) {
return null;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-baza</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-baza">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="1.8"/>
<installed facet="jst.utility" version="1.0"/>
</faceted-project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-baza</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.20.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
package ogloszenia;
/** W tej klasie trzeba wpisać swoje ustawienia ścieżek. */
public class Ustawienia {
public static final String SQLITE_PATH = "C:/Tools/ogloszenia/ogloszenia.db";
public static final String PHOTOS_PATH = "C:/Tools/ogloszenia/foto";
}
package ogloszenia.baza.sqlite;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import ogloszenia.Ustawienia;
import ogloszenia.baza.sqlite.OgloszenieDAO;
import ogloszenia.baza.sqlite.SprzedawcaDAO;
import ogloszenia.exn.BladBazyDanych;
public class DostepDoBazySqlite implements AutoCloseable {
private Connection c;
DostepDoBazySqlite(Connection con) {
c = con;
}
public static DostepDoBazySqlite newSQLite(String sqliteFile) throws BladBazyDanych {
try {
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:" + sqliteFile);
return new DostepDoBazySqlite(con);
} catch (Exception e) {
throw new BladBazyDanych("Błąd podczas otwierania bazy danych ("+e.getMessage()+")", e);
}
}
public static DostepDoBazySqlite newSQLite() throws BladBazyDanych {
return newSQLite(Ustawienia.SQLITE_PATH);
}
@Override
public void close() {
try {
if(c != null)
c.close();
} catch (SQLException e) {
System.err.println("Błąd podczas close: " + e.getMessage());
}
}
Connection c() {
return c;
}
public void beginTransaction() throws BladBazyDanych {
try {
c.setAutoCommit(false);
//c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
} catch (SQLException e) {
throw new BladBazyDanych("beginTransaction: " + e.getMessage(), e);
}
}
public void endTransaction(boolean commit) throws BladBazyDanych {
try {
if(commit) {
c.commit();
} else {
c.rollback();
}
c.setAutoCommit(true);
} catch (SQLException e) {
throw new BladBazyDanych("endTransaction: " + e.getMessage(), e);
}
}
public OgloszenieDAO newOgloszenieDAO() {
return new OgloszenieDAO(this);
}
public SprzedawcaDAO newSprzedawcaDAO() {
return new SprzedawcaDAO(this);
}
}
package ogloszenia.baza.sqlite;
import java.time.LocalDateTime;
import ogloszenia.model.Paliwo;
public class Konwersje {
public static LocalDateTime dateFromString(String s) {
return s == null ? null : LocalDateTime.parse(s.replace(' ', 'T'));
}
public static String dateToString(LocalDateTime localDateTime) {
return localDateTime == null ? null : localDateTime.toString().replace('T', ' ');
}
public static Paliwo paliwoFromString(String s) {
return s == null ? null : Paliwo.valueOf(s.trim().toUpperCase());
}
public static String paliwoToString(Paliwo paliwo) {
return paliwo == null ? null : paliwo.toString();
}
}
package ogloszenia.baza.sqlite;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.Ogloszenie;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.model.Silnik;
import ogloszenia.model.Sprzedawca;
public class OgloszenieDAO {
private DostepDoBazySqlite db;
OgloszenieDAO(DostepDoBazySqlite db) {
this.db = db;
}
public DostepDoBazySqlite getDBHandler() {
return db;
}
public List<Integer> listaIdOgloszen() throws BladBazyDanych {
final String sql = "SELECT id_ogloszenia FROM ogloszenia ORDER BY id_ogloszenia";
List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
lista.add(rs.getInt(1));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("listaIdOgloszen: "+e.getMessage(), e);
}
return lista;
}
public OgloszenieSamochodowe byId(int idOgloszenia) throws BladBazyDanych, NieznanyRekord {
final String sql = "SELECT * FROM ogloszenia WHERE id_ogloszenia = ?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idOgloszenia);
try(ResultSet rs = stmt.executeQuery()) {
if(rs.next()) {
return ogloszenieZResultSet(rs);
} else {
throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia);
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie byId: "+e.getMessage(), e);
}
}
/** Wczytuje także rekord Sprzedawca
* @param idOgloszenia
* @return
* @throws SQLException
* @throws NieznanyRekord
*/
public OgloszenieSamochodowe byIdFull(int idOgloszenia) throws BladBazyDanych, NieznanyRekord {
final String sql = "SELECT * FROM ogloszenia WHERE id_ogloszenia = ?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idOgloszenia);
try(ResultSet rs = stmt.executeQuery()) {
if(rs.next()) {
OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs);
doczytajSprzedawce(ogl);
return ogl;
} else {
throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia);
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie byIdFull: "+e.getMessage(), e);
}
}
public List<OgloszenieSamochodowe> readAll() throws BladBazyDanych {
final String sql = "SELECT * FROM ogloszenia";
List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
lista.add(ogloszenieZResultSet(rs));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie readAll: "+e.getMessage(), e);
}
return lista;
}
public List<OgloszenieSamochodowe> readAllFull() throws BladBazyDanych {
final String sql = "SELECT * FROM ogloszenia";
List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs);
try {
doczytajSprzedawce(ogl);
} catch (NieznanyRekord e) {
e.printStackTrace();
}
lista.add(ogl);
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie readAllFull: "+e.getMessage(), e);
}
return lista;
}
public List<OgloszenieSamochodowe> ogloszeniaWedlugCeny(BigDecimal cenaOd, BigDecimal cenaDo) throws BladBazyDanych {
final String sql = "SELECT * FROM ogloszenia"
+ " WHERE cena BETWEEN ? AND ?"
+ " ORDER BY id_ogloszenia";
if(cenaOd == null)
cenaOd = BigDecimal.ZERO;
if(cenaDo == null)
cenaDo = new BigDecimal(100000000);
List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setBigDecimal(1, cenaOd);
stmt.setBigDecimal(2, cenaDo);
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs);
try {
doczytajSprzedawce(ogl);
} catch (NieznanyRekord e) {
e.printStackTrace();
}
lista.add(ogl);
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Błąd bazy w metodzie ogloszeniaWedlugCeny: " + e.getMessage(), e);
}
return lista;
}
public List<OgloszenieSamochodowe> ogloszeniaSprzedawcy(int idSprzedawcy) throws BladBazyDanych {
final String sql = "SELECT * FROM ogloszenia WHERE id_sprzedawcy = ?";
List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
lista.add(ogloszenieZResultSet(rs));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("ogloszeniaSprzedawcy: "+e.getMessage(), e);
}
return lista;
}
public List<Integer> listaIdOgloszenSprzedawcy(int idSprzedawcy) throws BladBazyDanych {
final String sql = "SELECT id_ogloszenia FROM ogloszenia WHERE id_sprzedawcy = ?";
List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
lista.add(rs.getInt(1));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("listaIdOgloszenSprzedawcy: "+e.getMessage(), e);
}
return lista;
}
public void save(OgloszenieSamochodowe ogl) throws BladBazyDanych {
if(ogl.getIdOgloszenia() == null) {
// wstawiamy nowy rekord korzystajac z autoincrement
insertNew(ogl);
} else if(! update(ogl)) {
insert(ogl);
}
}
public boolean insert(OgloszenieSamochodowe ogl) throws BladBazyDanych {
uzupelnijDate(ogl);
final String sql = "INSERT INTO ogloszenia("
+ " id_ogloszenia, id_sprzedawcy, data_wystawienia, cena, tytul, opis,"
+ " marka, model, generacja, kolor, rocznik, przebieg, pojemnosc, moc, paliwo)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, ogl.getIdOgloszenia());
stmt.setInt(2, ogl.getIdSprzedawcy());
stmt.setString(3, Konwersje.dateToString(ogl.getDataWystawienia()));
stmt.setBigDecimal(4, ogl.getCena());
stmt.setString(5, ogl.getTytul());
stmt.setString(6, ogl.getOpis());
stmt.setString(7, ogl.getMarka());
stmt.setString(8, ogl.getModel());
stmt.setString(9, ogl.getGeneracja());
stmt.setString(10, ogl.getKolor());
stmt.setInt(11, ogl.getRocznik());
stmt.setInt(12, ogl.getPrzebieg());
Silnik silnik = ogl.getSilnik();
if(silnik != null) {
stmt.setFloat(13, silnik.getMoc());
stmt.setFloat(14, silnik.getPojemnosc());
stmt.setString(15, Konwersje.paliwoToString(silnik.getPaliwo()));
}
int ile = stmt.executeUpdate();
return (ile > 0);
} catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie insert: "+e.getMessage(), e);
}
}
public int insertNew(OgloszenieSamochodowe ogl) throws BladBazyDanych {
uzupelnijDate(ogl);
final String sql = "INSERT INTO ogloszenia("
+ " id_sprzedawcy, data_wystawienia, cena, tytul, opis, marka, model, generacja, kolor, rocznik, przebieg, pojemnosc, moc, paliwo)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try {
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, ogl.getIdSprzedawcy());
stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia()));
stmt.setBigDecimal(3, ogl.getCena());
stmt.setString(4, ogl.getTytul());
stmt.setString(5, ogl.getOpis());
stmt.setString(6, ogl.getMarka());
stmt.setString(7, ogl.getModel());
stmt.setString(8, ogl.getGeneracja());
stmt.setString(9, ogl.getKolor());
stmt.setInt(10, ogl.getRocznik());
stmt.setInt(11, ogl.getPrzebieg());
Silnik silnik = ogl.getSilnik();
if(silnik != null) {
stmt.setFloat(12, silnik.getPojemnosc());
stmt.setFloat(13, silnik.getMoc());
stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo()));
}
int ile = stmt.executeUpdate();
if(ile == 0) {
throw new SQLException("Nie mogę wstawić nowego rekordu ogloszenie");
}
}
try(Statement stmt = db.c().createStatement();
ResultSet rs = stmt.executeQuery("SELECT last_insert_rowid()")) {
if(rs.next()) {
int noweId = rs.getInt(1);
ogl.setIdOgloszenia(noweId);
return noweId;
} else {
throw new SQLException("Nie mogę pobrać id nowego rekordu ogloszenie");
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie insertNew: "+e.getMessage(), e);
}
}
public boolean update(OgloszenieSamochodowe ogl) throws BladBazyDanych {
final String sql = "UPDATE ogloszenia SET id_sprzedawcy=?, data_wystawienia=?, cena=?,"
+ " tytul=?, opis=?, marka=?, model=?, generacja=?,"
+ " kolor=?, rocznik=?, przebieg=?, pojemnosc=?, moc=?, paliwo=?"
+ " WHERE id_ogloszenia=?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, ogl.getIdSprzedawcy());
stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia()));
stmt.setBigDecimal(3, ogl.getCena());
stmt.setString(4, ogl.getTytul());
stmt.setString(5, ogl.getOpis());
stmt.setString(6, ogl.getMarka());
stmt.setString(7, ogl.getModel());
stmt.setString(8, ogl.getGeneracja());
stmt.setString(9, ogl.getKolor());
stmt.setInt(10, ogl.getRocznik());
stmt.setInt(11, ogl.getPrzebieg());
Silnik silnik = ogl.getSilnik();
if(silnik != null) {
stmt.setFloat(12, silnik.getPojemnosc());
stmt.setFloat(13, silnik.getMoc());
stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo()));
}
stmt.setInt(15, ogl.getIdOgloszenia());
int ile = stmt.executeUpdate();
return (ile > 0);
} catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie update: "+e.getMessage(), e);
}
}
public void zmienCene(int idOgloszenia, BigDecimal nowaCena)
throws BladBazyDanych, NieznanyRekord {
final String sql = "UPDATE ogloszenia SET cena=?"
+ " WHERE id_ogloszenia=?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setBigDecimal(1, nowaCena);
stmt.setInt(2, idOgloszenia);
int ile = stmt.executeUpdate();
if(ile == 0) {
throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia);
}
} catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie zmienCene: "+e.getMessage(), e);
}
}
public void delete(int idOgloszenia)
throws BladBazyDanych, NieznanyRekord {
final String sql = "DELETE FROM ogloszenia WHERE id_ogloszenia=?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idOgloszenia);
int ile = stmt.executeUpdate();
if(ile == 0) {
throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia);
}
} catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie delete: "+e.getMessage(), e);
}
}
private OgloszenieSamochodowe ogloszenieZResultSet(ResultSet rs) throws SQLException {
Silnik silnik = new Silnik(rs.getFloat("moc"),
rs.getFloat("pojemnosc"),
Konwersje.paliwoFromString(rs.getString("paliwo")));
return new OgloszenieSamochodowe(
rs.getInt("id_ogloszenia"),
rs.getInt("id_sprzedawcy"),
Konwersje.dateFromString(rs.getString("data_wystawienia")),
rs.getBigDecimal("cena"),
rs.getString("tytul"),
rs.getString("opis"),
rs.getString("marka"),
rs.getString("model"),
rs.getString("generacja"),
rs.getString("kolor"),
rs.getInt("rocznik"),
rs.getInt("przebieg"),
silnik
);
}
void doczytajSprzedawce(Ogloszenie ogl) throws BladBazyDanych, NieznanyRekord {
SprzedawcaDAO sDao = db.newSprzedawcaDAO();
Sprzedawca sprzedawca = sDao.byId(ogl.getIdSprzedawcy());
ogl.setSprzedawca(sprzedawca);
}
private void uzupelnijDate(OgloszenieSamochodowe ogl) {
if(ogl.getDataWystawienia() == null) {
ogl.setDataWystawienia(LocalDateTime.now());
}
}
}
package ogloszenia.baza.sqlite;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.Adres;
import ogloszenia.model.Sprzedawca;
public class SprzedawcaDAO {
private DostepDoBazySqlite db;
SprzedawcaDAO(DostepDoBazySqlite db) {
this.db = db;
}
public DostepDoBazySqlite getDBHandler() {
return db;
}
public List<Integer> listaIdSprzedawcow() throws BladBazyDanych {
final String sql = "SELECT id_sprzedawcy FROM sprzedawcy";
List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
lista.add(rs.getInt(1));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("listaIdSprzedawcow: "+e.getMessage(), e);
}
return lista;
}
public Sprzedawca byId(int idSprzedawcy) throws BladBazyDanych, NieznanyRekord {
final String sql = "SELECT * FROM sprzedawcy WHERE id_sprzedawcy = ?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) {
if(rs.next()) {
return sprzedawcaZResultSet(rs);
} else {
throw new NieznanyRekord("Nieznany sprzedawca " + idSprzedawcy);
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Sprzedawca byId: "+e.getMessage(), e);
}
}
public List<Sprzedawca> readAll() throws BladBazyDanych {
final String sql = "SELECT * FROM sprzedawcy";
List<Sprzedawca> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
lista.add(sprzedawcaZResultSet(rs));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Sprzedawca readAll: "+e.getMessage(), e);
}
return lista;
}
private Sprzedawca sprzedawcaZResultSet(ResultSet rs) throws SQLException {
Adres adres = new Adres(rs.getString("ulica"),
rs.getString("kod_pocztowy"), rs.getString("miasto"));
return new Sprzedawca(
rs.getInt("id_sprzedawcy"), rs.getString("nazwa"),
adres, rs.getString("telefon"), rs.getString("email")
);
}
}
package ogloszenia.baza.sqlite;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.OgloszenieSamochodowe;
public class TestInsertUpdate {
public static void main(String[] args) {
try(DostepDoBazySqlite baza = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = baza.newOgloszenieDAO();
OgloszenieSamochodowe auto = dao.byId(2);
auto.setIdOgloszenia(103);
auto.setOpis("Opis złotego Matiza");
dao.save(auto);
System.out.println("Gotowe");
} catch (BladBazyDanych | NieznanyRekord e) {
e.printStackTrace();
}
}
}
package ogloszenia.baza.sqlite;
import java.time.LocalTime;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.OgloszenieSamochodowe;
public class TestInsertWstawNowy {
public static void main(String[] args) {
try(DostepDoBazySqlite baza = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = baza.newOgloszenieDAO();
OgloszenieSamochodowe auto = dao.byId(3);
auto.setIdOgloszenia(null);
auto.setOpis("Nowy z godziny " + LocalTime.now());
dao.save(auto);
} catch (BladBazyDanych e) {
e.printStackTrace();
} catch (NieznanyRekord e) {
e.printStackTrace();
}
}
}
package ogloszenia.baza.sqlite;
import java.util.List;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.OgloszenieSamochodowe;
public class TestOdczytOgloszen {
public static void main(String[] args) {
try (DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
List<OgloszenieSamochodowe> lista = dao.readAllFull();
for (OgloszenieSamochodowe ogloszenie : lista) {
System.out.println(ogloszenie);
}
} catch (BladBazyDanych e) {
e.printStackTrace();
}
}
}
package ogloszenia.baza.sqlite;
import java.util.List;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.Sprzedawca;
public class TestOdczytSprzedawcow {
public static void main(String[] args) {
try (DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite("ogloszenia.db")) {
SprzedawcaDAO dao = db.newSprzedawcaDAO();
List<Sprzedawca> lista = dao.readAll();
for (Sprzedawca sprzedawca : lista) {
System.out.println(sprzedawca);
}
} catch (BladBazyDanych e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-ear</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding/<project>=UTF-8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-ear-1.0">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/ear-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/application" tag="defaultRootSource"/>
<dependent-module archiveName="ogloszenia-soap-1.0.war" deploy-path="/" handle="module:/resource/ogloszenia-soap/ogloszenia-soap">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="ogloszenia-rest-1.0.war" deploy-path="/" handle="module:/resource/ogloszenia-rest/ogloszenia-rest">
<dependency-type>uses</dependency-type>
</dependent-module>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="jst.ear" version="1.3"/>
</faceted-project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-ear</artifactId>
<packaging>ear</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<generateApplicationXml>true</generateApplicationXml>
<skinnyWars>true</skinnyWars>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-soap</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-rest</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-klient-rest</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-klient-rest</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.5.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.5.0.Final</version>
</dependency>
</dependencies>
</project>
package ogloszenia.klient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
public class Klient1_Generyczny {
public static void main(String[] args) {
System.out.println("Przygotowania...");
Client client = ClientBuilder.newClient();
WebTarget targetRoot = client.target(Ustawienia.ADRES);
WebTarget targetOgloszenia = targetRoot.path("/ogloszenia");
Invocation invocation = targetOgloszenia.request().buildGet();
System.out.println("Wysyłam zapytanie...");
Response response = invocation.invoke();
System.out.println("Przyszła odpowiedź");
System.out.println("status: " + response.getStatus());
System.out.println("media type: " + response.getMediaType());
String dane = response.readEntity(String.class);
System.out.println("Treść odpowiedzi:");
System.out.println("------------");
System.out.println(dane);
System.out.println("------------");
System.out.println("KONIEC");
// Nie można po raz kolejny wywołać readEntity - dane są odczytywane jednorazowo
// String dane2 = response.readEntity(String.class);
// System.out.println(dane2);
}
}
package ogloszenia.klient;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
public class Klient2_Generyczny {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Podaj numer ogłoszenia");
int numer = sc.nextInt();
System.out.println("Pobieram zdjęcie nr " + numer + "...");
Client client = ClientBuilder.newClient();
WebTarget targetRoot = client.target(Ustawienia.ADRES);
WebTarget targetFoto = targetRoot.path("/ogloszenia/{id}/foto");
Invocation invocation = targetFoto.resolveTemplate("id", numer)
.request().buildGet();
Response response = invocation.invoke();
int status = response.getStatus();
System.out.println("Przyszła odpowiedź");
System.out.println("status: " + status);
System.out.println("media type: " + response.getMediaType());
if(status == 200) {
InputStream stream = response.readEntity(InputStream.class);
try {
Files.copy(stream, Paths.get("foto" + numer + ".jpg"),
StandardCopyOption.REPLACE_EXISTING);
System.out.println("Gotowe, plik zapisany");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package ogloszenia.klient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
public class Klient3_JAXB {
public static void main(String[] args) {
System.out.println("Przygotowania...");
Client client = ClientBuilder.newClient();
WebTarget targetRoot = client.target(Ustawienia.ADRES);
WebTarget targetOgloszenia = targetRoot.path("/ogloszenia");
Invocation invocation = targetOgloszenia.request().buildGet();
System.out.println("Wysyłam zapytanie...");
Response response = invocation.invoke();
System.out.println("Przyszła odpowiedź");
System.out.println("status: " + response.getStatus());
System.out.println("media type: " + response.getMediaType());
ListaOgloszen lista = response.readEntity(ListaOgloszen.class);
System.out.println("Mam listę ogłoszeń:");
for(OgloszenieSamochodowe ogloszenie : lista.ogloszenia) {
System.out.println(ogloszenie.getMarka() + " " + ogloszenie.getModel() + " " + ogloszenie.getCena());
}
}
}
package ogloszenia.klient;
import java.math.BigDecimal;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.model.Paliwo;
import ogloszenia.model.Silnik;
public class Klient4_JAXB_Post {
public static void main(String[] args) {
OgloszenieSamochodowe noweOgloszenie = new OgloszenieSamochodowe(null, 2, null, new BigDecimal("33000.33"), "Nowe BMW", "Sprzedam fajne BMW", "BMW", "X5", null, "czarny", 2010, 99900, new Silnik(200f, 3.0f, Paliwo.BENZYNA));
System.out.println("Ogloszenie do zapisania: " + noweOgloszenie);
Client client = ClientBuilder.newClient();
WebTarget targetRoot = client.target(Ustawienia.ADRES);
WebTarget targetOgloszenia = targetRoot.path("/ogloszenia");
Entity<OgloszenieSamochodowe> entity = Entity.entity(noweOgloszenie, "application/xml");
Invocation invocation = targetOgloszenia.request().buildPost(entity);
System.out.println("Wysyłam zapytanie...");
Response response = invocation.invoke();
System.out.println("Przyszła odpowiedź");
System.out.println("status: " + response.getStatus());
System.out.println("media type: " + response.getMediaType());
OgloszenieSamochodowe zapisane = response.readEntity(OgloszenieSamochodowe.class);
System.out.println("Zapisano ogloszenie pod numerem: " + zapisane.getIdOgloszenia());
System.out.println(zapisane);
}
}
package ogloszenia.klient;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.rest.IOgloszenia;
public class Klient5_Proxy {
public static void main(String[] args) {
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(Ustawienia.ADRES);
IOgloszenia proxy = target.proxy(IOgloszenia.class);
System.out.println("Wysyłam zapytanie");
try {
ListaOgloszen lista = proxy.wszystkieOgloszenia();
System.out.println("Odczytałem " + lista.ogloszenia.size() + " ogłoszeń:");
for(OgloszenieSamochodowe ogl : lista.ogloszenia) {
System.out.println(ogl);
}
} catch (BladBazyDanych e) {
// wyjątki nie działają w sposób przezroczysty
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.math.BigDecimal;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import ogloszenia.exn.BladDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.rest.IOgloszenia;
public class Klient3_Proxy_CXF {
private Client klient;
private WebTarget root;
public static void main(String[] args) {
new Klient3_Proxy_CXF().run();
}
public void run() {
SerwisOgloszeniowy proxy = JAXRSClientFactory.
create(Ustawienia.ADRES_USLUGI, IOgloszenia.class);
try {
OgloszenieSamochodowe ogl2 = proxy.ogloszeniePoId(2);
System.out.println(ogl2);
System.out.println(ogl2.getCena());
ogl2.setCena(new BigDecimal(66000));
proxy.zapiszOgloszenie(2, ogl2);
System.out.println("done");
} catch (BladDanych | NieznanyRekord e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.math.BigDecimal;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.model.Paliwo;
import ogloszenia.model.Silnik;
import ogloszenia.rest.IOgloszenia;
public class Klient6_Proxy_Post {
public static void main(String[] args) {
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(Ustawienia.ADRES);
IOgloszenia proxy = target.proxy(IOgloszenia.class);
OgloszenieSamochodowe noweOgloszenie = new OgloszenieSamochodowe(null, 2, null, new BigDecimal("33000.33"), "Nowe BMW", "Sprzedam fajne BMW", "BMW", "X5", null, "czarny", 2010, 99900, new Silnik(200f, 3.0f, Paliwo.BENZYNA));
System.out.println("Ogłoszenie do zapisania: " + noweOgloszenie);
try {
OgloszenieSamochodowe zapisane = proxy.zapiszOgloszenie(noweOgloszenie);
System.out.println("Zapisano ogłoszenie");
System.out.println(zapisane);
} catch (BladBazyDanych e) {
// wyjątki nie działają w sposób przezroczysty
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.math.BigDecimal;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.rest.IOgloszenia;
public class Klient7_Proxy_ZmianaCeny {
public static void main(String[] args) {
//int id = 2;
int id = 24;
BigDecimal zmiana = new BigDecimal("250.00");
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(Ustawienia.ADRES);
IOgloszenia proxy = target.proxy(IOgloszenia.class);
System.out.println("Wysyłam zapytania...");
try {
BigDecimal staraCena = proxy.getCena(id);
System.out.println("Stara cena: " + staraCena);
BigDecimal nowaCena = staraCena.add(zmiana);
proxy.setCena(id, nowaCena);
BigDecimal jeszczeNowszaCena = proxy.getCena(id);
System.out.println("Cena po zapisie: " + jeszczeNowszaCena);
} catch (BladBazyDanych e) {
// wyjątki nie działają w sposób przezroczysty - po stronie klienta nie zobaczę wyjątków z własnej aplikacji serwerowej
e.printStackTrace();
} catch (NieznanyRekord e) {
System.out.println("wyjątek NIEZNANY REKORD");
e.printStackTrace();
} catch(NotFoundException e) {
// ale w zależności od kodu powrotu HTTP pojawią się wyspecjalizowane wyjątki JAX-RS
System.out.println("404 NOT FOUND");
Response response = e.getResponse();
String tresc = response.readEntity(String.class);
System.out.println(tresc);
e.printStackTrace();
} catch(InternalServerErrorException e) {
System.out.println("500 INTERNAL SERVER ERROR");
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
public class Ustawienia {
public static final String ADRES = "http://localhost:8080/ogloszenia-rest";
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-klient-soap</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-klient-soap</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
package ogloszenia.klient;
import java.io.File;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service.Mode;
import ogloszenia.soap.Nazwy;
/* W tym przykładzie zapytanie jest tworzone z pliku XML, a odpowiedź w formie XML wypisywana na konsolę.
* Tryb PAYLOAD oznacza, że podaje i odbiera się tylko zawartość soap:Body, a całe SOAP-owe opakowanie jest dodawane automatycznie.
*/
public class KlientDispatch1 {
public static void main(String[] args) {
SerwisOgloszeniowyService serwis = new SerwisOgloszeniowyService();
Dispatch<Source> dispatch = serwis.createDispatch(Nazwy.PORT_QNAME, Source.class, Mode.PAYLOAD);
StreamSource src = new StreamSource(new File("zapytanie1.xml"));
Source result = dispatch.invoke(src);
wypiszXmlZSource(result);
System.out.println("\n\nGotowe");
}
static void wypiszXmlZSource(Source xml) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
StreamResult res = new StreamResult(System.out);
t.transform(xml, res);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;
/* W tym przykładzie w ogóle nie używamy wygenerowanych klas, to można zrobić "zawsze", nawet bez procedury wsimport.
* Treść analogiczna do KlientDispatch1.
*/
public class KlientDispatch2 {
private static final QName PORT_NAME = new QName("http://soap.ogloszenia/", "SerwisOgloszeniowyPort");
private static final QName SERVICE_NAME = new QName("http://soap.ogloszenia/", "SerwisOgloszeniowyService");
private static final String WSDL = "http://localhost:8080/09-OgloszeniaSoap/SerwisOgloszeniowy?wsdl";
public static void main(String[] args) {
try {
final URL WSDL_URL = new URL(WSDL);
Service serwis = Service.create(WSDL_URL, SERVICE_NAME);
Dispatch<Source> dispatch = serwis.createDispatch(PORT_NAME, Source.class, Mode.PAYLOAD);
StreamSource src = new StreamSource(new File("zapytanie1.xml"));
Source result = dispatch.invoke(src);
wypiszXmlZSource(result);
System.out.println("\n\nGotowe");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
static void wypiszXmlZSource(Source xml) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
StreamResult res = new StreamResult(System.out);
t.transform(xml, res);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;
/* Zapytanie pobierane jest z pliku XML, ale odpowiedź jest przetwarzana przez arkusz XSLT, aby uzyskać HTML.
* To tylko przykład pokazujący, dlaczego obsługa danych XML-owych na poziomie Source może mieć sens.
* W tym programie też nie są używane wygenerowane klasy klienta.
*/
public class KlientDispatch3 {
private static final QName PORT_NAME = new QName("http://soap.ogloszenia/", "SerwisOgloszeniowyPort");
private static final QName SERVICE_NAME = new QName("http://soap.ogloszenia/", "SerwisOgloszeniowyService");
private static final String WSDL = "http://localhost:8080/09-OgloszeniaSoap/SerwisOgloszeniowy?wsdl";
public static void main(String[] args) {
try {
final URL WSDL_URL = new URL(WSDL);
Service serwis = Service.create(WSDL_URL, SERVICE_NAME);
Dispatch<Source> dispatch = serwis.createDispatch(PORT_NAME, Source.class, Mode.PAYLOAD);
StreamSource src = new StreamSource(new File("zapytanie1.xml"));
Source result = dispatch.invoke(src);
przeksztalcXml(result);
System.out.println("\n\nGotowe");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
static void przeksztalcXml(Source xml) {
try {
StreamSource xsl = new StreamSource(new File("arkusz.xsl"));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer(xsl);
StreamResult res = new StreamResult(new File("wynik3.html"));
t.transform(xml, res);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.util.List;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.soap.ISerwisOgloszeniowy;
public class KlientProxy1 {
public static void main(String[] args) {
SerwisOgloszeniowyService service = new SerwisOgloszeniowyService();
ISerwisOgloszeniowy port = service.getSerwisOgloszeniowyPort();
try {
List<OgloszenieSamochodowe> wszystkieOgloszenia = port.wszystkieOgloszenia();
System.out.println("Mam " + wszystkieOgloszenia.size() + " ogłoszeń");
for (OgloszenieSamochodowe ogl : wszystkieOgloszenia) {
System.out.println(ogl.getTytul() + " " + ogl.getCena());
}
} catch (BladBazyDanych e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import ogloszenia.exn.BladAplikacji;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.soap.ISerwisOgloszeniowy;
public class KlientProxy2 {
public static void main(String[] args) {
int id = Integer.parseInt(JOptionPane.showInputDialog("Podaj ID ogłoszenia"));
SerwisOgloszeniowyService service = new SerwisOgloszeniowyService();
ISerwisOgloszeniowy port = service.getSerwisOgloszeniowyPort();
try {
OgloszenieSamochodowe ogloszenie = port.jednoOgloszenie(id);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel label = new JLabel(ogloszenie.getMarka() + " " + ogloszenie.getModel() + " rocznik " + ogloszenie.getRocznik());
label.setFont(new Font("Arial", Font.BOLD, 28));
label.setForeground(Color.BLUE);
panel.add(label);
panel.add(Box.createVerticalStrut(10));
label = new JLabel(ogloszenie.getTytul());
label.setFont(new Font("Arial", Font.BOLD, 28));
panel.add(label);
panel.add(Box.createVerticalStrut(10));
label = new JLabel(ogloszenie.getOpis());
label.setFont(new Font("Arial", Font.PLAIN, 22));
label.setForeground(Color.RED);
panel.add(label);
panel.add(Box.createVerticalStrut(10));
try {
byte[] foto = port.foto(id);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(foto));
ImageIcon icon = new ImageIcon(image);
panel.add(new JLabel(icon));
} catch (Exception e) {
System.err.println("Nie da rady wczytać zdjęcia, " + e);
}
JOptionPane.showMessageDialog(null, panel);
} catch (BladAplikacji e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
import ogloszenia.soap.ISerwisOgloszeniowy;
import ogloszenia.soap.Nazwy;
/* PC: Jak klasa wygenerowana w projekcie 11.
* Znając najważniejsze cechy: adnotację @WebServiceClient i dziedziczenie z klasy Service,
* łatwo możemy takie coś utworzyć od zera.
* Zostawiam tylko minimum potrzebne tu do działania.
*/
@WebServiceClient(targetNamespace = Nazwy.NAMESPACE,
name = Nazwy.SERVICE_NAME,
wsdlLocation = Nazwy.WSDL)
public class SerwisOgloszeniowyService extends Service {
// Zostawiam tylko konstruktor przyjmujący domyślne ustawienia
public SerwisOgloszeniowyService() {
super(Nazwy.WSDL_URL, Nazwy.SERVICE_QNAME);
}
@WebEndpoint(name = Nazwy.PORT_NAME)
public ISerwisOgloszeniowy getSerwisOgloszeniowyPort() {
return super.getPort(Nazwy.PORT_QNAME, ISerwisOgloszeniowy.class);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-model</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-model">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="1.8"/>
<installed facet="jst.utility" version="1.0"/>
</faceted-project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-model</artifactId>
</project>
\ No newline at end of file
package ogloszenia.exn;
public class BladAplikacji extends Exception {
public BladAplikacji() {
super();
}
public BladAplikacji(String message, Throwable cause) {
super(message, cause);
}
public BladAplikacji(String message) {
super(message);
}
public BladAplikacji(Throwable cause) {
super(cause);
}
}
package ogloszenia.exn;
public class BladBazyDanych extends BladAplikacji {
public BladBazyDanych() {
super();
}
public BladBazyDanych(String message, Throwable cause) {
super(message, cause);
}
public BladBazyDanych(String message) {
super(message);
}
public BladBazyDanych(Throwable cause) {
super(cause);
}
}
package ogloszenia.exn;
public class NieznanyRekord extends BladAplikacji {
public NieznanyRekord() {
super();
}
public NieznanyRekord(String message) {
super(message);
}
}
package ogloszenia.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Adres {
private String ulica;
@XmlElement(name="kod-pocztowy")
private String kodPocztowy;
private String miasto;
public Adres() {
}
public Adres(String ulica, String kodPocztowy, String miasto) {
this.ulica = ulica;
this.kodPocztowy = kodPocztowy;
this.miasto = miasto;
}
public String getUlica() {
return ulica;
}
public String getKodPocztowy() {
return kodPocztowy;
}
public String getMiasto() {
return miasto;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((kodPocztowy == null) ? 0 : kodPocztowy.hashCode());
result = prime * result + ((miasto == null) ? 0 : miasto.hashCode());
result = prime * result + ((ulica == null) ? 0 : ulica.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Adres other = (Adres) obj;
if (kodPocztowy == null) {
if (other.kodPocztowy != null)
return false;
} else if (!kodPocztowy.equals(other.kodPocztowy))
return false;
if (miasto == null) {
if (other.miasto != null)
return false;
} else if (!miasto.equals(other.miasto))
return false;
if (ulica == null) {
if (other.ulica != null)
return false;
} else if (!ulica.equals(other.ulica))
return false;
return true;
}
@Override
public String toString() {
return String.format("%s, %s %s", ulica, kodPocztowy, miasto);
}
}
package ogloszenia.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="ogloszenia")
public class ListaOgloszen {
@XmlElement(name="ogloszenie")
public List<OgloszenieSamochodowe> ogloszenia;
public static ListaOgloszen nowa(List<OgloszenieSamochodowe> ogloszenia) {
ListaOgloszen lista = new ListaOgloszen();
lista.ogloszenia = ogloszenia;
return lista;
}
}
package ogloszenia.model;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import ogloszenia.xml.AdapterDaty;
public class Ogloszenie {
@XmlAttribute(name="id")
private Integer idOgloszenia;
@XmlAttribute(name="id-sprzedawcy")
// @XmlTransient - to by spowodowalo niewypisywanie tej informacji w ogole
private Integer idSprzedawcy;
@XmlElement(name="wystawione")
@XmlJavaTypeAdapter(AdapterDaty.class)
private LocalDateTime dataWystawienia;
private BigDecimal cena;
private String tytul;
private String opis;
private Sprzedawca sprzedawca;
public Ogloszenie() {
}
public Ogloszenie(Integer idOgloszenia, Integer idSprzedawcy, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia;
this.idSprzedawcy = idSprzedawcy;
this.dataWystawienia = dataWystawienia;
this.cena = cena;
this.tytul = tytul;
this.opis = opis;
}
/** Pozostawia pole sprzedawca równe null. */
public Ogloszenie(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia;
this.dataWystawienia = dataWystawienia;
this.cena = cena;
this.tytul = tytul;
this.opis = opis;
this.idSprzedawcy = null;
}
/** Wartość idSprzedawcy pobiera z obiektu sprzedawca. */
public Ogloszenie(Integer idOgloszenia, Sprzedawca sprzedawca, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia;
this.dataWystawienia = dataWystawienia;
this.cena = cena;
this.tytul = tytul;
this.opis = opis;
this.sprzedawca = sprzedawca;
if(sprzedawca != null) {
this.idSprzedawcy = sprzedawca.getIdSprzedawcy();
}
}
public Integer getIdOgloszenia() {
return idOgloszenia;
}
public void setIdOgloszenia(Integer noweId) {
this.idOgloszenia = noweId;
}
public String getTytul() {
return tytul;
}
public BigDecimal getCena() {
return cena;
}
public Integer getIdSprzedawcy() {
return idSprzedawcy;
}
public Sprzedawca getSprzedawca() {
return sprzedawca;
}
public void setSprzedawca(Sprzedawca sprzedawca) {
this.sprzedawca = sprzedawca;
}
public String getOpis() {
return opis;
}
public void setOpis(String opis) {
this.opis = opis;
}
public void setCena(BigDecimal cena) {
this.cena = cena;
}
public LocalDateTime getDataWystawienia() {
return dataWystawienia;
}
public void setDataWystawienia(LocalDateTime dataWystawienia) {
this.dataWystawienia = dataWystawienia;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cena == null) ? 0 : cena.hashCode());
result = prime * result + ((dataWystawienia == null) ? 0 : dataWystawienia.hashCode());
result = prime * result + ((idOgloszenia == null) ? 0 : idOgloszenia.hashCode());
result = prime * result + ((idSprzedawcy == null) ? 0 : idSprzedawcy.hashCode());
result = prime * result + ((opis == null) ? 0 : opis.hashCode());
result = prime * result + ((sprzedawca == null) ? 0 : sprzedawca.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ogloszenie other = (Ogloszenie) obj;
if (cena == null) {
if (other.cena != null)
return false;
} else if (!cena.equals(other.cena))
return false;
if (dataWystawienia == null) {
if (other.dataWystawienia != null)
return false;
} else if (!dataWystawienia.equals(other.dataWystawienia))
return false;
if (idOgloszenia == null) {
if (other.idOgloszenia != null)
return false;
} else if (!idOgloszenia.equals(other.idOgloszenia))
return false;
if (idSprzedawcy == null) {
if (other.idSprzedawcy != null)
return false;
} else if (!idSprzedawcy.equals(other.idSprzedawcy))
return false;
if (opis == null) {
if (other.opis != null)
return false;
} else if (!opis.equals(other.opis))
return false;
if (sprzedawca == null) {
if (other.sprzedawca != null)
return false;
} else if (!sprzedawca.equals(other.sprzedawca))
return false;
return true;
}
@Override
public String toString() {
return String.format("Ogłoszenie #%d: sprzedawca #%s, cena: %.2f\n %s",
idOgloszenia, idSprzedawcy, cena, tytul);
}
}
package ogloszenia.model;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="ogloszenie")
public class OgloszenieSamochodowe extends Ogloszenie {
private String marka;
private String model;
private String generacja;
@XmlElement(name="rok-produkcji")
public int rocznik;
private int przebieg;
private String kolor;
private Silnik silnik;
public OgloszenieSamochodowe() {
}
public OgloszenieSamochodowe(Integer idOgloszenia, Integer idSprzedawcy,
LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis,
String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) {
super(idOgloszenia, idSprzedawcy, dataWystawienia, cena, tytul, opis);
this.marka = marka;
this.model = model;
this.generacja = generacja;
this.kolor = kolor;
this.rocznik = rocznik;
this.przebieg = przebieg;
this.silnik = silnik;
}
/** Pozostawia pole sprzedawca równe null. */
public OgloszenieSamochodowe(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis,
String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) {
super(idOgloszenia, dataWystawienia, cena, tytul, opis);
this.marka = marka;
this.model = model;
this.generacja = generacja;
this.kolor = kolor;
this.rocznik = rocznik;
this.przebieg = przebieg;
this.silnik = silnik;
}
/** Wartość idSprzedawcy pobiera z obiektu sprzedawca. */
public OgloszenieSamochodowe(Integer idOgloszenia, Sprzedawca sprzedawca,
LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis,
String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) {
super(idOgloszenia, sprzedawca, dataWystawienia, cena, tytul, opis);
this.marka = marka;
this.model = model;
this.generacja = generacja;
this.kolor = kolor;
this.rocznik = rocznik;
this.przebieg = przebieg;
this.silnik = silnik;
}
public String getMarka() {
return marka.toUpperCase();
}
public void setMarka(String marka) {
this.marka = marka;
}
public String getModel() {
return model;
}
public String getGeneracja() {
return generacja;
}
public String getKolor() {
return kolor;
}
public int getRocznik() {
return rocznik;
}
public int getPrzebieg() {
return przebieg;
}
public Silnik getSilnik() {
return silnik;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((generacja == null) ? 0 : generacja.hashCode());
result = prime * result + ((kolor == null) ? 0 : kolor.hashCode());
result = prime * result + ((marka == null) ? 0 : marka.hashCode());
result = prime * result + ((model == null) ? 0 : model.hashCode());
result = prime * result + rocznik;
result = prime * result + ((silnik == null) ? 0 : silnik.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
OgloszenieSamochodowe other = (OgloszenieSamochodowe) obj;
if (generacja == null) {
if (other.generacja != null)
return false;
} else if (!generacja.equals(other.generacja))
return false;
if (kolor == null) {
if (other.kolor != null)
return false;
} else if (!kolor.equals(other.kolor))
return false;
if (marka == null) {
if (other.marka != null)
return false;
} else if (!marka.equals(other.marka))
return false;
if (model == null) {
if (other.model != null)
return false;
} else if (!model.equals(other.model))
return false;
if (rocznik != other.rocznik)
return false;
if (silnik == null) {
if (other.silnik != null)
return false;
} else if (!silnik.equals(other.silnik))
return false;
return true;
}
@Override
public String toString() {
return super.toString() + "\n" +
String.format(" Samochód: %s %s %s (%s), rok %d, silnik: %s",
marka, model, generacja, kolor, rocznik, silnik);
}
}
package ogloszenia.model;
import javax.xml.bind.annotation.XmlEnumValue;
public enum Paliwo {
@XmlEnumValue("benzyna") BENZYNA,
@XmlEnumValue("diesel") OLEJ,
@XmlEnumValue("lpg") GAZ;
}
package ogloszenia.model;
public class Silnik {
private Float moc;
private Float pojemnosc;
private Paliwo paliwo;
public Silnik() {
}
public Silnik(Float moc, Float pojemnosc, Paliwo paliwo) {
this.moc = moc;
this.pojemnosc = pojemnosc;
this.paliwo = paliwo;
}
public Float getMoc() {
return moc;
}
public Float getPojemnosc() {
return pojemnosc;
}
public Paliwo getPaliwo() {
return paliwo;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((moc == null) ? 0 : moc.hashCode());
result = prime * result + ((paliwo == null) ? 0 : paliwo.hashCode());
result = prime * result + ((pojemnosc == null) ? 0 : pojemnosc.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Silnik other = (Silnik) obj;
if (moc == null) {
if (other.moc != null)
return false;
} else if (!moc.equals(other.moc))
return false;
if (paliwo != other.paliwo)
return false;
if (pojemnosc == null) {
if (other.pojemnosc != null)
return false;
} else if (!pojemnosc.equals(other.pojemnosc))
return false;
return true;
}
@Override
public String toString() {
return String.format("%.1f %s, %.0f KM", pojemnosc, paliwo, moc);
}
}
package ogloszenia.model;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Sprzedawca {
@XmlAttribute(name="id")
private Integer idSprzedawcy;
private String nazwa;
private Adres adres;
private String telefon;
private String email;
public Sprzedawca() {
}
public Sprzedawca(Integer idSprzedawcy, String nazwa, Adres adres, String telefon, String email) {
this.idSprzedawcy = idSprzedawcy;
this.nazwa = nazwa;
this.adres = adres;
this.telefon = telefon;
this.email = email;
}
public Integer getIdSprzedawcy() {
return idSprzedawcy;
}
public String getNazwa() {
return nazwa;
}
public Adres getAdres() {
return adres;
}
public String getTelefon() {
return telefon;
}
public String getEmail() {
return email;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((adres == null) ? 0 : adres.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((idSprzedawcy == null) ? 0 : idSprzedawcy.hashCode());
result = prime * result + ((nazwa == null) ? 0 : nazwa.hashCode());
result = prime * result + ((telefon == null) ? 0 : telefon.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Sprzedawca other = (Sprzedawca) obj;
if (adres == null) {
if (other.adres != null)
return false;
} else if (!adres.equals(other.adres))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (idSprzedawcy == null) {
if (other.idSprzedawcy != null)
return false;
} else if (!idSprzedawcy.equals(other.idSprzedawcy))
return false;
if (nazwa == null) {
if (other.nazwa != null)
return false;
} else if (!nazwa.equals(other.nazwa))
return false;
if (telefon == null) {
if (other.telefon != null)
return false;
} else if (!telefon.equals(other.telefon))
return false;
return true;
}
@Override
public String toString() {
return String.format("Sprzedawca #%d: %s adres: %s, tel.: %s, email: %s",
idSprzedawcy, nazwa, adres, telefon, email);
}
}
@XmlAccessorType(XmlAccessType.FIELD)
package ogloszenia.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
package ogloszenia.xml;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class AdapterDaty extends XmlAdapter<String, LocalDateTime> {
private static final DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Override
public LocalDateTime unmarshal(String s) {
return LocalDateTime.parse(s, FORMAT);
//return LocalDateTime.parse(s); // domyslny format
}
@Override
public String marshal(LocalDateTime d) {
return FORMAT.format(d);
//return d.toString(); // domyslny format
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-rest</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.jboss.tools.ws.jaxrs.metamodelBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.jboss.tools.ws.jaxrs.nature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/webapp"/>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="target/m2e-wtp/web-resources"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-rest-1.0">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="ogloszenia-api-1.0.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ogloszenia-api/ogloszenia-api">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="ogloszenia-model-1.0.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ogloszenia-model/ogloszenia-model">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="ogloszenia-baza-1.0.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ogloszenia-baza/ogloszenia-baza">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="ogloszenia-rest"/>
<property name="java-output-path" value="/ogloszenia-rest/target/classes"/>
</wb-module>
</project-modules>
<root>
<facet id="jst.jsf">
<node name="libprov">
<attribute name="provider-id" value="jsf-no-op-library-provider"/>
</node>
</facet>
<facet id="jst.jaxrs">
<node name="libprov">
<attribute name="provider-id" value="jaxrs-no-op-library-provider"/>
</node>
</facet>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.8"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="jst.web" version="3.1"/>
<installed facet="jst.jsf" version="2.2"/>
<installed facet="jst.jaxrs" version="2.0"/>
</faceted-project>
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-rest</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-baza</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
package ogloszenia.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/")
public class Aplikacja extends Application {
}
package ogloszenia.rest;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import ogloszenia.exn.BladAplikacji;
@Provider
public class BladAplikacjiMapper implements ExceptionMapper<BladAplikacji> {
@Override
public Response toResponse(BladAplikacji e) {
final String html = "<html><body>"
+ "<h2>500 Server Error</h2>"
+ "<p style='color:red'>" + e.getMessage() + "</p>"
+ "</body></html>";
return Response.serverError()
.type("text/html")
.entity(html)
.build();
}
}
package ogloszenia.rest;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import ogloszenia.exn.NieznanyRekord;
@Provider
public class NieznanyRekordMapper implements ExceptionMapper<NieznanyRekord> {
@Override
public Response toResponse(NieznanyRekord e) {
final String html = "<html><body>"
+ "<h2>404 Not Found</h2>"
+ "<p style='color:#BB0044'>" + e.getMessage() + "</p>"
+ "</body></html>";
return Response.status(404)
.type("text/html")
.entity(html)
.build();
}
}
package ogloszenia.rest;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import ogloszenia.Ustawienia;
import ogloszenia.baza.sqlite.DostepDoBazySqlite;
import ogloszenia.baza.sqlite.OgloszenieDAO;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
public class ROgloszenia implements IOgloszenia {
public ListaOgloszen wszystkieOgloszenia() throws BladBazyDanych {
try(DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
ListaOgloszen lista = new ListaOgloszen();
lista.ogloszenia = dao.readAllFull();
return lista;
}
}
public OgloszenieSamochodowe zapiszOgloszenie(OgloszenieSamochodowe ogloszenie) throws BladBazyDanych {
try(DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
dao.save(ogloszenie);
return ogloszenie;
}
}
public OgloszenieSamochodowe jednoOgloszenie(int idOgloszenia) throws BladBazyDanych, NieznanyRekord {
try(DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
return dao.byIdFull(idOgloszenia);
}
}
public BigDecimal getCena(int idOgloszenia) throws BladBazyDanych, NieznanyRekord {
try(DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
OgloszenieSamochodowe ogloszenie = dao.byId(idOgloszenia);
return ogloszenie.getCena();
}
}
public void setCena(int idOgloszenia,
BigDecimal nowaCena) throws BladBazyDanych, NieznanyRekord {
try(DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
dao.zmienCene(idOgloszenia, nowaCena);
}
}
public void usun(int idOgloszenia) throws BladBazyDanych, NieznanyRekord {
try(DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
dao.delete(idOgloszenia);
}
}
public byte[] foto(int idOgloszenia) throws NieznanyRekord {
String plik = idOgloszenia + ".jpg";
try {
return Files.readAllBytes(Paths.get(Ustawienia.PHOTOS_PATH, plik));
} catch (IOException e) {
throw new NieznanyRekord("Brak zdjęcia nr " + idOgloszenia);
}
}
}
package ogloszenia.rest;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import ogloszenia.baza.sqlite.DostepDoBazySqlite;
import ogloszenia.baza.sqlite.SprzedawcaDAO;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.Adres;
import ogloszenia.model.Sprzedawca;
// /sprzedawcy - ma zwracać listę wszystkich sprzedawców
// /sprzedawcy/1 - ma zwracać dane jednego sprzedawcy
// /sprzedawcy/1/adres - sam adres sprzedawcy
// @Produces można umieścić na poziomie klasy - domyślna wartość dla wszystkich metod (metoda może zmienić)
@Path("/sprzedawcy")
@Produces("application/xml")
public class RSprzedawcy {
@GET
public List<Sprzedawca> wszyscySprzedawcy() throws BladBazyDanych {
try(DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
SprzedawcaDAO dao = db.newSprzedawcaDAO();
return dao.readAll();
}
}
@GET
@Path("/{id}")
public Sprzedawca jedenSprzedawca(
@PathParam("id") int idSprzedawcy) throws BladBazyDanych, NieznanyRekord {
try(DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
SprzedawcaDAO dao = db.newSprzedawcaDAO();
return dao.byId(idSprzedawcy);
}
}
@GET
@Path("/{id}/adres")
public Adres adresSprzedawcy(
@PathParam("id") int idSprzedawcy) throws BladBazyDanych, NieznanyRekord {
try(DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
SprzedawcaDAO dao = db.newSprzedawcaDAO();
return dao.byId(idSprzedawcy).getAdres();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-soap</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/webapp"/>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="target/m2e-wtp/web-resources"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-soap-1.0">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="ogloszenia-api-1.0.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ogloszenia-api/ogloszenia-api">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="ogloszenia-model-1.0.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ogloszenia-model/ogloszenia-model">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="ogloszenia-baza-1.0.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ogloszenia-baza/ogloszenia-baza">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="ogloszenia-soap"/>
<property name="java-output-path" value="/ogloszenia-soap/target/classes"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.web" version="2.5"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-soap</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-baza</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
package ogloszenia.soap;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import javax.jws.WebService;
import ogloszenia.baza.sqlite.DostepDoBazySqlite;
import ogloszenia.baza.sqlite.OgloszenieDAO;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.OgloszenieSamochodowe;
@WebService(endpointInterface="ogloszenia.soap.ISerwisOgloszeniowy",
targetNamespace=Nazwy.NAMESPACE,
portName=Nazwy.PORT_NAME,
serviceName=Nazwy.SERVICE_NAME)
public class SerwisOgloszeniowy implements ISerwisOgloszeniowy {
public List<OgloszenieSamochodowe> wszystkieOgloszenia() throws BladBazyDanych {
try (DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
return dao.readAllFull();
}
}
public OgloszenieSamochodowe jednoOgloszenie(int idOgloszenia)
throws BladBazyDanych, NieznanyRekord {
try (DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
return dao.byIdFull(idOgloszenia);
}
}
public void zapiszOgloszenia(OgloszenieSamochodowe ogloszenie)
throws BladBazyDanych {
try (DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
dao.save(ogloszenie);
}
}
public byte[] foto(int idOgloszenia) throws NieznanyRekord {
final String katalog = "/home/patryk/auta/foto";
final String plik = idOgloszenia + ".jpg";
try {
return Files.readAllBytes(Paths.get(katalog, plik));
} catch (IOException e) {
throw new NieznanyRekord("Nie umiem wczytać zdjęcia nr "+idOgloszenia);
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>ogloszenia-model</module>
<module>ogloszenia-baza</module>
<module>ogloszenia-rest</module>
<module>ogloszenia-klient-rest</module>
<module>ogloszenia-api</module>
<module>ogloszenia-soap</module>
<module>ogloszenia-klient-soap</module>
<module>ogloszenia-ear</module>
</modules>
</project>
Projekt wielomodułowy pokazujący przede wszystkim możliwości tworzenia klienta REST dla własnego serwera, ale także kilka innych możliwości.
Poza programem szkolenia dodałem tutaj też stronę serwera i klienta w wersji SOAP
także wykonane w technice współdielenia kodu Javy (interfejsu i modelu) między klientem a serwerem.
Opis wg modułów
* ogloszenia-model
Klasy modelu takie jak OgloszenieSamochodowe.
Używany przez wszystkie pozostałe moduły.
* ogloszenia-db
Implementacja dostepu do bazy danych (ostatecznie tylko SQLite).
Używany przez moduły działające na serwerze (soap i rest)
* ogloszenia-api
Definiuje interfejsy będące częścią wspólną strony serwera i strony klienta
(po zmianach: zarówno w wersji REST, jak i SOAP).
* ogloszenia-soap
Implementacja usługi w wersji SOAP, jak w projekcie 09, z tym że wydzieliłem (do modułu api)
interfejs ISerwisOgloszeniowy zawierający większość adnotacji JAX-WS.
Nie ma także handlerów.
* ogloszenia-rest
Implementacja usługi w wersji REST, podobna do projektu 15 (jedna klasa na katalog zasobów),
z tym że adnotacje JAX-RS są teraz umieszczone w interfejsach z modułu api.
* ogloszenia-ear
Projekt EAR zbierający wszystkie moduły, które mają działać na serwerze: ogloszenia-soap i ogloszenia-rest
* ogloszenia-klient-soap
Klient SOAP, różniący się tym od projektu 11, że tutaj klasy modelu oraz interfejs
SEI (ISerwisOgloszeniowy) są współdzielone między klientem a serwerem (za pośrednictwem modułu api).
Nie było etapu generowania klas na podstawie WSDL.
* ogloszenia-klient-rest
Klient REST, prezentujący kilka sposobów dostępu do serwera, m.in. podejście z obiektem proxy
- jest to niestandardowe rozszerzenie biblioteki RestEasy (JBoss).
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment