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;
029
030import org.opencms.importexport.CmsVfsImportExportHandler;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsIllegalArgumentException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.widgets.CmsCheckboxWidget;
038import org.opencms.widgets.CmsSelectWidget;
039import org.opencms.widgets.CmsSelectWidgetOption;
040import org.opencms.widgets.CmsVfsFileWidget;
041import org.opencms.workplace.CmsWidgetDialog;
042import org.opencms.workplace.CmsWidgetDialogParameter;
043import org.opencms.workplace.CmsWorkplaceSettings;
044import org.opencms.workplace.explorer.CmsNewResourceXmlPage;
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.List;
053import java.util.Locale;
054import java.util.Map;
055import java.util.TreeMap;
056
057import javax.servlet.ServletException;
058import javax.servlet.http.HttpServletRequest;
059import javax.servlet.http.HttpServletResponse;
060import javax.servlet.jsp.PageContext;
061
062import org.apache.commons.logging.Log;
063
064/**
065 * Widget dialog that sets the settings to move page elements to another Locale.<p>
066 *
067 * @since 6.0.1
068 */
069public class CmsElementChangeLocaleDialog extends CmsWidgetDialog {
070
071    /** Localized message keys prefix. */
072    public static final String KEY_PREFIX = "changelocale";
073
074    /** Defines which pages are valid for this dialog. */
075    public static final String[] PAGES = {"page1"};
076
077    /** The import JSP report workplace URI. */
078    protected static final String CHANGELOCALE_ACTION_REPORT = PATH_WORKPLACE
079        + "admin/contenttools/reports/changelocale.jsp";
080
081    /** The log object for this class. */
082    private static final Log LOG = CmsLog.getLog(CmsElementChangeLocaleDialog.class);
083
084    /** The settings object that is edited on this dialog. */
085    private CmsElementChangeLocaleSettings m_settings;
086
087    /**
088     * Public constructor with JSP action element.<p>
089     *
090     * @param jsp an initialized JSP action element
091     */
092    public CmsElementChangeLocaleDialog(CmsJspActionElement jsp) {
093
094        super(jsp);
095    }
096
097    /**
098     * Public constructor with JSP variables.<p>
099     *
100     * @param context the JSP page context
101     * @param req the JSP request
102     * @param res the JSP response
103     */
104    public CmsElementChangeLocaleDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
105
106        this(new CmsJspActionElement(context, req, res));
107    }
108
109    /**
110     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
111     */
112    @Override
113    public void actionCommit() throws IOException, ServletException {
114
115        List errors = new ArrayList();
116        setDialogObject(m_settings);
117
118        try {
119
120            if (m_settings.getOldLocale().equals(m_settings.getNewLocale())) {
121                // old Locale is equals to new one, show error
122                throw new CmsIllegalArgumentException(
123                    Messages.get().container(Messages.ERR_CHANGEELEMENTLOCALE_LOCALE_EQUAL_0));
124            }
125
126            Map params = new HashMap();
127            // set the name of this class to get dialog object in report
128            params.put(CmsElementChangeLocaleReport.PARAM_CLASSNAME, this.getClass().getName());
129            // set style to display report in correct layout
130            params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
131            // set close link to get back to overview after finishing the import
132            params.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/contenttools"));
133            // redirect to the report output JSP
134            getToolManager().jspForwardPage(this, CHANGELOCALE_ACTION_REPORT, params);
135
136        } catch (CmsIllegalArgumentException e) {
137            errors.add(e);
138        }
139        // set the list of errors to display when saving failed
140        setCommitErrors(errors);
141    }
142
143    /**
144     * Returns the selector widget options to build a Locale selector widget.<p>
145     *
146     * @return the selector widget options to build a Locale selector widget
147     */
148    public List getLocaleConfigOptions() {
149
150        List result = new ArrayList();
151
152        List locales = OpenCms.getLocaleManager().getAvailableLocales();
153        Iterator i = locales.iterator();
154        while (i.hasNext()) {
155            Locale locale = (Locale)i.next();
156            String localeStr = locale.toString();
157            String localeDisplayStr = locale.getDisplayName(getLocale());
158
159            result.add(new CmsSelectWidgetOption(localeStr, false, localeDisplayStr));
160        }
161
162        return result;
163    }
164
165    /**
166     * returns the selector widget options to build a template selector widget.<p>
167     *
168     * @return the selector widget options to build a template selector widget
169     */
170    public List getTemplateConfigOptions() {
171
172        List result = new ArrayList();
173        result.add(new CmsSelectWidgetOption("", true, key(Messages.GUI_CHANGEELEMENTLOCALE_DIALOG_TEMPLATE_ALL_0)));
174
175        TreeMap templates = null;
176        try {
177            // get all available templates
178            templates = CmsNewResourceXmlPage.getTemplates(getCms(), null);
179        } catch (CmsException e) {
180            // can usually be ignored
181            if (LOG.isInfoEnabled()) {
182                LOG.info(e.getLocalizedMessage(), e);
183            }
184        }
185        if (templates != null) {
186            // templates found, create option and value lists
187            Iterator i = templates.entrySet().iterator();
188            while (i.hasNext()) {
189                Map.Entry entry = (Map.Entry)i.next();
190                String key = (String)entry.getKey();
191                String path = (String)entry.getValue();
192                result.add(new CmsSelectWidgetOption(path, false, key));
193            }
194        }
195        return result;
196    }
197
198    /**
199     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
200     */
201    @Override
202    protected String createDialogHtml(String dialog) {
203
204        StringBuffer result = new StringBuffer(1024);
205
206        // create table
207        result.append(createWidgetTableStart());
208
209        // show error header once if there were validation errors
210        result.append(createWidgetErrorHeader());
211
212        // create export file name block
213        result.append(createWidgetBlockStart(key(Messages.GUI_CHANGEELEMENTLOCALE_DIALOG_BLOCK_SETTINGS_0)));
214        result.append(createDialogRowsHtml(0, 4));
215        result.append(createWidgetBlockEnd());
216
217        // close table
218        result.append(createWidgetTableEnd());
219
220        return result.toString();
221    }
222
223    /**
224     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
225     */
226    @Override
227    protected void defineWidgets() {
228
229        // initialize the export object to use for the dialog
230        initSettingsObject();
231
232        // set localized key prefix
233        setKeyPrefix(KEY_PREFIX);
234
235        addWidget(new CmsWidgetDialogParameter(
236            m_settings,
237            "vfsFolder",
238            "/",
239            PAGES[0],
240            new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
241            1,
242            1));
243
244        addWidget(new CmsWidgetDialogParameter(m_settings, "includeSubFolders", PAGES[0], new CmsCheckboxWidget()));
245        addWidget(
246            new CmsWidgetDialogParameter(
247                m_settings,
248                "template",
249                PAGES[0],
250                new CmsSelectWidget(getTemplateConfigOptions())));
251
252        List localeSelections = getLocaleConfigOptions();
253        addWidget(
254            new CmsWidgetDialogParameter(m_settings, "oldLocale", PAGES[0], new CmsSelectWidget(localeSelections)));
255        addWidget(
256            new CmsWidgetDialogParameter(m_settings, "newLocale", PAGES[0], new CmsSelectWidget(localeSelections)));
257    }
258
259    /**
260     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
261     */
262    @Override
263    protected String[] getPageArray() {
264
265        return PAGES;
266    }
267
268    /**
269     * @see org.opencms.workplace.CmsWorkplace#initMessages()
270     */
271    @Override
272    protected void initMessages() {
273
274        // add specific dialog resource bundle
275        addMessages(Messages.get().getBundleName());
276        // add workplace messages
277        addMessages("org.opencms.workplace.workplace");
278        // add default resource bundles
279        super.initMessages();
280    }
281
282    /**
283     * Initializes the settings object to work with depending on the dialog state and request parameters.<p>
284     */
285    protected void initSettingsObject() {
286
287        Object o;
288
289        if (CmsStringUtil.isEmpty(getParamAction())) {
290            o = new CmsVfsImportExportHandler();
291        } else {
292            // this is not the initial call, get the job object from session
293            o = getDialogObject();
294        }
295
296        if (!(o instanceof CmsElementChangeLocaleSettings)) {
297            // create a new export handler object
298            m_settings = new CmsElementChangeLocaleSettings();
299        } else {
300            // reuse export handler object stored in session
301            m_settings = (CmsElementChangeLocaleSettings)o;
302        }
303
304    }
305
306    /**
307     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
308     */
309    @Override
310    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
311
312        // initialize parameters and dialog actions in super implementation
313        super.initWorkplaceRequestValues(settings, request);
314
315        // save the current state of the export handler (may be changed because of the widget values)
316        setDialogObject(m_settings);
317    }
318}