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.flex.CmsFlexCache;
031import org.opencms.flex.CmsFlexController;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsEvent;
034import org.opencms.main.I_CmsEventListener;
035import org.opencms.main.OpenCms;
036import org.opencms.widgets.CmsCheckboxWidget;
037import org.opencms.widgets.CmsRadioSelectWidget;
038import org.opencms.widgets.CmsSelectWidgetOption;
039import org.opencms.workplace.CmsDialog;
040import org.opencms.workplace.CmsWidgetDialog;
041import org.opencms.workplace.CmsWidgetDialogParameter;
042
043import java.util.ArrayList;
044import java.util.Collections;
045import java.util.List;
046
047import javax.servlet.http.HttpServletRequest;
048import javax.servlet.http.HttpServletResponse;
049import javax.servlet.jsp.JspException;
050import javax.servlet.jsp.PageContext;
051
052/**
053 * The flex cache clear dialog.<p>
054 *
055 * @since 7.0.5
056 */
057public class CmsFlexCacheClearDialog extends CmsWidgetDialog {
058
059    /** localized messages Keys prefix. */
060    public static final String KEY_PREFIX = "flex.clear";
061
062    /** Defines which pages are valid for this dialog. */
063    public static final String[] PAGES = {"page1"};
064
065    /** Clean up mode constant. */
066    private static final String MODE_ALL = "all";
067
068    /** Clean up mode constant. */
069    private static final String MODE_VARIATIONS = "variations";
070
071    /** Widget value. */
072    private String m_mode;
073
074    /** Widget value. */
075    private boolean m_offline;
076
077    /** Widget value. */
078    private boolean m_online;
079
080    /**
081     * Public constructor with JSP action element.<p>
082     *
083     * @param jsp an initialized JSP action element
084     */
085    public CmsFlexCacheClearDialog(CmsJspActionElement jsp) {
086
087        super(jsp);
088
089    }
090
091    /**
092     * Public constructor with JSP variables.<p>
093     *
094     * @param context the JSP page context
095     * @param req the JSP request
096     * @param res the JSP response
097     */
098    public CmsFlexCacheClearDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
099
100        this(new CmsJspActionElement(context, req, res));
101    }
102
103    /**
104     * Sends a clear caches event.<p>
105     *
106     * @throws JspException if something goes wrong
107     */
108    public void actionClearCaches() throws JspException {
109
110        OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, null));
111
112        setAction(CmsDialog.ACTION_CANCEL);
113        actionCloseDialog();
114    }
115
116    /**
117     * Commits the edited group to the db.<p>
118     */
119    @Override
120    public void actionCommit() {
121
122        try {
123            int action = -1;
124            if (isOnline() && isOffline()) {
125                if (getMode().equals(MODE_ALL)) {
126                    action = CmsFlexCache.CLEAR_ALL;
127                } else {
128                    action = CmsFlexCache.CLEAR_ENTRIES;
129                }
130            } else if (isOnline()) {
131                if (getMode().equals(MODE_ALL)) {
132                    action = CmsFlexCache.CLEAR_ONLINE_ALL;
133                } else {
134                    action = CmsFlexCache.CLEAR_ONLINE_ENTRIES;
135                }
136            } else if (isOffline()) {
137                if (getMode().equals(MODE_ALL)) {
138                    action = CmsFlexCache.CLEAR_OFFLINE_ALL;
139                } else {
140                    action = CmsFlexCache.CLEAR_OFFLINE_ENTRIES;
141                }
142            } else {
143                if (getMode().equals(MODE_ALL)) {
144                    action = CmsFlexCache.CLEAR_ALL;
145                } else {
146                    action = CmsFlexCache.CLEAR_ENTRIES;
147                }
148            }
149            OpenCms.fireCmsEvent(
150                new CmsEvent(
151                    I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR,
152                    Collections.<String, Object> singletonMap(CmsFlexCache.CACHE_ACTION, Integer.valueOf(action))));
153        } catch (Exception e) {
154            setCommitErrors(Collections.singletonList((Throwable)e));
155        }
156    }
157
158    /**
159     * Purges the jsp repository.<p>
160     *
161     * @throws JspException if something goes wrong
162     */
163    public void actionPurgeJspRepository() throws JspException {
164
165        OpenCms.fireCmsEvent(
166            new CmsEvent(I_CmsEventListener.EVENT_FLEX_PURGE_JSP_REPOSITORY, Collections.<String, Object> emptyMap()));
167        OpenCms.fireCmsEvent(
168            new CmsEvent(
169                I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR,
170                Collections.<String, Object> singletonMap("action", Integer.valueOf(CmsFlexCache.CLEAR_ENTRIES))));
171
172        setAction(CmsDialog.ACTION_CANCEL);
173        actionCloseDialog();
174    }
175
176    /**
177     * Returns the mode.<p>
178     *
179     * @return the mode
180     */
181    public String getMode() {
182
183        return m_mode;
184    }
185
186    /**
187     * Returns the offline.<p>
188     *
189     * @return the offline
190     */
191    public boolean isOffline() {
192
193        return m_offline;
194    }
195
196    /**
197     * Returns the online.<p>
198     *
199     * @return the online
200     */
201    public boolean isOnline() {
202
203        return m_online;
204    }
205
206    /**
207     * Sets the mode.<p>
208     *
209     * @param mode the mode to set
210     */
211    public void setMode(String mode) {
212
213        m_mode = mode;
214    }
215
216    /**
217     * Sets the offline.<p>
218     *
219     * @param offline the offline to set
220     */
221    public void setOffline(boolean offline) {
222
223        m_offline = offline;
224    }
225
226    /**
227     * Sets the online.<p>
228     *
229     * @param online the online to set
230     */
231    public void setOnline(boolean online) {
232
233        m_online = online;
234    }
235
236    /**
237     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
238     *
239     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
240     *
241     * @param dialog the dialog (page) to get the HTML for
242     * @return the dialog HTML for all defined widgets of the named dialog (page)
243     */
244    @Override
245    protected String createDialogHtml(String dialog) {
246
247        StringBuffer result = new StringBuffer(1024);
248
249        // create widget table
250        result.append(createWidgetTableStart());
251
252        // show error header once if there were validation errors
253        result.append(createWidgetErrorHeader());
254
255        int n = 1;
256
257        // initialize the cache object to use for the dialog
258        CmsFlexController controller = (CmsFlexController)getJsp().getRequest().getAttribute(
259            CmsFlexController.ATTRIBUTE_NAME);
260        CmsFlexCache cache = controller.getCmsCache();
261
262        // widgets to display
263        if (cache.cacheOffline()) {
264            n = 2;
265        }
266        if (dialog.equals(PAGES[0])) {
267            // create the widgets for the first dialog page
268            result.append(dialogBlockStart(key(Messages.GUI_FLEXCACHE_LABEL_CLEAN_BLOCK_0)));
269            result.append(createWidgetTableStart());
270            result.append(createDialogRowsHtml(0, n));
271            result.append(createWidgetTableEnd());
272            result.append(dialogBlockEnd());
273        }
274
275        // close widget table
276        result.append(createWidgetTableEnd());
277
278        return result.toString();
279    }
280
281    /**
282     * @see org.opencms.workplace.CmsWidgetDialog#defaultActionHtmlEnd()
283     */
284    @Override
285    protected String defaultActionHtmlEnd() {
286
287        return "";
288    }
289
290    /**
291     * Creates the list of widgets for this dialog.<p>
292     */
293    @Override
294    protected void defineWidgets() {
295
296        setKeyPrefix(KEY_PREFIX);
297
298        // initialize the cache object to use for the dialog
299        CmsFlexController controller = (CmsFlexController)getJsp().getRequest().getAttribute(
300            CmsFlexController.ATTRIBUTE_NAME);
301        CmsFlexCache cache = controller.getCmsCache();
302
303        setOffline(true);
304        setOnline(true);
305        setMode(MODE_ALL);
306
307        // widgets to display
308        if (cache.cacheOffline()) {
309            addWidget(new CmsWidgetDialogParameter(this, "offline", PAGES[0], new CmsCheckboxWidget()));
310        }
311        addWidget(new CmsWidgetDialogParameter(this, "online", PAGES[0], new CmsCheckboxWidget()));
312        addWidget(new CmsWidgetDialogParameter(this, "mode", PAGES[0], new CmsRadioSelectWidget(getModes())));
313    }
314
315    /**
316     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
317     */
318    @Override
319    protected String[] getPageArray() {
320
321        return PAGES;
322    }
323
324    /**
325     * @see org.opencms.workplace.CmsWorkplace#initMessages()
326     */
327    @Override
328    protected void initMessages() {
329
330        // add specific dialog resource bundle
331        addMessages(Messages.get().getBundleName());
332        // add default resource bundles
333        super.initMessages();
334    }
335
336    /**
337     * Overridden to set the online help path for this dialog.<p>
338     *
339     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
340     */
341    @Override
342    protected void initWorkplaceMembers(CmsJspActionElement jsp) {
343
344        super.initWorkplaceMembers(jsp);
345        setOnlineHelpUriCustom("/cache/flex/clean/");
346    }
347
348    /**
349     * Returns a list with the possible modes for the clean action.<p>
350     *
351     * @return a list with the possible modes for the clean action
352     */
353    private List getModes() {
354
355        ArrayList ret = new ArrayList();
356
357        ret.add(
358            new CmsSelectWidgetOption(
359                MODE_VARIATIONS,
360                getMode().equals(MODE_VARIATIONS),
361                key(Messages.GUI_FLEXCACHE_CLEAN_MODE_VARIATIONS_0)));
362        ret.add(
363            new CmsSelectWidgetOption(
364                MODE_ALL,
365                getMode().equals(MODE_ALL),
366                key(Messages.GUI_FLEXCACHE_CLEAN_MODE_ALL_0)));
367
368        return ret;
369    }
370}