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.CmsLuceneField;
036import org.opencms.search.fields.CmsLuceneFieldConfiguration;
037import org.opencms.search.fields.CmsSearchField;
038import org.opencms.search.fields.CmsSearchFieldMapping;
039import org.opencms.search.fields.I_CmsSearchFieldConfiguration;
040import org.opencms.search.fields.I_CmsSearchFieldMapping;
041import org.opencms.workplace.CmsWidgetDialog;
042import org.opencms.workplace.CmsWorkplaceSettings;
043
044import java.util.ArrayList;
045import java.util.HashMap;
046import java.util.Iterator;
047import java.util.List;
048import java.util.Map;
049
050import javax.servlet.http.HttpServletRequest;
051import javax.servlet.http.HttpServletResponse;
052import javax.servlet.jsp.PageContext;
053
054/**
055 *
056 * Abstract widget dialog for all dialogs working with <code>{@link CmsSearchFieldMapping}</code>.<p>
057 *
058 * @since 6.5.5
059 */
060public class A_CmsMappingDialog extends CmsWidgetDialog {
061
062    /** localized messages Keys prefix. */
063    public static final String KEY_PREFIX = "fieldconfiguration.field.mapping";
064
065    /** Defines which pages are valid for this dialog. */
066    public static final String[] PAGES = {"page1"};
067
068    /**
069     * The request parameter for the field to work with when contacting
070     * this dialog from another. <p>
071     *
072     */
073    public static final String PARAM_FIELD = "field";
074
075    /**
076     * The request parameter for the fieldconfiguration to work with when contacting
077     * this dialog from another. <p>
078     *
079     */
080    public static final String PARAM_FIELDCONFIGURATION = "fieldconfiguration";
081
082    /**
083     * The request parameter for the mapping type to work with when contacting
084     * this dialog from another. <p>
085     *
086     */
087    public static final String PARAM_PARAM = "param";
088
089    /**
090     * The request parameter for the mapping type to work with when contacting
091     * this dialog from another. <p>
092     *
093     */
094    public static final String PARAM_TYPE = "type";
095
096    /** The user object that is edited on this dialog. */
097    protected CmsSearchField m_field;
098
099    /** The user object that is edited on this dialog. */
100    protected I_CmsSearchFieldConfiguration m_fieldconfiguration;
101
102    /** The user object that is edited on this dialog. */
103    protected I_CmsSearchFieldMapping m_mapping;
104
105    /** The search manager singleton for convenient access. **/
106    protected CmsSearchManager m_searchManager;
107
108    /** Stores the value of the request parameter for the search index Name. */
109    private String m_paramField;
110
111    /** Stores the value of the request parameter for the search index Name. */
112    private String m_paramFieldConfiguration;
113
114    /** Stores the value of the request parameter for the mapping param. */
115    private String m_paramParam;
116
117    /** Stores the value of the request parameter for the mapping type. */
118    private String m_paramType;
119
120    /**
121     * Public constructor with JSP action element.<p>
122     *
123     * @param jsp an initialized JSP action element
124     */
125    public A_CmsMappingDialog(CmsJspActionElement jsp) {
126
127        super(jsp);
128    }
129
130    /**
131     * Public constructor with JSP variables.<p>
132     *
133     * @param context the JSP page context
134     * @param req the JSP request
135     * @param res the JSP response
136     */
137    public A_CmsMappingDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
138
139        this(new CmsJspActionElement(context, req, res));
140    }
141
142    /**
143     * Writes the updated search configuration back to the XML
144     * configuration file and refreshes the complete list.<p>
145     */
146    protected static void writeConfiguration() {
147
148        // update the XML configuration
149        OpenCms.writeConfiguration(CmsSearchConfiguration.class);
150    }
151
152    /**
153     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
154     */
155    @Override
156    public void actionCommit() {
157
158        List<Throwable> errors = new ArrayList<Throwable>();
159
160        try {
161
162            // if new create it first
163            boolean found = false;
164            Iterator<I_CmsSearchFieldMapping> itMappings = m_field.getMappings().iterator();
165            while (itMappings.hasNext()) {
166                I_CmsSearchFieldMapping curMapping = itMappings.next();
167                if (curMapping.getType().toString().equals(m_mapping.getType().toString())
168                    && (((curMapping.getParam() == null) && (m_mapping.getParam() == null))
169                        || (curMapping.getParam().equals(m_mapping.getParam())))) {
170                    found = true;
171                }
172            }
173            if (!found) {
174                m_field.addMapping(m_mapping);
175            }
176            writeConfiguration();
177
178        } catch (Throwable t) {
179            errors.add(t);
180        }
181
182        // set the list of errors to display when saving failed
183        setCommitErrors(errors);
184
185    }
186
187    /**
188     * Returns the request parameter value for parameter field. <p>
189     *
190     * @return the request parameter value for parameter field
191     */
192    public String getParamField() {
193
194        return m_paramField;
195    }
196
197    /**
198     * Returns the request parameter value for parameter fieldconfiguration. <p>
199     *
200     * @return the request parameter value for parameter fieldconfiguration
201     */
202    public String getParamFieldconfiguration() {
203
204        return m_paramFieldConfiguration;
205    }
206
207    /**
208     * Returns the request parameter value for parameter mapping param. <p>
209     *
210     * @return the request parameter value for parameter mapping param
211     */
212    public String getParamParam() {
213
214        return m_paramParam;
215    }
216
217    /**
218     * Returns the request parameter value for parameter mapping type. <p>
219     *
220     * @return the request parameter value for parameter mapping type
221     */
222    public String getParamType() {
223
224        return m_paramType;
225    }
226
227    /**
228     * Sets the request parameter value for parameter field. <p>
229     *
230     * @param field the request parameter value for parameter field
231     */
232    public void setParamField(String field) {
233
234        m_paramField = field;
235    }
236
237    /**
238     * Sets the request parameter value for parameter fieldconfiguration. <p>
239     *
240     * @param fieldconfiguration the request parameter value for parameter fieldconfiguration
241     */
242    public void setParamFieldconfiguration(String fieldconfiguration) {
243
244        m_paramFieldConfiguration = fieldconfiguration;
245    }
246
247    /**
248     * Sets the request parameter value for parameter mapping param. <p>
249     *
250     * @param param the request parameter value for parameter mapping param
251     */
252    public void setParamParam(String param) {
253
254        m_paramParam = param;
255    }
256
257    /**
258     * Sets the request parameter value for parameter mapping type. <p>
259     *
260     * @param type the request parameter value for parameter mapping type
261     */
262    public void setParamType(String type) {
263
264        m_paramType = type;
265    }
266
267    /**
268     * @see org.opencms.workplace.CmsWidgetDialog#defaultActionHtmlEnd()
269     */
270    @Override
271    protected String defaultActionHtmlEnd() {
272
273        return "";
274    }
275
276    /**
277     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
278     */
279    @Override
280    protected void defineWidgets() {
281
282        initUserObject();
283        setKeyPrefix(KEY_PREFIX);
284    }
285
286    /**
287     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
288     */
289    @Override
290    protected String[] getPageArray() {
291
292        return PAGES;
293    }
294
295    /**
296     * Initializes the user object to work with depending on the dialog state and request parameters.<p>
297     *
298     */
299    protected void initUserObject() {
300
301        if (m_fieldconfiguration == null) {
302            try {
303                m_fieldconfiguration = m_searchManager.getFieldConfiguration(getParamFieldconfiguration());
304                if (m_fieldconfiguration == null) {
305                    m_fieldconfiguration = new CmsLuceneFieldConfiguration();
306                }
307            } catch (Exception e) {
308                m_fieldconfiguration = new CmsLuceneFieldConfiguration();
309            }
310        }
311
312        if (m_field == null) {
313            try {
314                Iterator<CmsSearchField> itFields = m_fieldconfiguration.getFields().iterator();
315                while (itFields.hasNext()) {
316                    CmsSearchField curField = itFields.next();
317                    if (curField.getName().equals(getParamField())) {
318                        m_field = curField;
319                        break;
320                    }
321                }
322                if (m_field == null) {
323                    m_field = new CmsLuceneField();
324                }
325            } catch (Exception e) {
326                m_field = new CmsLuceneField();
327            }
328        }
329        if (m_mapping == null) {
330            try {
331                Iterator<I_CmsSearchFieldMapping> itMappings = m_field.getMappings().iterator();
332                while (itMappings.hasNext()) {
333                    I_CmsSearchFieldMapping curMapping = itMappings.next();
334                    if (curMapping.getType().toString().equals(getParamType())
335                        && (((curMapping.getParam() == null) && getParamParam().equals("-"))
336                            || (curMapping.getParam().equals(getParamParam())))) {
337                        m_mapping = curMapping;
338                        break;
339                    }
340                }
341                if (m_mapping == null) {
342                    m_mapping = new CmsSearchFieldMapping(true);
343                }
344            } catch (Exception e) {
345                m_mapping = new CmsSearchFieldMapping(true);
346            }
347        }
348    }
349
350    /**
351     * Overridden to initialize the internal <code>CmsSearchManager</code> before initWorkplaceRequestValues ->
352     * defineWidgets ->  will access it (NPE). <p>
353     *
354     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
355     */
356    @Override
357    protected void initWorkplaceMembers(CmsJspActionElement jsp) {
358
359        m_searchManager = OpenCms.getSearchManager();
360        super.initWorkplaceMembers(jsp);
361    }
362
363    /**
364     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
365     */
366    @SuppressWarnings({"unchecked", "rawtypes"})
367    @Override
368    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
369
370        // initialize parameters and dialog actions in super implementation
371        super.initWorkplaceRequestValues(settings, request);
372
373        // save the current search index source
374        Map dialogObject = (Map)getDialogObject();
375        if (dialogObject == null) {
376            dialogObject = new HashMap();
377            dialogObject.put(PARAM_FIELDCONFIGURATION, m_fieldconfiguration);
378            dialogObject.put(PARAM_FIELD, m_field);
379            dialogObject.put(PARAM_PARAM, m_mapping.getParam());
380            if (m_mapping.getType() != null) {
381                dialogObject.put(PARAM_TYPE, m_mapping.getType().toString());
382            } else {
383                dialogObject.put(PARAM_TYPE, m_mapping.getType());
384            }
385            setDialogObject(dialogObject);
386        }
387
388    }
389
390    /**
391     * Checks if the new search index dialog has to be displayed.<p>
392     *
393     * @return <code>true</code> if the new search index dialog has to be displayed
394     */
395    protected boolean isNewMapping() {
396
397        return DIALOG_INITIAL.equals(getParamAction());
398    }
399
400    /**
401     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
402     */
403    @Override
404    protected void validateParamaters() throws Exception {
405
406        if (!isNewMapping()) {
407            // test the needed parameters
408            if ((getParamField() == null) && (getJsp().getRequest().getParameter("name.0") == null)) {
409                throw new CmsIllegalStateException(
410                    Messages.get().container(Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1, PARAM_FIELD));
411            }
412        }
413    }
414}