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.file.CmsObject;
031import org.opencms.flex.CmsFlexCache;
032import org.opencms.flex.CmsFlexController;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.main.CmsRuntimeException;
035import org.opencms.workplace.list.A_CmsListDialog;
036import org.opencms.workplace.list.CmsListColumnAlignEnum;
037import org.opencms.workplace.list.CmsListColumnDefinition;
038import org.opencms.workplace.list.CmsListDirectAction;
039import org.opencms.workplace.list.CmsListItem;
040import org.opencms.workplace.list.CmsListItemDetails;
041import org.opencms.workplace.list.CmsListItemDetailsFormatter;
042import org.opencms.workplace.list.CmsListMetadata;
043import org.opencms.workplace.list.CmsListOrderEnum;
044
045import java.util.ArrayList;
046import java.util.Collections;
047import java.util.Iterator;
048import java.util.List;
049
050import javax.servlet.http.HttpServletRequest;
051import javax.servlet.http.HttpServletResponse;
052import javax.servlet.jsp.PageContext;
053
054/**
055 * Flex Cache content view.<p>
056 *
057 * @since 7.0.5
058 */
059public class CmsFlexCacheList extends A_CmsListDialog {
060
061    /** list action id constant. */
062    public static final String LIST_ACTION_ICON = "ai";
063
064    /** list column id constant. */
065    public static final String LIST_COLUMN_ICON = "ci";
066
067    /** list column id constant. */
068    public static final String LIST_COLUMN_KEY = "ck";
069
070    /** list column id constant. */
071    public static final String LIST_COLUMN_PROJECT = "cp";
072
073    /** list column id constant. */
074    public static final String LIST_COLUMN_RESOURCE = "cr";
075
076    /** list item detail id constant. */
077    public static final String LIST_DETAIL_VARIATIONS = "dv";
078
079    /** list id constant. */
080    public static final String LIST_ID = "lfc";
081
082    /**
083     * Public constructor.<p>
084     *
085     * @param jsp an initialized JSP action element
086     */
087    public CmsFlexCacheList(CmsJspActionElement jsp) {
088
089        super(
090            jsp,
091            LIST_ID,
092            Messages.get().container(Messages.GUI_FLEXCACHE_LIST_NAME_0),
093            LIST_COLUMN_RESOURCE,
094            CmsListOrderEnum.ORDER_ASCENDING,
095            LIST_COLUMN_RESOURCE);
096    }
097
098    /**
099     * Public constructor with JSP variables.<p>
100     *
101     * @param context the JSP page context
102     * @param req the JSP request
103     * @param res the JSP response
104     */
105    public CmsFlexCacheList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
106
107        this(new CmsJspActionElement(context, req, res));
108    }
109
110    /**
111     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlStart()
112     */
113    @Override
114    public String defaultActionHtmlStart() {
115
116        return getList().listJs() + dialogContentStart(getParamTitle());
117    }
118
119    /**
120     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
121     */
122    @Override
123    public void executeListMultiActions() throws CmsRuntimeException {
124
125        throwListUnsupportedActionException();
126    }
127
128    /**
129     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
130     */
131    @Override
132    public void executeListSingleActions() {
133
134        throwListUnsupportedActionException();
135    }
136
137    /**
138     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
139     */
140    @Override
141    protected void fillDetails(String detailId) {
142
143        CmsObject cms = getCms();
144        CmsFlexController controller = (CmsFlexController)getJsp().getRequest().getAttribute(
145            CmsFlexController.ATTRIBUTE_NAME);
146        CmsFlexCache cache = controller.getCmsCache();
147
148        // get content
149        List entries = getList().getAllContent();
150        Iterator itEntries = entries.iterator();
151        while (itEntries.hasNext()) {
152            CmsListItem item = (CmsListItem)itEntries.next();
153            String resName = item.getId();
154            StringBuffer html = new StringBuffer(512);
155            try {
156                if (detailId.equals(LIST_DETAIL_VARIATIONS)) {
157                    // variations
158                    List variations = new ArrayList(cache.getCachedVariations(resName, cms));
159                    Collections.sort(variations);
160                    Iterator itVariations = variations.iterator();
161                    while (itVariations.hasNext()) {
162                        String var = (String)itVariations.next();
163                        html.append(var);
164                        if (itVariations.hasNext()) {
165                            html.append("<br>");
166                        }
167                        html.append("\n");
168                    }
169                } else {
170                    continue;
171                }
172            } catch (Exception e) {
173                // ignore
174            }
175            item.set(detailId, html.toString());
176        }
177    }
178
179    /**
180     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
181     */
182    @Override
183    protected List getListItems() {
184
185        List ret = new ArrayList();
186
187        CmsFlexController controller = (CmsFlexController)getJsp().getRequest().getAttribute(
188            CmsFlexController.ATTRIBUTE_NAME);
189        CmsFlexCache cache = controller.getCmsCache();
190
191        // get content
192        Iterator itResources = new ArrayList(cache.getCachedResources(getCms())).iterator();
193        while (itResources.hasNext()) {
194            String resource = (String)itResources.next();
195            CmsListItem item = getList().newItem(resource);
196            String resName = resource;
197            String project = "";
198            if (resource.endsWith(CmsFlexCache.CACHE_OFFLINESUFFIX)) {
199                resName = resource.substring(0, resource.length() - CmsFlexCache.CACHE_OFFLINESUFFIX.length());
200                project = "Offline";
201            }
202            if (resource.endsWith(CmsFlexCache.CACHE_ONLINESUFFIX)) {
203                resName = resource.substring(0, resource.length() - CmsFlexCache.CACHE_ONLINESUFFIX.length());
204                project = "Online";
205            }
206            item.set(LIST_COLUMN_RESOURCE, resName);
207            item.set(LIST_COLUMN_PROJECT, project);
208            item.set(LIST_COLUMN_KEY, cache.getCachedKey(resource, getCms()));
209            ret.add(item);
210        }
211
212        return ret;
213    }
214
215    /**
216     * @see org.opencms.workplace.CmsWorkplace#initMessages()
217     */
218    @Override
219    protected void initMessages() {
220
221        // add specific dialog resource bundle
222        addMessages(Messages.get().getBundleName());
223        // add default resource bundles
224        super.initMessages();
225    }
226
227    /**
228     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
229     */
230    @Override
231    protected void setColumns(CmsListMetadata metadata) {
232
233        // create column for icon display
234        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
235        iconCol.setName(Messages.get().container(Messages.GUI_FLEXCACHE_LIST_COLS_ICON_0));
236        iconCol.setWidth("20");
237        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
238        iconCol.setSorteable(false);
239
240        CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON);
241        iconAction.setName(Messages.get().container(Messages.GUI_FLEXCACHE_LIST_ACTION_ICON_NAME_0));
242        iconAction.setIconPath("tools/cache/buttons/flexentry.png");
243        iconAction.setEnabled(false);
244        iconCol.addDirectAction(iconAction);
245        // add it to the list definition
246        metadata.addColumn(iconCol);
247
248        // create column for resource name
249        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_RESOURCE);
250        nameCol.setName(Messages.get().container(Messages.GUI_FLEXCACHE_LIST_COLS_RESOURCE_0));
251        nameCol.setWidth("60%");
252        // add it to the list definition
253        metadata.addColumn(nameCol);
254
255        // create column for project
256        CmsListColumnDefinition projectCol = new CmsListColumnDefinition(LIST_COLUMN_PROJECT);
257        projectCol.setName(Messages.get().container(Messages.GUI_FLEXCACHE_LIST_COLS_PROJECT_0));
258        projectCol.setWidth("15%");
259        // add it to the list definition
260        metadata.addColumn(projectCol);
261
262        // create column for cache key
263        CmsListColumnDefinition keyCol = new CmsListColumnDefinition(LIST_COLUMN_KEY);
264        keyCol.setName(Messages.get().container(Messages.GUI_FLEXCACHE_LIST_COLS_KEY_0));
265        keyCol.setWidth("25%");
266        // add it to the list definition
267        metadata.addColumn(keyCol);
268    }
269
270    /**
271     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
272     */
273    @Override
274    protected void setIndependentActions(CmsListMetadata metadata) {
275
276        // add variations details
277        CmsListItemDetails variationsDetails = new CmsListItemDetails(LIST_DETAIL_VARIATIONS);
278        variationsDetails.setAtColumn(LIST_COLUMN_RESOURCE);
279        variationsDetails.setVisible(false);
280        variationsDetails.setShowActionName(
281            Messages.get().container(Messages.GUI_FLEXCACHE_DETAIL_SHOW_VARIATIONS_NAME_0));
282        variationsDetails.setShowActionHelpText(
283            Messages.get().container(Messages.GUI_FLEXCACHE_DETAIL_SHOW_VARIATIONS_HELP_0));
284        variationsDetails.setHideActionName(
285            Messages.get().container(Messages.GUI_FLEXCACHE_DETAIL_HIDE_VARIATIONS_NAME_0));
286        variationsDetails.setHideActionHelpText(
287            Messages.get().container(Messages.GUI_FLEXCACHE_DETAIL_HIDE_VARIATIONS_HELP_0));
288        variationsDetails.setName(Messages.get().container(Messages.GUI_FLEXCACHE_DETAIL_VARIATIONS_NAME_0));
289        variationsDetails.setFormatter(
290            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_FLEXCACHE_DETAIL_VARIATIONS_NAME_0)));
291        metadata.addItemDetails(variationsDetails);
292    }
293
294    /**
295     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
296     */
297    @Override
298    protected void setMultiActions(CmsListMetadata metadata) {
299
300        // no multi actions
301    }
302}