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.workplace.tools.cache;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.ui.apps.cacheadmin.CmsImageCacheHelper;
032import org.opencms.util.CmsFileUtil;
033import org.opencms.widgets.CmsDisplayWidget;
034import org.opencms.workplace.CmsWidgetDialog;
035import org.opencms.workplace.CmsWidgetDialogParameter;
036
037import java.util.ArrayList;
038
039import javax.servlet.http.HttpServletRequest;
040import javax.servlet.http.HttpServletResponse;
041import javax.servlet.jsp.PageContext;
042
043/**
044 * The image cache overview dialog.<p>
045 *
046 * @since 7.0.5
047 */
048public class CmsImageCacheOverviewDialog extends CmsWidgetDialog {
049
050    /** localized messages Keys prefix. */
051    public static final String KEY_PREFIX = "image.stats";
052
053    /** Defines which pages are valid for this dialog. */
054    public static final String[] PAGES = {"page1"};
055
056    /** Image Caches current size. */
057    private String m_curSize;
058
059    /** Image Caches keys. */
060    private String m_keys;
061
062    /** Image Caches variations. */
063    private String m_variations;
064
065    /**
066     * Public constructor with JSP action element.<p>
067     *
068     * @param jsp an initialized JSP action element
069     */
070    public CmsImageCacheOverviewDialog(CmsJspActionElement jsp) {
071
072        super(jsp);
073
074    }
075
076    /**
077     * Public constructor with JSP variables.<p>
078     *
079     * @param context the JSP page context
080     * @param req the JSP request
081     * @param res the JSP response
082     */
083    public CmsImageCacheOverviewDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
084
085        this(new CmsJspActionElement(context, req, res));
086    }
087
088    /**
089     * Commits the edited group to the db.<p>
090     */
091    @Override
092    public void actionCommit() {
093
094        // no saving needed
095        setCommitErrors(new ArrayList());
096    }
097
098    /**
099     * Returns the current size.<p>
100     *
101     * @return the current size
102     */
103    public String getCurSize() {
104
105        return m_curSize;
106    }
107
108    /**
109     * Returns the number of keys.<p>
110     *
111     * @return the number of keys
112     */
113    public String getKeys() {
114
115        return m_keys;
116    }
117
118    /**
119     * Returns the number of variations.<p>
120     *
121     * @return the number of variations
122     */
123    public String getVariations() {
124
125        return m_variations;
126    }
127
128    /**
129     * Sets the current size.<p>
130     *
131     * @param curSize the current size to set
132     */
133    public void setCurSize(String curSize) {
134
135        m_curSize = curSize;
136    }
137
138    /**
139     * Sets the number of keys.<p>
140     *
141     * @param keys the number of keys to set
142     */
143    public void setKeys(String keys) {
144
145        m_keys = keys;
146    }
147
148    /**
149     * Sets the number of variations.<p>
150     *
151     * @param variations the number of variations to set
152     */
153    public void setVariations(String variations) {
154
155        m_variations = variations;
156    }
157
158    /**
159     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
160     *
161     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
162     *
163     * @param dialog the dialog (page) to get the HTML for
164     * @return the dialog HTML for all defined widgets of the named dialog (page)
165     */
166    @Override
167    protected String createDialogHtml(String dialog) {
168
169        StringBuffer result = new StringBuffer(1024);
170
171        // create widget table
172        result.append(createWidgetTableStart());
173
174        // show error header once if there were validation errors
175        result.append(createWidgetErrorHeader());
176
177        if (dialog.equals(PAGES[0])) {
178            // create the widgets for the first dialog page
179            result.append(dialogBlockStart(key(Messages.GUI_IMAGECACHE_LABEL_STATS_BLOCK_0)));
180            result.append(createWidgetTableStart());
181            result.append(createDialogRowsHtml(0, 1));
182            result.append(createWidgetTableEnd());
183            result.append(dialogBlockEnd());
184            result.append(dialogBlockStart(key(Messages.GUI_IMAGECACHE_LABEL_MEMORY_BLOCK_0)));
185            result.append(createWidgetTableStart());
186            result.append(createDialogRowsHtml(2, 2));
187            result.append(createWidgetTableEnd());
188            result.append(dialogBlockEnd());
189        }
190
191        // close widget table
192        result.append(createWidgetTableEnd());
193
194        return result.toString();
195    }
196
197    /**
198     * @see org.opencms.workplace.CmsWidgetDialog#defaultActionHtmlEnd()
199     */
200    @Override
201    protected String defaultActionHtmlEnd() {
202
203        return "";
204    }
205
206    /**
207     * Creates the list of widgets for this dialog.<p>
208     */
209    @Override
210    protected void defineWidgets() {
211
212        // initialize the cache object to use for the dialog
213        initCacheObject();
214
215        setKeyPrefix(KEY_PREFIX);
216
217        // widgets to display
218        addWidget(new CmsWidgetDialogParameter(this, "keys", PAGES[0], new CmsDisplayWidget()));
219        addWidget(new CmsWidgetDialogParameter(this, "variations", PAGES[0], new CmsDisplayWidget()));
220        addWidget(new CmsWidgetDialogParameter(this, "curSize", PAGES[0], new CmsDisplayWidget()));
221    }
222
223    /**
224     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
225     */
226    @Override
227    protected String[] getPageArray() {
228
229        return PAGES;
230    }
231
232    /**
233     * Initializes the cache object.<p>
234     */
235    protected void initCacheObject() {
236
237        CmsImageCacheHelper helper = new CmsImageCacheHelper(getCms(), false, false, true);
238        setKeys("" + helper.getFilesCount());
239        setVariations("" + helper.getVariationsCount());
240        setCurSize(CmsFileUtil.formatFilesize(helper.getVariationsSize(), getLocale()));
241    }
242
243    /**
244     * @see org.opencms.workplace.CmsWorkplace#initMessages()
245     */
246    @Override
247    protected void initMessages() {
248
249        // add specific dialog resource bundle
250        addMessages(Messages.get().getBundleName());
251        // add default resource bundles
252        super.initMessages();
253    }
254
255    /**
256     * Overridden to set the online help path for this dialog.<p>
257     *
258     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
259     */
260    @Override
261    protected void initWorkplaceMembers(CmsJspActionElement jsp) {
262
263        super.initWorkplaceMembers(jsp);
264        setOnlineHelpUriCustom("/cache/flex/");
265    }
266}