Skip to content
Browse files

optymalizacja kodu

  • Loading branch information...
1 parent 6f3d30b commit b255654c937ed2e0246c01f801288998dd0de1be Kamui committed Dec 17, 2013
View
16 .settings/org.eclipse.wst.common.project.facet.core.xml
@@ -1,8 +1,8 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<faceted-project>
- <fixed facet="wst.jsdt.web"/>
- <installed facet="java" version="1.7"/>
- <installed facet="jst.web" version="3.0"/>
- <installed facet="wst.jsdt.web" version="1.0"/>
- <installed facet="jst.jaxrs" version="1.1"/>
-</faceted-project>
+<?xml version="1.0" encoding="UTF-8"?>
+<faceted-project>
+ <fixed facet="wst.jsdt.web"/>
+ <installed facet="java" version="1.7"/>
+ <installed facet="jst.web" version="3.0"/>
+ <installed facet="wst.jsdt.web" version="1.0"/>
+ <installed facet="jst.jaxrs" version="1.1"/>
+</faceted-project>
View
1 src/main/java/Main.java
@@ -54,6 +54,7 @@ public static void main(String[] args) {
Book b3=mgr.get(1);
System.out.println(b3.getTitle());
+ uow.close();
}
}
View
6 src/main/java/database/HibUnitOfWork.java
@@ -84,4 +84,10 @@ public void commit() {
getSession().close();
}
}
+
+ @Override
+ public void close() {
+ // TODO Auto-generated method stub
+
+ }
}
View
26 src/main/java/database/MysqlUnitOfWork.java
@@ -18,7 +18,7 @@ public MysqlUnitOfWork(String url) {
super();
this.url=url;
try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
+ //Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(url);
}catch(SQLException ex)
{
@@ -50,24 +50,26 @@ public void commit() {
conn.setAutoCommit(false);
saveChanges();
conn.commit();
+ clear();
}catch(SQLException ex)
{
ex.printStackTrace();
}
- finally
- {
- try{
- if(conn!=null && !conn.isClosed())
- {
- conn.close();
- conn=null;
- }
- }
- catch(Exception ex)
+ }
+
+ public void close()
+ {
+ try{
+ if(connection!=null && !connection.isClosed())
{
- ex.printStackTrace();
+ connection.close();
+ connection=null;
}
}
+ catch(Exception ex)
+ {
+ ex.printStackTrace();
+ }
}
}
View
8 src/main/java/database/UnitOfWork.java
@@ -8,7 +8,7 @@
import model.EntityBase;
import database.unitofwork.IUnitOfWork;
import database.unitofwork.IUnitOfWorkRepository;
-@RequestScoped
+//@RequestScoped
public class UnitOfWork extends UnitOfWorkBase{
@@ -36,4 +36,10 @@ public void commit() {
deleted.clear();
}
+ @Override
+ public void close() {
+ // TODO Auto-generated method stub
+
+ }
+
}
View
4 src/main/java/database/UnitOfWorkBase.java
@@ -52,6 +52,10 @@ protected void saveChanges() {
{
deleted.get(ent).persistDeleted(ent);
}
+ }
+
+ protected void clear()
+ {
added.clear();
changed.clear();
deleted.clear();
View
2 src/main/java/database/builder/AuthorBuilder.java
@@ -5,7 +5,7 @@
import javax.enterprise.context.RequestScoped;
import model.Author;
-@RequestScoped
+//@RequestScoped
public class AuthorBuilder implements IEntityBuilder<Author>{
@Override
View
2 src/main/java/database/builder/BookBuilder.java
@@ -5,7 +5,7 @@
import javax.enterprise.context.RequestScoped;
import model.Book;
-@RequestScoped
+//@RequestScoped
public class BookBuilder implements IEntityBuilder<Book>{
@Override
View
2 src/main/java/database/managers/impl/HsqlAuthorManager.java
@@ -42,7 +42,7 @@
private Statement stmt;
- @Inject
+ //@Inject
public HsqlAuthorManager(IUnitOfWork uow, IEntityBuilder<Author> builder) {
super(uow);
View
4 src/main/java/database/managers/impl/MysqlAuthorManager.java
@@ -21,7 +21,7 @@
import database.managers.IAuthorManager;
import database.unitofwork.IUnitOfWork;
-@RequestScoped
+//@RequestScoped
public class MysqlAuthorManager extends ManagerBase<Author> implements IAuthorManager{
private IAuthorManager authorMgr;
@@ -36,7 +36,7 @@
private IEntityBuilder<Author> builder;
private Statement stmt;
- @Inject
+ //@Inject
public MysqlAuthorManager(IUnitOfWork uow, IEntityBuilder<Author> builder) {
super(uow);
View
4 src/main/java/database/managers/impl/MysqlBookManager.java
@@ -22,7 +22,7 @@
import database.managers.IBookManager;
import database.unitofwork.IUnitOfWork;
-@RequestScoped
+//@RequestScoped
public class MysqlBookManager extends MysqlManagerBase<Book> implements IBookManager{
public MysqlBookManager(MysqlUnitOfWork uow, IEntityBuilder<Book> builder) {
@@ -36,7 +36,6 @@ public void setAuthors(Book b) {
@Override
protected void setUpdateQuery(EntityBase ent) throws SQLException {
Book b = (Book)ent;
- if(update == null) update =getUpdate();
update.setString(1, b.getPublisher());
update.setString(2, b.getTitle());
update.setInt(3, b.getYear());
@@ -48,7 +47,6 @@ protected void setUpdateQuery(EntityBase ent) throws SQLException {
protected void setInsertQuery(EntityBase ent) throws SQLException {
Book b = (Book)ent;
- if(insert==null) insert = getInsert();
insert.setString(1, b.getPublisher());
insert.setString(2, b.getTitle());
insert.setInt(3, b.getYear());
View
76 src/main/java/database/managers/impl/MysqlManagerBase.java
@@ -57,8 +57,21 @@ public MysqlManagerBase(MysqlUnitOfWork uow, IEntityBuilder<E> builder) {
stmt.executeUpdate(getCreateQuery());
}
-
-
+
+ insert = getConnection().prepareStatement(getInsertQuery());
+ delete = getConnection().prepareStatement(""
+ + "DELETE FROM "
+ + getTableName()
+ + " WHERE id=?");
+ getById=getConnection().prepareStatement(""
+ + "SELECT * FROM "
+ + getTableName()
+ + " WHERE id=?");
+ getAll = getConnection().prepareStatement("SELECT * FROM "
+ + getTableName()
+ + " LIMIT ? ?");
+
+ update = getConnection().prepareStatement(getUpdateQuery());
} catch (Exception e) {
e.printStackTrace();
}
@@ -71,8 +84,8 @@ public E get(int id) {
E result = null;
try{
- getGetById().setInt(1, id);
- ResultSet rs = getGetById().executeQuery();
+ getById.setInt(1, id);
+ ResultSet rs = getById.executeQuery();
while(rs.next())
{
result = builder.build(rs);
@@ -91,9 +104,9 @@ public E get(int id) {
try{
int n = request.getPageIndex()*request.getPageSize();
- getGetAll().setInt(1, n);
- getGetAll().setInt(2, request.getPageSize());
- ResultSet rs = getGetAll().executeQuery();
+ getAll.setInt(1, n);
+ getAll.setInt(2, request.getPageSize());
+ ResultSet rs = getAll.executeQuery();
while(rs.next())
{
E result = builder.build(rs);
@@ -112,7 +125,7 @@ public void persistAdd(EntityBase ent) {
try {
setInsertQuery(ent);
- getInsert().executeUpdate();
+ insert.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
@@ -124,8 +137,8 @@ public void persistAdd(EntityBase ent) {
public void persistDeleted(EntityBase ent) {
try {
- getDelete().setInt(1, ent.getId());
- getDelete().executeUpdate();
+ delete.setInt(1, ent.getId());
+ delete.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -137,55 +150,14 @@ public void persistUpdated(EntityBase ent) {
try{
setUpdateQuery(ent);
- getUpdate().executeUpdate();
+ update.executeUpdate();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
-
- private PreparedStatement getGetById() throws SQLException {
- if(getById==null||getById.isClosed())
- getById=getConnection().prepareStatement(""
- + "SELECT * FROM "
- + getTableName()
- + " WHERE id=?");
- return getById;
- }
-
- private PreparedStatement getGetAll() throws SQLException {
- if(getAll==null || getAll.isClosed())
- getAll = getConnection().prepareStatement("SELECT * FROM "
- + getTableName()
- + " LIMIT ? ?");
- return getAll;
- }
-
- protected PreparedStatement getUpdate() throws SQLException {
- if(update==null||update.isClosed())
- update = getConnection().prepareStatement(getUpdateQuery());
- return update;
- }
-
- private PreparedStatement getDelete() throws SQLException {
- if(delete == null || delete.isClosed())
- delete = getConnection().prepareStatement(""
- + "DELETE FROM "
- + getTableName()
- + " WHERE id=?");
- return delete;
- }
-
- protected PreparedStatement getInsert() throws SQLException {
- if(insert==null || insert.isClosed())
- insert = getConnection().prepareStatement(getInsertQuery());
- return insert;
- }
-
-
-
protected abstract void setUpdateQuery(EntityBase ent) throws SQLException;
protected abstract void setInsertQuery(EntityBase ent) throws SQLException;
protected abstract String getUpdateQuery();
View
2 src/main/java/database/unitofwork/IUnitOfWork.java
@@ -8,5 +8,5 @@
public void registerDeleted(EntityBase ent, IUnitOfWorkRepository repo);
public void registerUpdated(EntityBase ent, IUnitOfWorkRepository repo);
public void commit();
-
+ public void close();
}
View
36 src/main/java/webservices/AuthorRestService.java
@@ -1,36 +0,0 @@
-package webservices;
-
-import javax.ejb.Stateless;
-import javax.inject.Inject;
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-
-import model.Author;
-import database.managers.IAuthorManager;
-import dto.AuthorSummaryDto;
-
-@Path("author")
-@Stateless
-public class AuthorRestService {
-
- @Inject
- IAuthorManager mgr;
-
- @GET
- @Path("/giveauthor/{id}")
- @Produces("application/json")
- public AuthorSummaryDto giveAuthor(@PathParam("id") int id)
- {
-
- Author a = mgr.get(id);
-
- AuthorSummaryDto res = new AuthorSummaryDto();
- res.setId(a.getId());
- res.setName(a.getName());
- res.setSurname(a.getSurname());
-
- return res;
- }
-}
View
34 src/main/java/webservices/AuthorService.java
@@ -1,34 +0,0 @@
-package webservices;
-
-import javax.inject.Inject;
-import javax.jws.WebMethod;
-import javax.jws.WebService;
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-
-import model.Author;
-import database.managers.IAuthorManager;
-import dto.AuthorSummaryDto;
-
-@WebService
-public class AuthorService {
-
- @Inject
- IAuthorManager mgr;
-
- @WebMethod
- public AuthorSummaryDto giveAuthor(@PathParam("id") int id)
- {
-
- Author a = mgr.get(id);
-
- AuthorSummaryDto res = new AuthorSummaryDto();
- res.setId(a.getId());
- res.setName(a.getName());
- res.setSurname(a.getSurname());
-
- return res;
- }
-}
View
66 src/main/java/webservices/BookRestService.java
@@ -1,66 +0,0 @@
-package webservices;
-
-
-import javax.ejb.Stateless;
-import javax.enterprise.context.SessionScoped;
-import javax.inject.Inject;
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-
-import model.Book;
-import database.UnitOfWork;
-import database.builder.BookBuilder;
-import database.builder.IEntityBuilder;
-import database.managers.IBookManager;
-import database.managers.impl.MysqlBookManager;
-import dto.BookDto;
-import dto.BookSummaryDto;
-
-@Path("book")
-@Stateless
-public class BookRestService {
-
- @Inject
- private IBookManager mgr;
-
- @GET
- @Path("/givebook/{id}")
- @Produces("application/json")
- public BookDto giveBook(@PathParam("id") int id)
- {
- //UnitOfWork uow = new UnitOfWork();
- //IEntityBuilder<Book> builder = new BookBuilder();
- //IBookManager mgr = new MysqlBookManager(uow,builder,null);
- Book b = mgr.get(id);
- BookDto result = new BookDto();
- result.setPublisher(b.getPublisher());
- result.setTitle(b.getTitle());
- result.setYear(b.getYear());
-
- return result;
-
- }
-
- public BookSummaryDto saveBook(BookDto request)
- {
- //UnitOfWork uow = new UnitOfWork();
- //IEntityBuilder<Book> builder = new BookBuilder();
- //IBookManager mgr = new MysqlBookManager(uow,builder,null);
- Book b = new Book();
- b.setPublisher(request.getPublisher());
- b.setTitle(request.getTitle());
- b.setYear(request.getYear());
- mgr.save(b);
- //uow.commit();
- mgr.saveChanges();
- BookSummaryDto result = new BookSummaryDto();
- result.setPublisher(b.getPublisher());
- result.setTitle(b.getTitle());
- result.setYear(b.getYear());
-
- return result;
-
- }
-}
View
54 src/main/java/webservices/BookService.java
@@ -1,54 +0,0 @@
-package webservices;
-
-import javax.inject.Inject;
-import javax.jws.WebMethod;
-import javax.jws.WebService;
-
-import model.Book;
-import database.UnitOfWork;
-import database.builder.BookBuilder;
-import database.builder.IEntityBuilder;
-import database.managers.IBookManager;
-import database.managers.impl.MysqlBookManager;
-import dto.*;
-
-@WebService
-public class BookService {
-
- @Inject
- private IBookManager mgr;
-
- @WebMethod
- public BookDto giveBook(int request)
- {
- Book b = mgr.get(request);
- BookDto result = new BookDto();
- result.setPublisher(b.getPublisher());
- result.setTitle(b.getTitle());
- result.setYear(b.getYear());
-
- return result;
-
- }
-
- @WebMethod
- public BookSummaryDto saveBook(BookDto request)
- {
- //UnitOfWork uow = new UnitOfWork();
- //IEntityBuilder<Book> builder = new BookBuilder();
- //IBookManager mgr = new MysqlBookManager(uow,builder,null);
- Book b = new Book();
- b.setPublisher(request.getPublisher());
- b.setTitle(request.getTitle());
- b.setYear(request.getYear());
- mgr.save(b);
- mgr.saveChanges();
- BookSummaryDto result = new BookSummaryDto();
- result.setPublisher(b.getPublisher());
- result.setTitle(b.getTitle());
- result.setYear(b.getYear());
-
- return result;
-
- }
-}
View
19 src/main/webapp/WEB-INF/web.xml
@@ -1,16 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
-<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
+<web-app
+xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+xmlns="http://java.sun.com/xml/ns/javaee"
+xmlns:web="http://java.sun.com/xml/ns/javaee"
+xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+version="2.5">
<display-name>library</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>Jersey Web Application</servlet-name>
- <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Jersey Web Application</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
</web-app>

0 comments on commit b255654

Please sign in to comment.
Something went wrong with that request. Please try again.