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.content.propertyviewer;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsPropertyDefinition;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.widgets.CmsCheckboxWidget;
037import org.opencms.widgets.CmsDisplayWidget;
038import org.opencms.widgets.CmsInputWidget;
039import org.opencms.widgets.CmsSelectWidget;
040import org.opencms.widgets.CmsVfsFileWidget;
041import org.opencms.workplace.CmsWidgetDialog;
042import org.opencms.workplace.CmsWidgetDialogParameter;
043import org.opencms.workplace.CmsWorkplace;
044import org.opencms.workplace.CmsWorkplaceSettings;
045import org.opencms.workplace.tools.CmsToolDialog;
046import org.opencms.workplace.tools.CmsToolManager;
047
048import java.io.IOException;
049import java.util.ArrayList;
050import java.util.HashMap;
051import java.util.Iterator;
052import java.util.LinkedList;
053import java.util.List;
054import java.util.Map;
055
056import org.apache.commons.logging.Log;
057
058import jakarta.servlet.ServletException;
059import jakarta.servlet.http.HttpServletRequest;
060import jakarta.servlet.http.HttpServletResponse;
061import jakarta.servlet.jsp.PageContext;
062
063/**
064 * Widget dialog that collects the options for the property view.
065 * <p>
066 *
067 * @since 7.5.1
068 */
069public class CmsPropertyviewDialog extends CmsWidgetDialog {
070
071    /**
072     * The settings bean for this dialog.
073     * <p>
074     *
075     */
076    public class CmsPropertyviewDialogSettings {
077
078        /** Display message. */
079        private String m_message;
080
081        /** The paths to collect resources. */
082        private List<String> m_paths = new LinkedList<String>();
083
084        /** The properties to show. */
085        private List<String> m_properties = new LinkedList<String>();
086
087        /** The value of the property to search. */
088        private String m_propValue;
089
090        /** If true siblings will also be inspected. */
091        private boolean m_showSiblings;
092
093        /**
094         * The default constructor.
095         * <p>
096         */
097        public CmsPropertyviewDialogSettings() {
098
099            super();
100            m_paths.add("/");
101            m_properties.add("Title");
102        }
103
104        /**
105         * @return the message
106         */
107        public String getMessage() {
108
109            return m_message;
110        }
111
112        /**
113         * @return the paths
114         */
115        public List<String> getPaths() {
116
117            return m_paths;
118        }
119
120        /**
121         * @return the properties
122         */
123        public List<String> getProperties() {
124
125            return m_properties;
126        }
127
128        /**
129         * Returns the value of the property to search.<p>
130         *
131         * @return the property value
132         */
133        public String getPropValue() {
134
135            return m_propValue;
136        }
137
138        /**
139         * @return the showSiblings
140         */
141        public boolean isShowSiblings() {
142
143            return m_showSiblings;
144        }
145
146        /**
147         * @param message the message to set
148         */
149        public void setMessage(final String message) {
150
151            m_message = message;
152        }
153
154        /**
155         * @param paths the paths to set
156         */
157        public void setPaths(final List<String> paths) {
158
159            m_paths = paths;
160        }
161
162        /**
163         * @param properties the properties to set
164         */
165        public void setProperties(final List<String> properties) {
166
167            m_properties = properties;
168        }
169
170        /**
171         * Sets the property value to search.<p>
172         *
173         * @param propValue the property value to set
174         */
175        public void setPropValue(String propValue) {
176
177            m_propValue = propValue;
178        }
179
180        /**
181         * @param showSiblings the showSiblings to set
182         */
183        public void setShowSiblings(final boolean showSiblings) {
184
185            m_showSiblings = showSiblings;
186        }
187
188    }
189
190    /** localized messages Keys prefix. */
191    public static final String KEY_PREFIX = "propertyviewer";
192
193    /** Defines which pages are valid for this dialog. */
194    public static final String[] PAGES = {"page1"};
195
196    /** The log object for this class. */
197    private static final Log LOG = CmsLog.getLog(CmsPropertyviewDialog.class);
198
199    /** The widget mapped data container. */
200    private CmsPropertyviewDialogSettings m_settings = new CmsPropertyviewDialogSettings();
201
202    /**
203     * Public constructor with JSP action element.
204     * <p>
205     * @param jsp an initialized JSP action element
206     */
207    public CmsPropertyviewDialog(final CmsJspActionElement jsp) {
208
209        super(jsp);
210    }
211
212    /**
213     * Public constructor with JSP variables.
214     * <p>
215     *
216     * @param context the JSP page context
217     * @param req the JSP request
218     * @param res the JSP response
219     */
220    public CmsPropertyviewDialog(
221        final PageContext context,
222        final HttpServletRequest req,
223        final HttpServletResponse res) {
224
225        this(new CmsJspActionElement(context, req, res));
226
227    }
228
229    /**
230     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
231     */
232    @Override
233    public void actionCommit() throws IOException, ServletException {
234
235        initDialogObject();
236        List<Throwable> errors = new ArrayList<Throwable>();
237        Map<String, String[]> params = new HashMap<String, String[]>();
238        List<String> paths = m_settings.getPaths();
239        params.put(CmsPropertyviewList.PARAM_RESOURCES, new String[] {CmsStringUtil.listAsString(paths, ",")});
240        List<String> props = m_settings.getProperties();
241        params.put(CmsPropertyviewList.PARAM_PROPERTIES, new String[] {CmsStringUtil.listAsString(props, ",")});
242        params.put(CmsPropertyviewList.PARAM_SIBLINGS, new String[] {String.valueOf(m_settings.isShowSiblings())});
243        // value to search in property
244        params.put(CmsPropertyviewList.PARAM_PROPERTY_VALUE, new String[] {m_settings.getPropValue()});
245        // set style to display report in correct layout
246        params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
247        // set close link to get back to overview after finishing the import
248        params.put(PARAM_CLOSELINK, new String[] {CmsToolManager.linkForToolPath(getJsp(), "propertyviewer")});
249
250        // redirect to the report output JSP
251        getToolManager().jspForwardPage(
252            this,
253            CmsWorkplace.PATH_WORKPLACE + "admin/contenttools/propertyviewer-list.jsp",
254            params);
255
256        // set the list of errors to display when saving failed
257        setCommitErrors(errors);
258    }
259
260    /**
261     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
262     */
263    @Override
264    protected String createDialogHtml(final String dialog) {
265
266        StringBuffer result = new StringBuffer(1024);
267
268        // create table
269        result.append(createWidgetTableStart());
270
271        // show error header once if there were validation errors
272        result.append(createWidgetErrorHeader());
273
274        // create export file name block
275        result.append(createWidgetBlockStart(key(Messages.GUI_PROPERTYVIEW_ADMIN_TOOL_BLOCK_0)));
276        result.append(createDialogRowsHtml(0, 4));
277        result.append(createWidgetBlockEnd());
278
279        // close table
280        result.append(createWidgetTableEnd());
281
282        return result.toString();
283    }
284
285    /**
286     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
287     */
288    @Override
289    protected void defineWidgets() {
290
291        setKeyPrefix(KEY_PREFIX);
292
293        addWidget(
294            new CmsWidgetDialogParameter(
295                m_settings,
296                "message",
297                key(Messages.GUI_MODULES_DETAIL_DIALOG_MESSAGE_0),
298                PAGES[0],
299                new CmsDisplayWidget(),
300                1,
301                1));
302        addWidget(
303            new CmsWidgetDialogParameter(
304                m_settings,
305                "paths",
306                "/",
307                PAGES[0],
308                new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
309                1,
310                CmsWidgetDialogParameter.MAX_OCCURENCES));
311        addWidget(
312            new CmsWidgetDialogParameter(
313                m_settings,
314                "properties",
315                "/",
316                PAGES[0],
317                new CmsSelectWidget(getPropertySelectWidgetConfiguration()),
318                1,
319                CmsWidgetDialogParameter.MAX_OCCURENCES));
320        addWidget(
321            new CmsWidgetDialogParameter(
322                m_settings,
323                "showSiblings",
324                String.valueOf(false),
325                PAGES[0],
326                new CmsCheckboxWidget(),
327                1,
328                1));
329        addWidget(new CmsWidgetDialogParameter(m_settings, "propValue", PAGES[0], new CmsInputWidget()));
330    }
331
332    /**
333     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
334     */
335    @Override
336    protected String[] getPageArray() {
337
338        return PAGES;
339    }
340
341    /**
342     * @see org.opencms.workplace.CmsWorkplace#initMessages()
343     */
344    @Override
345    protected void initMessages() {
346
347        // add specific dialog resource bundle
348        addMessages(Messages.get().getBundleName());
349        // add default resource bundles
350        super.initMessages();
351    }
352
353    /**
354     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings,
355     *      jakarta.servlet.http.HttpServletRequest)
356     */
357    @Override
358    protected void initWorkplaceRequestValues(final CmsWorkplaceSettings settings, final HttpServletRequest request) {
359
360        initDialogObject();
361        // initialize parameters and dialog actions in super implementation
362        super.initWorkplaceRequestValues(settings, request);
363    }
364
365    /**
366     * Reads all available properties in the system and returns a config string for a select widget.
367     * <p>
368     *
369     * @return a select widget configuration String for available properties in the system.
370     */
371    private String getPropertySelectWidgetConfiguration() {
372
373        String result = "";
374        CmsObject cms = getCms();
375        try {
376            List<CmsPropertyDefinition> props = cms.readAllPropertyDefinitions();
377            StringBuffer buffer = new StringBuffer();
378            Iterator<CmsPropertyDefinition> it = props.iterator();
379            CmsPropertyDefinition prop;
380            while (it.hasNext()) {
381                prop = it.next();
382                buffer.append(prop.getName());
383                if (it.hasNext()) {
384                    buffer.append('|');
385                }
386            }
387            result = buffer.toString();
388
389        } catch (CmsException e) {
390            if (LOG.isErrorEnabled()) {
391                LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_PROPERTYVIEWER_READALLPROPS_0), e);
392            }
393        }
394        return result;
395    }
396
397    /**
398     * Initializes the dialog object.
399     * <p>
400     */
401    private void initDialogObject() {
402
403        Object o = getDialogObject();
404        if (o != null) {
405            m_settings = (CmsPropertyviewDialogSettings)o;
406        } else {
407            m_settings = new CmsPropertyviewDialogSettings();
408            setDialogObject(m_settings);
409        }
410    }
411}