1 /*
2 * Copyright 2009 :torweg free software group
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */18 package org.torweg.pulse.site.content;
19 20 import java.util.ArrayList;
21 import java.util.List;
22 23 import org.hibernate.Session;
24 importorg.torweg.pulse.bundle.Controller;
25 importorg.torweg.pulse.service.event.RedirectEvent;
26 importorg.torweg.pulse.service.request.Command;
27 importorg.torweg.pulse.service.request.ServiceRequest;
28 importorg.torweg.pulse.site.map.SitemapNode;
29 importorg.torweg.pulse.util.entity.Node;
30 31 /**
32 * abstract base class for content displayers.
33 *
34 * @author Thomas Weber
35 * @version $Revision: 1398 $
36 */37 publicclass AbstractContentDisplayer extendsController {
38 39 /**
40 * actually chooses the right {@code AbstractBasicContent} for the
41 * given {@code Command}.
42 *
43 * @param command
44 * the current {@code Command}
45 * @param s
46 * the Hibernate<sup>TM</sup> {@code Session}
47 * @return the {@code CMSContent} to be displayed
48 */49 publicstaticfinalAbstractBasicContent chooseContent(
50 finalCommand command, final Session s) {
51 /* contentId overrides SitemapNodeID */52 if (command.getParameter("contentId") != null) {
53 Long id = Long.parseLong(command.getParameter("contentId")
54 .getFirstValue());
55 return (AbstractBasicContent) s.get(Content.class, id);
56 } elseif (command.getSitemapID() != null) {
57 Long id = command.getSitemapID();
58 59 SitemapNode node = (SitemapNode) s.get(SitemapNode.class, id);
60 if (node == null) {
61 returnnull;
62 } elseif (node.getView() != null) {
63 return (AbstractBasicContent) node.getView().getContent();
64 } else {
65 /* if the node has no view, find an ancestor with a view */66 while (node.getParent() != null) {
67 node = (SitemapNode) node.getParent();
68 if ((node.getView() != null)
69 && (node.getView().getContent() instanceofAbstractBasicContent)) {
70 return (AbstractBasicContent) node.getView()
71 .getContent();
72 }
73 }
74 }
75 }
76 returnnull;
77 }
78 79 /**
80 * prepares a redirect, if the suffix of the {@code Command} does not
81 * match the suffix of the {@code Content}.
82 *
83 * @param request
84 * the current request
85 * @param abc
86 * the content
87 */88 protectedfinalvoid prepareRedirect(finalServiceRequest request,
89 finalAbstractBasicContent abc) {
90 Command command = request.getCommand();
91 Command rdCommand = command.createCopy();
92 if (command.getHttpParameter("contentId") != null) {
93 rdCommand.addPulseParameter("contentId", command.getHttpParameter(
94 "contentId").getFirstValue());
95 rdCommand.removeHttpParameter("contentId");
96 }
97 rdCommand.setSuffix(abc.getSuffix());
98 RedirectEvent redirect = newRedirectEvent(rdCommand
99 .toCommandURL(request));
100 request.getEventManager().addEvent(redirect);
101 }
102 103 /**
104 * returns the children of the {@code SitemapNode} specified by the
105 * given {@code Command}.
106 *
107 * @param request
108 * the current request
109 * @param s
110 * the hibernate session
111 * @return the {@code SitemapNode}
112 */113 protected List<SitemapNode> getChildrenForContentGroup(
114 finalServiceRequest request, final Session s) {
115 ArrayList<SitemapNode> children = new ArrayList<SitemapNode>();
116 if (request.getCommand().getSitemapID() != null) {
117 SitemapNode sn = (SitemapNode) s.get(SitemapNode.class, request
118 .getCommand().getSitemapID());
119 if ((sn == null) || !sn.hasChildren()) {
120 return children;
121 }
122 for (Node n : sn.getChildren()) {
123 SitemapNode child = (SitemapNode) n;
124 if ((child != null) && (child.getView() != null)
125 && (child.getView().getContent() != null)
126 && child.isVisible()
127 && request.getUser().hasAllowanceFor(child)) {
128 129 child.getView().getContent().initLazyFields();
130 131 /* add to list */132 children.add(child);
133 }
134 }
135 }
136 return children;
137 }
138 139 }
140