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.main.CmsRuntimeException;
032import org.opencms.ui.apps.cacheadmin.CmsImageCacheHelper;
033import org.opencms.workplace.list.A_CmsListDialog;
034import org.opencms.workplace.list.CmsListColumnAlignEnum;
035import org.opencms.workplace.list.CmsListColumnDefinition;
036import org.opencms.workplace.list.CmsListDefaultAction;
037import org.opencms.workplace.list.CmsListDirectAction;
038import org.opencms.workplace.list.CmsListItem;
039import org.opencms.workplace.list.CmsListItemDetails;
040import org.opencms.workplace.list.CmsListItemDetailsFormatter;
041import org.opencms.workplace.list.CmsListMetadata;
042import org.opencms.workplace.list.CmsListOpenResourceAction;
043import org.opencms.workplace.list.CmsListOrderEnum;
044
045import java.util.ArrayList;
046import java.util.Iterator;
047import java.util.List;
048
049import javax.servlet.http.HttpServletRequest;
050import javax.servlet.http.HttpServletResponse;
051import javax.servlet.jsp.PageContext;
052
053/**
054 * Image Cache content view.<p>
055 *
056 * @since 7.0.5
057 */
058public class CmsImageCacheList extends A_CmsListDialog {
059
060    /** list action id constant. */
061    public static final String LIST_ACTION_ICON = "ai";
062
063    /** list column id constant. */
064    public static final String LIST_COLUMN_ICON = "ci";
065
066    /** list column id constant. */
067    public static final String LIST_COLUMN_LENGTH = "cl";
068
069    /** list column id constant. */
070    public static final String LIST_COLUMN_RESOURCE = "cr";
071
072    /** list column id constant. */
073    public static final String LIST_COLUMN_SIZE = "cs";
074
075    /** List default action id constant. */
076    public static final String LIST_DEFACTION_OPEN = "edo";
077
078    /** list item detail id constant. */
079    public static final String LIST_DETAIL_SIZE = "ds";
080
081    /** list item detail id constant. */
082    public static final String LIST_DETAIL_VARIATIONS = "dv";
083
084    /** list id constant. */
085    public static final String LIST_ID = "lfc";
086
087    /**
088     * Public constructor.<p>
089     *
090     * @param jsp an initialized JSP action element
091     */
092    public CmsImageCacheList(CmsJspActionElement jsp) {
093
094        super(
095            jsp,
096            LIST_ID,
097            Messages.get().container(Messages.GUI_IMAGECACHE_LIST_NAME_0),
098            LIST_COLUMN_RESOURCE,
099            CmsListOrderEnum.ORDER_ASCENDING,
100            LIST_COLUMN_RESOURCE);
101    }
102
103    /**
104     * Public constructor with JSP variables.<p>
105     *
106     * @param context the JSP page context
107     * @param req the JSP request
108     * @param res the JSP response
109     */
110    public CmsImageCacheList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
111
112        this(new CmsJspActionElement(context, req, res));
113    }
114
115    /**
116     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlStart()
117     */
118    @Override
119    public String defaultActionHtmlStart() {
120
121        return getList().listJs() + dialogContentStart(getParamTitle());
122    }
123
124    /**
125     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
126     */
127    @Override
128    public void executeListMultiActions() throws CmsRuntimeException {
129
130        throwListUnsupportedActionException();
131    }
132
133    /**
134     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
135     */
136    @Override
137    public void executeListSingleActions() {
138
139        throwListUnsupportedActionException();
140    }
141
142    /**
143     * Checks if the image size should be shown or not.<p>
144     *
145     * @return <code>true</code> if the image size should be shown
146     */
147    public boolean showSize() {
148
149        CmsListItemDetails details = getList().getMetadata().getItemDetailDefinition(LIST_DETAIL_SIZE);
150        return (details != null) && details.isVisible();
151    }
152
153    /**
154     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
155     */
156    @Override
157    protected void fillDetails(String detailId) {
158
159        // get content
160        CmsImageCacheHelper helper = new CmsImageCacheHelper(getCms(), true, showSize(), false);
161
162        List entries = getList().getAllContent();
163        Iterator itEntries = entries.iterator();
164        while (itEntries.hasNext()) {
165            CmsListItem item = (CmsListItem)itEntries.next();
166            String resName = item.getId();
167            StringBuffer html = new StringBuffer(512);
168            try {
169                // variations
170                Iterator itVariations = helper.getVariations(resName).iterator();
171                while (itVariations.hasNext()) {
172                    String var = (String)itVariations.next();
173                    html.append(var);
174                    if (itVariations.hasNext()) {
175                        html.append("<br>");
176                    }
177                    html.append("\n");
178                }
179            } catch (Exception e) {
180                // ignore
181            }
182            item.set(LIST_DETAIL_VARIATIONS, html.toString());
183        }
184    }
185
186    /**
187     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
188     */
189    @Override
190    protected List getListItems() {
191
192        List ret = new ArrayList();
193        boolean showSize = showSize();
194        getList().getMetadata().getColumnDefinition(LIST_COLUMN_SIZE).setVisible(showSize);
195        String width = "80%";
196        if (showSize) {
197            width = "60%";
198        }
199        getList().getMetadata().getColumnDefinition(LIST_COLUMN_RESOURCE).setWidth(width);
200
201        // get content
202        CmsImageCacheHelper helper = new CmsImageCacheHelper(getCms(), false, showSize, false);
203        Iterator itResources = helper.getAllCachedImages().iterator();
204        while (itResources.hasNext()) {
205            String resource = (String)itResources.next();
206            CmsListItem item = getList().newItem(resource);
207            String resName = resource;
208            item.set(LIST_COLUMN_RESOURCE, resName);
209            if (showSize) {
210                item.set(LIST_COLUMN_SIZE, helper.getSize(resName));
211            }
212            item.set(LIST_COLUMN_LENGTH, helper.getLength(resName));
213            ret.add(item);
214        }
215
216        return ret;
217    }
218
219    /**
220     * @see org.opencms.workplace.CmsWorkplace#initMessages()
221     */
222    @Override
223    protected void initMessages() {
224
225        // add specific dialog resource bundle
226        addMessages(Messages.get().getBundleName());
227        // add default resource bundles
228        super.initMessages();
229    }
230
231    /**
232     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
233     */
234    @Override
235    protected void setColumns(CmsListMetadata metadata) {
236
237        // create column for icon display
238        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
239        iconCol.setName(Messages.get().container(Messages.GUI_IMAGECACHE_LIST_COLS_ICON_0));
240        iconCol.setWidth("20");
241        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
242        iconCol.setSorteable(false);
243
244        CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON);
245        iconAction.setName(Messages.get().container(Messages.GUI_IMAGECACHE_LIST_ACTION_ICON_NAME_0));
246        iconAction.setIconPath("tools/cache/buttons/imageentry.png");
247        iconAction.setEnabled(false);
248        iconCol.addDirectAction(iconAction);
249        // add it to the list definition
250        metadata.addColumn(iconCol);
251
252        // create column for resource name
253        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_RESOURCE);
254        nameCol.setName(Messages.get().container(Messages.GUI_IMAGECACHE_LIST_COLS_RESOURCE_0));
255        nameCol.setWidth("60%");
256        // add resource open action
257        CmsListDefaultAction resourceOpenDefAction = new CmsListOpenResourceAction(
258            LIST_DEFACTION_OPEN,
259            LIST_COLUMN_RESOURCE);
260        resourceOpenDefAction.setEnabled(true);
261        nameCol.addDefaultAction(resourceOpenDefAction);
262
263        // add it to the list definition
264        metadata.addColumn(nameCol);
265
266        // create column for size
267        CmsListColumnDefinition sizeCol = new CmsListColumnDefinition(LIST_COLUMN_SIZE);
268        sizeCol.setName(Messages.get().container(Messages.GUI_IMAGECACHE_LIST_COLS_SIZE_0));
269        sizeCol.setWidth("20%");
270        // add it to the list definition
271        metadata.addColumn(sizeCol);
272
273        // create column for length
274        CmsListColumnDefinition lengthCol = new CmsListColumnDefinition(LIST_COLUMN_LENGTH);
275        lengthCol.setName(Messages.get().container(Messages.GUI_IMAGECACHE_LIST_COLS_LENGTH_0));
276        lengthCol.setWidth("20%");
277        // add it to the list definition
278        metadata.addColumn(lengthCol);
279    }
280
281    /**
282     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
283     */
284    @Override
285    protected void setIndependentActions(CmsListMetadata metadata) {
286
287        // add variations details
288        CmsListItemDetails variationsDetails = new CmsListItemDetails(LIST_DETAIL_VARIATIONS);
289        variationsDetails.setAtColumn(LIST_COLUMN_RESOURCE);
290        variationsDetails.setVisible(false);
291        variationsDetails.setShowActionName(
292            Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_SHOW_VARIATIONS_NAME_0));
293        variationsDetails.setShowActionHelpText(
294            Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_SHOW_VARIATIONS_HELP_0));
295        variationsDetails.setHideActionName(
296            Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_HIDE_VARIATIONS_NAME_0));
297        variationsDetails.setHideActionHelpText(
298            Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_HIDE_VARIATIONS_HELP_0));
299        variationsDetails.setName(Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_VARIATIONS_NAME_0));
300        variationsDetails.setFormatter(
301            new CmsListItemDetailsFormatter(
302                Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_VARIATIONS_NAME_0)));
303        metadata.addItemDetails(variationsDetails);
304
305        // add size details
306        CmsListItemDetails sizeDetails = new CmsListItemDetails(LIST_DETAIL_SIZE);
307        sizeDetails.setShowActionName(Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_SHOW_SIZE_NAME_0));
308        sizeDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_SHOW_SIZE_HELP_0));
309        sizeDetails.setHideActionName(Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_HIDE_SIZE_NAME_0));
310        sizeDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_HIDE_SIZE_HELP_0));
311        sizeDetails.setName(Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_SIZE_NAME_0));
312        sizeDetails.setFormatter(
313            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_IMAGECACHE_DETAIL_SIZE_NAME_0)));
314        sizeDetails.setVisible(false);
315        metadata.addItemDetails(sizeDetails);
316    }
317
318    /**
319     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
320     */
321    @Override
322    protected void setMultiActions(CmsListMetadata metadata) {
323
324        // no multi actions
325    }
326}