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.searchindex;
029
030import org.opencms.search.CmsSearchIndexSource;
031import org.opencms.search.I_CmsSearchIndex;
032import org.opencms.ui.CmsVaadinUtils;
033import org.opencms.ui.apps.I_CmsCRUDApp;
034import org.opencms.ui.components.CmsBasicDialog;
035
036import java.util.Iterator;
037import java.util.List;
038
039import com.vaadin.ui.Button;
040import com.vaadin.ui.Button.ClickEvent;
041import com.vaadin.ui.FormLayout;
042import com.vaadin.v7.shared.ui.label.ContentMode;
043import com.vaadin.v7.ui.Label;
044
045/**
046 * Class for the dialog to show source information of a given index.<p>
047 */
048public class CmsSourceDialog extends CmsBasicDialog {
049
050    /**vaadin serial id.*/
051    private static final long serialVersionUID = 4302977301857481351L;
052
053    /**vaadin serial id.*/
054    private Button m_cancelButton;
055
056    /**vaadin serial id.*/
057    private FormLayout m_layout;
058
059    /**Manager app. */
060    private I_CmsCRUDApp<I_CmsSearchIndex> m_manager;
061
062    /**
063     * public constructor.<p>
064     * @param app calling app instance
065     *
066     * @param cancel runnable to be started when the dialog gets closed
067     */
068    public CmsSourceDialog(I_CmsCRUDApp<I_CmsSearchIndex> app, final Runnable cancel) {
069
070        m_manager = app;
071        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
072
073        m_cancelButton.addClickListener(new Button.ClickListener() {
074
075            private static final long serialVersionUID = -4321889329235244258L;
076
077            public void buttonClick(ClickEvent event) {
078
079                cancel.run();
080            }
081        });
082    }
083
084    /**
085     * Sets the search index to show information about.<p>
086     *
087     * @param searchindex to be displayed
088     */
089    public void setSource(String searchindex) {
090
091        Label label = new Label();
092        label.setContentMode(ContentMode.HTML);
093
094        label.setValue(getSources(searchindex));
095
096        m_layout.removeAllComponents();
097        m_layout.addComponent(label);
098    }
099
100    /**
101     * Fills details of the index source into the given item. <p>
102     *
103     * @param indexName name of index
104     * @return String representation of information about given index
105     *
106     */
107    private String getSources(String indexName) {
108
109        StringBuffer html = new StringBuffer();
110        // search for the corresponding A_CmsSearchIndex:
111        I_CmsSearchIndex idx = m_manager.getElement(indexName);
112
113        html.append("<ul>\n");
114        // get the index sources (nice API)
115        for (CmsSearchIndexSource idxSource : idx.getSources()) {
116            html.append("  <li>\n").append("    ").append("name      : ").append(idxSource.getName()).append("\n");
117            html.append("  </li>");
118
119            html.append("  <li>\n").append("    ").append("indexer   : ").append(
120                idxSource.getIndexerClassName()).append("\n");
121            html.append("  </li>");
122
123            html.append("  <li>\n").append("    ").append("resources : ").append("\n");
124            html.append("    <ul>\n");
125            List<String> resources = idxSource.getResourcesNames();
126            Iterator<String> itResources = resources.iterator();
127            while (itResources.hasNext()) {
128                html.append("    <li>\n").append("      ").append(itResources.next()).append("\n");
129                html.append("    </li>\n");
130            }
131            html.append("    </ul>\n");
132            html.append("  </li>");
133
134            html.append("  <li>\n").append("    ").append("doctypes : ").append("\n");
135            html.append("    <ul>\n");
136            resources = idxSource.getDocumentTypes();
137            itResources = resources.iterator();
138            while (itResources.hasNext()) {
139                html.append("    <li>\n").append("      ").append(itResources.next()).append("\n");
140                html.append("    </li>\n");
141            }
142            html.append("    </ul>\n");
143            html.append("  </li>");
144        }
145
146        html.append("</ul>\n");
147        return html.toString();
148    }
149}