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.configuration.CmsSearchConfiguration;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsIllegalStateException;
033import org.opencms.main.OpenCms;
034import org.opencms.search.CmsSearchManager;
035import org.opencms.search.fields.CmsLuceneFieldConfiguration;
036import org.opencms.search.fields.CmsSearchField;
037import org.opencms.search.fields.I_CmsSearchFieldConfiguration;
038import org.opencms.workplace.CmsWidgetDialog;
039import org.opencms.workplace.CmsWorkplaceSettings;
040
041import java.util.ArrayList;
042import java.util.HashMap;
043import java.util.List;
044import java.util.Map;
045
046import javax.servlet.http.HttpServletRequest;
047import javax.servlet.http.HttpServletResponse;
048import javax.servlet.jsp.PageContext;
049
050/**
051 *
052 * Abstract widget dialog for all dialogs working with <code>{@link CmsLuceneFieldConfiguration}</code>.<p>
053 *
054 * @since 6.5.5
055 */
056public class A_CmsFieldConfigurationDialog extends CmsWidgetDialog {
057
058    /** localized messages Keys prefix. */
059    public static final String KEY_PREFIX = "fieldconfiguration";
060
061    /** Defines which pages are valid for this dialog. */
062    public static final String[] PAGES = {"page1"};
063
064    /** The request parameter for the fieldconfiguration to work with when contacting
065     * this dialog from another. */
066    public static final String PARAM_FIELDCONFIGURATION = "fieldconfiguration";
067
068    /** The user object that is edited on this dialog. */
069    protected I_CmsSearchFieldConfiguration m_fieldconfiguration;
070
071    /** The search manager singleton for convenient access. **/
072    protected CmsSearchManager m_searchManager;
073
074    /** Stores the value of the request parameter for the search index Name. */
075    private String m_paramFieldConfiguration;
076
077    /**
078     * Public constructor with JSP action element.<p>
079     *
080     * @param jsp an initialized JSP action element
081     */
082    public A_CmsFieldConfigurationDialog(CmsJspActionElement jsp) {
083
084        super(jsp);
085    }
086
087    /**
088     * Public constructor with JSP variables.<p>
089     *
090     * @param context the JSP page context
091     * @param req the JSP request
092     * @param res the JSP response
093     */
094    public A_CmsFieldConfigurationDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
095
096        this(new CmsJspActionElement(context, req, res));
097    }
098
099    /**
100     * Writes the updated search configuration back to the XML
101     * configuration file and refreshes the complete list.<p>
102     */
103    protected static void writeConfiguration() {
104
105        // update the XML configuration
106        OpenCms.writeConfiguration(CmsSearchConfiguration.class);
107    }
108
109    /**
110     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
111     */
112    @Override
113    public void actionCommit() {
114
115        List<Throwable> errors = new ArrayList<Throwable>();
116
117        try {
118            // if new create it first
119            if (m_searchManager.getFieldConfiguration(m_fieldconfiguration.getName()) == null) {
120                m_searchManager.addFieldConfiguration(m_fieldconfiguration);
121            }
122            if (checkWriteConfiguration()) {
123                writeConfiguration();
124            }
125        } catch (Throwable t) {
126            errors.add(t);
127        }
128
129        // set the list of errors to display when saving failed
130        setCommitErrors(errors);
131
132    }
133
134    /**
135     * Returns the request parameter value for parameter fieldconfiguration. <p>
136     *
137     * @return the request parameter value for parameter fieldconfiguration
138     */
139    public String getParamFieldconfiguration() {
140
141        return m_paramFieldConfiguration;
142    }
143
144    /**
145     * Sets the request parameter value for parameter fieldconfiguration. <p>
146     *
147     * @param fieldconfiguration the request parameter value for parameter fieldconfiguration
148     */
149    public void setParamFieldconfiguration(String fieldconfiguration) {
150
151        m_paramFieldConfiguration = fieldconfiguration;
152    }
153
154    /**
155     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
156     *
157     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
158     *
159     * @param dialog the dialog (page) to get the HTML for
160     * @return the dialog HTML for all defined widgets of the named dialog (page)
161     */
162    @Override
163    protected String createDialogHtml(String dialog) {
164
165        StringBuffer result = new StringBuffer(1024);
166
167        result.append(createWidgetTableStart());
168        // show error header once if there were validation errors
169        result.append(createWidgetErrorHeader());
170
171        if (dialog.equals(PAGES[0])) {
172            // create the widgets for the first dialog page
173            result.append(dialogBlockStart(key(Messages.GUI_LABEL_FIELDCONFIGURATION_BLOCK_SETTINGS_0)));
174            result.append(createWidgetTableStart());
175            result.append(createDialogRowsHtml(0, 0));
176            result.append(createWidgetTableEnd());
177            result.append(dialogBlockEnd());
178        }
179
180        result.append(createWidgetTableEnd());
181
182        // Output the list with document types:
183        return result.toString();
184    }
185
186    /**
187     * @see org.opencms.workplace.CmsWidgetDialog#defaultActionHtmlEnd()
188     */
189    @Override
190    protected String defaultActionHtmlEnd() {
191
192        return "";
193    }
194
195    /**
196     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
197     */
198    @Override
199    protected void defineWidgets() {
200
201        initUserObject();
202        setKeyPrefix(KEY_PREFIX);
203    }
204
205    /**
206     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
207     */
208    @Override
209    protected String[] getPageArray() {
210
211        return PAGES;
212    }
213
214    /**
215     * Returns the root path of this dialog (path relative to "/system/workplace/admin").<p>
216     *
217     * @return the root path of this dialog (path relative to "/system/workplace/admin")
218     */
219    protected String getToolPath() {
220
221        return "/searchindex/fieldconfigurations/fieldconfiguration";
222    }
223
224    /**
225     * @see org.opencms.workplace.CmsWorkplace#initMessages()
226     */
227    @Override
228    protected void initMessages() {
229
230        // add specific dialog resource bundle
231        addMessages(Messages.get().getBundleName());
232        // add default resource bundles
233        super.initMessages();
234    }
235
236    /**
237     * Initializes the user object to work with depending on the dialog state and request parameters.<p>
238     *
239     */
240    protected void initUserObject() {
241
242        if (m_fieldconfiguration == null) {
243            try {
244                m_fieldconfiguration = m_searchManager.getFieldConfiguration(getParamFieldconfiguration());
245                if (m_fieldconfiguration == null) {
246                    m_fieldconfiguration = new CmsLuceneFieldConfiguration();
247                }
248            } catch (Exception e) {
249                m_fieldconfiguration = new CmsLuceneFieldConfiguration();
250            }
251        }
252    }
253
254    /**
255     * Overridden to initialize the internal <code>CmsSearchManager</code> before initWorkplaceRequestValues ->
256     * defineWidgets ->  will access it (NPE). <p>
257     *
258     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
259     */
260    @Override
261    protected void initWorkplaceMembers(CmsJspActionElement jsp) {
262
263        m_searchManager = OpenCms.getSearchManager();
264        super.initWorkplaceMembers(jsp);
265    }
266
267    /**
268     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
269     */
270    @Override
271    @SuppressWarnings({"unchecked", "rawtypes"})
272    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
273
274        // initialize parameters and dialog actions in super implementation
275        super.initWorkplaceRequestValues(settings, request);
276
277        // save the current search index source
278        Map dialogObject = (Map)getDialogObject();
279        if (dialogObject == null) {
280            dialogObject = new HashMap();
281            dialogObject.put(PARAM_FIELDCONFIGURATION, m_fieldconfiguration);
282            setDialogObject(dialogObject);
283        }
284
285    }
286
287    /**
288     * Checks if the new search index dialog has to be displayed.<p>
289     *
290     * @return <code>true</code> if the new search index dialog has to be displayed
291     */
292    protected boolean isNewFieldConfiguration() {
293
294        return DIALOG_INITIAL.equals(getParamAction());
295    }
296
297    /**
298     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
299     */
300    @Override
301    protected void validateParamaters() throws Exception {
302
303        if (!isNewFieldConfiguration()) {
304            // test the needed parameters
305            if ((getParamFieldconfiguration() == null) && (getJsp().getRequest().getParameter("name.0") == null)) {
306                throw new CmsIllegalStateException(
307                    Messages.get().container(Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1, PARAM_FIELDCONFIGURATION));
308            }
309        }
310    }
311
312    /**
313     * Checks the configuration to write.<p>
314     *
315     * @return true if configuration is valid, otherwise false
316     */
317    private boolean checkWriteConfiguration() {
318
319        if ((m_fieldconfiguration != null) || m_fieldconfiguration.getFields().isEmpty()) {
320            for (CmsSearchField field : m_fieldconfiguration.getFields()) {
321                if (field.getMappings().isEmpty()) {
322                    return false;
323                }
324            }
325            return true;
326        }
327        return false;
328    }
329}