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.commons;
029
030import org.opencms.file.CmsResource;
031import org.opencms.file.history.I_CmsHistoryResource;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.CmsRuntimeException;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.workplace.list.A_CmsListDialog;
038import org.opencms.workplace.list.CmsListColumnAlignEnum;
039import org.opencms.workplace.list.CmsListColumnDefinition;
040import org.opencms.workplace.list.CmsListDateMacroFormatter;
041import org.opencms.workplace.list.CmsListDirectAction;
042import org.opencms.workplace.list.CmsListItem;
043import org.opencms.workplace.list.CmsListMetadata;
044import org.opencms.workplace.list.CmsListMultiAction;
045import org.opencms.workplace.list.CmsListOrderEnum;
046import org.opencms.workplace.list.CmsListPrintIAction;
047import org.opencms.workplace.list.CmsListResourceIconAction;
048import org.opencms.workplace.list.I_CmsListFormatter;
049
050import java.util.ArrayList;
051import java.util.Date;
052import java.util.Iterator;
053import java.util.List;
054import java.util.Locale;
055
056import org.apache.commons.logging.Log;
057
058/**
059 * Generates the list of deleted resources to be used by ajax in the dialog.<p>
060 *
061 * @since 6.9.1
062 */
063public class CmsDeletedResourcesList extends A_CmsListDialog {
064
065    /** Log instance for this class. */
066    private static final Log LOG = CmsLog.getLog(CmsDeletedResourcesList.class);
067
068    /** Standard list button location. */
069    public static final String ICON_LIST_WARNING = "list/warning.png";
070
071    /** Standard list button location. */
072    public static final String ICON_MULTI_RESTORE = "tools/ex_history/buttons/restore.png";
073
074    /** List action id constant. */
075    public static final String LIST_ACTION_CONFLICT = "drlac";
076
077    /** List action id constant. */
078    public static final String LIST_ACTION_ICON = "drlai";
079
080    /** List column id constant. */
081    public static final String LIST_COLUMN_CONFLICT = "drlcc";
082
083    /** List column id constant. */
084    public static final String LIST_COLUMN_DELETION_DATE = "drlcdd";
085
086    /** List column id constant. */
087    public static final String LIST_COLUMN_ICON = "drlci";
088
089    /** List column id constant. */
090    public static final String LIST_COLUMN_NAME = "drlcn";
091
092    /** List column id constant. */
093    public static final String LIST_COLUMN_TYPEID = "drlct";
094
095    /** List column id constant. */
096    public static final String LIST_COLUMN_VERSION = "drlcv";
097
098    /** List id constant. */
099    public static final String LIST_ID = "drl";
100
101    /** List action id constant. */
102    public static final String LIST_MACTION_RESTORE = "mr";
103
104    /** Should deleted resources be displayed for the current folder or the subtree. */
105    private boolean m_readTree;
106
107    /** The name of the resource to display the deleted entries. */
108    private String m_resourcename;
109
110    /**
111     * Public constructor with JSP action element.<p>
112     *
113     * @param jsp an initialized JSP action element
114     * @param resourcename the name of the resource to display deleted entries for
115     * @param readTree display deleted resources for the subtree
116     */
117    public CmsDeletedResourcesList(CmsJspActionElement jsp, String resourcename, boolean readTree) {
118
119        super(
120            jsp,
121            LIST_ID,
122            Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_NAME_0),
123            null,
124            CmsListOrderEnum.ORDER_ASCENDING,
125            null);
126
127        // set the style to show common workplace dialog layout
128        setParamStyle("");
129
130        // prevent paging, usually there are only few model files
131        getList().setMaxItemsPerPage(Integer.MAX_VALUE);
132
133        // hide print button
134        getList().getMetadata().getIndependentAction(CmsListPrintIAction.LIST_ACTION_ID).setVisible(false);
135
136        // suppress the box around the list
137        getList().setBoxed(false);
138
139        // hide title of the list
140        //getList().setShowTitle(false);
141
142        m_readTree = readTree;
143        m_resourcename = resourcename;
144    }
145
146    /**
147     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
148     */
149    @Override
150    public void executeListMultiActions() throws CmsRuntimeException {
151
152        // noop
153    }
154
155    /**
156     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
157     */
158    @Override
159    public void executeListSingleActions() throws CmsRuntimeException {
160
161        // noop
162    }
163
164    /**
165     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
166     */
167    @Override
168    protected void fillDetails(String detailId) {
169
170        // noop
171    }
172
173    /**
174     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
175     */
176    @Override
177    protected List<CmsListItem> getListItems() throws CmsException {
178
179        List<CmsListItem> ret = new ArrayList<CmsListItem>();
180
181        List<I_CmsHistoryResource> list = getCms().readDeletedResources(m_resourcename, m_readTree);
182        Iterator<I_CmsHistoryResource> iter = list.iterator();
183        while (iter.hasNext()) {
184            I_CmsHistoryResource res = iter.next();
185
186            CmsListItem item = getList().newItem(res.getStructureId().toString());
187            String resourcePath = getCms().getSitePath((CmsResource)res);
188            item.set(LIST_COLUMN_NAME, m_resourcename + "|" + resourcePath);
189            LOG.info("deleted resource list, name = " + m_resourcename + "|" + resourcePath);
190            item.set(LIST_COLUMN_DELETION_DATE, new Date(res.getDateLastModified()));
191            item.set(LIST_COLUMN_VERSION, String.valueOf(res.getVersion()));
192            item.set(LIST_COLUMN_TYPEID, String.valueOf(res.getTypeId()));
193            ret.add(item);
194        }
195
196        return ret;
197    }
198
199    /**
200     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
201     */
202    @Override
203    protected void setColumns(CmsListMetadata metadata) {
204
205        // add column icon
206        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
207        iconCol.setName(Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_COLS_ICON_0));
208        iconCol.setWidth("20");
209        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
210        iconCol.setSorteable(false);
211
212        // add icon action
213        CmsListResourceIconAction iconAction = new CmsListResourceIconAction(
214            LIST_ACTION_ICON,
215            LIST_COLUMN_TYPEID,
216            getCms());
217        iconAction.setName(Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_ACTION_ICON_0));
218        iconAction.setEnabled(false);
219        iconCol.addDirectAction(iconAction);
220        metadata.addColumn(iconCol);
221
222        // add column for conflict
223        CmsListColumnDefinition conflictCol = new CmsListColumnDefinition(LIST_COLUMN_CONFLICT);
224        conflictCol.setName(Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_COLS_CONFLICT_0));
225        conflictCol.setWidth("20");
226        conflictCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
227        conflictCol.setSorteable(false);
228
229        // add conflict action
230        CmsListDirectAction conflictAction = new CmsListResourceIconAction(LIST_ACTION_CONFLICT, null, getCms()) {
231
232            /**
233             * @see org.opencms.workplace.list.CmsListResourceIconAction#getIconPath()
234             */
235            @Override
236            public String getIconPath() {
237
238                return ICON_LIST_WARNING;
239            }
240
241            /**
242             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
243             */
244            @Override
245            public boolean isVisible() {
246
247                String path = (String)getItem().get(LIST_COLUMN_NAME);
248                return getCms().existsResource(path);
249            }
250
251        };
252        conflictAction.setName(Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_ACTION_WARNING_0));
253        conflictAction.setEnabled(false);
254        conflictCol.addDirectAction(conflictAction);
255        metadata.addColumn(conflictCol);
256
257        // add column for name
258        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
259        nameCol.setName(Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_COLS_NAME_0));
260        nameCol.setSorteable(false);
261        nameCol.setWidth("60%");
262        nameCol.setFormatter(new I_CmsListFormatter() {
263
264            /**
265             * @see org.opencms.workplace.list.I_CmsListFormatter#format(java.lang.Object, java.util.Locale)
266             */
267            @SuppressWarnings("synthetic-access")
268            @Override
269            public String format(Object data, Locale locale) {
270
271                String[] dataArray = CmsStringUtil.splitAsArray((String)data, "|");
272                String resourceName = dataArray[0];
273                String resourcePath = dataArray[1];
274
275                String orgResourcePath = resourcePath;
276                LOG.info("deleted resource list: formatting '" + data + "'");
277
278                while (CmsStringUtil.isNotEmptyOrWhitespaceOnly(resourcePath)) {
279
280                    try {
281                        getCms().readResource(getCms().getRequestContext().removeSiteRoot(resourcePath));
282                        break;
283                    } catch (CmsException e) {
284                        LOG.info(
285                            "deleted resource list: could not read resource "
286                                + getCms().getRequestContext().removeSiteRoot(resourcePath)
287                                + " ["
288                                + resourcePath
289                                + "] ",
290                            e);
291                        resourcePath = CmsResource.getParentFolder(resourcePath);
292                    }
293                }
294                try {
295                    if (resourcePath != null) {
296                        resourcePath = resourcePath.substring(resourceName.length());
297                    } else {
298                        resourcePath = "";
299                    }
300                    orgResourcePath = orgResourcePath.substring(resourceName.length());
301                } catch (Exception e) {
302                    LOG.error(
303                        "Path inconsistency in deleted resource list: data='"
304                            + data
305                            + "' resourcePath='"
306                            + resourcePath
307                            + "' resourceName='"
308                            + resourceName
309                            + "' siteRoot='"
310                            + getCms().getRequestContext().getSiteRoot()
311                            + "' ",
312                        e);
313                    resourcePath = "";
314                }
315
316                StringBuffer ret = new StringBuffer();
317                ret.append(resourcePath);
318                ret.append("<span style=\"color:#0000aa;\">");
319                ret.append(
320                    orgResourcePath.startsWith(resourcePath)
321                    ? orgResourcePath.substring(resourcePath.length())
322                    : orgResourcePath);
323                ret.append("</span>");
324
325                return ret.toString();
326            }
327
328        });
329        metadata.addColumn(nameCol);
330
331        // add column for deletion date
332        CmsListColumnDefinition delDateCol = new CmsListColumnDefinition(LIST_COLUMN_DELETION_DATE);
333        delDateCol.setName(Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_COLS_DEL_DATE_0));
334        delDateCol.setSorteable(false);
335        delDateCol.setWidth("20%");
336        delDateCol.setFormatter(CmsListDateMacroFormatter.getDefaultDateFormatter());
337        metadata.addColumn(delDateCol);
338
339        // add column for version
340        CmsListColumnDefinition versionCol = new CmsListColumnDefinition(LIST_COLUMN_VERSION);
341        versionCol.setName(Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_COLS_VERSION_0));
342        versionCol.setSorteable(false);
343        versionCol.setWidth("20%");
344        versionCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
345        metadata.addColumn(versionCol);
346
347        // add column for typeid (invisible)
348        CmsListColumnDefinition typeidCol = new CmsListColumnDefinition(LIST_COLUMN_TYPEID);
349        typeidCol.setName(Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_COLS_TYPEID_0));
350        typeidCol.setVisible(false);
351        metadata.addColumn(typeidCol);
352    }
353
354    /**
355     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
356     */
357    @Override
358    protected void setIndependentActions(CmsListMetadata metadata) {
359
360        // noop
361    }
362
363    /**
364     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
365     */
366    @Override
367    protected void setMultiActions(CmsListMetadata metadata) {
368
369        // add restore multi action
370        CmsListMultiAction restoreMultiAction = new CmsListMultiAction(LIST_MACTION_RESTORE);
371        restoreMultiAction.setName(
372            Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_MACTION_RESTORE_NAME_0));
373        restoreMultiAction.setIconPath(ICON_MULTI_RESTORE);
374        restoreMultiAction.setHelpText(
375            Messages.get().container(Messages.GUI_DELETED_RESOURCES_LIST_MACTION_RESTORE_HELP_0));
376        restoreMultiAction.setVisible(false);
377        metadata.addMultiAction(restoreMultiAction);
378    }
379
380}