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.db.CmsUserSettings;
031import org.opencms.i18n.CmsEncoder;
032import org.opencms.i18n.CmsMessageContainer;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.workplace.CmsDialog;
035import org.opencms.workplace.CmsMultiDialog;
036import org.opencms.workplace.CmsWorkplace;
037import org.opencms.workplace.explorer.CmsResourceUtil;
038import org.opencms.workplace.list.A_CmsListDialog;
039import org.opencms.workplace.list.A_CmsListExplorerDialog;
040import org.opencms.workplace.list.A_CmsListIndependentJsAction;
041import org.opencms.workplace.list.CmsListColumnDefinition;
042import org.opencms.workplace.list.CmsListDirectAction;
043import org.opencms.workplace.list.CmsListExplorerColumn;
044import org.opencms.workplace.list.CmsListItemDetails;
045import org.opencms.workplace.list.CmsListMetadata;
046import org.opencms.workplace.list.CmsListResourceProjStateAction;
047import org.opencms.workplace.list.I_CmsListAction;
048import org.opencms.workplace.list.I_CmsListResourceCollector;
049
050import java.util.Iterator;
051import java.util.List;
052import java.util.Map;
053
054/**
055 * Explorer dialog for the project files view.<p>
056 *
057 * @since 6.0.0
058 */
059public class CmsLockedResourcesList extends A_CmsListExplorerDialog {
060
061    /** list action id constant. */
062    public static final String LIST_DETAIL_OWN_LOCKS = "dol";
063
064    /** list action id constant. */
065    public static final String LIST_DETAIL_OWN_LOCKS_HIDE = "dolh";
066
067    /** list action id constant. */
068    public static final String LIST_DETAIL_OWN_LOCKS_SHOW = "dols";
069
070    /** list id constant. */
071    public static final String LIST_ID = "llr";
072
073    /** List column id constant. */
074    protected static final String LIST_COLUMN_IS_RELATED = "ecir";
075
076    /** The internal collector instance. */
077    private I_CmsListResourceCollector m_collector;
078
079    /** The parameter map for creating the ajax request in the independent action.  */
080    private Map<String, String> m_lockParams;
081
082    /**
083     * Public constructor with JSP action element.<p>
084     *
085     * @param jsp an initialized JSP action element
086     * @param lockedResources the list of locked resources (as root paths)
087     * @param relativeTo the current folder
088     * @param lockParams the parameter map for creating the ajax request in the independent action
089     */
090    public CmsLockedResourcesList(
091        CmsJspActionElement jsp,
092        List<String> lockedResources,
093        String relativeTo,
094        Map<String, String> lockParams) {
095
096        super(jsp, LIST_ID, Messages.get().container(Messages.GUI_LOCKED_FILES_LIST_NAME_0));
097        m_collector = new CmsLockedResourcesCollector(this, lockedResources);
098
099        // prevent paging
100        getList().setMaxItemsPerPage(Integer.MAX_VALUE);
101
102        // set the right resource util parameters
103        CmsResourceUtil resUtil = getResourceUtil();
104        resUtil.setAbbrevLength(50);
105        resUtil.setRelativeTo(getCms().getRequestContext().addSiteRoot(relativeTo));
106
107        m_lockParams = lockParams;
108        getList().getMetadata().getItemDetailDefinition(LIST_DETAIL_OWN_LOCKS).setVisible(
109            Boolean.valueOf(getLockParams().get(CmsLock.PARAM_SHOWOWNLOCKS)).booleanValue());
110    }
111
112    /**
113     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
114     */
115    @Override
116    public void executeListMultiActions() {
117
118        throwListUnsupportedActionException();
119    }
120
121    /**
122     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
123     */
124    @Override
125    public void executeListSingleActions() {
126
127        throwListUnsupportedActionException();
128    }
129
130    /**
131     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#getCollector()
132     */
133    @Override
134    public I_CmsListResourceCollector getCollector() {
135
136        return m_collector;
137    }
138
139    /**
140     * Returns the parameter map for creating the ajax request in the independent action.<p>
141     *
142     * @return the parameter map for creating the ajax request in the independent action
143     */
144    public Map<String, String> getLockParams() {
145
146        return m_lockParams;
147    }
148
149    /**
150     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
151     */
152    @Override
153    protected void fillDetails(String detailId) {
154
155        // no-details
156    }
157
158    /**
159     * @see org.opencms.workplace.CmsWorkplace#initMessages()
160     */
161    @Override
162    protected void initMessages() {
163
164        // add specific dialog resource bundle
165        addMessages(Messages.get().getBundleName());
166        // add default resource bundles
167        super.initMessages();
168    }
169
170    /**
171     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#isColumnVisible(int)
172     */
173    @Override
174    protected boolean isColumnVisible(int colFlag) {
175
176        boolean isVisible = (colFlag == CmsUserSettings.FILELIST_TITLE);
177        isVisible = isVisible || (colFlag == CmsUserSettings.FILELIST_LOCKEDBY);
178        isVisible = isVisible || (colFlag == LIST_COLUMN_TYPEICON.hashCode());
179        isVisible = isVisible || (colFlag == LIST_COLUMN_LOCKICON.hashCode());
180        isVisible = isVisible || (colFlag == LIST_COLUMN_PROJSTATEICON.hashCode());
181        isVisible = isVisible || (colFlag == LIST_COLUMN_NAME.hashCode());
182        return isVisible;
183    }
184
185    /**
186     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
187     */
188    @Override
189    protected void setColumns(CmsListMetadata metadata) {
190
191        super.setColumns(metadata);
192
193        Iterator<CmsListColumnDefinition> it = metadata.getColumnDefinitions().iterator();
194        while (it.hasNext()) {
195            CmsListColumnDefinition colDefinition = it.next();
196            colDefinition.setSorteable(false);
197            if (colDefinition.getId().equals(LIST_COLUMN_NAME)) {
198                colDefinition.removeDefaultAction(LIST_DEFACTION_OPEN);
199                colDefinition.setWidth("60%");
200            } else if (colDefinition.getId().equals(LIST_COLUMN_PROJSTATEICON)) {
201                colDefinition.removeDirectAction(LIST_ACTION_PROJSTATEICON);
202                // add resource state icon action
203                CmsListDirectAction resourceProjStateAction = new CmsListResourceProjStateAction(
204                    LIST_ACTION_PROJSTATEICON) {
205
206                    /**
207                     * @see org.opencms.workplace.list.CmsListResourceProjStateAction#getIconPath()
208                     */
209                    @Override
210                    public String getIconPath() {
211
212                        if (((Boolean)getItem().get(LIST_COLUMN_IS_RELATED)).booleanValue()) {
213                            return "explorer/related_resource.png";
214                        }
215                        return super.getIconPath();
216                    }
217
218                    /**
219                     * @see org.opencms.workplace.list.CmsListResourceProjStateAction#getName()
220                     */
221                    @Override
222                    public CmsMessageContainer getName() {
223
224                        if (((Boolean)getItem().get(LIST_COLUMN_IS_RELATED)).booleanValue()) {
225                            return Messages.get().container(Messages.GUI_PUBLISH_RELATED_RESOURCE_0);
226                        }
227                        return super.getName();
228                    }
229                };
230                resourceProjStateAction.setEnabled(false);
231                colDefinition.addDirectAction(resourceProjStateAction);
232            }
233        }
234
235        CmsListColumnDefinition relatedCol = new CmsListExplorerColumn(LIST_COLUMN_IS_RELATED);
236        relatedCol.setName(
237            org.opencms.workplace.explorer.Messages.get().container(
238                org.opencms.workplace.explorer.Messages.GUI_INPUT_NAME_0));
239        relatedCol.setVisible(false);
240        relatedCol.setPrintable(false);
241        metadata.addColumn(relatedCol);
242    }
243
244    /**
245     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
246     */
247    @Override
248    protected void setIndependentActions(CmsListMetadata metadata) {
249
250        /**
251         * Class to render a javascript driven detail action button.<p>
252         */
253        abstract class DetailsJsAction extends A_CmsListIndependentJsAction {
254
255            /**
256             * Default constructor.<p>
257             *
258             * @param id the action id
259             */
260            public DetailsJsAction(String id) {
261
262                super(id);
263            }
264
265            /**
266             * @see org.opencms.workplace.list.CmsListIndependentAction#buttonHtml(org.opencms.workplace.CmsWorkplace)
267             */
268            @Override
269            public String buttonHtml(CmsWorkplace wp) {
270
271                StringBuffer html = new StringBuffer(1024);
272                html.append("\t<span id='");
273                html.append(getId());
274                html.append("' class=\"link");
275                html.append("\"");
276                html.append(" onClick=\"");
277                html.append(resolveOnClic(wp));
278                html.append("\"");
279                html.append(">");
280                html.append("<img src='");
281                html.append(CmsWorkplace.getSkinUri());
282                html.append(super.getIconPath());
283                html.append("'");
284                html.append(" alt='");
285                html.append(super.getName().key(wp.getLocale()));
286                html.append("'");
287                html.append(" title='");
288                html.append(super.getName().key(wp.getLocale()));
289                html.append("'");
290                html.append(">");
291                html.append("&nbsp;");
292                html.append("<a href='#'>");
293                html.append(super.getName().key(wp.getLocale()));
294                html.append("</a>");
295                html.append("</span>");
296                return html.toString();
297            }
298
299            /**
300             * Returns an ajax request call code.<p>
301             *
302             * @param wp the workplace context
303             * @param showOwnLocks if to show or hide the own locked resources
304             *
305             * @return html code
306             */
307            protected String getRequestLink(CmsWorkplace wp, boolean showOwnLocks) {
308
309                Map<String, String> params = ((CmsLockedResourcesList)wp).getLockParams();
310                StringBuffer html = new StringBuffer(128);
311                html.append(
312                    "javascript:{ajaxReportContent = ''; document.getElementById('ajaxreport').innerHTML = ajaxWaitMessage; makeRequest('");
313                html.append(wp.getJsp().link("/system/workplace/commons/report-locks.jsp"));
314                html.append("', '");
315                boolean needsAmpersand = false;
316                if (params.get(CmsMultiDialog.PARAM_RESOURCELIST) != null) {
317                    html.append(CmsMultiDialog.PARAM_RESOURCELIST);
318                    html.append("=");
319                    html.append(CmsEncoder.escapeXml(params.get(CmsMultiDialog.PARAM_RESOURCELIST)));
320                    needsAmpersand = true;
321                }
322                if (params.get(CmsDialog.PARAM_RESOURCE) != null) {
323                    if (needsAmpersand) {
324                        html.append("&");
325                    }
326                    html.append(CmsDialog.PARAM_RESOURCE);
327                    html.append("=");
328                    html.append(CmsEncoder.escapeXml(params.get(CmsDialog.PARAM_RESOURCE)));
329                    needsAmpersand = true;
330                }
331                if (params.get(CmsLock.PARAM_INCLUDERELATED) != null) {
332                    if (needsAmpersand) {
333                        html.append("&");
334                    }
335                    html.append(CmsLock.PARAM_INCLUDERELATED);
336                    html.append("=");
337                    html.append(CmsEncoder.escapeXml(params.get(CmsLock.PARAM_INCLUDERELATED)));
338                }
339                if (needsAmpersand) {
340                    html.append("&");
341                }
342                html.append(CmsLock.PARAM_SHOWOWNLOCKS);
343                html.append("=").append(showOwnLocks).append("', 'doReportUpdate');}");
344                return html.toString();
345            }
346        }
347
348        I_CmsListAction hideAction = new DetailsJsAction(LIST_DETAIL_OWN_LOCKS_HIDE) {
349
350            /**
351             * @see org.opencms.workplace.list.A_CmsListIndependentJsAction#jsCode(CmsWorkplace)
352             */
353            @Override
354            public String jsCode(CmsWorkplace wp) {
355
356                return getRequestLink(wp, false);
357            }
358        };
359        hideAction.setIconPath(A_CmsListDialog.ICON_DETAILS_HIDE);
360        hideAction.setName(Messages.get().container(Messages.GUI_LOCK_DETAIL_HIDE_OWN_LOCKS_NAME_0));
361        hideAction.setHelpText(Messages.get().container(Messages.GUI_LOCK_DETAIL_HIDE_OWN_LOCKS_HELP_0));
362
363        I_CmsListAction showAction = new DetailsJsAction(LIST_DETAIL_OWN_LOCKS_SHOW) {
364
365            /**
366             * @see org.opencms.workplace.list.A_CmsListIndependentJsAction#jsCode(CmsWorkplace)
367             */
368            @Override
369            public String jsCode(CmsWorkplace wp) {
370
371                return getRequestLink(wp, true);
372            }
373        };
374        showAction.setIconPath(A_CmsListDialog.ICON_DETAILS_SHOW);
375        showAction.setName(Messages.get().container(Messages.GUI_LOCK_DETAIL_SHOW_OWN_LOCKS_NAME_0));
376        showAction.setHelpText(Messages.get().container(Messages.GUI_LOCK_DETAIL_SHOW_OWN_LOCKS_HELP_0));
377
378        // create list item detail
379        CmsListItemDetails relationsDetails = new CmsListItemDetails(LIST_DETAIL_OWN_LOCKS);
380        relationsDetails.setAtColumn(LIST_COLUMN_NAME);
381        relationsDetails.setVisible(false);
382        relationsDetails.setFormatter(new CmsPublishBrokenRelationFormatter());
383        relationsDetails.setHideAction(hideAction);
384        relationsDetails.setShowAction(showAction);
385
386        // add resources info item detail to meta data
387        metadata.addItemDetails(relationsDetails);
388    }
389
390    /**
391     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
392     */
393    @Override
394    protected void setMultiActions(CmsListMetadata metadata) {
395
396        // no LMAs, and remove default search action
397        metadata.setSearchAction(null);
398    }
399}