001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.menu;
024:
025: import java.io.IOException;
026: import java.io.StringWriter;
027: import java.sql.SQLException;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Collections;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.StringTokenizer;
034:
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: import org.w3c.dom.Element;
039:
040: import biz.hammurapi.web.ActionServlet;
041: import biz.hammurapi.web.ActionsBase;
042: import biz.hammurapi.web.RequestContext;
043: import biz.hammurapi.xml.dom.AbstractDomObject;
044: import biz.hammurapi.xml.dom.DomSerializable;
045:
046: /**
047: * This class provides actions to render help topics
048: * @author Pavel
049: */
050: public class HelpActions extends ActionsBase {
051:
052: /**
053: * This method returns topic content to be rendered in Ajax tooltip.
054: * Content is truncated to "db/max-tooltip-length" global parameter, which defaults to 2000.
055: * Truncation rules:
056: * 1. if {{tooltip-end}} is present in the first <code>max-tooltip-length</code> character then toolip is truncated at that point.
057: * 2. Otherwise it is truncated at the end of the last paragraph which fits into <code>max-tooltip-length</code>.
058: * 3. If there is no such character then content is truncted at the last word which fits into <code>max-tooltip-length</code>.
059: * If content is truncated then ellipsis (...) appears at the end of the content fragment or value of "tail" parameter.
060: * @param request
061: * @param response
062: * @param servlet
063: * @return
064: * @throws SQLException
065: */
066: public String topicContent(HttpServletRequest request,
067: HttpServletResponse response, ActionServlet servlet)
068: throws SQLException {
069: String idStr = request.getParameter("id");
070: if (idStr == null) {
071: return "Missing help topic id";
072: }
073: RequestContext requestContext = new RequestContext(request);
074: Help help = getMenu(request).findHelp(idStr, requestContext);
075: if (help == null) {
076: return "Invalid topic id: " + idStr;
077: }
078: return help.getTooltip(requestContext);
079: }
080:
081: /**
082: * This method returns help content for a menu item in XML format. Help content is constructed from help topics, which don't
083: * have slash character in their name. Construction rules:
084: * 1. Menu Description goes to DESCRIPTION xml element
085: * 2. Menu name goes to NAME
086: * 3. Menu title goes to TITLE
087: * 4. Help topic with name "main" becomes the top section. other topics become sub-sections in the main topic unless they have slash in their
088: * names. Topics with # in their names become subtopics in the topics with the same prefix before #.
089: * Topics with / in the name are not included to the menu item article but become additional articles mounted under the main article in the
090: * help tree. Topics with names without slashes equal to name elements in "slashed" names are considered parents of those "slashed" topics.
091: * Names with # characters preceding / character are considered illegal.
092: * @param request
093: * @param response
094: * @param servlet
095: * @return
096: * @throws SQLException
097: */
098: public Object page(final HttpServletRequest request,
099: HttpServletResponse response, ActionServlet servlet)
100: throws SQLException {
101: String idStr = request.getParameter("id");
102: RequestContext requestContext = new RequestContext(request);
103: final Help help = idStr == null ? getMenu(request).getHelp(
104: requestContext) : getMenu(request).findHelp(idStr,
105: requestContext);
106: if (help == null) {
107: return "Invalid help id: " + idStr;
108: }
109:
110: final String content = help.getContent(1, requestContext, true,
111: true, false);
112: final Collection path = help.getPath(requestContext);
113:
114: return new DomSerializable() {
115:
116: public void toDom(Element holder) {
117: AbstractDomObject.addTextElement(holder, "content",
118: content);
119: if (!path.isEmpty()) {
120: Element pathElement = AbstractDomObject.addElement(
121: holder, "path");
122: Iterator it = path.iterator();
123: while (it.hasNext()) {
124: Help element = (Help) it.next();
125: Element pee = AbstractDomObject.addTextElement(
126: pathElement, "path-element", element
127: .getName());
128: String title = element.getTitle();
129: if (!isBlank(title)) {
130: pee.setAttribute("title", title);
131: }
132: pee.setAttribute("id", element.getId());
133: }
134: }
135: holder.setAttribute("title", help.getName());
136: }
137:
138: };
139: }
140:
141: /**
142: * @param request
143: * @param response
144: * @param servlet
145: * @return XML representation of menu item subtree excluding trees of sub-items.
146: */
147: public Object menuTree(HttpServletRequest request,
148: HttpServletResponse response, ActionServlet servlet) {
149: String idStr = request.getParameter("parentId");
150: if (idStr == null) {
151: return "Missing menu id";
152: }
153: int id = Integer.parseInt(idStr);
154: Menu menu = getMenu(request).findById(id);
155: if (menu == null) {
156: return "Invalid menu id: " + id;
157: }
158: final RequestContext rc = new RequestContext(request);
159: final Help help = menu.getHelp(rc);
160: return new DomSerializable() {
161:
162: public void toDom(Element holder) {
163: help.toDom(rc, holder);
164: }
165:
166: };
167: }
168:
169: /**
170: * @param request
171: * @param response
172: * @param servlet
173: * @return XML representation of menu item subtree excluding trees of sub-items.
174: */
175: public Object search(HttpServletRequest request,
176: HttpServletResponse response, ActionServlet servlet) {
177: String keywords = request.getParameter("keywords");
178: if (isBlank(keywords)) {
179: return "Search string is blank";
180: }
181: StringTokenizer st = new StringTokenizer(keywords);
182: Collection terms = new ArrayList();
183: while (st.hasMoreTokens()) {
184: terms.add(st.nextToken().toLowerCase());
185: }
186:
187: List findings = new ArrayList();
188: RequestContext requestContext = new RequestContext(request);
189: getMenu(request).getHelp(requestContext).search(terms,
190: requestContext, findings);
191: if (findings.isEmpty()) {
192: return "No results";
193: }
194:
195: Collections.sort(findings);
196:
197: return findings;
198: }
199:
200: /**
201: * @param request
202: * @param response
203: * @param servlet
204: * @return XML representation of menu item subtree excluding trees of sub-items.
205: * @throws SQLException
206: * @throws IOException
207: */
208: public Object singlePage(HttpServletRequest request,
209: HttpServletResponse response, ActionServlet servlet)
210: throws IOException, SQLException {
211: StringWriter ret = new StringWriter();
212: RequestContext requestContext = new RequestContext(request);
213: getMenu(request).getHelp(requestContext)
214: .writeSinglePageContent(requestContext, ret);
215: final String content = ret.toString();
216: return new DomSerializable() {
217:
218: public void toDom(Element holder) {
219: AbstractDomObject.addTextElement(holder, "content",
220: content);
221: }
222:
223: };
224: }
225:
226: /**
227: * @param request
228: * @param response
229: * @param servlet
230: * @return Help content converted to PDF document.
231: */
232: public Object pdf(HttpServletRequest request,
233: HttpServletResponse response, ActionServlet servlet) {
234: //response.setContentType("application/pdf");
235: return "PDF export is not yet implemented";
236: }
237:
238: /**
239: * To generate help frame.
240: * @param request
241: * @param response
242: * @param servlet
243: * @return
244: */
245: public Object menuHelp(final HttpServletRequest request,
246: HttpServletResponse response, ActionServlet servlet) {
247: return new DomSerializable() {
248:
249: public void toDom(Element holder) {
250: String helpId = request.getParameter("id");
251: if (helpId != null) {
252: Help help = getMenu(request).findHelp(helpId,
253: new RequestContext(request));
254: if (help != null) {
255: holder.setAttribute("page", help.getPageId());
256: if (help.isSection()) {
257: holder.setAttribute("topic", help.getId());
258: }
259:
260: }
261: }
262:
263: }
264:
265: };
266: }
267: }
|