1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.forum.frontend.action;
35
36 import info.magnolia.jcr.util.PropertyUtil;
37 import info.magnolia.module.forum.ForumConfiguration;
38 import info.magnolia.module.forum.ForumManager;
39 import info.magnolia.module.forum.PagedResult;
40 import info.magnolia.rendering.model.RenderingModel;
41 import info.magnolia.rendering.template.RenderableDefinition;
42
43 import java.util.Collection;
44
45 import javax.jcr.Node;
46 import javax.jcr.RepositoryException;
47
48
49
50
51
52
53
54
55 public class ThreadView<RD extends RenderableDefinition> extends AbstractThreadAction<RD> {
56
57 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ThreadView.class);
58
59 protected static final String PROPERTYNAME = "threadId";
60 private long page;
61 private PagedResult messages;
62
63 protected void setMessages(PagedResult messages) {
64 this.messages = messages;
65 }
66
67 public ThreadView(Node content, RD renderable, RenderingModel<?> parent) {
68 super(content, renderable, parent);
69 }
70
71 public ThreadView(Node content, RD renderable, RenderingModel<?> parent, ForumManager forumManager, ForumConfiguration config) {
72 super(content, renderable, parent, forumManager, config);
73 }
74
75 public void setPage(long page) {
76 this.page = page;
77 }
78
79 public String execute() {
80 try {
81 if (getContent().hasProperty(PROPERTYNAME)) {
82 threadId = PropertyUtil.getString(getContent(),PROPERTYNAME);
83 }
84 if (threadId == null) {
85 throw new IllegalStateException("No thread id given");
86 }
87 thread = forumManager.getThread(threadId);
88 forum = forumManager.getForumFromThread(thread);
89
90 if (page < 1) {
91 page = 1;
92 }
93 messages = forumManager.getMessages(thread, page);
94
95 return "success";
96 } catch (RepositoryException e) {
97
98 throw new RuntimeException(e);
99
100 }
101 }
102
103 public Collection<Node> getMessages() throws RepositoryException {
104 return messages.getNodePage();
105 }
106
107 public long getPageCount() {
108 if (forumConfig.getMessagesPerPage() < 1) {
109 return 1;
110 }
111
112 return (long) Math.ceil((double) messages.getTotalNodeCount() / (double) forumConfig.getMessagesPerPage());
113 }
114 }