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.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 javax.servlet.ServletException;
057import javax.servlet.http.HttpServletRequest;
058import javax.servlet.http.HttpServletResponse;
059import javax.servlet.jsp.PageContext;
060
061import org.apache.commons.logging.Log;
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(new CmsWidgetDialogParameter(
294            m_settings,
295            "message",
296            key(Messages.GUI_MODULES_DETAIL_DIALOG_MESSAGE_0),
297            PAGES[0],
298            new CmsDisplayWidget(),
299            1,
300            1));
301        addWidget(new CmsWidgetDialogParameter(
302            m_settings,
303            "paths",
304            "/",
305            PAGES[0],
306            new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
307            1,
308            CmsWidgetDialogParameter.MAX_OCCURENCES));
309        addWidget(
310            new CmsWidgetDialogParameter(
311                m_settings,
312                "properties",
313                "/",
314                PAGES[0],
315                new CmsSelectWidget(getPropertySelectWidgetConfiguration()),
316                1,
317                CmsWidgetDialogParameter.MAX_OCCURENCES));
318        addWidget(new CmsWidgetDialogParameter(
319            m_settings,
320            "showSiblings",
321            String.valueOf(false),
322            PAGES[0],
323            new CmsCheckboxWidget(),
324            1,
325            1));
326        addWidget(new CmsWidgetDialogParameter(m_settings, "propValue", PAGES[0], new CmsInputWidget()));
327    }
328
329    /**
330     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
331     */
332    @Override
333    protected String[] getPageArray() {
334
335        return PAGES;
336    }
337
338    /**
339     * @see org.opencms.workplace.CmsWorkplace#initMessages()
340     */
341    @Override
342    protected void initMessages() {
343
344        // add specific dialog resource bundle
345        addMessages(Messages.get().getBundleName());
346        // add default resource bundles
347        super.initMessages();
348    }
349
350    /**
351     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings,
352     *      javax.servlet.http.HttpServletRequest)
353     */
354    @Override
355    protected void initWorkplaceRequestValues(final CmsWorkplaceSettings settings, final HttpServletRequest request) {
356
357        initDialogObject();
358        // initialize parameters and dialog actions in super implementation
359        super.initWorkplaceRequestValues(settings, request);
360    }
361
362    /**
363     * Reads all available properties in the system and returns a config string for a select widget.
364     * <p>
365     *
366     * @return a select widget configuration String for available properties in the system.
367     */
368    private String getPropertySelectWidgetConfiguration() {
369
370        String result = "";
371        CmsObject cms = getCms();
372        try {
373            List<CmsPropertyDefinition> props = cms.readAllPropertyDefinitions();
374            StringBuffer buffer = new StringBuffer();
375            Iterator<CmsPropertyDefinition> it = props.iterator();
376            CmsPropertyDefinition prop;
377            while (it.hasNext()) {
378                prop = it.next();
379                buffer.append(prop.getName());
380                if (it.hasNext()) {
381                    buffer.append('|');
382                }
383            }
384            result = buffer.toString();
385
386        } catch (CmsException e) {
387            if (LOG.isErrorEnabled()) {
388                LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_PROPERTYVIEWER_READALLPROPS_0), e);
389            }
390        }
391        return result;
392    }
393
394    /**
395     * Initializes the dialog object.
396     * <p>
397     */
398    private void initDialogObject() {
399
400        Object o = getDialogObject();
401        if (o != null) {
402            m_settings = (CmsPropertyviewDialogSettings)o;
403        } else {
404            m_settings = new CmsPropertyviewDialogSettings();
405            setDialogObject(m_settings);
406        }
407    }
408}