Skip to content

Commit

Permalink
Add ability to lazy load articles(nodes) from specific user.
Browse files Browse the repository at this point in the history
  • Loading branch information
jam_0917@sina.com authored and jam_0917@sina.com committed Jan 4, 2013
1 parent ed7b7be commit c8d3f01
Show file tree
Hide file tree
Showing 24 changed files with 273 additions and 396 deletions.
3 changes: 2 additions & 1 deletion timeLine/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry excluding="main/test/" kind="src" path="src"/>
<classpathentry kind="con" path="melibrary.com.genuitec.eclipse.j2eedt.core.MYECLIPSE_JAVAEE_5_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Spring3.1"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-5.1.0-bin.jar"/>
Expand Down Expand Up @@ -62,5 +62,6 @@
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate3.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Hibernate3.6"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aopalliance-1.0.jar"/>
<classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
</classpath>
Binary file added timeLine/WebRoot/WEB-INF/lib/aopalliance-1.0.jar
Binary file not shown.
51 changes: 29 additions & 22 deletions timeLine/WebRoot/WEB-INF/spring-config/services.xml
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

<!-- Annotation driven -->
<context:annotation-config/>
<!--Component load based-->
<context:component-scan base-package="com.hywang.timeline"/>
<context:annotation-config />

<!--Component load based -->
<context:component-scan base-package="com.hywang.timeline" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/timeline"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/timeline" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>





<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations" value="classpath:/com/hywang/timeline/persistence/hbm/*.hbm.xml"/>
<property name="mappingLocations"
value="classpath:/com/hywang/timeline/persistence/hbm/*.hbm.xml" />
<property name="schemaUpdate" value="false" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.query.substitutions"> true 1, false 0, yes 'Y', no 'N'</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.query.substitutions"> true 1, false 0, yes 'Y', no 'N'</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.validator.apply_to_ddl">false</prop>
<prop key="hibernate.validator.autoregister_listeners">false</prop>
<prop key="hibernate.validator.autoregister_listeners">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.max_fetch_depth">0</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>

<bean id="logger" class="com.hywang.timeline.utils.time.TimeMeasureLoggerImpl"></bean>
</bean>

<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="txManager" />

<bean id="timeMeasure" class="com.hywang.timeline.utils.time.TimeMeasure">
<property name="logger" ref="logger"></property>
<property name="logger" ref="logger"></property>
</bean>

</beans>
25 changes: 13 additions & 12 deletions timeLine/src/com/hywang/timeline/actions/ArticleAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.hywang.timeline.entity.TimeLineNode;
import com.hywang.timeline.entity.User;
import com.hywang.timeline.persistence.dao.TimlineNodeDAO;
import com.hywang.timeline.services.TimlineNodeService;
import com.opensymphony.xwork2.ActionSupport;

@Controller("articleAction")
Expand All @@ -38,15 +38,17 @@ public class ArticleAction extends BaseAction {

private JSONObject deleteStatus = null;

@Autowired
private TimlineNodeDAO nodeDao;

private TimlineNodeService nodeService;

public TimlineNodeDAO getNodeDao() {
return nodeDao;
}

public void setNodeDao(TimlineNodeDAO nodeDao) {
this.nodeDao = nodeDao;
public TimlineNodeService getNodeService() {
return nodeService;
}

@Autowired
public void setNodeService(TimlineNodeService nodeService) {
this.nodeService = nodeService;
}

public JSONObject getDeleteStatus() {
Expand Down Expand Up @@ -96,7 +98,7 @@ public String createArticle() {
if (userObject != null) {
User user = (User) userObject;
node.setAuthor(user);
nodeDao.addNode(node); //cascade update
nodeService.addNode(node); //cascade update
}
returnCode = CREATE_SUCCESS;
logger.info("Article create success!");
Expand Down Expand Up @@ -253,7 +255,7 @@ public String listArticles() {
"user");
if (userObject != null && userObject instanceof User) {
User user = (User) userObject;
Set<TimeLineNode> nodes = nodeDao.getNodesByUser(user);
Set<TimeLineNode> nodes = user.getNodes(); //lazy loading
listNodes.put("Result", "OK");
// ID,startDate,endDate,headLine,text,tags,media,caption,credit
for (TimeLineNode node : nodes) {
Expand All @@ -268,7 +270,6 @@ public String listArticles() {
row.put("caption", node.getCaption());
row.put("credit", node.getCredit());
dataArray.add(JSONObject.fromObject(row).toString());
// dataArray.add(JSONObject.fromObject(row).toString());
}
listNodes.put("Records", dataArray);
returnCode = LIST_SUCCESS;
Expand Down Expand Up @@ -296,7 +297,7 @@ public String deleteArticle() {
String nodeid = (String) httpServletRequest.getParameter("ID");
if (nodeid != null) {
int intNodeId = Integer.parseInt(nodeid);
nodeDao.deleteNodeById(intNodeId);
nodeService.deleteNodeById(intNodeId);
deleteStatus.put("Result", "OK");
returnCode = DELETE_SUCCESS;
}
Expand Down
16 changes: 8 additions & 8 deletions timeLine/src/com/hywang/timeline/actions/TimeLineInitAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.stereotype.Controller;

import com.hywang.timeline.entity.TimeLineNode;
import com.hywang.timeline.persistence.dao.TimlineNodeDAO;
import com.hywang.timeline.services.TimlineNodeService;
import com.hywang.timeline.utils.PropertiesUtil;
import com.hywang.timeline.utils.time.TimeMeasure;
import com.hywang.timeline.utils.timeline.TimeLineNodeUtil;
Expand All @@ -25,15 +25,15 @@ public class TimeLineInitAction extends BaseAction {

private JSONObject timelineObj = null;

@Autowired
private TimlineNodeDAO timelineDAO;
private TimlineNodeService nodeService;

public TimlineNodeDAO getTimelineDAO() {
return timelineDAO;
public TimlineNodeService getNodeService() {
return nodeService;
}

public void setTimelineDAO(TimlineNodeDAO timelineDAO) {
this.timelineDAO = timelineDAO;
@Autowired
public void setNodeService(TimlineNodeService nodeService) {
this.nodeService = nodeService;
}

public JSONObject getTimelineObj() {
Expand Down Expand Up @@ -66,7 +66,7 @@ public String execute() throws Exception {
TimeMeasure.begin(initArticles_key);
try {

allNodes = timelineDAO.getAllNodes();
allNodes = nodeService.getAllNodes();
TimeMeasure.step(initArticles_key, "Loading articles from database");
for (TimeLineNode node : allNodes) {
if (node.getIsStartNode()) { // if node is start node,init the
Expand Down
25 changes: 13 additions & 12 deletions timeLine/src/com/hywang/timeline/actions/UserAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import com.hywang.timeline.entity.LogonValidationInfo;
import com.hywang.timeline.entity.User;
import com.hywang.timeline.persistence.dao.UserDAO;
import com.hywang.timeline.services.UserService;
import com.hywang.timeline.utils.cipher.CipherUtil;
import com.hywang.timeline.utils.web.CookiesManager;
import com.opensymphony.xwork2.ActionContext;
Expand All @@ -28,16 +28,17 @@ public class UserAction extends BaseAction {

private String username;

@Autowired
private UserDAO udao;

private UserService userService;


public UserDAO getUdao() {
return udao;
public UserService getUserService() {
return userService;
}

public void setUdao(UserDAO udao) {
this.udao = udao;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}

public String getUsername() {
Expand All @@ -59,7 +60,7 @@ public String login() {

User user = null;
try {
user = udao.getUserByName(uname);
user = userService.getUserByName(uname,true); // Need to init children as a proxy,so the lazy load will be work
if (user != null) {
if (CipherUtil.validatePassword(user.getUserPwd(), pwd)) {
success = true;
Expand All @@ -68,7 +69,7 @@ public String login() {
// if remember,need to record to database
if (remember != null && !"".equals(remember)) {
if ("true".equals(remember)) { //$NON-NLS-N$
rememberUser(uname, user, udao);
rememberUser(uname, user, userService);
}
}
}
Expand All @@ -95,7 +96,7 @@ public String logout(){
System.out.println("delete user from session: "+session.getId());
String sessionid = CookiesManager.getValue(httpServletRequest.getCookies(), "sessionid");
try {
udao.deleteAutoLoginState(username, sessionid);
userService.deleteAutoLoginState(username, sessionid);
returnCode=ActionSupport.SUCCESS;
} catch (Exception e) {
logger.error(e);
Expand All @@ -104,7 +105,7 @@ public String logout(){
return returnCode;
}

private void rememberUser(String uname, User user, UserDAO udao)
private void rememberUser(String uname, User user, UserService udao)
throws Exception {
String sessionId = httpServletRequest.getSession().getId();
generateAutoLogonCookies(user, sessionId);
Expand Down Expand Up @@ -157,7 +158,7 @@ public String register() {


try {
udao.addUser(user);
userService.addUser(user);
httpServletRequest.getSession().setAttribute("user", user);
returnCode=ActionSupport.SUCCESS;
} catch (Exception e) {
Expand Down
18 changes: 10 additions & 8 deletions timeLine/src/com/hywang/timeline/filter/UserAutoLogonFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hywang.timeline.entity.User;
import com.hywang.timeline.persistence.dao.UserDAO;
import com.hywang.timeline.services.UserService;

public class UserAutoLogonFilter implements Filter {

private UserDAO userDao;
private UserService userService;

public UserDAO getUserDao() {
return userDao;
public UserService getUserService() {
return userService;
}

public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}

public void destroy() {
Expand Down Expand Up @@ -78,7 +80,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
}
try {
isAutoLogin = userDao.getAutoLoginState(username, sessionid);// 如果在数据库中找到了相应记录,则说明可以自动登录。
isAutoLogin = userService.getAutoLoginState(username, sessionid);// 如果在数据库中找到了相应记录,则说明可以自动登录。
if (isAutoLogin) {
user.setId(Integer.parseInt(userid));
user.setUserName(username);
Expand All @@ -100,7 +102,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha

public void init(FilterConfig config) throws ServletException {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
userDao= (UserDAO) wac.getBean("userDao");
userService= (UserService) wac.getBean("userService");
}

}
62 changes: 0 additions & 62 deletions timeLine/src/com/hywang/timeline/persistence/dao/HibernateDAO.java

This file was deleted.

Loading

0 comments on commit c8d3f01

Please sign in to comment.