001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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 jakarta.servlet.http.HttpServletRequest;
049import jakarta.servlet.http.HttpServletResponse;
050import jakarta.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(
129            new CmsWidgetDialogParameter(
130                getSearchIndexIndex(),
131                "rebuildMode",
132                "",
133                PAGES[0],
134                new CmsSelectWidget(getRebuildModeWidgetConfiguration()),
135                0,
136                1));
137        addWidget(
138            new CmsWidgetDialogParameter(
139                getSearchIndexIndex(),
140                "localeString",
141                "",
142                PAGES[0],
143                new CmsSelectWidget(getLocaleWidgetConfiguration()),
144                0,
145                1));
146        addWidget(
147            new CmsWidgetDialogParameter(
148                getSearchIndexIndex(),
149                "project",
150                "",
151                PAGES[0],
152                new CmsSelectWidget(getProjectWidgetConfiguration()),
153                0,
154                1));
155        addWidget(
156            new CmsWidgetDialogParameter(
157                getSearchIndexIndex(),
158                "fieldConfigurationName",
159                "",
160                PAGES[0],
161                new CmsSelectWidget(getFieldConfigurationWidgetConfiguration()),
162                0,
163                1));
164    }
165
166    /**
167     * Creates the options  for the search field configuration.<p>
168     *
169     * @return the option list
170     */
171    private List<CmsSelectWidgetOption> getFieldConfigurationWidgetConfiguration() {
172
173        List<CmsSelectWidgetOption> result = new ArrayList<CmsSelectWidgetOption>();
174        if (getSearchIndexIndex() instanceof CmsSolrIndex) {
175            List<CmsSolrFieldConfiguration> fieldConfigurations = m_searchManager.getFieldConfigurationsSolr();
176            for (CmsSearchFieldConfiguration config : fieldConfigurations) {
177                CmsSelectWidgetOption option = new CmsSelectWidgetOption(
178                    config.getName(),
179                    (config.getName()).equals(CmsSearchFieldConfiguration.STR_STANDARD));
180                result.add(option);
181            }
182        } else {
183            List<CmsLuceneFieldConfiguration> fieldConfigurations = m_searchManager.getFieldConfigurationsLucene();
184            for (CmsSearchFieldConfiguration config : fieldConfigurations) {
185                CmsSelectWidgetOption option = new CmsSelectWidgetOption(
186                    config.getName(),
187                    (config.getName()).equals(CmsSearchFieldConfiguration.STR_STANDARD));
188                result.add(option);
189            }
190        }
191        return result;
192    }
193
194    /**
195     * Returns the locale widget configuration.<p>
196     *
197     * @return the locale widget configuration
198     */
199    private List<CmsSelectWidgetOption> getLocaleWidgetConfiguration() {
200
201        List<CmsSelectWidgetOption> result = new ArrayList<CmsSelectWidgetOption>();
202        for (Locale locale : m_searchManager.getAnalyzers().keySet()) {
203            CmsSelectWidgetOption option = new CmsSelectWidgetOption(
204                locale.toString(),
205                locale.equals(getSearchIndexIndex().getLocale()));
206            result.add(option);
207        }
208        return result;
209    }
210
211    /**
212     * Returns the project widget configuration.<p>
213     *
214     * @return the project widget configuration
215     */
216    private List<CmsSelectWidgetOption> getProjectWidgetConfiguration() {
217
218        List<CmsSelectWidgetOption> result = new ArrayList<CmsSelectWidgetOption>();
219        try {
220            List<CmsProject> projects = OpenCms.getOrgUnitManager().getAllManageableProjects(getCms(), "", true);
221            projects.add(getCms().readProject(CmsProject.ONLINE_PROJECT_ID));
222            for (CmsProject project : projects) {
223                CmsSelectWidgetOption option = new CmsSelectWidgetOption(project.getName(), project.equals(project));
224                result.add(option);
225            }
226        } catch (CmsException e) {
227            // should never happen
228        }
229        return result;
230    }
231
232    /**
233     * Returns the rebuild mode widget configuration.<p>
234     *
235     * @return the rebuild mode widget configuration
236     */
237    private List<CmsSelectWidgetOption> getRebuildModeWidgetConfiguration() {
238
239        List<CmsSelectWidgetOption> result = new ArrayList<CmsSelectWidgetOption>();
240        String rebuildMode = getSearchIndexIndex().getRebuildMode();
241        result.add(new CmsSelectWidgetOption("auto", "auto".equals(rebuildMode)));
242        result.add(new CmsSelectWidgetOption("manual", "manual".equals(rebuildMode)));
243        result.add(new CmsSelectWidgetOption("offline", "offline".equals(rebuildMode)));
244        return result;
245    }
246
247}