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.searchindex;
029
030import org.opencms.file.CmsProject;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.OpenCms;
034import org.opencms.search.fields.CmsLuceneFieldConfiguration;
035import org.opencms.search.fields.CmsSearchFieldConfiguration;
036import org.opencms.search.solr.CmsSolrFieldConfiguration;
037import org.opencms.search.solr.CmsSolrIndex;
038import org.opencms.widgets.CmsDisplayWidget;
039import org.opencms.widgets.CmsInputWidget;
040import org.opencms.widgets.CmsSelectWidget;
041import org.opencms.widgets.CmsSelectWidgetOption;
042import org.opencms.workplace.CmsWidgetDialogParameter;
043
044import java.util.ArrayList;
045import java.util.List;
046import java.util.Locale;
047
048import javax.servlet.http.HttpServletRequest;
049import javax.servlet.http.HttpServletResponse;
050import javax.servlet.jsp.PageContext;
051
052/**
053 *
054 * Dialog to edit new or existing search index in the administration view.<p>
055 *
056 * @since 6.0.0
057 */
058public class CmsEditSearchIndexDialog extends A_CmsEditSearchIndexDialog {
059
060    /**
061     * Public constructor with JSP action element.<p>
062     *
063     * @param jsp an initialized JSP action element
064     */
065    public CmsEditSearchIndexDialog(CmsJspActionElement jsp) {
066
067        super(jsp);
068    }
069
070    /**
071     * Public constructor with JSP variables.<p>
072     *
073     * @param context the JSP page context
074     * @param req the JSP request
075     * @param res the JSP response
076     */
077    public CmsEditSearchIndexDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
078
079        this(new CmsJspActionElement(context, req, res));
080    }
081
082    /**
083     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
084     *
085     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
086     *
087     * @param dialog the dialog (page) to get the HTML for
088     * @return the dialog HTML for all defined widgets of the named dialog (page)
089     *
090     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
091     */
092    @Override
093    protected String createDialogHtml(String dialog) {
094
095        StringBuffer result = new StringBuffer(1024);
096
097        result.append(createWidgetTableStart());
098        // show error header once if there were validation errors
099        result.append(createWidgetErrorHeader());
100
101        if (dialog.equals(PAGES[0])) {
102            // create the widgets for the first dialog page
103            result.append(dialogBlockStart(key(Messages.GUI_LABEL_SEARCHINDEX_BLOCK_SETTINGS_0)));
104            result.append(createWidgetTableStart());
105            result.append(createDialogRowsHtml(0, 4));
106            result.append(createWidgetTableEnd());
107            result.append(dialogBlockEnd());
108        }
109
110        result.append(createWidgetTableEnd());
111        return result.toString();
112    }
113
114    /**
115     * @see org.opencms.workplace.tools.searchindex.A_CmsEditSearchIndexDialog#defineWidgets()
116     */
117    @Override
118    protected void defineWidgets() {
119
120        super.defineWidgets();
121
122        // widgets to display
123        if ((getSearchIndexIndex() == null) || (getSearchIndexIndex().getName() == null)) {
124            addWidget(new CmsWidgetDialogParameter(getSearchIndexIndex(), "name", PAGES[0], new CmsInputWidget()));
125        } else {
126            addWidget(new CmsWidgetDialogParameter(getSearchIndexIndex(), "name", PAGES[0], new CmsDisplayWidget()));
127        }
128        addWidget(new CmsWidgetDialogParameter(
129            getSearchIndexIndex(),
130            "rebuildMode",
131            "",
132            PAGES[0],
133            new CmsSelectWidget(getRebuildModeWidgetConfiguration()),
134            0,
135            1));
136        addWidget(new CmsWidgetDialogParameter(
137            getSearchIndexIndex(),
138            "localeString",
139            "",
140            PAGES[0],
141            new CmsSelectWidget(getLocaleWidgetConfiguration()),
142            0,
143            1));
144        addWidget(new CmsWidgetDialogParameter(
145            getSearchIndexIndex(),
146            "project",
147            "",
148            PAGES[0],
149            new CmsSelectWidget(getProjectWidgetConfiguration()),
150            0,
151            1));
152        addWidget(new CmsWidgetDialogParameter(
153            getSearchIndexIndex(),
154            "fieldConfigurationName",
155            "",
156            PAGES[0],
157            new CmsSelectWidget(getFieldConfigurationWidgetConfiguration()),
158            0,
159            1));
160    }
161
162    /**
163     * Creates the options  for the search field configuration.<p>
164     *
165     * @return the option list
166     */
167    private List<CmsSelectWidgetOption> getFieldConfigurationWidgetConfiguration() {
168
169        List<CmsSelectWidgetOption> result = new ArrayList<CmsSelectWidgetOption>();
170        if (getSearchIndexIndex() instanceof CmsSolrIndex) {
171            List<CmsSolrFieldConfiguration> fieldConfigurations = m_searchManager.getFieldConfigurationsSolr();
172            for (CmsSearchFieldConfiguration config : fieldConfigurations) {
173                CmsSelectWidgetOption option = new CmsSelectWidgetOption(
174                    config.getName(),
175                    (config.getName()).equals(CmsSearchFieldConfiguration.STR_STANDARD));
176                result.add(option);
177            }
178        } else {
179            List<CmsLuceneFieldConfiguration> fieldConfigurations = m_searchManager.getFieldConfigurationsLucene();
180            for (CmsSearchFieldConfiguration config : fieldConfigurations) {
181                CmsSelectWidgetOption option = new CmsSelectWidgetOption(
182                    config.getName(),
183                    (config.getName()).equals(CmsSearchFieldConfiguration.STR_STANDARD));
184                result.add(option);
185            }
186        }
187        return result;
188    }
189
190    /**
191     * Returns the locale widget configuration.<p>
192     *
193     * @return the locale widget configuration
194     */
195    private List<CmsSelectWidgetOption> getLocaleWidgetConfiguration() {
196
197        List<CmsSelectWidgetOption> result = new ArrayList<CmsSelectWidgetOption>();
198        for (Locale locale : m_searchManager.getAnalyzers().keySet()) {
199            CmsSelectWidgetOption option = new CmsSelectWidgetOption(
200                locale.toString(),
201                locale.equals(getSearchIndexIndex().getLocale()));
202            result.add(option);
203        }
204        return result;
205    }
206
207    /**
208     * Returns the project widget configuration.<p>
209     *
210     * @return the project widget configuration
211     */
212    private List<CmsSelectWidgetOption> getProjectWidgetConfiguration() {
213
214        List<CmsSelectWidgetOption> result = new ArrayList<CmsSelectWidgetOption>();
215        try {
216            List<CmsProject> projects = OpenCms.getOrgUnitManager().getAllManageableProjects(getCms(), "", true);
217            projects.add(getCms().readProject(CmsProject.ONLINE_PROJECT_ID));
218            for (CmsProject project : projects) {
219                CmsSelectWidgetOption option = new CmsSelectWidgetOption(project.getName(), project.equals(project));
220                result.add(option);
221            }
222        } catch (CmsException e) {
223            // should never happen
224        }
225        return result;
226    }
227
228    /**
229     * Returns the rebuild mode widget configuration.<p>
230     *
231     * @return the rebuild mode widget configuration
232     */
233    private List<CmsSelectWidgetOption> getRebuildModeWidgetConfiguration() {
234
235        List<CmsSelectWidgetOption> result = new ArrayList<CmsSelectWidgetOption>();
236        String rebuildMode = getSearchIndexIndex().getRebuildMode();
237        result.add(new CmsSelectWidgetOption("auto", "auto".equals(rebuildMode)));
238        result.add(new CmsSelectWidgetOption("manual", "manual".equals(rebuildMode)));
239        result.add(new CmsSelectWidgetOption("offline", "offline".equals(rebuildMode)));
240        return result;
241    }
242
243}