001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.report;
029
030import org.opencms.main.CmsLog;
031
032import java.util.ArrayList;
033import java.util.List;
034import java.util.Locale;
035
036import org.apache.commons.logging.Log;
037
038import com.google.common.collect.Lists;
039
040/**
041 * Report class for displaying reports to the user in the workplace.<p>
042 *
043 * The difference to the older CmsHtmlReport class is that this can be used from both the old and the new Vaadin-based workplace, without
044 * having to decide when the report is created. Instead, the 'client' requesting report updates has to pass in a report update formatter instance,
045 * which is then used to format the report update for each case.
046 *
047 */
048public class CmsWorkplaceReport extends A_CmsReport {
049
050    /** Format constant for newlines. */
051    public static final int FORMAT_NEWLINE = -1;
052
053    /** Logger instance for this class. */
054    @SuppressWarnings("unused")
055    private static final Log LOG = CmsLog.getLog(CmsWorkplaceReport.class);
056
057    /** The list of report objects e.g. String, CmsPageLink, Exception ... */
058    private List<Object> m_content;
059
060    /**
061     * Counter to remember what is already shown,
062     * indicates the next index of the m_content list that has to be reported.
063     */
064    private int m_indexNext;
065
066    /** The log report to send output to. */
067    private CmsLogReport m_logReport;
068
069    /** If set to <code>true</code> nothing is kept in memory. */
070    private boolean m_transient;
071
072    /**
073     * Constructs a new report using the provided locale for the output language.<p>
074     *
075     * @param locale the locale to use for the output language
076     * @param siteRoot the site root of the user who started this report (may be <code>null</code>)
077     * @param isTransient If set to <code>true</code> nothing is kept in memory
078     * @param logChannel the log channel to send the report output to (or null if this shouldn't be done)
079     */
080    public CmsWorkplaceReport(Locale locale, String siteRoot, boolean isTransient, Object logChannel) {
081
082        init(locale, siteRoot);
083        if (logChannel != null) {
084            m_logReport = new CmsLogReport(locale, logChannel);
085        }
086        m_content = new ArrayList<Object>(256);
087        m_transient = isTransient;
088
089    }
090
091    /**
092     * Constructs a new report using the provided locale for the output language.<p>
093     *
094     * @param locale the locale to use for the output language
095     * @param siteRoot the site root of the user who started this report (may be <code>null</code>)
096     * @param logChannel the log channel to send the report output to (or null if this shouldn't be done)
097     */
098    public CmsWorkplaceReport(Locale locale, String siteRoot, Object logChannel) {
099
100        this(locale, siteRoot, false, logChannel);
101    }
102
103    /**
104     * @see org.opencms.report.I_CmsReport#getReportUpdate()
105     */
106    public synchronized String getReportUpdate() {
107
108        return getReportUpdate(new CmsClassicJavascriptReportUpdateFormatter(getLocale()));
109
110    }
111
112    /**
113     * @see org.opencms.report.A_CmsReport#getReportUpdate(org.opencms.report.I_CmsReportUpdateFormatter)
114     */
115    @Override
116    public synchronized String getReportUpdate(I_CmsReportUpdateFormatter formatter) {
117
118        int indexEnd = m_content.size();
119        List<CmsReportUpdateItem> itemsToFormat = Lists.newArrayList();
120        for (int i = m_indexNext; i < indexEnd; i++) {
121            int pos = m_transient ? 0 : i;
122            Object obj = m_content.get(pos);
123            CmsReportUpdateItem entry = (CmsReportUpdateItem)obj;
124            itemsToFormat.add(entry);
125            if (m_transient) {
126                m_content.remove(m_indexNext);
127            }
128        }
129        String result = formatter.formatReportUpdate(itemsToFormat);
130        m_indexNext = m_transient ? 0 : indexEnd;
131        return result;
132    }
133
134    /**
135     * @see org.opencms.report.A_CmsReport#print(java.lang.String, int)
136     */
137    @Override
138    public synchronized void print(String value, int format) {
139
140        if (m_logReport != null) {
141            m_logReport.print(value, format);
142        }
143        if (format == I_CmsReport.FORMAT_ERROR) {
144            addError(value);
145        } else if (format == I_CmsReport.FORMAT_WARNING) {
146            addWarning(value);
147        }
148        CmsReportUpdateItem message = new CmsReportUpdateItem(CmsReportFormatType.byId(format), value);
149        m_content.add(message);
150        setLastEntryTime(System.currentTimeMillis());
151    }
152
153    /**
154     * @see org.opencms.report.I_CmsReport#println()
155     */
156    public void println() {
157
158        if (m_logReport != null) {
159            m_logReport.println();
160        }
161        print("", FORMAT_NEWLINE);
162    }
163
164    /**
165     * @see org.opencms.report.I_CmsReport#println(java.lang.Throwable)
166     */
167    public synchronized void println(Throwable t) {
168
169        if (m_logReport != null) {
170            m_logReport.println(t);
171        }
172        addError(t.getMessage());
173        m_content.add(new CmsReportUpdateItem(CmsReportFormatType.fmtException, t));
174        setLastEntryTime(System.currentTimeMillis());
175    }
176
177    /**
178     * Rewinds the report update to the start of the report.<p>
179     */
180    public void rewind() {
181
182        m_indexNext = 0;
183    }
184
185}