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