[myblog] r427 committed - fixed the guice-persist's use....

4 views
Skip to first unread message

myb...@googlecode.com

unread,
May 13, 2011, 5:29:15 AM5/13/11
to mybl...@googlegroups.com
Revision: 427
Author: rory.cn
Date: Fri May 13 02:28:19 2011
Log: fixed the guice-persist's use.
Must inject the EntityManager with Provider.
http://code.google.com/p/google-guice/wiki/JPA

Finished a very basic version entry add.
http://code.google.com/p/myblog/source/detail?r=427

Modified:

/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/BlogServiceImpl.java

/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/CategoryServiceImpl.java

/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/CommentServiceImpl.java

/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/EntryServiceImpl.java

/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/TagServiceImpl.java

/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/UserServiceImpl.java

/trunk/myblog-web/src/main/java/com/jdkcn/myblog/guice/MyblogInitializer.java

/trunk/myblog-web/src/main/java/com/jdkcn/myblog/web/listener/MyblogServletContextListener.java

/trunk/myblog-web/src/main/java/com/jdkcn/myblog/web/page/admin/AddEntry.java
/trunk/myblog-webapp/src/main/webapp/WEB-INF/templates/adm/addentry.html

=======================================
---
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/BlogServiceImpl.java
Tue May 10 09:25:02 2011
+++
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/BlogServiceImpl.java
Fri May 13 02:28:19 2011
@@ -32,6 +32,8 @@
import javax.persistence.EntityManager;

import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.persist.Transactional;
import com.jdkcn.myblog.domain.Blog;
import com.jdkcn.myblog.service.BlogService;

@@ -42,20 +44,21 @@
public class BlogServiceImpl implements BlogService {

@Inject
- private EntityManager em;
+ private Provider<EntityManager> entityManagerProvider;

/**
* {@inheritDoc}
*/
+ @Transactional
public Blog saveOrUpdate(Blog blog) {
- return em.merge(blog);
+ return entityManagerProvider.get().merge(blog);
}

/**
* {@inheritDoc}
*/
public Blog get(String id) {
- return em.find(Blog.class, id);
+ return entityManagerProvider.get().find(Blog.class, id);
}

/**
@@ -63,6 +66,6 @@
*/
@SuppressWarnings("unchecked")
public List<Blog> getAllBlogs() {
- return (List<Blog>) em.createQuery("from Blog").getResultList();
+ return (List<Blog>) entityManagerProvider.get().createQuery("from
Blog").getResultList();
}
}
=======================================
---
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/CategoryServiceImpl.java
Fri Apr 29 09:30:38 2011
+++
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/CategoryServiceImpl.java
Fri May 13 02:28:19 2011
@@ -35,12 +35,12 @@
import org.apache.commons.lang.StringUtils;

import com.google.inject.Inject;
+import com.google.inject.Provider;
import com.google.inject.persist.Transactional;
import com.jdkcn.myblog.domain.Category;
+import com.jdkcn.myblog.domain.Category.Type;
import com.jdkcn.myblog.exception.DuplicateException;
import com.jdkcn.myblog.service.CategoryService;
-
-import static com.jdkcn.myblog.domain.Category.Type;
/**
* @author <a href="mailto:ror...@gmail.com">Rory, Ye</a>
* @since May 10, 2010 11:03:56 AM
@@ -49,7 +49,9 @@
public class CategoryServiceImpl implements CategoryService {

@Inject
- private EntityManager em;
+ private Provider<EntityManager> entityManagerProvider;
+
+

/**
* {@inheritDoc}
@@ -59,10 +61,10 @@
public List<Category> getRoots(Type type) {
Query query = null;
if (type != null) {
- query = em.createQuery("from Category where type = :type and parent is
null");
+ query = entityManagerProvider.get().createQuery("from Category where
type = :type and parent is null");
query.setParameter("type", type);
} else {
- query = em.createQuery("from Category where parent is null");
+ query = entityManagerProvider.get().createQuery("from Category where
parent is null");
}
return query.getResultList();
}
@@ -73,9 +75,9 @@
public Category getByName(String name, String parentId) {
Query query = null;
if (StringUtils.isBlank(parentId)) {
- query = em.createQuery("from Category where name = :name and parent is
null");
+ query = entityManagerProvider.get().createQuery("from Category where
name = :name and parent is null");
} else {
- query = em.createQuery("from Category where name = :name and parent.id
= :parentId");
+ query = entityManagerProvider.get().createQuery("from Category where
name = :name and parent.id = :parentId");
query.setParameter("parentId", parentId);
}
query.setParameter("name", name);
@@ -107,7 +109,7 @@
if (exist != null && !StringUtils.equals(category.getId(),
exist.getId())) {
throw new DuplicateException("category in parentId[" + parentId + "]
duplicate with name:" + category.getName());
}
- Category savedCategory = em.merge(category);
+ Category savedCategory = entityManagerProvider.get().merge(category);
if (category.getParent() != null) {
category.getParent().getChildren().add(savedCategory);
}
@@ -118,7 +120,7 @@
* {@inheritDoc}
*/
public Category get(String id) {
- return em.find(Category.class, id);
+ return entityManagerProvider.get().find(Category.class, id);
}

/**
@@ -128,10 +130,10 @@
Integer order = null;
Query query = null;
if (StringUtils.isNotBlank(parentId)) {
- query = em.createQuery("select max(cate.order) from Category cate where
cate.parent.id = :parentId");
+ query = entityManagerProvider.get().createQuery("select max(cate.order)
from Category cate where cate.parent.id = :parentId");
query.setParameter("parentId", parentId);
} else {
- query = em.createQuery("select max(cate.order) from Category cate where
cate.parent is null");
+ query = entityManagerProvider.get().createQuery("select max(cate.order)
from Category cate where cate.parent is null");
}
order = (Integer) query.getSingleResult();
return order == null ? 0 : order;
=======================================
---
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/CommentServiceImpl.java
Thu Apr 28 21:55:25 2011
+++
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/CommentServiceImpl.java
Fri May 13 02:28:19 2011
@@ -34,6 +34,8 @@
import org.apache.commons.lang.StringUtils;

import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.persist.Transactional;
import com.jdkcn.myblog.domain.Comment;
import com.jdkcn.myblog.domain.Comment.Status;
import com.jdkcn.myblog.service.CommentService;
@@ -45,11 +47,12 @@
public class CommentServiceImpl implements CommentService {

@Inject
- private EntityManager em;
+ private Provider<EntityManager> entityManagerProvider;

/**
* {@inheritDoc}
*/
+ @Transactional
public Comment saveOrUpdate(Comment comment) {
if (comment.getAuthor() != null &&
StringUtils.isBlank(comment.getAuthorName())) {
comment.setAuthorName(comment.getAuthor().getUsername());
@@ -60,14 +63,14 @@
if (comment.getStatus() == null) {
comment.setStatus(Status.WAITING_FOR_APPROVE);
}
- return em.merge(comment);
+ return entityManagerProvider.get().merge(comment);
}

/**
* {@inheritDoc}
*/
public Comment get(String id) {
- return em.find(Comment.class, id);
+ return entityManagerProvider.get().find(Comment.class, id);
}

}
=======================================
---
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/EntryServiceImpl.java
Mon May 9 08:45:42 2011
+++
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/EntryServiceImpl.java
Fri May 13 02:28:19 2011
@@ -38,6 +38,7 @@
import org.joda.time.DateTimeZone;

import com.google.inject.Inject;
+import com.google.inject.Provider;
import com.google.inject.persist.Transactional;
import com.jdkcn.myblog.condition.EntryCondition;
import com.jdkcn.myblog.domain.Entry;
@@ -58,7 +59,7 @@
public class EntryServiceImpl implements EntryService {

@Inject
- private EntityManager em;
+ private Provider<EntityManager> entityManagerProvider;

/**
* {@inheritDoc}
@@ -96,14 +97,14 @@
entry.setUpdateDateGMT(dateTime.withZone(zoneUTC).toDate());
dateTime = new DateTime(entry.getCreateDate());
entry.setCreateDateGMT(dateTime.withZone(zoneUTC).toDate());
- return em.merge(entry);
+ return entityManagerProvider.get().merge(entry);
}

/**
* {@inheritDoc}
*/
public Entry get(String id) {
- return em.find(Entry.class, id);
+ return entityManagerProvider.get().find(Entry.class, id);
}

/**
@@ -111,7 +112,7 @@
*/
@SuppressWarnings("unchecked")
public Entry getByName(String blogId, String name) {
- Query query = em.createQuery("from Entry entry where entry.blog.id
= :blogId and entry.name = :name");
+ Query query = entityManagerProvider.get().createQuery("from Entry entry
where entry.blog.id = :blogId and entry.name = :name");
query.setParameter("blogId", blogId);
query.setParameter("name", name);
List<Entry> entries = query.getResultList();
@@ -129,10 +130,10 @@
condition = new EntryCondition();
}
String hql = condition.decorateHql();
- Query countQuery = em.createQuery("select count(*) " + hql);
+ Query countQuery = entityManagerProvider.get().createQuery("select
count(*) " + hql);
condition.setParameters(countQuery);

- Query query = em.createQuery(sorter.decorate(hql));
+ Query query =
entityManagerProvider.get().createQuery(sorter.decorate(hql));
condition.setParameters(query);

long totalCount = (Long)countQuery.getSingleResult();
=======================================
---
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/TagServiceImpl.java
Tue May 3 03:49:20 2011
+++
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/TagServiceImpl.java
Fri May 13 02:28:19 2011
@@ -30,6 +30,8 @@
import javax.persistence.EntityManager;

import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.persist.Transactional;
import com.jdkcn.myblog.domain.Tag;
import com.jdkcn.myblog.service.TagService;

@@ -40,20 +42,21 @@
public class TagServiceImpl implements TagService {

@Inject
- private EntityManager em;
+ private Provider<EntityManager> entityManagerProvider;

/**
* {@inheritDoc}
*/
+ @Transactional
public Tag saveOrUpdate(Tag tag) {
- return em.merge(tag);
+ return entityManagerProvider.get().merge(tag);
}

/**
* {@inheritDoc}
*/
public Tag get(String id) {
- return em.find(Tag.class, id);
+ return entityManagerProvider.get().find(Tag.class, id);
}

}
=======================================
---
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/UserServiceImpl.java
Thu Apr 28 04:58:50 2011
+++
/trunk/myblog-service/src/main/java/com/jdkcn/myblog/service/impl/UserServiceImpl.java
Fri May 13 02:28:19 2011
@@ -35,6 +35,7 @@
import org.apache.commons.lang.StringUtils;

import com.google.inject.Inject;
+import com.google.inject.Provider;
import com.google.inject.persist.Transactional;
import com.jdkcn.myblog.domain.User;
import com.jdkcn.myblog.exception.DuplicateException;
@@ -47,7 +48,7 @@
public class UserServiceImpl implements UserService {

@Inject
- private EntityManager em;
+ private Provider<EntityManager> entityManagerProvider;

/**
* {@inheritDoc}
@@ -62,14 +63,14 @@
if (exist != null && !StringUtils.equals(user.getId(), exist.getId())) {
throw new DuplicateException(User.class.getName(), "email",
user.getEmail());
}
- return em.merge(user);
+ return entityManagerProvider.get().merge(user);
}

/**
* {@inheritDoc}
*/
public User get(String id) {
- return em.find(User.class, id);
+ return entityManagerProvider.get().find(User.class, id);
}

/**
@@ -77,7 +78,7 @@
*/
@SuppressWarnings("unchecked")
public User getByUsername(String username) {
- Query query = em.createQuery("from User u where u.username = :username");
+ Query query = entityManagerProvider.get().createQuery("from User u where
u.username = :username");
query.setParameter("username", username);
List<User> users = query.getResultList();
if (users.isEmpty()) {
@@ -91,7 +92,7 @@
*/
@SuppressWarnings("unchecked")
public User getByEmail(String email) {
- Query query = em.createQuery("from User u where u.email = :email");
+ Query query = entityManagerProvider.get().createQuery("from User u where
u.email = :email");
query.setParameter("email", email);
List<User> users = query.getResultList();
if (users.isEmpty()) {
=======================================
---
/trunk/myblog-web/src/main/java/com/jdkcn/myblog/guice/MyblogInitializer.java
Tue May 10 09:25:02 2011
+++
/trunk/myblog-web/src/main/java/com/jdkcn/myblog/guice/MyblogInitializer.java
Fri May 13 02:28:19 2011
@@ -56,6 +56,7 @@
this.username = username;
this.password = password;
this.email = email;
+ checkInit();
}

private Blog createDefaultBlog(User user) {
=======================================
---
/trunk/myblog-web/src/main/java/com/jdkcn/myblog/web/listener/MyblogServletContextListener.java
Tue May 3 09:06:32 2011
+++
/trunk/myblog-web/src/main/java/com/jdkcn/myblog/web/listener/MyblogServletContextListener.java
Fri May 13 02:28:19 2011
@@ -62,8 +62,7 @@
*/
@Override
protected void configureServlets() {
- filter("*.html").through(PersistFilter.class);
- filter("*.jspx").through(PersistFilter.class);
+ filter("/*").through(PersistFilter.class);
filter("/adm/*","/adm").through(UserLoginFilter.class);
// serve("/test.jspx").with(TestServlet.class);
}
=======================================
---
/trunk/myblog-web/src/main/java/com/jdkcn/myblog/web/page/admin/AddEntry.java
Thu May 12 00:42:10 2011
+++
/trunk/myblog-web/src/main/java/com/jdkcn/myblog/web/page/admin/AddEntry.java
Fri May 13 02:28:19 2011
@@ -27,16 +27,20 @@
*/
package com.jdkcn.myblog.web.page.admin;

+import javax.servlet.http.HttpSession;
+
import org.apache.commons.lang.StringUtils;

import com.google.inject.Inject;
-import com.google.inject.name.Named;
import com.google.sitebricks.Show;
import com.google.sitebricks.http.Get;
import com.google.sitebricks.http.Post;
import com.google.sitebricks.rendering.Decorated;
import com.jdkcn.myblog.domain.Entry;
+import com.jdkcn.myblog.domain.User;
+import com.jdkcn.myblog.service.BlogService;
import com.jdkcn.myblog.service.EntryService;
+import com.jdkcn.myblog.web.filter.UserLoginFilter;

/**
* @author <a href="mailto:ror...@gmail.com">Rory</a>
@@ -49,19 +53,52 @@
@Inject
private EntryService entryService;

+ @Inject
+ private BlogService blogService;
+
+ @Inject
+ private HttpSession httpSession;
+
+ private Entry entry = new Entry();
+
+ private String blogId;
+
+ public Entry getEntry() {
+ return entry;
+ }
+
+ public void setEntry(Entry entry) {
+ this.entry = entry;
+ }
+
+ public String getBlogId() {
+ return blogId;
+ }
+
+ public void setBlogId(String blogId) {
+ this.blogId = blogId;
+ }
+
@Get
public void Get() {
}

@Post
- public String save(@Named("entry")Entry entry, @Named("blogId")String
blogId) {
+ public String save() {
if (StringUtils.isNotBlank(entry.getName())) {
Entry exist = entryService.getByName(blogId, entry.getName());
if (exist != null) {
- return "";
+ return "/adm/entry/add";
}
}
+ entry.setBlog(blogService.get(blogId));
+ if (StringUtils.isBlank(entry.getExcerpt())) {
+ entry.setExcerpt(entry.getContent());
+ }
+ entry.setType(Entry.Type.ENTRY);
+ entry.setStatus(Entry.Status.PUBLISH);
+
entry.setAuthor((User)httpSession.getAttribute(UserLoginFilter.CURRENT_USER));
entryService.saveOrUpdate(entry);
- return "";
+ return "/adm/entries";
}
}
=======================================
---
/trunk/myblog-webapp/src/main/webapp/WEB-INF/templates/adm/addentry.html
Thu May 12 00:42:10 2011
+++
/trunk/myblog-webapp/src/main/webapp/WEB-INF/templates/adm/addentry.html
Fri May 13 02:28:19 2011
@@ -25,12 +25,16 @@
<form action="/adm/entry/add" method="post">
<fieldset>
<legend>Entry info</legend>
- <!-- <input type="hidden" name="blogId" value="${blog.id}"/> -->
+ <input type="hidden" name="blogId" value="${blog.id}"/>
<div>
<label>Title</label>
<input id="title" name="entry.title" type="text" value="" />
</div>
<div>
+ <label>Name</label>
+ <input id="name" name="entry.name" type="text" value="" />
+ </div>
+ <div>
<label>Content</label>
<textarea rows="10" cols="50" id="content"
style="width:700px;height:300px;" name="entry.content">
</textarea>

Reply all
Reply to author
Forward
0 new messages