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.ui.apps.cacheadmin;
029
030import org.opencms.flex.CmsFlexCache;
031import org.opencms.main.CmsEvent;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.I_CmsEventListener;
035import org.opencms.main.OpenCms;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.FontOpenCms;
039import org.opencms.ui.apps.Messages;
040import org.opencms.ui.components.CmsBasicDialog;
041import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
042import org.opencms.ui.components.OpenCmsTheme;
043
044import java.util.ArrayList;
045import java.util.Collections;
046import java.util.HashMap;
047import java.util.List;
048
049import org.apache.commons.logging.Log;
050
051import com.vaadin.ui.Button;
052import com.vaadin.ui.Button.ClickEvent;
053import com.vaadin.ui.Button.ClickListener;
054import com.vaadin.ui.Component;
055import com.vaadin.v7.ui.HorizontalLayout;
056import com.vaadin.ui.Panel;
057import com.vaadin.ui.UI;
058import com.vaadin.v7.ui.VerticalLayout;
059import com.vaadin.ui.Window;
060import com.vaadin.ui.themes.ValoTheme;
061
062/**
063 * Vaadin Layout with Buttons to clear the different types of cache.<p>
064 */
065public class CmsFlushCache extends Panel {
066
067    /**
068     * Interface for the dialogs.<p>
069     */
070    public interface I_CloseableDialog {
071
072        /**
073         * Sets the runnable which should be run to close window.
074         *
075         * @param closeRunnable runnable
076         *  */
077        void setCloseRunnable(Runnable closeRunnable);
078
079        /**
080         * Sets the runnable which should be run after ok action.<p>
081         *
082         * @param okRunnable runnable
083         */
084        void setOkRunnable(Runnable okRunnable);
085    }
086
087    /** The logger for this class. */
088    static Log LOG = CmsLog.getLog(CmsFlushCache.class.getName());
089
090    /**Width of buttons.*/
091    private static final String BUTTON_WIDTH = "250px";
092
093    /**Vaadin serial id.*/
094    private static final long serialVersionUID = -8868998646787654217L;
095
096    /**Icon for clean flex cache.*/
097    private VerticalLayout m_flushes;
098
099    /**
100     * public constructor.<p>
101     */
102    public CmsFlushCache() {
103
104        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
105        m_flushes.setSpacing(true);
106        m_flushes.setMargin(true);
107        setWidthUndefined();
108        Component c = getButtonLayout(3, new Runnable() {
109
110            public void run() {
111
112                // nothing to do
113
114            }
115
116        });
117        m_flushes.addComponent(c);
118
119    }
120
121    /**
122     * Creates the button layout.<p>
123     *
124     * @param size number ob buttons per line
125     * @param clickedRunnable click runnable
126     * @return VerticalLayout
127     */
128    protected static VerticalLayout getButtonLayout(int size, final Runnable clickedRunnable) {
129
130        VerticalLayout layout = new VerticalLayout();
131        layout.setSpacing(true);
132        layout.setMargin(true);
133        layout.addStyleName(ValoTheme.LAYOUT_HORIZONTAL_WRAPPING);
134
135        Button clean_flex = getFlushButton(
136            Messages.GUI_CACHE_FLEXCACHE_CLEAN_ADMIN_TOOL_NAME_0,
137            Messages.GUI_CACHE_FLEXCACHE_CLEAN_ADMIN_TOOL_HELP_0,
138            new CmsFlexCacheCleanDialog(),
139            clickedRunnable);
140
141        Button clean_image = getFlushButton(
142            Messages.GUI_CACHE_IMAGECACHE_CLEAN_ADMIN_TOOL_NAME_0,
143            Messages.GUI_CACHE_IMAGECACHE_CLEAN_ADMIN_TOOL_HELP_0,
144            new CmsImageCacheCleanDialog(),
145            clickedRunnable);
146
147        Button clean_core = getFlushButton(
148            Messages.GUI_CACHE_CORECACHE_CLEAN_ADMIN_TOOL_NAME_0,
149            Messages.GUI_CACHE_CORECACHE_CLEAN_ADMIN_TOOL_HELP_0,
150            new CmsConfirmSimpleFlushDialog(
151                CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_CORECACHE_CLEAN_ADMIN_TOOL_CONF_0)),
152            new Runnable() {
153
154                public void run() {
155
156                    OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, null));
157                    clickedRunnable.run();
158                }
159            });
160
161        Button clean_repo = getFlushButton(
162            Messages.GUI_CACHE_JSP_REPOSITORY_ADMIN_TOOL_NAME_0,
163            Messages.GUI_CACHE_JSP_REPOSITORY_ADMIN_TOOL_HELP_0,
164            new CmsConfirmSimpleFlushDialog(
165                CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_JSP_REPOSITORY_ADMIN_TOOL_CONF_0)),
166            new Runnable() {
167
168                public void run() {
169
170                    OpenCms.fireCmsEvent(
171                        new CmsEvent(
172                            I_CmsEventListener.EVENT_FLEX_PURGE_JSP_REPOSITORY,
173                            Collections.<String, Object> emptyMap()));
174                    OpenCms.fireCmsEvent(
175                        new CmsEvent(
176                            I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR,
177                            Collections.<String, Object> singletonMap(
178                                "action",
179                                Integer.valueOf(CmsFlexCache.CLEAR_ENTRIES))));
180                    clickedRunnable.run();
181                }
182            });
183
184        Button reini = getFlushButton(
185            Messages.GUI_CACHE_REINI_TOOL_NAME_0,
186            Messages.GUI_CACHE_REINI_TOOL_NAME_HELP_0,
187            new CmsConfirmSimpleFlushDialog(CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_REINI_TOOL_CONF_0)),
188            new Runnable() {
189
190                public void run() {
191
192                    try {
193                        // re-initialize the workplace
194                        OpenCms.getWorkplaceManager().initialize(A_CmsUI.getCmsObject());
195                        // fire "clear caches" event to reload all cached resource bundles
196                        OpenCms.fireCmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, new HashMap<String, Object>());
197                        clickedRunnable.run();
198                    } catch (CmsException e) {
199                        LOG.error("Unable to reinitialize workspace", e);
200                    }
201                }
202            });
203
204        List<Component> elements = new ArrayList<Component>();
205        elements.add(clean_core);
206        elements.add(clean_flex);
207        elements.add(clean_image);
208        elements.add(reini);
209        elements.add(clean_repo);
210
211        HorizontalLayout row = new HorizontalLayout();
212        row.setSpacing(true);
213        for (int i = 0; i < elements.size(); i++) {
214
215            if (size > 0) {
216                row.addComponent(elements.get(i));
217
218                if (((i % (size - 1)) == 0) & (i > 0)) {
219                    layout.addComponent(row);
220                    row = new HorizontalLayout();
221                    row.setSpacing(true);
222                }
223
224            } else {
225                layout.addComponent(elements.get(i));
226            }
227        }
228
229        return layout;
230    }
231
232    /**
233     * Creates the button which opens the flush dialogs.<p>
234     *
235     * @return Button
236     */
237    protected static Button getFlushToolButton() {
238
239        Button res = new Button(FontOpenCms.CACHE);
240        res.addStyleName(ValoTheme.BUTTON_BORDERLESS);
241        res.addStyleName(OpenCmsTheme.TOOLBAR_BUTTON);
242        res.addClickListener(new ClickListener() {
243
244            /**vaadin serial id. */
245            private static final long serialVersionUID = 7110311791006484514L;
246
247            public void buttonClick(ClickEvent event) {
248
249                Window window = CmsBasicDialog.prepareWindow(DialogWidth.content);
250
251                window.setContent(new CmsFlushButtonHolderDialog(window));
252                A_CmsUI.get().addWindow(window);
253            }
254
255        });
256        return res;
257    }
258
259    /**
260     * Creates Button to flush cashes.<p>
261     *
262     * @param captionMessage caption
263     * @param descriptionMessage description
264     * @param dialog layout to be shown
265     * @param okRun runnable called by clicking button
266     * @return button with click listener
267     */
268    private static Button getFlushButton(
269        String captionMessage,
270        String descriptionMessage,
271        final I_CloseableDialog dialog,
272        final Runnable okRun) {
273
274        Button ret = new Button();
275        ret.setWidth(BUTTON_WIDTH);
276        ret.addStyleName("v-button");
277        ret.addStyleName("o-button-blue");
278        ret.setCaption(CmsVaadinUtils.getMessageText(captionMessage));
279        ret.setDescription(CmsVaadinUtils.getMessageText(descriptionMessage));
280
281        ret.addClickListener(new ClickListener() {
282
283            private static final long serialVersionUID = -4513263981209222571L;
284
285            public void buttonClick(ClickEvent event) {
286
287                final Window window = CmsBasicDialog.prepareWindow();
288                dialog.setCloseRunnable(new Runnable() {
289
290                    public void run() {
291
292                        window.close();
293                    }
294                });
295                dialog.setOkRunnable(okRun);
296                window.setContent((Component)dialog);
297                window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_CLEAN_CONFIRM_0));
298                UI.getCurrent().addWindow(window);
299            }
300        });
301        return ret;
302    }
303}