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.file.CmsResource;
032import org.opencms.file.collectors.I_CmsResourceCollector;
033import org.opencms.i18n.CmsMessageContainer;
034import org.opencms.jsp.CmsJspActionElement;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.OpenCms;
038import org.opencms.relations.CmsRelationType;
039import org.opencms.util.CmsStringUtil;
040import org.opencms.workplace.explorer.CmsResourceUtil;
041import org.opencms.workplace.list.A_CmsListExplorerDialog;
042import org.opencms.workplace.list.CmsListColumnDefinition;
043import org.opencms.workplace.list.CmsListDirectAction;
044import org.opencms.workplace.list.CmsListExplorerColumn;
045import org.opencms.workplace.list.CmsListItem;
046import org.opencms.workplace.list.CmsListMetadata;
047import org.opencms.workplace.list.CmsListResourceProjStateAction;
048import org.opencms.workplace.list.I_CmsListResourceCollector;
049
050import java.util.ArrayList;
051import java.util.Iterator;
052import java.util.List;
053import java.util.Map;
054import java.util.Map.Entry;
055
056import org.apache.commons.logging.Log;
057
058/**
059 * List for resources with relations to a given resource.<p>
060 *
061 * @since 6.9.1
062 */
063public class CmsResourceLinkRelationList extends A_CmsListExplorerDialog {
064
065    /** List column id constant. */
066    protected static final String LIST_COLUMN_RELATION_TYPE = "crt";
067
068    /** The log object for this class. */
069    protected static final Log LOG = CmsLog.getLog(CmsResourceLinkRelationList.class);
070
071    /** The list id for this class. */
072    private static final String LIST_ID = "lrlr";
073
074    /** The list holds the broken links list item ids. */
075    private List<String> m_brokenLinks;
076
077    /** The resource collector for this class. */
078    private I_CmsListResourceCollector m_collector;
079
080    /** Indicates if the current request shows the source resources for the relations are shown. */
081    private boolean m_isSource;
082
083    /** The map to map resources to relation types. */
084    private Map<CmsResource, List<CmsRelationType>> m_relationTypes;
085
086    /**
087     * Default constructor.<p>
088     *
089     * @param jsp an initialized JSP action element
090     * @param isSource indicates if the source resources of the relations are shown in the list
091     */
092    public CmsResourceLinkRelationList(CmsJspActionElement jsp, boolean isSource) {
093
094        super(jsp, LIST_ID, Messages.get().container(Messages.GUI_LINK_RELATION_LIST_NAME_0));
095        m_isSource = isSource;
096        I_CmsResourceCollector collector = getCollector();
097        if ((collector != null) && (collector instanceof CmsListResourceLinkRelationCollector)) {
098            ((CmsListResourceLinkRelationCollector)collector).setSource(isSource);
099        }
100        // set the right resource util parameters
101        CmsResourceUtil resUtil = getResourceUtil();
102        resUtil.setAbbrevLength(50);
103        resUtil.setRelativeTo(CmsResource.getParentFolder(getParamResource()));
104        resUtil.setSiteMode(CmsResourceUtil.SITE_MODE_MATCHING);
105    }
106
107    /**
108     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
109     */
110    @Override
111    public void executeListMultiActions() {
112
113        throwListUnsupportedActionException();
114    }
115
116    /**
117     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
118     */
119    @Override
120    public void executeListSingleActions() {
121
122        throwListUnsupportedActionException();
123    }
124
125    /**
126     * Returns the list to identify the resources with broken links.<p>
127     *
128     * @return the list to identify the resources with broken links
129     */
130    public List<String> getBrokenLinks() {
131
132        return m_brokenLinks;
133    }
134
135    /**
136     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#getCollector()
137     */
138    @Override
139    public I_CmsListResourceCollector getCollector() {
140
141        if (m_collector == null) {
142            m_collector = new CmsListResourceLinkRelationCollector(this, getParamResource(), isSource());
143        }
144        return m_collector;
145    }
146
147    /**
148     * Returns the relationTypes.<p>
149     *
150     * @return the relationTypes
151     */
152    public Map<CmsResource, List<CmsRelationType>> getRelationTypes() {
153
154        return m_relationTypes;
155    }
156
157    /**
158     * Returns the isSource.<p>
159     *
160     * @return the isSource
161     */
162    public boolean isSource() {
163
164        return m_isSource;
165    }
166
167    /**
168     * Sets the list to identify the resources with broken links.<p>
169     *
170     * @param brokenLinks the list to identify the resources with broken links
171     */
172    public void setBrokenLinks(List<String> brokenLinks) {
173
174        m_brokenLinks = brokenLinks;
175    }
176
177    /**
178     * Sets the relationTypes.<p>
179     *
180     * @param relationTypes the relationTypes to set
181     */
182    public void setRelationTypes(Map<CmsResource, List<CmsRelationType>> relationTypes) {
183
184        m_relationTypes = relationTypes;
185    }
186
187    /**
188     * Sets the isSource.<p>
189     *
190     * @param isSource the isSource to set
191     */
192    public void setSource(boolean isSource) {
193
194        m_isSource = isSource;
195    }
196
197    /**
198     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlStart()
199     */
200    @Override
201    protected String defaultActionHtmlStart() {
202
203        return getList().listJs() + CmsListExplorerColumn.getExplorerStyleDef() + dialogContentStart(getParamTitle());
204    }
205
206    /**
207     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
208     */
209    @Override
210    protected void fillDetails(String detailId) {
211
212        // empty
213    }
214
215    /**
216     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#getListItems()
217     */
218    @Override
219    protected List<CmsListItem> getListItems() throws CmsException {
220
221        List<CmsListItem> newItems = new ArrayList<CmsListItem>();
222        List<CmsListItem> items = super.getListItems();
223        Iterator<CmsListItem> itItems = items.iterator();
224        while (itItems.hasNext()) {
225            CmsListItem item = itItems.next();
226            CmsResource resource = getResourceUtil(item).getResource();
227
228            CmsRelationType relationType = getRelationTypes().get(resource).remove(0);
229            String localizedRelationType = relationType.getLocalizedName(getMessages());
230
231            Map<String, Object> itemValues = item.getValues();
232            CmsListItem newItem = getList().newItem(localizedRelationType + "_" + resource.getStructureId().toString());
233
234            Iterator<Entry<String, Object>> itItemValuesKeys = itemValues.entrySet().iterator();
235            while (itItemValuesKeys.hasNext()) {
236                Entry<String, Object> e = itItemValuesKeys.next();
237                String currentKey = e.getKey();
238                newItem.set(currentKey, e.getValue());
239            }
240            newItem.set(LIST_COLUMN_RELATION_TYPE, localizedRelationType);
241            newItems.add(newItem);
242        }
243        return newItems;
244    }
245
246    /**
247     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#isColumnVisible(int)
248     */
249    @Override
250    protected boolean isColumnVisible(int colFlag) {
251
252        boolean isVisible = (colFlag == CmsUserSettings.FILELIST_TITLE);
253        isVisible = isVisible || (colFlag == LIST_COLUMN_TYPEICON.hashCode());
254        isVisible = isVisible || (colFlag == LIST_COLUMN_LOCKICON.hashCode());
255        isVisible = isVisible || (colFlag == LIST_COLUMN_PROJSTATEICON.hashCode());
256        isVisible = isVisible || (colFlag == LIST_COLUMN_NAME.hashCode());
257        isVisible = isVisible || (colFlag == CmsUserSettings.FILELIST_TYPE);
258        isVisible = isVisible || (colFlag == CmsUserSettings.FILELIST_SIZE);
259        isVisible = isVisible
260            || ((colFlag == LIST_COLUMN_SITE.hashCode()) && (OpenCms.getSiteManager().getSites().size() > 1));
261        return isVisible;
262    }
263
264    /**
265     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
266     */
267    @Override
268    protected void setColumns(CmsListMetadata metadata) {
269
270        super.setColumns(metadata);
271        // position 3: project state icon, resource is inside or outside current project
272        CmsListColumnDefinition projStateIconCol = metadata.getColumnDefinition(LIST_COLUMN_PROJSTATEICON);
273        projStateIconCol.removeDirectAction(LIST_ACTION_PROJSTATEICON);
274
275        // add resource icon action
276        CmsListDirectAction resourceProjStateAction = new CmsListResourceProjStateAction(LIST_ACTION_PROJSTATEICON) {
277
278            /**
279             * @see org.opencms.workplace.list.CmsListResourceProjStateAction#getIconPath()
280             */
281            @Override
282            public String getIconPath() {
283
284                if (((CmsResourceLinkRelationList)getWp()).getBrokenLinks() != null) {
285                    if (((CmsResourceLinkRelationList)getWp()).getBrokenLinks().contains(getItem().getId())) {
286                        return "buttons/deletecontent.png";
287                    }
288                }
289                return super.getIconPath();
290            }
291
292            /**
293             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getName()
294             */
295            @Override
296            public CmsMessageContainer getName() {
297
298                if (((CmsResourceLinkRelationList)getWp()).getBrokenLinks() != null) {
299                    if (((CmsResourceLinkRelationList)getWp()).getBrokenLinks().contains(getItem().getId())) {
300                        return Messages.get().container(Messages.GUI_RELATION_LIST_BROKEN_HELP_0);
301                    }
302                }
303                return super.getName();
304            }
305        };
306        resourceProjStateAction.setEnabled(false);
307        projStateIconCol.addDirectAction(resourceProjStateAction);
308
309        CmsListColumnDefinition relationTypeCol = new CmsListResourceLinkRelationExplorerColumn(
310            LIST_COLUMN_RELATION_TYPE);
311        relationTypeCol.setName(Messages.get().container(Messages.GUI_RELATION_LIST_TYPE_NAME_0));
312        metadata.addColumn(relationTypeCol, 4);
313    }
314
315    /**
316     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
317     */
318    @Override
319    protected void setMultiActions(CmsListMetadata metadata) {
320
321        // empty
322    }
323
324    /**
325     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
326     */
327    @Override
328    protected void validateParamaters() throws Exception {
329
330        if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamResource())) {
331            throw new Exception();
332        }
333    }
334}