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, 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.wrapper;
029
030import org.opencms.i18n.I_CmsMessageBundle;
031import org.opencms.report.I_CmsReport;
032
033import java.util.Arrays;
034import java.util.Collection;
035
036/**
037 * Wrapper for writing reports.
038 *
039 * It allows to write the same messages to multiple reports and has high-level interface for writing.
040 *
041 * @author Daniel Seidel
042 *
043 * @version $Revision: 1.0 $
044 *
045 * @since 12.0.0
046 */
047public class CmsReportWrapper {
048
049    /** The reports to write to. */
050    private Collection<I_CmsReport> m_reports;
051
052    /** The message bundle to read the messages from. */
053    private I_CmsMessageBundle m_messages;
054
055    /**
056     * Constructor for the wrapper.
057     *
058     * @param messages the message bundle to read the printed messages from.
059     * @param reports the reports to write to.
060     */
061    public CmsReportWrapper(I_CmsMessageBundle messages, Collection<I_CmsReport> reports) {
062
063        m_messages = messages;
064        m_reports = reports;
065    }
066
067    /**
068     * Constructor for the wrapper.
069     *
070     * @param messages the message bundle to read the printed messages from.
071     * @param report a sequence of reports to write the messages to.
072     */
073    public CmsReportWrapper(I_CmsMessageBundle messages, I_CmsReport... report) {
074
075        m_messages = messages;
076        m_reports = Arrays.asList(report);
077    }
078
079    /**
080     * Adds a warning to the report (invisible in the GUI).
081     * @param warning the warning to add.
082     */
083    public void reportAddWarning(Object warning) {
084
085        for (I_CmsReport r : m_reports) {
086            r.addWarning(warning);
087        }
088    }
089
090    /**
091     * Print a message in default style.
092     * @param message the message
093     * @param params the parameters
094     */
095    public void reportDefault(String message, Object... params) {
096
097        for (I_CmsReport r : m_reports) {
098            r.println(m_messages.container(message, params));
099        }
100
101    }
102
103    /**
104     * Print a message in default style without linebreak.
105     * @param message the message
106     * @param params the parameters
107     */
108    public void reportDefaultNoBreak(String message, Object... params) {
109
110        for (I_CmsReport r : m_reports) {
111            r.print(m_messages.container(message, params));
112        }
113
114    }
115
116    /**
117     * Report failed.
118     * @param withDots with dots or only the word.
119     */
120    public void reportFailed(boolean withDots) {
121
122        for (I_CmsReport r : m_reports) {
123            r.println(
124                DefaultReportMessages.get().container(
125                    withDots ? DefaultReportMessages.REPORT_FAILED_0 : DefaultReportMessages.REPORT_FAILED_NO_DOTS_0),
126                I_CmsReport.FORMAT_ERROR);
127        }
128    }
129
130    /**
131     * Report failed.
132     * @param message the message to print
133     * @param params parameters of the message
134     */
135    public void reportFailed(String message, Object... params) {
136
137        for (I_CmsReport r : m_reports) {
138            if (null != message) {
139                r.print(m_messages.container(message, params), I_CmsReport.FORMAT_ERROR);
140            }
141            r.println(
142                DefaultReportMessages.get().container(DefaultReportMessages.REPORT_FAILED_0),
143                I_CmsReport.FORMAT_ERROR);
144        }
145    }
146
147    /**
148     * Print a message as headline.
149     * @param message the message
150     * @param params the parameters
151     */
152    public void reportHeadline(String message, Object... params) {
153
154        for (I_CmsReport r : m_reports) {
155            r.println(m_messages.container(message, params), I_CmsReport.FORMAT_HEADLINE);
156        }
157    }
158
159    /**
160     * Print a message as headline.
161     * @param message the message
162     * @param params the parameters
163     */
164    public void reportHeadlineNoBreak(String message, Object... params) {
165
166        for (I_CmsReport r : m_reports) {
167            r.print(m_messages.container(message, params), I_CmsReport.FORMAT_HEADLINE);
168        }
169    }
170
171    /**
172     * Prints an empty line.
173     */
174    public void reportNewline() {
175
176        for (I_CmsReport r : m_reports) {
177            r.println();
178        }
179    }
180
181    /**
182     * Print a message as note.
183     * @param message the message
184     * @param params the parameters
185     */
186    public void reportNote(String message, Object... params) {
187
188        for (I_CmsReport r : m_reports) {
189            r.println(m_messages.container(message, params), I_CmsReport.FORMAT_NOTE);
190        }
191
192    }
193
194    /**
195     * Print a message as note without linebreak.
196     * @param message the message
197     * @param params the parameters
198     */
199    public void reportNoteNoBreak(String message, Object... params) {
200
201        for (I_CmsReport r : m_reports) {
202            r.print(m_messages.container(message, params), I_CmsReport.FORMAT_NOTE);
203        }
204
205    }
206
207    /**
208     * Report ok.
209     * @param withDots with dots or only the word.
210     */
211    public void reportOk(boolean withDots) {
212
213        for (I_CmsReport r : m_reports) {
214            r.println(
215                DefaultReportMessages.get().container(
216                    withDots ? DefaultReportMessages.REPORT_OK_0 : DefaultReportMessages.REPORT_OK_NO_DOTS_0),
217                I_CmsReport.FORMAT_OK);
218        }
219    }
220
221    /**
222     * Report ok.
223     * @param message the message to print
224     * @param params parameters of the message
225     */
226    public void reportOk(String message, Object... params) {
227
228        for (I_CmsReport r : m_reports) {
229            if (null != message) {
230                r.print(m_messages.container(message, params), I_CmsReport.FORMAT_OK);
231            }
232            r.println(DefaultReportMessages.get().container(DefaultReportMessages.REPORT_OK_0), I_CmsReport.FORMAT_OK);
233        }
234    }
235
236    /**
237     * Report ok.
238     * @param withDots with dots or only the word.
239     */
240    public void reportSkipped(boolean withDots) {
241
242        for (I_CmsReport r : m_reports) {
243            r.println(
244                DefaultReportMessages.get().container(
245                    withDots ? DefaultReportMessages.REPORT_SKIPPED_0 : DefaultReportMessages.REPORT_SKIPPED_NO_DOTS_0),
246                I_CmsReport.FORMAT_WARNING);
247        }
248    }
249
250    /**
251     * Report skipped.
252     * @param message the message to print
253     * @param params parameters of the message
254     */
255    public void reportSkipped(String message, Object... params) {
256
257        for (I_CmsReport r : m_reports) {
258            if (null != message) {
259                r.print(m_messages.container(message, params), I_CmsReport.FORMAT_WARNING);
260            }
261            r.println(
262                DefaultReportMessages.get().container(DefaultReportMessages.REPORT_SKIPPED_0),
263                I_CmsReport.FORMAT_WARNING);
264        }
265
266    }
267
268    /**
269     * Print a message in warning style.
270     * @param message the message
271     * @param params the parameters
272     */
273    public void reportWarning(String message, Object... params) {
274
275        for (I_CmsReport r : m_reports) {
276            r.println(m_messages.container(message, params), I_CmsReport.FORMAT_WARNING);
277        }
278    }
279
280    /**
281     * Print a message in warning style.
282     * @param message the message
283     * @param params the parameters
284     */
285    public void reportWarningNoBreak(String message, Object... params) {
286
287        for (I_CmsReport r : m_reports) {
288            r.println(m_messages.container(message, params), I_CmsReport.FORMAT_WARNING);
289        }
290    }
291}