[myblog] r440 committed - add Account in domain.

0 views
Skip to first unread message

myb...@googlecode.com

unread,
May 23, 2011, 2:01:31 PM5/23/11
to mybl...@googlegroups.com
Revision: 440
Author: Ror...@gmail.com
Date: Mon May 23 08:53:05 2011
Log: add Account in domain.
http://code.google.com/p/myblog/source/detail?r=440

Added:
/trunk/myblog-domain/src/main/java/com/jdkcn/myblog/domain/Account.java
Modified:
/trunk/myblog-core/src/main/java/com/jdkcn/myblog/util/Sorter.java
/trunk/myblog-domain/src/main/java/com/jdkcn/myblog/domain/Blog.java
/trunk/myblog-domain/src/main/java/com/jdkcn/myblog/domain/User.java

/trunk/myblog-service/src/test/java/com/jdkcn/myblog/service/BlogServiceTest.java

/trunk/myblog-service/src/test/java/com/jdkcn/myblog/service/CommentServiceTest.java
/trunk/myblog-service/src/test/resources/META-INF/persistence.xml

/trunk/myblog-web/src/main/java/com/jdkcn/myblog/guice/MyblogInitializer.java
/trunk/myblog-webapp/src/main/resources/META-INF/persistence.xml.hsqldb_mem
/trunk/myblog-webapp/src/main/resources/META-INF/persistence.xml.mysql
/trunk/myblog-webapp/src/main/resources/META-INF/persistence.xml.postgresql

=======================================
--- /dev/null
+++ /trunk/myblog-domain/src/main/java/com/jdkcn/myblog/domain/Account.java
Mon May 23 08:53:05 2011
@@ -0,0 +1,83 @@
+/**
+ * Copyright (c) 2005-2011, Rory Ye
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * * Redistributions of source code must retain the above copyright
notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
notice,
+ * this list of conditions and the following disclaimer in the
documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the Jdkcn.com nor the names of its
contributors may
+ * be used to endorse or promote products derived from this software
without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jdkcn.myblog.domain;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+
+/**
+ * @author <a href="mailto:ror...@gmail.com">Rory</a>
+ * @version $Id$
+ */
+@Entity
+@Table(name = BaseDomain.TABLE_PREFIX + "account")
+public class Account extends BaseDomain {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -8987362662125268431L;
+
+ private User user;
+
+ private String loginId;
+
+ private String password;
+
+ @ManyToOne
+ @JoinColumn(name = "user_id")
+ public User getUser() {
+ return user;
+ }
+
+ public void setUser(User user) {
+ this.user = user;
+ }
+
+ @Column(name = "login_id", length = 100, unique = true, nullable = false)
+ public String getLoginId() {
+ return loginId;
+ }
+
+ public void setLoginId(String loginId) {
+ this.loginId = loginId;
+ }
+
+ @Column(name = "password")
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+}
=======================================
--- /trunk/myblog-core/src/main/java/com/jdkcn/myblog/util/Sorter.java Sat
May 7 22:37:48 2011
+++ /trunk/myblog-core/src/main/java/com/jdkcn/myblog/util/Sorter.java Mon
May 23 08:53:05 2011
@@ -42,25 +42,25 @@
private static final long serialVersionUID = 757552624922780352L;

/**
- * 排序类型
- */
- private static enum Type {
-
- /**
- * 升序
- */
- ASCENDING,
-
- /**
- * 降序
- */
- DESCENDING
- }
-
- /**
- * 排序指示对象
- */
- public static class Sort implements Serializable {
+ * sorter type
+ */
+ private static enum Type {
+
+ /**
+ * ascending
+ */
+ ASCENDING,
+
+ /**
+ * descending
+ */
+ DESCENDING
+ }
+
+ /**
+ * The sort object for each property sort.
+ */
+ public static class Sort implements Serializable {

private static final long serialVersionUID = -1599386645244047497L;

@@ -69,89 +69,93 @@
public Type type;

/**
- * 用默认构造器
* @param property
+ * sort property
* @param type
+ * the sort type
*/
- private Sort(String property, Type type) {
- this.property = property;
- this.type = type;
- }
-
- public boolean isAscending() {
- return type == Type.ASCENDING;
- }
-
- @Override
- public String toString() {
- return property + " " + (type ==
Type.ASCENDING ? "asc" : "desc");
- }
- }
-
- private List<Sort> sorts = new ArrayList<Sort>();
-
- public List<Sort> getSorts() {
+ private Sort(String property, Type type) {
+ this.property = property;
+ this.type = type;
+ }
+
+ public boolean isAscending() {
+ return type == Type.ASCENDING;
+ }
+
+ @Override
+ public String toString() {
+ return property + " " + (type == Type.ASCENDING ? "asc" : "desc");
+ }
+ }
+
+ private List<Sort> sorts = new ArrayList<Sort>();
+
+ public List<Sort> getSorts() {
return sorts;
}

/**
- * 升序
- * @param property sort property
- * @return this
- */
- public Sorter asc(String property) {
- for (Sort sort: sorts) {
- if (StringUtils.equals(property, sort.property)) {
- sorts.remove(sort);
- break;
- }
- }
- sorts.add(new Sort(property, Type.ASCENDING));
- return this;
- }
-
- /**
- * 降序
- *
- * @param property sort property
- * @return this
- */
- public Sorter desc(String property) {
- for (Sort sort: sorts) {
- if (StringUtils.equals(property, sort.property)) {
- sorts.remove(sort);
- break;
- }
- }
- sorts.add(new Sort(property, Type.DESCENDING));
- return this;
- }
-
-
- /**
- * 附加order语句到hql上
- *
- * @param hql hql
- * @return decorated hql
- */
- public String decorate(String hql) {
-
- if (sorts.isEmpty()) {
- return hql;
- }
-
- StringBuilder queryBuilder = new StringBuilder(hql);
- if (StringUtils.containsIgnoreCase(hql, "order by")) {
- for (Sort sort : sorts) {
- queryBuilder.append(",").append(sort.toString());
- }
- } else {
- queryBuilder.append(" order by ");
- for (Sort sort : sorts) {
- queryBuilder.append(sort.toString()).append(",");
- }
- queryBuilder.deleteCharAt(queryBuilder.length() -1);
- }
- return queryBuilder.toString();
- }
-}
+ * ascending
+ *
+ * @param property
+ * sort property
+ * @return this
+ */
+ public Sorter asc(String property) {
+ for (Sort sort : sorts) {
+ if (StringUtils.equals(property, sort.property)) {
+ sorts.remove(sort);
+ break;
+ }
+ }
+ sorts.add(new Sort(property, Type.ASCENDING));
+ return this;
+ }
+
+ /**
+ * descending
+ *
+ * @param property
+ * sort property
+ * @return this
+ */
+ public Sorter desc(String property) {
+ for (Sort sort : sorts) {
+ if (StringUtils.equals(property, sort.property)) {
+ sorts.remove(sort);
+ break;
+ }
+ }
+ sorts.add(new Sort(property, Type.DESCENDING));
+ return this;
+ }
+
+ /**
+ * add order in hql
+ *
+ * @param hql
+ * hql
+ * @return decorated hql
+ */
+ public String decorate(String hql) {
+
+ if (sorts.isEmpty()) {
+ return hql;
+ }
+
+ StringBuilder queryBuilder = new StringBuilder(hql);
+ if (StringUtils.containsIgnoreCase(hql, "order by")) {
+ for (Sort sort : sorts) {
+ queryBuilder.append(",").append(sort.toString());
+ }
+ } else {
+ queryBuilder.append(" order by ");
+ for (Sort sort : sorts) {
+ queryBuilder.append(sort.toString()).append(",");
+ }
+ queryBuilder.deleteCharAt(queryBuilder.length() - 1);
+ }
+ return queryBuilder.toString();
+ }
+}
=======================================
--- /trunk/myblog-domain/src/main/java/com/jdkcn/myblog/domain/Blog.java
Tue May 10 09:25:02 2011
+++ /trunk/myblog-domain/src/main/java/com/jdkcn/myblog/domain/Blog.java
Mon May 23 08:53:05 2011
@@ -27,8 +27,14 @@
*/
package com.jdkcn.myblog.domain;

+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.CollectionTable;
import javax.persistence.Column;
+import javax.persistence.ElementCollection;
import javax.persistence.Entity;
+import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@@ -50,13 +56,21 @@

private String description;

- private String url;
+ private List<String> urls;

private Integer defaultPageSize = Constants.DEFAULT_PAGE_SIZE;

private String theme;

private User owner;
+
+ public Blog addUrl(String url) {
+ if (urls == null) {
+ urls = new ArrayList<String>();
+ }
+ urls.add(url);
+ return this;
+ }

@ManyToOne
@JoinColumn(name = "owner_id")
@@ -94,17 +108,22 @@
public void setName(String name) {
this.name = name;
}
-
- @Column(name = "url", length = 255)
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- @Column(name = "description", length = 2048)
+
+ @ElementCollection(fetch = FetchType.EAGER)
+ @CollectionTable(
+ name = BaseDomain.TABLE_PREFIX + "blog_url",
+ joinColumns = @JoinColumn(name = "blog_id")
+ )
+ @Column(name = "url")
+ public List<String> getUrls() {
+ return urls;
+ }
+
+ public void setUrls(List<String> urls) {
+ this.urls = urls;
+ }
+
+ @Column(name = "description", length = 2048)
public String getDescription() {
return description;
}
=======================================
--- /trunk/myblog-domain/src/main/java/com/jdkcn/myblog/domain/User.java
Wed Apr 27 22:43:33 2011
+++ /trunk/myblog-domain/src/main/java/com/jdkcn/myblog/domain/User.java
Mon May 23 08:53:05 2011
@@ -27,8 +27,13 @@
*/
package com.jdkcn.myblog.domain;

+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
+import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
@@ -48,11 +53,20 @@

private String email;

- private String openid;
-
- private String displayName;
-
- @Column(name = "username", length = 100, nullable = false)
+ private String nickname;
+
+ private Set<Account> accounts = new LinkedHashSet<Account>();
+
+ @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "user")
+ public Set<Account> getAccounts() {
+ return accounts;
+ }
+
+ public void setAccounts(Set<Account> accounts) {
+ this.accounts = accounts;
+ }
+
+ @Column(name = "username", length = 100, nullable = false)
public String getUsername() {
return username;
}
@@ -78,24 +92,14 @@
public void setEmail(String email) {
this.email = email;
}
-
- @Column(name = "openid", length = 255)
- public String getOpenid() {
- return openid;
- }
-
- public void setOpenid(String openid) {
- this.openid = openid;
- }
-
- @Column(name = "display_name", length = 200)
- public String getDisplayName() {
- return displayName;
- }
-
- public void setDisplayName(String displayName) {
- this.displayName = displayName;
- }
-
+
+ @Column(name = "nickname", length = 200, unique = true)
+ public String getNickname() {
+ return nickname;
+ }
+
+ public void setNickname(String nickname) {
+ this.nickname = nickname;
+ }

}
=======================================
---
/trunk/myblog-service/src/test/java/com/jdkcn/myblog/service/BlogServiceTest.java
Thu Apr 28 02:06:47 2011
+++
/trunk/myblog-service/src/test/java/com/jdkcn/myblog/service/BlogServiceTest.java
Mon May 23 08:53:05 2011
@@ -52,7 +52,7 @@
public void testCreateOrUpdate() throws Exception {
Blog blog = new Blog();
blog.setName("Just Myblog.");
- blog.setUrl("http://jdkcn.com");
+ blog.addUrl("http://jdkcn.com");

assertNull(blog.getId());

=======================================
---
/trunk/myblog-service/src/test/java/com/jdkcn/myblog/service/CommentServiceTest.java
Fri Apr 29 09:30:38 2011
+++
/trunk/myblog-service/src/test/java/com/jdkcn/myblog/service/CommentServiceTest.java
Mon May 23 08:53:05 2011
@@ -63,7 +63,7 @@
BlogService blogService = injector.getInstance(BlogService.class);
blog = new Blog();
blog.setName("Test blog");
- blog.setUrl("http://jdkcn.com");
+ blog.addUrl("http://jdkcn.com");
blog = blogService.saveOrUpdate(blog);

UserService userService = injector.getInstance(UserService.class);
=======================================
--- /trunk/myblog-service/src/test/resources/META-INF/persistence.xml Thu
Apr 28 21:55:25 2011
+++ /trunk/myblog-service/src/test/resources/META-INF/persistence.xml Mon
May 23 08:53:05 2011
@@ -10,6 +10,7 @@
<class>com.jdkcn.myblog.domain.Entry</class>
<class>com.jdkcn.myblog.domain.Category</class>
<class>com.jdkcn.myblog.domain.User</class>
+ <class>com.jdkcn.myblog.domain.Account</class>
<class>com.jdkcn.myblog.domain.Blog</class>
<class>com.jdkcn.myblog.domain.Tag</class>
<class>com.jdkcn.myblog.domain.Comment</class>
=======================================
---
/trunk/myblog-web/src/main/java/com/jdkcn/myblog/guice/MyblogInitializer.java
Fri May 13 02:28:19 2011
+++
/trunk/myblog-web/src/main/java/com/jdkcn/myblog/guice/MyblogInitializer.java
Mon May 23 08:53:05 2011
@@ -64,7 +64,7 @@
blog.setName("Another Myblog");
blog.setDefaultPageSize(Constants.DEFAULT_PAGE_SIZE);
blog.setDescription("Another Myblog.");
- blog.setUrl("http://code.google.com/p/myblog");
+ blog.addUrl("http://code.google.com/p/myblog");
blog.setOwner(user);
return blogService.saveOrUpdate(blog);
}
@@ -77,7 +77,7 @@
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
- user.setDisplayName(username);
+ user.setNickname(username);
return userService.saveOrUpdate(user);
}

=======================================
---
/trunk/myblog-webapp/src/main/resources/META-INF/persistence.xml.hsqldb_mem
Thu Apr 28 21:55:25 2011
+++
/trunk/myblog-webapp/src/main/resources/META-INF/persistence.xml.hsqldb_mem
Mon May 23 08:53:05 2011
@@ -10,6 +10,7 @@
<class>com.jdkcn.myblog.domain.Entry</class>
<class>com.jdkcn.myblog.domain.Category</class>
<class>com.jdkcn.myblog.domain.User</class>
+ <class>com.jdkcn.myblog.domain.Account</class>
<class>com.jdkcn.myblog.domain.Blog</class>
<class>com.jdkcn.myblog.domain.Tag</class>
<class>com.jdkcn.myblog.domain.Comment</class>
=======================================
--- /trunk/myblog-webapp/src/main/resources/META-INF/persistence.xml.mysql
Thu Apr 28 21:55:25 2011
+++ /trunk/myblog-webapp/src/main/resources/META-INF/persistence.xml.mysql
Mon May 23 08:53:05 2011
@@ -10,6 +10,7 @@
<class>com.jdkcn.myblog.domain.Entry</class>
<class>com.jdkcn.myblog.domain.Category</class>
<class>com.jdkcn.myblog.domain.User</class>
+ <class>com.jdkcn.myblog.domain.Account</class>
<class>com.jdkcn.myblog.domain.Blog</class>
<class>com.jdkcn.myblog.domain.Tag</class>
<class>com.jdkcn.myblog.domain.Comment</class>
=======================================
---
/trunk/myblog-webapp/src/main/resources/META-INF/persistence.xml.postgresql
Thu Apr 28 21:55:25 2011
+++
/trunk/myblog-webapp/src/main/resources/META-INF/persistence.xml.postgresql
Mon May 23 08:53:05 2011
@@ -10,6 +10,7 @@
<class>com.jdkcn.myblog.domain.Entry</class>
<class>com.jdkcn.myblog.domain.Category</class>
<class>com.jdkcn.myblog.domain.User</class>
+ <class>com.jdkcn.myblog.domain.Account</class>
<class>com.jdkcn.myblog.domain.Blog</class>
<class>com.jdkcn.myblog.domain.Tag</class>
<class>com.jdkcn.myblog.domain.Comment</class>
Reply all
Reply to author
Forward
0 new messages