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, 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.ui.apps.linkvalidation;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsPropertyDefinition;
032import org.opencms.file.CmsResource;
033import org.opencms.main.CmsException;
034import org.opencms.main.OpenCms;
035import org.opencms.relations.CmsInternalLinksValidator;
036import org.opencms.relations.CmsRelation;
037import org.opencms.ui.A_CmsUI;
038import org.opencms.ui.CmsVaadinUtils;
039import org.opencms.ui.apps.A_CmsWorkplaceApp;
040import org.opencms.ui.apps.CmsFileExplorer;
041import org.opencms.ui.apps.Messages;
042import org.opencms.ui.components.CmsFileTable;
043import org.opencms.ui.components.CmsResourceTableProperty;
044import org.opencms.util.CmsStringUtil;
045
046import java.util.Iterator;
047import java.util.LinkedHashMap;
048import java.util.List;
049import java.util.Map;
050
051import com.vaadin.server.Sizeable.Unit;
052import com.vaadin.ui.Component;
053import com.vaadin.ui.HorizontalSplitPanel;
054import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
055import com.vaadin.v7.ui.VerticalLayout;
056
057/**
058 * Class for the Link validation app.<p>
059 */
060public class CmsLinkValidationApp extends A_CmsWorkplaceApp {
061
062    /**
063     * Validator.<p>
064     */
065    public class InternalValidator extends A_CmsLinkValidator {
066
067        /**Link validator. */
068        CmsInternalLinksValidator validator;
069
070        /**
071         * @see org.opencms.ui.apps.linkvalidation.A_CmsLinkValidator#failedResources(java.util.List)
072         */
073        @Override
074        public List<CmsResource> failedResources(List<String> resources) {
075
076            validator = new CmsInternalLinksValidator(A_CmsUI.getCmsObject(), resources);
077            return validator.getResourcesWithBrokenLinks();
078        }
079
080        /**
081         * @see org.opencms.ui.apps.linkvalidation.A_CmsLinkValidator#failMessage(org.opencms.file.CmsResource)
082         */
083        @Override
084        public String failMessage(CmsResource resource) {
085
086            String res = "";
087            List<CmsRelation> brokenLinks = validator.getBrokenLinksForResource(resource.getRootPath());
088            if (brokenLinks != null) {
089                Iterator<CmsRelation> j = brokenLinks.iterator();
090                while (j.hasNext()) {
091                    res += getBrokenLinkString(j.next().getTargetPath());
092                }
093            }
094            return res;
095        }
096
097        /**
098         * @see org.opencms.ui.apps.linkvalidation.A_CmsLinkValidator#getClickListener()
099         */
100        @Override
101        public ItemClickListener getClickListener() {
102
103            return null;
104        }
105
106        /**
107         * @see org.opencms.ui.apps.linkvalidation.A_CmsLinkValidator#getPropertyName()
108         */
109        @Override
110        public String getPropertyName() {
111
112            return "BrokenLinks";
113        }
114
115        /**
116         * @see org.opencms.ui.apps.linkvalidation.A_CmsLinkValidator#getTableProperties()
117         */
118        @Override
119        public Map<CmsResourceTableProperty, Integer> getTableProperties() {
120
121            property = new CmsResourceTableProperty(
122                getPropertyName(),
123                String.class,
124                "",
125                org.opencms.ui.apps.Messages.GUI_LINKVALIDATION_BROKENLINKS_DETAIL_LINKS_NAME_0,
126                true,
127                0,
128                200);
129            Map<CmsResourceTableProperty, Integer> res = new LinkedHashMap<CmsResourceTableProperty, Integer>(
130                CmsFileTable.DEFAULT_TABLE_PROPERTIES);
131            res.put(property, Integer.valueOf(0));
132            return res;
133        }
134
135        /**
136         * get string to show for broken link.<p>
137         *
138         * 1:1 the same like old workplace app<p>
139         *
140         * @param rootPath to get Broken links for.
141         * @return broken link string
142         */
143        private String getBrokenLinkString(String rootPath) {
144
145            String ret = "";
146
147            CmsObject rootCms;
148            try {
149                rootCms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
150
151                rootCms.getRequestContext().setSiteRoot("");
152
153                String siteRoot = OpenCms.getSiteManager().getSiteRoot(rootPath);
154                String siteName = siteRoot;
155                if (siteRoot != null) {
156                    try {
157
158                        siteName = rootCms.readPropertyObject(
159                            siteRoot,
160                            CmsPropertyDefinition.PROPERTY_TITLE,
161                            false).getValue(siteRoot);
162                    } catch (CmsException e) {
163                        siteName = siteRoot;
164                    }
165                    ret = rootPath.substring(siteRoot.length());
166                } else {
167                    siteName = "/";
168                }
169                if (!A_CmsUI.getCmsObject().getRequestContext().getSiteRoot().equals(siteRoot)) {
170                    ret = CmsVaadinUtils.getMessageText(
171                        org.opencms.workplace.commons.Messages.GUI_DELETE_SITE_RELATION_2,
172                        new Object[] {siteName, rootPath});
173                }
174            } catch (CmsException e1) {
175                //
176            }
177            return ret;
178        }
179
180    }
181
182    /**
183     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getBreadCrumbForState(java.lang.String)
184     */
185    @Override
186    protected LinkedHashMap<String, String> getBreadCrumbForState(String state) {
187
188        LinkedHashMap<String, String> crumbs = new LinkedHashMap<String, String>();
189
190        //Main page.
191        if (CmsStringUtil.isEmptyOrWhitespaceOnly(state)) {
192            crumbs.put("", CmsVaadinUtils.getMessageText(Messages.GUI_LINKVALIDATION_ADMIN_TOOL_NAME_SHORT_0));
193            return crumbs;
194        }
195        return new LinkedHashMap<String, String>(); //size==1 & state was not empty -> state doesn't match to known path
196    }
197
198    /**
199     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getComponentForState(java.lang.String)
200     */
201    @Override
202    protected Component getComponentForState(String state) {
203
204        if (state.isEmpty()) {
205            m_rootLayout.setMainHeightFull(true);
206            return getInternalComponent();
207        }
208        return null;
209    }
210
211    /**
212     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getSubNavEntries(java.lang.String)
213     */
214    @Override
215    protected List<NavEntry> getSubNavEntries(String state) {
216
217        return null;
218    }
219
220    /**
221     * Returns the component for the internal link validation.<p>
222     *
223     * @return vaadin component
224     */
225    private HorizontalSplitPanel getInternalComponent() {
226
227        m_rootLayout.setMainHeightFull(true);
228        HorizontalSplitPanel sp = new HorizontalSplitPanel();
229        sp.setSizeFull();
230        VerticalLayout result = new VerticalLayout();
231        result.setSizeFull();
232        VerticalLayout intro = CmsVaadinUtils.getInfoLayout(Messages.GUI_LINKVALIDATION_INTRODUCTION_0);
233        VerticalLayout nullResult = CmsVaadinUtils.getInfoLayout(Messages.GUI_LINKVALIDATION_NO_BROKEN_LINKS_0);
234
235        nullResult.setVisible(false);
236        CmsLinkValidationInternalTable table = new CmsLinkValidationInternalTable(
237            intro,
238            nullResult,
239            new InternalValidator());
240        table.setVisible(false);
241        table.setSizeFull();
242        table.setWidth("100%");
243
244        result.addComponent(table);
245        result.addComponent(intro);
246        result.addComponent(nullResult);
247
248        VerticalLayout leftCol = new VerticalLayout();
249        leftCol.setSizeFull();
250        CmsInternalResources resources = new CmsInternalResources(table);
251        leftCol.addComponent(resources);
252
253        leftCol.setExpandRatio(resources, 1);
254        sp.setFirstComponent(leftCol);
255        sp.setSecondComponent(result);
256        sp.setSplitPosition(CmsFileExplorer.LAYOUT_SPLIT_POSITION, Unit.PIXELS);
257        return sp;
258    }
259}