Skip to content

Commit

Permalink
implemented basic user/posts/timeline services
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerh committed Apr 19, 2009
1 parent f622f57 commit bc5feb1
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.aurifa.demo.strutter.model.User;
import org.aurifa.demo.strutter.service.MessageService;
import org.aurifa.demo.strutter.service.UserService;
import org.aurifa.demo.strutter.exception.NonExistentUserException;
import org.aurifa.demo.strutter.helper.Replicator;
import org.aurifa.demo.strutter.helper.HibernateLazyKillingReplicator;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.convention.annotation.Results;
Expand All @@ -15,6 +18,7 @@

import javax.annotation.Resource;
import java.util.List;
import java.util.ArrayList;

/**
* <code>PostsController</code>
Expand All @@ -23,15 +27,15 @@
* @version $Id: $
*/
@Results({
@Result(name="success", type="redirectAction", params = {"actionName", "posts"})
@Result(name="success", type="redirectAction", params = {"actionName", "posts/%{id}"})
})
@Transactional
public class PostsController extends ValidationAwareSupport implements ModelDriven<Object>, Validateable {

private Message model = new Message();
private Long id;
private String author;
private List<Message> list;
private User model = new User();
private Message message = new Message();
private String id;
private List<Message> messages = new ArrayList<Message>();

@Resource
MessageService messageService;
Expand All @@ -47,83 +51,98 @@ public void setUserService(UserService userService) {
this.userService = userService;
}

// GET /user/1
// GET /posts/1
public HttpHeaders show() {
return new DefaultHttpHeaders("show").disableCaching();
try {
List<Message> temp = messageService.getPosts(model.getAlias());
Replicator r = new HibernateLazyKillingReplicator();
for ( Message m : temp) {
messages.add(r.deepCopy(m));
}
} catch (NonExistentUserException e) {

}
// messages = messageService.getTestMessages();
return new DefaultHttpHeaders("show").setLocationId(model.getAlias()).disableCaching();
}

// GET /user

public HttpHeaders index() {
list = messageService.findAll();
return new DefaultHttpHeaders("index").disableCaching();
try {
List<Message> temp = messageService.getPosts(model.getAlias());
Replicator r = new HibernateLazyKillingReplicator();
for ( Message m : temp) {
messages.add(r.deepCopy(m));
}
} catch (NonExistentUserException e) {

}
// messages = messageService.getTestMessages();
return new DefaultHttpHeaders("show").setLocationId(model.getAlias()).disableCaching();
}

// POST /user

// POST /posts
public HttpHeaders create() {
User user = userService.get(model.getAuthor().getAlias());
if ( user != null) {
model.setAuthor(user);
model = messageService.saveOrUpdate(model);
if ( model != null) {
message.setAuthor(model);
message = messageService.saveOrUpdate(message);
addActionMessage("New Message created successfully");
} else {
addActionError("User '"+ model.getAuthor().getAlias() + "' is unknown");
addActionError("User '"+ model.getAlias() + "' is unknown");
return new DefaultHttpHeaders("error");
}

return new DefaultHttpHeaders("success").setLocationId(model.getId());
}

// UPDATE /user/1
public String update() {
model = messageService.saveOrUpdate(model);
addActionMessage("Message updated successfully");
return "success";
return new DefaultHttpHeaders("success").setLocationId(model.getAlias());
}


// GET /user/1/edit
public String edit() {
return "edit";
// GET /posts/1/editNew
public HttpHeaders editNew() {
message = new Message();
//return "editNew";
return new DefaultHttpHeaders("editNew").setLocationId(model.getAlias()).disableCaching();
}


// GET /user/new
public String editNew() {
model = new Message();
return "editNew";
}

// GET /user/1/deleteConfirm
// GET /posts/1/deleteConfirm
public String deleteConfirm() {
return "deleteConfirm";
}

// DELETE /user/1
// DELETE /posts/1
public String destroy() {
messageService.delete(model);
message = messageService.get(message.getId());
messageService.delete(message);
addActionMessage("Message removed successfully");
return "success";
}


public void setId(Long id) {

public void setId(String id) {
if ( id != null) {
model = messageService.get(id);
model = userService.get(id);
}

this.id = id;
}

public void setAuthor(String author) {
this.author = author;
public String getId() {
return id;
}

public Message getMessage() {
return message;
}

public void setMessage(Message message) {
this.message = message;
}

public Object getModel() {
return (list != null ? list : model);
return (messages != null ? messages : model);
}

public void validate() {
if ( model.getText() == null || model.getText().length() == 0) {
if ( message.getText() == null || message.getText().length() == 0) {
addFieldError("text", "The message text is empty");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,79 @@
package org.aurifa.demo.strutter.action;

import org.aurifa.demo.strutter.service.MessageService;
import org.aurifa.demo.strutter.service.UserService;
import org.aurifa.demo.strutter.model.Message;
import org.aurifa.demo.strutter.model.User;
import org.aurifa.demo.strutter.helper.Replicator;
import org.aurifa.demo.strutter.helper.HibernateLazyKillingReplicator;
import org.aurifa.demo.strutter.exception.NonExistentUserException;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ModelDriven;

import java.util.List;
import java.util.ArrayList;

/**
* <code>TimelineController</code>
*
* @author <a href="mailto:hermanns@aixcept.de">Rainer Hermanns</a>
* @version $Id: $
* @author Rainer Hermanns
*/
@Results({
@Result(name="success", type="redirectAction", params = {"actionName", "timeline"})
})
@Transactional
public class TimelineController implements ModelDriven<Object> {

@Resource
UserService userService;

@Resource
MessageService messageService;

User model = new User();
List<Message> list;

List<Message> messages = new ArrayList<Message>();
String id;

public void setUserService(UserService userService) {
this.userService = userService;
}

public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}

// GET /timeline/1
public HttpHeaders show() {
List<Message> temp = messageService.getTimeline(model.getAlias());
Replicator r = new HibernateLazyKillingReplicator();
for ( Message m : temp) {
messages.add(r.deepCopy(m));
}
return new DefaultHttpHeaders("show").disableCaching();

}

// GET /timeline
public HttpHeaders index() {
return new DefaultHttpHeaders("index").disableCaching();
}

public Object getModel() {
return list != null ? list : model;
return messages;
}

public void setId(String id) {
if ( id != null) {
model = userService.get(id);
}
this.id = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
@Transactional
public class UserController extends ValidationAwareSupport implements ModelDriven<Object>, Validateable {

private User model = new User();
private String id;
private User model = new User();
private User user;
private List<User> list;

@Resource
Expand Down Expand Up @@ -63,6 +64,8 @@ public HttpHeaders create() {

// UPDATE /user/1
public String update() {
model.setRealname(user.getRealname());
model.setShortBio(user.getShortBio());
model = userService.saveOrUpdate(model);
addActionMessage("User updated successfully");
return "success";
Expand All @@ -78,6 +81,7 @@ public String edit() {
// GET /user/new
public String editNew() {
model = new User();
user = model;
return "editNew";
}

Expand All @@ -101,11 +105,19 @@ public Object getModel() {
public void setId(String id) {
if ( id != null) {
model = userService.get(id);
user = model;
}

this.id = id;
}

public void setUser(User user) {
this.user = user;
}

public User getUser() {
return user;
}

public void validate() {
if ( model.getAlias() == null || model.getAlias().length() == 0) {
addFieldError("alias", "The alias name is empty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javax.annotation.Resource;
import java.util.List;
import java.util.ArrayList;

@Service
public class MessageService extends GenericEntityService<Message, Long> {
Expand Down Expand Up @@ -60,4 +61,17 @@ public List<Message> getPosts(String authorName) throws NonExistentUserException
}
}


public List<Message> getTestMessages() {
List<Message> msgs = new ArrayList<Message>();
for ( int i = 0; i < 50; i++) {
User user = new User("rhaix", "Rainer Hermanns", "Java Nerd");
Message m = new Message(user, "My Message");
msgs.add(m);

}
return msgs;


}
}
4 changes: 2 additions & 2 deletions server/src/main/resources/strutter-service-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

<bean id="defaultDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:aname"/>
<!--<property name="url" value="jdbc:hsqldb:file:./strutter;shutdown=true"/>-->
<!--<property name="url" value="jdbc:hsqldb:mem:aname"/>-->
<property name="url" value="jdbc:hsqldb:file:./strutter;shutdown=true"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Message ${id}</title>
<title>Message ${message.id}</title>
</head>
<body>
<form action="../${id}?_method=DELETE" method="post">
<form action="../${message.id}?_method=DELETE" method="post">
<p>
Are you sure you want to delete message ${id}?
</p>
Expand Down
8 changes: 4 additions & 4 deletions server/src/main/webapp/WEB-INF/content/posts-edit.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Message <s:property value="id" /></title>
<title>Message <s:property value="message.id" /></title>
</head>
<body>
<s:form method="post" action="%{#request.contextPath}/posts/%{id}">
<s:form method="post" action="%{#request.contextPath}/posts/%{message.id}">
<s:hidden name="_method" value="put" />
<table>
<s:textfield name="author.alias" label="Username" disabled="true"/>
<s:textarea name="text" label="Message"/>
<s:textfield name="message.author.alias" label="Username" disabled="true"/>
<s:textarea name="message.text" label="Message"/>
<tr>
<td colspan="2">
<s:submit />
Expand Down
6 changes: 3 additions & 3 deletions server/src/main/webapp/WEB-INF/content/posts-editNew.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<title>New Message</title>
</head>
<body>
<s:form method="post" action="%{#request.contextPath}/posts">
<s:form method="post" action="%{#request.contextPath}/posts/%{id}/create">
<table>
<s:textfield name="author.alias" label="Author"/>
<s:textarea name="text" label="Text"/>
<s:textfield name="message.author.alias" label="Author" value="%{id}" readonly="true"/>
<s:textarea name="message.text" label="Text" cols="40" rows="5" />
<tr>
<td colspan="2">
<s:submit />
Expand Down
Loading

0 comments on commit bc5feb1

Please sign in to comment.