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.link;
029
030import org.opencms.file.CmsPropertyDefinition;
031import org.opencms.file.CmsResource;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.OpenCms;
035import org.opencms.relations.CmsInternalLinksValidator;
036import org.opencms.relations.CmsRelation;
037import org.opencms.workplace.list.A_CmsListExplorerDialog;
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.I_CmsListResourceCollector;
043
044import java.util.ArrayList;
045import java.util.Iterator;
046import java.util.List;
047import java.util.Map;
048
049import javax.servlet.http.HttpServletRequest;
050import javax.servlet.http.HttpServletResponse;
051import javax.servlet.jsp.PageContext;
052
053/**
054 * Internal link validation Dialog.<p>
055 *
056 * @since 6.5.3
057 */
058public class CmsInternalLinkValidationList extends A_CmsListExplorerDialog {
059
060    /** List detail error. */
061    public static final String LIST_DETAIL_LINKS = "dl";
062
063    /** list id constant. */
064    public static final String LIST_ID = "lv";
065
066    /** The internal collector instance. */
067    private I_CmsListResourceCollector m_collector;
068
069    /** The validator class. */
070    private CmsInternalLinksValidator m_validator;
071
072    /**
073     * Public constructor.<p>
074     *
075     * @param jsp an initialized JSP action element
076     */
077    public CmsInternalLinkValidationList(CmsJspActionElement jsp) {
078
079        super(jsp, LIST_ID, Messages.get().container(Messages.GUI_BROKENLINKS_LIST_NAME_0));
080    }
081
082    /**
083     * Public constructor with JSP variables.<p>
084     *
085     * @param context the JSP page context
086     * @param req the JSP request
087     * @param res the JSP response
088     */
089    public CmsInternalLinkValidationList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
090
091        this(new CmsJspActionElement(context, req, res));
092    }
093
094    /**
095     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
096     */
097    @Override
098    public void executeListMultiActions() {
099
100        throwListUnsupportedActionException();
101    }
102
103    /**
104     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
105     */
106    @Override
107    public void executeListSingleActions() {
108
109        throwListUnsupportedActionException();
110    }
111
112    /**
113     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#getCollector()
114     */
115    @Override
116    public I_CmsListResourceCollector getCollector() {
117
118        if (m_collector == null) {
119            m_collector = new CmsInternalLinkValidationFilesCollector(
120                this,
121                getValidator().getResourcesWithBrokenLinks());
122        }
123        return m_collector;
124    }
125
126    /**
127     * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlStart()
128     */
129    @Override
130    protected String customHtmlStart() {
131
132        StringBuffer result = new StringBuffer(512);
133        if (getValidator().getNotVisibleResourcesCount() > 0) {
134            result.append(dialogBlockStart(key(Messages.GUI_BROKENLINKS_NOTICE_0)));
135            result.append("\n");
136            result.append(
137                key(
138                    Messages.GUI_BROKENLINKS_NOT_VISIBLE_RESOURCES_1,
139                    new Object[] {Integer.valueOf(getValidator().getNotVisibleResourcesCount())}));
140            result.append("\n");
141            result.append(dialogBlockEnd());
142        }
143        return result.toString();
144    }
145
146    /**
147     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
148     */
149    @Override
150    protected void fillDetails(String detailId) {
151
152        // get content
153        List resourceNames = getList().getAllContent();
154        Iterator i = resourceNames.iterator();
155        while (i.hasNext()) {
156            CmsListItem item = (CmsListItem)i.next();
157            CmsResource res = getCollector().getResource(getCms(), item);
158            // check if errors are enabled
159            StringBuffer html = new StringBuffer();
160            // broken links detail is enabled
161            if (detailId.equals(LIST_DETAIL_LINKS)) {
162                // get all errors for this resource and show them
163                List brokenLinks = getValidator().getBrokenLinksForResource(res.getRootPath());
164                if (brokenLinks != null) {
165                    Iterator j = brokenLinks.iterator();
166                    while (j.hasNext()) {
167                        CmsRelation brokenLink = (CmsRelation)j.next();
168                        String link = brokenLink.getTargetPath();
169                        String siteRoot = OpenCms.getSiteManager().getSiteRoot(link);
170                        String siteName = siteRoot;
171                        if (siteRoot != null) {
172                            String storedSiteRoot = getCms().getRequestContext().getSiteRoot();
173                            try {
174                                getCms().getRequestContext().setSiteRoot("/");
175                                siteName = getCms().readPropertyObject(
176                                    siteRoot,
177                                    CmsPropertyDefinition.PROPERTY_TITLE,
178                                    false).getValue(siteRoot);
179                            } catch (CmsException e) {
180                                siteName = siteRoot;
181                            } finally {
182                                getCms().getRequestContext().setSiteRoot(storedSiteRoot);
183                            }
184                            link = link.substring(siteRoot.length());
185                        } else {
186                            siteName = "/";
187                        }
188                        if (!getCms().getRequestContext().getSiteRoot().equals(siteRoot)) {
189                            link = key(
190                                org.opencms.workplace.commons.Messages.GUI_DELETE_SITE_RELATION_2,
191                                new Object[] {siteName, link});
192                        }
193                        html.append(link);
194                        html.append("<br>");
195                    }
196                    item.set(detailId, html.toString());
197                }
198            }
199        }
200    }
201
202    /**
203     * @see org.opencms.workplace.CmsWorkplace#initMessages()
204     */
205    @Override
206    protected void initMessages() {
207
208        // add specific dialog resource bundle
209        addMessages(Messages.get().getBundleName());
210        super.initMessages();
211    }
212
213    /**
214     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
215     */
216    @Override
217    protected void setIndependentActions(CmsListMetadata metadata) {
218
219        // create list item detail for broken links
220        CmsListItemDetails brokenLinks = new CmsListItemDetails(LIST_DETAIL_LINKS);
221        brokenLinks.setAtColumn(LIST_COLUMN_NAME);
222        brokenLinks.setVisible(true);
223        brokenLinks.setShowActionName(Messages.get().container(Messages.GUI_BROKENLINKS_DETAIL_SHOW_LINKS_NAME_0));
224        brokenLinks.setShowActionHelpText(Messages.get().container(Messages.GUI_BROKENLINKS_DETAIL_SHOW_LINKS_HELP_0));
225        brokenLinks.setHideActionName(Messages.get().container(Messages.GUI_BROKENLINKS_DETAIL_HIDE_LINKS_NAME_0));
226        brokenLinks.setHideActionHelpText(Messages.get().container(Messages.GUI_BROKENLINKS_DETAIL_HIDE_LINKS_HELP_0));
227        brokenLinks.setFormatter(
228            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_BROKENLINKS_DETAIL_LINKS_NAME_0)));
229        metadata.addItemDetails(brokenLinks);
230
231        super.setIndependentActions(metadata);
232    }
233
234    /**
235     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
236     */
237    @Override
238    protected void setMultiActions(CmsListMetadata metadata) {
239
240        // no LMA
241    }
242
243    /**
244     * Returns the link validator class.<p>
245     *
246     * @return the link validator class
247     */
248    private CmsInternalLinksValidator getValidator() {
249
250        if (m_validator == null) {
251            // get the content check result object
252            Map objects = (Map)getSettings().getDialogObject();
253            Object o = objects.get(CmsInternalLinkValidationDialog.class.getName());
254            List resources = new ArrayList();
255            if ((o != null) && (o instanceof List)) {
256                resources = (List)o;
257            }
258            m_validator = new CmsInternalLinksValidator(getCms(), resources);
259        }
260        return m_validator;
261    }
262
263}