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.file.CmsRequestContext;
031import org.opencms.i18n.CmsMessageContainer;
032import org.opencms.i18n.CmsMessages;
033import org.opencms.util.CmsStringUtil;
034
035import java.util.ArrayList;
036import java.util.List;
037import java.util.Locale;
038
039/**
040 * Base report class.<p>
041 *
042 * @since 6.0.0
043 */
044public abstract class A_CmsReport implements I_CmsReport {
045
046    /** Contains all error messages generated by the report. */
047    private List<Object> m_errors = new ArrayList<Object>();
048
049    /** Time of last report entry. */
050    private long m_lastEntryTime;
051
052    /** The locale this report is written in. */
053    private Locale m_locale;
054
055    /** The default report message bundle. */
056    private CmsMessages m_messages;
057
058    /** The original site root of the user who started this report. */
059    private String m_siteRoot;
060
061    /** Runtime of the report. */
062    private long m_starttime;
063
064    /** Contains all warning messages generated by the report. */
065    private List<Object> m_warnings = new ArrayList<Object>();
066
067    /**
068     * @see org.opencms.report.I_CmsReport#addError(java.lang.Object)
069     */
070    public void addError(Object obj) {
071
072        m_errors.add(obj);
073    }
074
075    /**
076     * @see org.opencms.report.I_CmsReport#addWarning(java.lang.Object)
077     */
078    public void addWarning(Object obj) {
079
080        m_warnings.add(obj);
081    }
082
083    /**
084     * @see org.opencms.report.I_CmsReport#formatRuntime()
085     */
086    public String formatRuntime() {
087
088        return CmsStringUtil.formatRuntime(getRuntime());
089    }
090
091    /**
092     * @see org.opencms.report.I_CmsReport#getErrors()
093     */
094    public List<Object> getErrors() {
095
096        return m_errors;
097    }
098
099    /**
100     * @see org.opencms.report.I_CmsReport#getLastEntryTime()
101     */
102    public long getLastEntryTime() {
103
104        return m_lastEntryTime;
105    }
106
107    /**
108     * @see org.opencms.report.I_CmsReport#getLocale()
109     */
110    public Locale getLocale() {
111
112        return m_locale;
113    }
114
115    /**
116     * @see org.opencms.report.I_CmsReport#getReportUpdate(org.opencms.report.I_CmsReportUpdateFormatter)
117     */
118    public String getReportUpdate(I_CmsReportUpdateFormatter formatter) {
119
120        return getReportUpdate();
121    }
122
123    /**
124     * @see org.opencms.report.I_CmsReport#getRuntime()
125     */
126    public long getRuntime() {
127
128        return System.currentTimeMillis() - m_starttime;
129    }
130
131    /**
132     * Returns the original site root of the user who started this report,
133     * or <code>null</code> if the original site root has not been set.<p>
134     *
135     * @return the original site root of the user who started this report
136     */
137    public String getSiteRoot() {
138
139        return m_siteRoot;
140    }
141
142    /**
143     * @see org.opencms.report.I_CmsReport#getWarnings()
144     */
145    public List<Object> getWarnings() {
146
147        return m_warnings;
148    }
149
150    /**
151     * @see org.opencms.report.I_CmsReport#hasError()
152     */
153    public boolean hasError() {
154
155        return (m_errors.size() > 0);
156    }
157
158    /**
159     * @see org.opencms.report.I_CmsReport#hasWarning()
160     */
161    public boolean hasWarning() {
162
163        return (m_warnings.size() > 0);
164    }
165
166    /**
167     * @see org.opencms.report.I_CmsReport#print(org.opencms.i18n.CmsMessageContainer)
168     */
169    public void print(CmsMessageContainer container) {
170
171        print(container.key(getLocale()), FORMAT_DEFAULT);
172    }
173
174    /**
175     * @see org.opencms.report.I_CmsReport#print(org.opencms.i18n.CmsMessageContainer, int)
176     */
177    public void print(CmsMessageContainer container, int format) {
178
179        print(container.key(getLocale()), format);
180    }
181
182    /**
183     * @see org.opencms.report.I_CmsReport#println(org.opencms.i18n.CmsMessageContainer)
184     */
185    public void println(CmsMessageContainer container) {
186
187        println(container.key(getLocale()), FORMAT_DEFAULT);
188    }
189
190    /**
191     * @see org.opencms.report.I_CmsReport#println(org.opencms.i18n.CmsMessageContainer, int)
192     */
193    public void println(CmsMessageContainer container, int format) {
194
195        println(container.key(getLocale()), format);
196    }
197
198    /**
199     * @see org.opencms.report.I_CmsReport#printMessageWithParam(org.opencms.i18n.CmsMessageContainer,Object)
200     */
201    public void printMessageWithParam(CmsMessageContainer container, Object param) {
202
203        print(container, I_CmsReport.FORMAT_NOTE);
204        print(Messages.get().container(Messages.RPT_ARGUMENT_1, param));
205        print(Messages.get().container(Messages.RPT_DOTS_0));
206    }
207
208    /**
209     * @see org.opencms.report.I_CmsReport#printMessageWithParam(int,int,org.opencms.i18n.CmsMessageContainer,Object)
210     */
211    public void printMessageWithParam(int m, int n, CmsMessageContainer container, Object param) {
212
213        print(
214            Messages.get().container(Messages.RPT_SUCCESSION_2, String.valueOf(m), String.valueOf(n)),
215            I_CmsReport.FORMAT_NOTE);
216        printMessageWithParam(container, param);
217    }
218
219    /**
220     * Removes the report site root prefix from the absolute path in the resource name,
221     * that is adjusts the resource name for the report site root.<p>
222     *
223     * If the site root for this report has not been set,
224     * or the resource name does not start with the report site root,
225     * the name it is left untouched.<p>
226     *
227     * @param resourcename the resource name (full path)
228     *
229     * @return the resource name adjusted for the report site root
230     *
231     * @see CmsRequestContext#removeSiteRoot(String)
232     */
233    public String removeSiteRoot(String resourcename) {
234
235        if (m_siteRoot == null) {
236            // site root has not been set
237            return resourcename;
238        }
239
240        String siteRoot = CmsRequestContext.getAdjustedSiteRoot(m_siteRoot, resourcename);
241        if ((siteRoot.equals(m_siteRoot)) && resourcename.startsWith(siteRoot)) {
242            resourcename = resourcename.substring(siteRoot.length());
243        }
244        return resourcename;
245    }
246
247    /**
248     * @see org.opencms.report.I_CmsReport#resetRuntime()
249     */
250    public void resetRuntime() {
251
252        m_starttime = System.currentTimeMillis();
253    }
254
255    /**
256     * Returns the default report message bundle.<p>
257     *
258     * @return the default report message bundle
259     */
260    protected CmsMessages getMessages() {
261
262        return m_messages;
263    }
264
265    /**
266     * Initializes some member variables for this report.<p>
267     *
268     * @param locale the locale for this report
269     * @param siteRoot the site root of the user who started this report (may be <code>null</code>)
270     */
271    protected void init(Locale locale, String siteRoot) {
272
273        m_starttime = System.currentTimeMillis();
274        m_locale = locale;
275        m_siteRoot = siteRoot;
276        m_messages = Messages.get().getBundle(locale);
277    }
278
279    /**
280     * Prints a String to the report.<p>
281     *
282     * @param value the String to add
283     */
284    protected void print(String value) {
285
286        print(value, FORMAT_DEFAULT);
287    }
288
289    /**
290     * Prints a String to the report, using the indicated formatting.<p>
291     *
292     * Use the constants starting with <code>FORMAT</code> from this interface
293     * to indicate which formatting to use.<p>
294     *
295     * @param value the message container to add
296     * @param format the formatting to use for the output
297     */
298    protected abstract void print(String value, int format);
299
300    /**
301     * Prints a String with line break to the report.<p>
302     *
303     * @param value the message container to add
304     */
305    protected void println(String value) {
306
307        println(value, FORMAT_DEFAULT);
308    }
309
310    /**
311     * Prints a String with line break to the report, using the indicated formatting.<p>
312     *
313     * Use the constants starting with <code>C_FORMAT</code> from this interface
314     * to indicate which formatting to use.<p>
315     *
316     * @param value the String to add
317     * @param format the formatting to use for the output
318     */
319    protected void println(String value, int format) {
320
321        print(value, format);
322        println();
323    }
324
325    /**
326     * Sets the time of the last report entry.<p>
327     *
328     * @param time the time of the actual entry
329     */
330    protected void setLastEntryTime(long time) {
331
332        m_lastEntryTime = time;
333    }
334}