001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsReport.java,v $
003: * Date : $Date: 2008-02-27 12:05:45 $
004: * Version: $Revision: 1.30 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.i18n.CmsMessageContainer;
036: import org.opencms.jsp.CmsJspActionElement;
037: import org.opencms.main.CmsException;
038: import org.opencms.main.CmsLog;
039: import org.opencms.main.OpenCms;
040: import org.opencms.report.A_CmsReportThread;
041: import org.opencms.report.I_CmsReport;
042: import org.opencms.util.CmsStringUtil;
043: import org.opencms.util.CmsUUID;
044:
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.http.HttpServletResponse;
047: import javax.servlet.jsp.PageContext;
048:
049: import org.apache.commons.logging.Log;
050:
051: /**
052: * Provides an output window for a CmsReport.<p>
053: *
054: * @author Alexander Kandzior
055: *
056: * @version $Revision: 1.30 $
057: *
058: * @since 6.0.0
059: */
060: public class CmsReport extends CmsMultiDialog {
061:
062: /** Request parameter key for the type of the report. */
063: public static final String PARAM_REPORT_CONTINUEKEY = "reportcontinuekey";
064:
065: /** Request parameter key for the type of the report. */
066: public static final String PARAM_REPORT_TYPE = "reporttype";
067:
068: /** Max. byte size of report output on client. */
069: public static final int REPORT_UPDATE_SIZE = 512000;
070:
071: /** Update time for report reloading. */
072: public static final int REPORT_UPDATE_TIME = 2000;
073:
074: /** The log object for this class. */
075: private static final Log LOG = CmsLog.getLog(CmsReport.class);
076:
077: /** Flag for refreching workplace .*/
078: private String m_paramRefreshWorkplace;
079:
080: /** The key name which contains the localized message for the continue checkbox. */
081: private String m_paramReportContinueKey;
082:
083: /** The type of this report. */
084: private String m_paramReportType;
085:
086: /** The thread to display in this report. */
087: private CmsUUID m_paramThread;
088:
089: /** The next thread to display after this report. */
090: private String m_paramThreadHasNext;
091:
092: /**
093: * Public constructor.<p>
094: *
095: * @param jsp an initialized JSP action element
096: */
097: public CmsReport(CmsJspActionElement jsp) {
098:
099: super (jsp);
100: }
101:
102: /**
103: * Public constructor with JSP variables.<p>
104: *
105: * @param context the JSP page context
106: * @param req the JSP request
107: * @param res the JSP response
108: */
109: public CmsReport(PageContext context, HttpServletRequest req,
110: HttpServletResponse res) {
111:
112: this (new CmsJspActionElement(context, req, res));
113: }
114:
115: /**
116: * Returns the style sheets for the report.<p>
117: *
118: * @param cms the current users context
119: * @return the style sheets for the report
120: */
121: public static String generateCssStyle(CmsObject cms) {
122:
123: StringBuffer result = new StringBuffer(128);
124: result.append("<style type='text/css'>\n");
125: String contents = "";
126: try {
127: contents = new String(cms.readFile(
128: CmsWorkplace.VFS_PATH_COMMONS + "style/report.css")
129: .getContents(), OpenCms.getSystemInfo()
130: .getDefaultEncoding());
131: } catch (Exception e) {
132: // ignore
133: }
134: if (CmsStringUtil.isEmpty(contents)) {
135: // css file not found, create default styles
136: result
137: .append("body { box-sizing: border-box; -moz-box-sizing: border-box; padding: 2px; margin: 0; color: WindowText; background-color: Window; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; }\n");
138: result
139: .append("div.main { box-sizing: border-box; -moz-box-sizing: border-box; color: WindowText; white-space: nowrap; }\n");
140: result
141: .append("span.head { color: #000099; font-weight: bold; }\n");
142: result.append("span.note { color: #666666; }\n");
143: result.append("span.ok { color: #009900; }\n");
144: result
145: .append("span.warn { color: #990000; padding-left: 40px; }\n");
146: result
147: .append("span.err { color: #990000; font-weight: bold; padding-left: 40px; }\n");
148: result
149: .append("span.throw { color: #990000; font-weight: bold; }\n");
150: result.append("span.link1 { color: #666666; }\n");
151: result
152: .append("span.link2 { color: #666666; padding-left: 40px; }\n");
153: result.append("span.link2 { color: #990000; }\n");
154: } else {
155: result.append(contents);
156: }
157: result.append("</style>\n");
158: return result.toString();
159: }
160:
161: /**
162: * Generates the footer for the extended report view.<p>
163: *
164: * @return html code
165: */
166: public static String generatePageEndExtended() {
167:
168: StringBuffer result = new StringBuffer(128);
169: result.append("</div>\n");
170: result.append("</body>\n");
171: result.append("</html>\n");
172: return result.toString();
173: }
174:
175: /**
176: * Generates the footer for the simple report view.<p>
177: *
178: * @return html code
179: */
180: public static String generatePageEndSimple() {
181:
182: StringBuffer result = new StringBuffer(128);
183: result.append("</td></tr>\n");
184: result.append("</table></div>\n");
185: result.append("</body>\n</html>");
186: return result.toString();
187: }
188:
189: /**
190: * Generates the header for the extended report view.<p>
191: *
192: * @param cms the current users context
193: * @param encoding the encoding string
194: *
195: * @return html code
196: */
197: public static String generatePageStartExtended(CmsObject cms,
198: String encoding) {
199:
200: StringBuffer result = new StringBuffer(128);
201: result.append("<html>\n<head>\n");
202: result
203: .append("<meta HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=");
204: result.append(encoding);
205: result.append("'>\n");
206: result.append(generateCssStyle(cms));
207: result.append("</head>\n");
208: result.append("<body style='overflow: auto;'>\n");
209: result.append("<div class='main'>\n");
210: return result.toString();
211: }
212:
213: /**
214: * Generates the header for the simple report view.<p>
215: *
216: * @param wp the workplace instance
217: *
218: * @return html code
219: */
220: public static String generatePageStartSimple(CmsWorkplace wp) {
221:
222: StringBuffer result = new StringBuffer(128);
223: result.append("<html>\n<head>\n");
224: result
225: .append("<meta HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=");
226: result.append(wp.getEncoding());
227: result.append("'>\n");
228: result.append("<link rel='stylesheet' type='text/css' href='");
229: result.append(wp.getStyleUri("workplace.css"));
230: result.append("'>\n");
231: result.append(generateCssStyle(wp.getCms()));
232: result.append("</head>\n");
233: result.append("<body style='background-color:Menu;'>\n");
234: result
235: .append("<div style='vertical-align:middle; height: 100%;'>\n");
236: result
237: .append("<table border='0' style='vertical-align:middle; height: 100%;'>\n");
238: result
239: .append("<tr><td width='40' align='center' valign='middle'><img name='report_img' src='");
240: result.append(CmsWorkplace.getSkinUri());
241: result
242: .append("commons/wait.gif' width='32' height='32' alt=''></td>\n");
243: result.append("<td valign='middle'>");
244: return result.toString();
245: }
246:
247: /**
248: * Returns an initialized CmsReport instance that is read from the request attributes.<p>
249: *
250: * This method is used by dialog elements.
251: * The dialog elements do not initialize their own workplace class,
252: * but use the initialized instance of the "master" class.
253: * This is required to ensure that parameters of the "master" class
254: * can properly be kept on the dialog elements.<p>
255: *
256: * To prevent null pointer exceptions, an empty dialog is returned if
257: * nothing is found in the request attributes.<p>
258: *
259: * @param context the JSP page context
260: * @param req the JSP request
261: * @param res the JSP response
262: * @return an initialized CmsDialog instance that is read from the request attributes
263: */
264: public static CmsReport initCmsReport(PageContext context,
265: HttpServletRequest req, HttpServletResponse res) {
266:
267: CmsReport wp = (CmsReport) req
268: .getAttribute(CmsWorkplace.SESSION_WORKPLACE_CLASS);
269: if (wp == null) {
270: // ensure that we don't get null pointers if the page is directly called
271: wp = new CmsReport(new CmsJspActionElement(context, req,
272: res));
273: }
274: return wp;
275: }
276:
277: /**
278: * Builds a button row with an "Ok", a "Cancel" and a "Details" button.<p>
279: *
280: * This row is displayed when the first report is running.<p>
281: *
282: * @param okAttrs optional attributes for the ok button
283: * @param cancelAttrs optional attributes for the cancel button
284: * @param detailsAttrs optional attributes for the details button
285: * @return the button row
286: */
287: public String dialogButtonsContinue(String okAttrs,
288: String cancelAttrs, String detailsAttrs) {
289:
290: if (CmsStringUtil.isEmptyOrWhitespaceOnly(detailsAttrs)) {
291: detailsAttrs = "";
292: } else {
293: detailsAttrs += " ";
294: }
295: return dialogButtons(new int[] { BUTTON_OK, BUTTON_CANCEL,
296: BUTTON_DETAILS }, new String[] { okAttrs, cancelAttrs,
297: detailsAttrs + "onclick=\"switchOutputFormat();\"" });
298: }
299:
300: /**
301: * Builds a button row with an "Ok", a "Cancel" and a "Details" button.<p>
302: *
303: * This row is used when a single report is running or after the first report has finished.<p>
304: *
305: * @param okAttrs optional attributes for the ok button
306: * @param cancelAttrs optional attributes for the cancel button
307: * @param detailsAttrs optional attributes for the details button
308: * @return the button row
309: */
310: public String dialogButtonsOkCancelDetails(String okAttrs,
311: String cancelAttrs, String detailsAttrs) {
312:
313: if (CmsStringUtil.isEmptyOrWhitespaceOnly(detailsAttrs)) {
314: detailsAttrs = "";
315: } else {
316: detailsAttrs += " ";
317: }
318:
319: if (Boolean.valueOf(getParamThreadHasNext()).booleanValue()
320: && CmsStringUtil
321: .isNotEmpty(getParamReportContinueKey())) {
322: return dialogButtons(
323: new int[] { BUTTON_OK, BUTTON_CANCEL,
324: BUTTON_DETAILS },
325: new String[] {
326: okAttrs,
327: cancelAttrs,
328: detailsAttrs
329: + "onclick=\"switchOutputFormat();\"" });
330: }
331: return dialogButtons(new int[] { BUTTON_OK, BUTTON_DETAILS },
332: new String[] {
333: okAttrs,
334: detailsAttrs
335: + "onclick=\"switchOutputFormat();\"" });
336: }
337:
338: /**
339: * Returns if the workplace must be refreshed.<p>
340: *
341: * @return <code>"true"</code> if the workplace must be refreshed.
342: */
343: public String getParamRefreshWorkplace() {
344:
345: return m_paramRefreshWorkplace;
346: }
347:
348: /**
349: * Returns the key name which contains the localized message for the continue checkbox.<p>
350: *
351: * @return the key name which contains the localized message for the continue checkbox
352: */
353: public String getParamReportContinueKey() {
354:
355: if (m_paramReportContinueKey == null) {
356: m_paramReportContinueKey = "";
357: }
358: return m_paramReportContinueKey;
359: }
360:
361: /**
362: * Returns the type of this report.<p>
363: *
364: * @return the type of this report
365: */
366: public String getParamReportType() {
367:
368: if (m_paramReportType == null) {
369: // the default report type is the simple report
370: setParamReportType(getSettings().getUserSettings()
371: .getWorkplaceReportType());
372: }
373:
374: return m_paramReportType;
375: }
376:
377: /**
378: * Returns the Thread id to display in this report.<p>
379: *
380: * @return the Thread id to display in this report
381: */
382: public String getParamThread() {
383:
384: if ((m_paramThread != null)
385: && (!m_paramThread.equals(CmsUUID.getNullUUID()))) {
386: return m_paramThread.toString();
387: } else {
388: return null;
389: }
390: }
391:
392: /**
393: * Returns if another report is following this report.<p>
394: *
395: * @return <code>"true"</code> if another report is following this report
396: */
397: public String getParamThreadHasNext() {
398:
399: if (m_paramThreadHasNext == null) {
400: m_paramThreadHasNext = "";
401: }
402: return m_paramThreadHasNext;
403: }
404:
405: /**
406: * Returns the part of the report that is ready for output.<p>
407: *
408: * @return the part of the report that is ready for output
409: */
410: public String getReportUpdate() {
411:
412: A_CmsReportThread thread = OpenCms.getThreadStore()
413: .retrieveThread(m_paramThread);
414: if (thread != null) {
415: return thread.getReportUpdate();
416: } else {
417: return "";
418: }
419: }
420:
421: /**
422: * Returns if the report generated an error output.<p>
423: *
424: * @return true if the report generated an error, otherwise false
425: */
426: public boolean hasError() {
427:
428: A_CmsReportThread thread = OpenCms.getThreadStore()
429: .retrieveThread(m_paramThread);
430: if (thread != null) {
431: return thread.hasError();
432: } else {
433: return false;
434: }
435: }
436:
437: /**
438: * Builds the start html of the page, including setting of DOCTYPE and
439: * inserting a header with the content-type.<p>
440: *
441: * This overloads the default method of the parent class.<p>
442: *
443: * @return the start html of the page
444: */
445: public String htmlStart() {
446:
447: return pageHtml(HTML_START, true);
448: }
449:
450: /**
451: * Builds the start html of the page, including setting of DOCTYPE and
452: * inserting a header with the content-type.<p>
453: *
454: * This overloads the default method of the parent class.<p>
455: *
456: * @param loadStyles if true, the defaul style sheet will be loaded
457: * @return the start html of the page
458: */
459: public String htmlStart(boolean loadStyles) {
460:
461: return pageHtml(HTML_START, loadStyles);
462: }
463:
464: /**
465: * Returns true if the report Thread is still alive (i.e. running), false otherwise.<p>
466: *
467: * @return true if the report Thread is still alive
468: */
469: public boolean isAlive() {
470:
471: A_CmsReportThread thread = OpenCms.getThreadStore()
472: .retrieveThread(m_paramThread);
473: if (thread != null) {
474: return thread.isAlive();
475: } else {
476: return false;
477: }
478: }
479:
480: /**
481: * Checks whether this is a simple report.<p>
482: *
483: * @return true, if the type of this report is a "simple"
484: */
485: public boolean isSimpleReport() {
486:
487: return getParamReportType().equalsIgnoreCase(
488: I_CmsReport.REPORT_TYPE_SIMPLE);
489: }
490:
491: /**
492: * Builds the start html of the page, including setting of DOCTYPE and
493: * inserting a header with the content-type.<p>
494: *
495: * This overloads the default method of the parent class.<p>
496: *
497: * @param segment the HTML segment (START / END)
498: * @param loadStyles if true, the defaul style sheet will be loaded
499: * @return the start html of the page
500: */
501: public String pageHtml(int segment, boolean loadStyles) {
502:
503: if (useNewStyle()) {
504: return super .pageHtml(segment, null, getParamTitle());
505: }
506: if (segment == HTML_START) {
507: StringBuffer result = new StringBuffer(512);
508: result
509: .append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n");
510: result.append("<html>\n<head>\n");
511: result
512: .append("<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=");
513: result.append(getEncoding());
514: result.append("\">\n");
515: if (loadStyles) {
516: result
517: .append("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
518: result.append(getStyleUri(getJsp(), "workplace.css"));
519: result.append("\">\n");
520: result.append("<script type=\"text/javascript\">\n");
521: result.append(dialogScriptSubmit());
522: result.append("</script>\n");
523: }
524: return result.toString();
525: } else {
526: return "</html>";
527: }
528:
529: }
530:
531: /**
532: * Returns an optional conclusion text to be displayed below the report output.<p>
533: *
534: * @return an optional conclusion text
535: */
536: public String reportConclusionText() {
537:
538: return "";
539: }
540:
541: /**
542: * Returns an optional introduction text to be displayed above the report output.<p>
543: *
544: * @return an optional introduction text
545: */
546: public String reportIntroductionText() {
547:
548: return "";
549: }
550:
551: /**
552: * Sets if the workplace must be refreshed.<p>
553: *
554: * @param value <code>"true"</code> (String) if the workplace must be refreshed.
555: */
556: public void setParamRefreshWorkplace(String value) {
557:
558: m_paramRefreshWorkplace = value;
559: }
560:
561: /**
562: * Sets the key name which contains the localized message for the continue checkbox.<p>
563: *
564: * @param key the key name which contains the localized message for the continue checkbox
565: */
566: public void setParamReportContinueKey(String key) {
567:
568: m_paramReportContinueKey = key;
569: }
570:
571: /**
572: * Sets the type of this report.<p>
573: *
574: * @param value the type of this report
575: */
576: public void setParamReportType(String value) {
577:
578: m_paramReportType = value;
579: }
580:
581: /**
582: * Sets the Thread id to display in this report.<p>
583: *
584: * @param value the Thread id to display in this report
585: */
586: public void setParamThread(String value) {
587:
588: m_paramThread = CmsUUID.getNullUUID();
589: if (value != null) {
590: try {
591: m_paramThread = new CmsUUID(value);
592: } catch (Exception e) {
593: // can usually be ignored
594: if (LOG.isInfoEnabled()) {
595: LOG.info(Messages.get().getBundle().key(
596: Messages.LOG_THREAD_CREATION_FAILED_1,
597: new Integer(value)), e);
598: }
599: }
600: }
601: }
602:
603: /**
604: * Sets if another report is following this report.<p>
605: *
606: * @param value <code>"true"</code> if another report is following this report
607: */
608: public void setParamThreadHasNext(String value) {
609:
610: m_paramThreadHasNext = value;
611: }
612:
613: /**
614: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
615: */
616: protected void initWorkplaceRequestValues(
617: CmsWorkplaceSettings settings, HttpServletRequest request) {
618:
619: // fill the parameter values in the get/set methods
620: fillParamValues(request);
621: // set the action for the JSP switch
622: if (REPORT_UPDATE.equals(getParamAction())) {
623: setAction(ACTION_REPORT_UPDATE);
624: } else {
625: setAction(ACTION_REPORT_BEGIN);
626: }
627: }
628:
629: /**
630: * Returns always true and does nothing else, has to be implemented.<p>
631: *
632: * @see org.opencms.workplace.CmsMultiDialog#performDialogOperation()
633: */
634: protected boolean performDialogOperation() throws CmsException {
635:
636: throw new CmsException(new CmsMessageContainer(null, ""));
637: }
638: }
|