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
56
57 public class ThreadView<RD extends RenderableDefinition> extends AbstractThreadAction<RD> {
58
59 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ThreadView.class);
60
61 protected static final String PROPERTYNAME = "threadId";
62 private long page;
63 private PagedResult messages;
64
65 protected void setMessages(PagedResult messages) {
66 this.messages = messages;
67 }
68
69 public ThreadView(Node content, RD renderable, RenderingModel<?> parent) {
70 super(content, renderable, parent);
71 }
72
73 public ThreadView(Node content, RD renderable, RenderingModel<?> parent, ForumManager forumManager, ForumConfiguration config) {
74 super(content, renderable, parent, forumManager, config);
75 }
76
77 public void setPage(long page) {
78 this.page = page;
79 }
80
81 @Override
82 public String execute() {
83 try {
84 if (getNode().hasProperty(PROPERTYNAME)) {
85 threadId = PropertyUtil.getString(getNode(),PROPERTYNAME);
86 }
87 if (threadId == null) {
88 throw new IllegalStateException("No thread id given");
89 }
90 thread = forumManager.getThread(threadId);
91 forum = forumManager.getForumFromThread(thread);
92
93 if (page < 1) {
94 page = 1;
95 }
96 messages = forumManager.getMessages(thread, page);
97
98 return "success";
99 } catch (RepositoryException e) {
100
101 throw new RuntimeException(e);
102
103 }
104 }
105
106 public Collection<Node> getMessages() throws RepositoryException {
107 return messages.getNodePage();
108 }
109
110 public long getPageCount() {
111 if (forumConfig.getMessagesPerPage() < 1) {
112 return 1;
113 }
114
115 return (long) Math.ceil((double) messages.getTotalNodeCount() / (double) forumConfig.getMessagesPerPage());
116 }
117 }