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.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.flex.CmsFlexCache;
033import org.opencms.flex.CmsFlexCacheKey;
034import org.opencms.main.CmsException;
035import org.opencms.main.OpenCms;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.ui.CmsCssIcon;
038import org.opencms.ui.CmsVaadinUtils;
039import org.opencms.ui.apps.Messages;
040import org.opencms.ui.apps.cacheadmin.CmsCacheViewApp.Mode;
041import org.opencms.ui.components.CmsBasicDialog;
042import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
043import org.opencms.ui.components.OpenCmsTheme;
044import org.opencms.ui.contextmenu.CmsContextMenu;
045import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
046import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry;
047import org.opencms.util.CmsStringUtil;
048
049import java.util.ArrayList;
050import java.util.Collections;
051import java.util.Iterator;
052import java.util.List;
053import java.util.Locale;
054import java.util.Set;
055
056import com.vaadin.server.Resource;
057import com.vaadin.shared.MouseEventDetails.MouseButton;
058import com.vaadin.ui.UI;
059import com.vaadin.ui.Window;
060import com.vaadin.ui.themes.ValoTheme;
061import com.vaadin.v7.data.Item;
062import com.vaadin.v7.data.util.IndexedContainer;
063import com.vaadin.v7.data.util.filter.Or;
064import com.vaadin.v7.data.util.filter.SimpleStringFilter;
065import com.vaadin.v7.event.ItemClickEvent;
066import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
067import com.vaadin.v7.ui.Table;
068
069/**
070 * Table showong content of flex cache.<p>
071 */
072public class CmsFlexCacheTable extends Table {
073
074    /**
075     * Menu entry for show variations option.<p>
076     */
077    class EntryVariations
078    implements I_CmsSimpleContextMenuEntry<Set<String>>, I_CmsSimpleContextMenuEntry.I_HasCssStyles {
079
080        /**
081         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
082         */
083        public void executeAction(Set<String> data) {
084
085            String resource = data.iterator().next();
086            showVariationsWindow(resource);
087        }
088
089        /**
090         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry.I_HasCssStyles#getStyles()
091         */
092        public String getStyles() {
093
094            return ValoTheme.LABEL_BOLD;
095        }
096
097        /**
098         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
099         */
100        public String getTitle(Locale locale) {
101
102            return CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_FLEXCACHE_LABEL_STATS_VARIATIONS_0);
103        }
104
105        /**
106         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
107         */
108        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
109
110            return (data != null) && (data.size() == 1)
111            ? CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE
112            : CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
113        }
114    }
115
116    /**Column for icon.*/
117    private static final String PROP_ICON = "icon";
118
119    /**Column for key.*/
120    private static final String PROP_KEY = "key";
121
122    /**Column for project name.*/
123    private static final String PROP_PROJECT = "project";
124
125    /**Column for resource-name.*/
126    private static final String PROP_RESOURCENAME = "name";
127
128    /**Column for variation count.*/
129    private static final String PROP_VARIATIONS = "variations";
130
131    /**vaadin serial id.*/
132    private static final long serialVersionUID = 836377854954208442L;
133
134    /** The context menu. */
135    CmsContextMenu m_menu;
136
137    /**Current flex cache.*/
138    private CmsFlexCache m_cache;
139
140    /**Indexed Container.*/
141    private IndexedContainer m_container;
142
143    /** The available menu entries. */
144    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
145
146    /**CmsObject at root.*/
147    private CmsObject m_rootCms;
148
149    /**
150     * public constructor. <p>
151     */
152    public CmsFlexCacheTable() {
153
154        m_cache = OpenCms.getFlexCache();
155
156        m_menu = new CmsContextMenu();
157        m_menu.setAsTableContextMenu(this);
158
159        m_container = new IndexedContainer();
160
161        m_container.addContainerProperty(PROP_ICON, Resource.class, new CmsCssIcon(OpenCmsTheme.ICON_CACHE));
162        m_container.addContainerProperty(PROP_RESOURCENAME, String.class, "");
163        m_container.addContainerProperty(PROP_PROJECT, String.class, "");
164        m_container.addContainerProperty(PROP_KEY, String.class, "");
165        m_container.addContainerProperty(PROP_VARIATIONS, Integer.class, Integer.valueOf(0));
166
167        setContainerDataSource(m_container);
168
169        setColumnHeader(
170            PROP_RESOURCENAME,
171            CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_FLEXCACHE_LIST_COLS_RESOURCE_0));
172        setColumnHeader(PROP_PROJECT, CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_FLEXCACHE_LIST_COLS_PROJECT_0));
173        setColumnHeader(PROP_KEY, CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_FLEXCACHE_LIST_COLS_KEY_0));
174        setColumnHeader(
175            PROP_VARIATIONS,
176            CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_FLEXCACHE_LIST_COLS_VARCOUNT_0));
177
178        setItemIconPropertyId(PROP_ICON);
179        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
180
181        setColumnWidth(null, 40);
182        setColumnWidth(PROP_VARIATIONS, 90);
183
184        setSelectable(true);
185
186        loadTableEntries();
187
188        addItemClickListener(new ItemClickListener() {
189
190            private static final long serialVersionUID = -4738296706762013443L;
191
192            public void itemClick(ItemClickEvent event) {
193
194                setValue(null);
195                select(event.getItemId());
196
197                //Right click or click on icon column (=null) -> show menu
198                if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
199                    m_menu.setEntries(getMenuEntries(), Collections.singleton(((String)getValue())));
200                    m_menu.openForTable(event, event.getItemId(), event.getPropertyId(), CmsFlexCacheTable.this);
201                }
202
203                if (event.getButton().equals(MouseButton.LEFT) & PROP_RESOURCENAME.equals(event.getPropertyId())) {
204                    showVariationsWindow((String)getValue());
205                }
206            }
207        });
208
209        setCellStyleGenerator(new CellStyleGenerator() {
210
211            private static final long serialVersionUID = 1L;
212
213            public String getStyle(Table source, Object itemId, Object propertyId) {
214
215                if (PROP_RESOURCENAME.equals(propertyId)) {
216                    return " " + OpenCmsTheme.HOVER_COLUMN;
217                }
218
219                return null;
220            }
221        });
222    }
223
224    /**
225     * Filters the table according to given search string.<p>
226     *
227     * @param search string to be looked for.
228     */
229    public void filterTable(String search) {
230
231        m_container.removeAllContainerFilters();
232        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(search)) {
233            m_container.addContainerFilter(
234                new Or(
235                    new SimpleStringFilter(PROP_RESOURCENAME, search, true, false),
236                    new SimpleStringFilter(PROP_KEY, search, true, false)));
237        }
238        if ((getValue() != null)) {
239            setCurrentPageFirstItemId(getValue());
240        }
241    }
242
243    /**
244     * Returns the available menu entries.<p>
245     *
246     * @return the menu entries
247     */
248    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
249
250        if (m_menuEntries == null) {
251            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
252            m_menuEntries.add(new EntryVariations()); //Option for Variations
253        }
254        return m_menuEntries;
255    }
256
257    /**
258     * Returns a cms object at root-site.<p>
259     *
260     * @return cmsobject
261     */
262    CmsObject getRootCms() {
263
264        try {
265            if (m_rootCms == null) {
266
267                m_rootCms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
268                m_rootCms.getRequestContext().setSiteRoot("");
269            }
270        } catch (CmsException e) {
271            //
272        }
273        return m_rootCms;
274
275    }
276
277    /**
278     * Shows dialog for variations of given resource.<p>
279     *
280     * @param resource to show variations for
281     */
282    void showVariationsWindow(String resource) {
283
284        final Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
285        CmsVariationsDialog variationsDialog = new CmsVariationsDialog(resource, new Runnable() {
286
287            public void run() {
288
289                window.close();
290
291            }
292
293        }, Mode.FlexCache);
294        try {
295            CmsResource resourceObject = getRootCms().readResource(CmsFlexCacheKey.getResourceName(resource));
296            variationsDialog.displayResourceInfo(Collections.singletonList(resourceObject));
297        } catch (CmsException e) {
298            //
299        }
300        window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_VIEW_FLEX_VARIATIONS_1, resource));
301        window.setContent(variationsDialog);
302        UI.getCurrent().addWindow(window);
303    }
304
305    /**
306     * Reads flex cache entries and puts them to table.<p>
307     */
308    private void loadTableEntries() {
309
310        setVisibleColumns(PROP_RESOURCENAME, PROP_PROJECT, PROP_KEY, PROP_VARIATIONS);
311
312        Iterator<String> itResources = new ArrayList<String>(
313            m_cache.getCachedResources(A_CmsUI.getCmsObject())).iterator();
314        while (itResources.hasNext()) {
315            String resource = itResources.next();
316            String resName = resource;
317            String project = "";
318            String key = "";
319            if (resource.endsWith(CmsFlexCache.CACHE_OFFLINESUFFIX)) {
320                resName = resource.substring(0, resource.length() - CmsFlexCache.CACHE_OFFLINESUFFIX.length());
321                project = "Offline";
322            }
323            if (resource.endsWith(CmsFlexCache.CACHE_ONLINESUFFIX)) {
324                resName = resource.substring(0, resource.length() - CmsFlexCache.CACHE_ONLINESUFFIX.length());
325                project = "Online";
326            }
327            key = m_cache.getCachedKey(resource, A_CmsUI.getCmsObject()).toString();
328
329            Item item = m_container.addItem(resource);
330            item.getItemProperty(PROP_RESOURCENAME).setValue(resName);
331            item.getItemProperty(PROP_PROJECT).setValue(project);
332            item.getItemProperty(PROP_KEY).setValue(key);
333            item.getItemProperty(PROP_VARIATIONS).setValue(
334                Integer.valueOf(m_cache.getCachedVariations(resource, A_CmsUI.getCmsObject()).size()));
335        }
336    }
337}