001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsElementChangeLocaleDialog.java,v $
003: * Date : $Date: 2008-02-27 12:05:37 $
004: * Version: $Revision: 1.7 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.tools.content;
033:
034: import org.opencms.importexport.CmsVfsImportExportHandler;
035: import org.opencms.jsp.CmsJspActionElement;
036: import org.opencms.main.CmsException;
037: import org.opencms.main.CmsIllegalArgumentException;
038: import org.opencms.main.CmsLog;
039: import org.opencms.main.OpenCms;
040: import org.opencms.util.CmsStringUtil;
041: import org.opencms.widgets.CmsCheckboxWidget;
042: import org.opencms.widgets.CmsSelectWidget;
043: import org.opencms.widgets.CmsSelectWidgetOption;
044: import org.opencms.widgets.CmsVfsFileWidget;
045: import org.opencms.workplace.CmsWidgetDialog;
046: import org.opencms.workplace.CmsWidgetDialogParameter;
047: import org.opencms.workplace.CmsWorkplaceSettings;
048: import org.opencms.workplace.explorer.CmsNewResourceXmlPage;
049: import org.opencms.workplace.tools.CmsToolDialog;
050: import org.opencms.workplace.tools.CmsToolManager;
051:
052: import java.io.IOException;
053: import java.util.ArrayList;
054: import java.util.HashMap;
055: import java.util.Iterator;
056: import java.util.List;
057: import java.util.Locale;
058: import java.util.Map;
059: import java.util.TreeMap;
060:
061: import javax.servlet.ServletException;
062: import javax.servlet.http.HttpServletRequest;
063: import javax.servlet.http.HttpServletResponse;
064: import javax.servlet.jsp.PageContext;
065:
066: import org.apache.commons.logging.Log;
067:
068: /**
069: * Widget dialog that sets the settings to move page elements to another Locale.<p>
070: *
071: * @author Andreas Zahner
072: *
073: * @version $Revision: 1.7 $
074: *
075: * @since 6.0.1
076: */
077: public class CmsElementChangeLocaleDialog extends CmsWidgetDialog {
078:
079: /** Localized message keys prefix. */
080: public static final String KEY_PREFIX = "changelocale";
081:
082: /** Defines which pages are valid for this dialog. */
083: public static final String[] PAGES = { "page1" };
084:
085: /** The import JSP report workplace URI. */
086: protected static final String CHANGELOCALE_ACTION_REPORT = PATH_WORKPLACE
087: + "admin/contenttools/reports/changelocale.jsp";
088:
089: /** The log object for this class. */
090: private static final Log LOG = CmsLog
091: .getLog(CmsElementChangeLocaleDialog.class);
092:
093: /** The settings object that is edited on this dialog. */
094: private CmsElementChangeLocaleSettings m_settings;
095:
096: /**
097: * Public constructor with JSP action element.<p>
098: *
099: * @param jsp an initialized JSP action element
100: */
101: public CmsElementChangeLocaleDialog(CmsJspActionElement jsp) {
102:
103: super (jsp);
104: }
105:
106: /**
107: * Public constructor with JSP variables.<p>
108: *
109: * @param context the JSP page context
110: * @param req the JSP request
111: * @param res the JSP response
112: */
113: public CmsElementChangeLocaleDialog(PageContext context,
114: HttpServletRequest req, HttpServletResponse res) {
115:
116: this (new CmsJspActionElement(context, req, res));
117: }
118:
119: /**
120: * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
121: */
122: public void actionCommit() throws IOException, ServletException {
123:
124: List errors = new ArrayList();
125: setDialogObject(m_settings);
126:
127: try {
128:
129: if (m_settings.getOldLocale().equals(
130: m_settings.getNewLocale())) {
131: // old Locale is equals to new one, show error
132: throw new CmsIllegalArgumentException(
133: Messages
134: .get()
135: .container(
136: Messages.ERR_CHANGEELEMENTLOCALE_LOCALE_EQUAL_0));
137: }
138:
139: Map params = new HashMap();
140: // set the name of this class to get dialog object in report
141: params.put(CmsElementChangeLocaleReport.PARAM_CLASSNAME,
142: this .getClass().getName());
143: // set style to display report in correct layout
144: params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
145: // set close link to get back to overview after finishing the import
146: params.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(
147: getJsp(), "/contenttools"));
148: // redirect to the report output JSP
149: getToolManager().jspForwardPage(this ,
150: CHANGELOCALE_ACTION_REPORT, params);
151:
152: } catch (CmsIllegalArgumentException e) {
153: errors.add(e);
154: }
155: // set the list of errors to display when saving failed
156: setCommitErrors(errors);
157: }
158:
159: /**
160: * Returns the selector widget options to build a Locale selector widget.<p>
161: *
162: * @return the selector widget options to build a Locale selector widget
163: */
164: public List getLocaleConfigOptions() {
165:
166: List result = new ArrayList();
167:
168: List locales = OpenCms.getLocaleManager().getAvailableLocales();
169: Iterator i = locales.iterator();
170: while (i.hasNext()) {
171: Locale locale = (Locale) i.next();
172: String language = locale.toString();
173: String displayLanguage = locale
174: .getDisplayLanguage(getLocale());
175:
176: result.add(new CmsSelectWidgetOption(language, false,
177: displayLanguage));
178: }
179:
180: return result;
181: }
182:
183: /**
184: * returns the selector widget options to build a template selector widget.<p>
185: *
186: * @return the selector widget options to build a template selector widget
187: */
188: public List getTemplateConfigOptions() {
189:
190: List result = new ArrayList();
191: result
192: .add(new CmsSelectWidgetOption(
193: "",
194: true,
195: key(Messages.GUI_CHANGEELEMENTLOCALE_DIALOG_TEMPLATE_ALL_0)));
196:
197: TreeMap templates = null;
198: try {
199: // get all available templates
200: templates = CmsNewResourceXmlPage.getTemplates(getCms(),
201: null);
202: } catch (CmsException e) {
203: // can usually be ignored
204: if (LOG.isInfoEnabled()) {
205: LOG.info(e);
206: }
207: }
208: if (templates != null) {
209: // templates found, create option and value lists
210: Iterator i = templates.entrySet().iterator();
211: while (i.hasNext()) {
212: Map.Entry entry = (Map.Entry) i.next();
213: String key = (String) entry.getKey();
214: String path = (String) entry.getValue();
215: result.add(new CmsSelectWidgetOption(path, false, key));
216: }
217: }
218: return result;
219: }
220:
221: /**
222: * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
223: */
224: protected String createDialogHtml(String dialog) {
225:
226: StringBuffer result = new StringBuffer(1024);
227:
228: // create table
229: result.append(createWidgetTableStart());
230:
231: // show error header once if there were validation errors
232: result.append(createWidgetErrorHeader());
233:
234: // create export file name block
235: result
236: .append(createWidgetBlockStart(key(Messages.GUI_CHANGEELEMENTLOCALE_DIALOG_BLOCK_SETTINGS_0)));
237: result.append(createDialogRowsHtml(0, 4));
238: result.append(createWidgetBlockEnd());
239:
240: // close table
241: result.append(createWidgetTableEnd());
242:
243: return result.toString();
244: }
245:
246: /**
247: * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
248: */
249: protected void defineWidgets() {
250:
251: // initialize the export object to use for the dialog
252: initSettingsObject();
253:
254: // set localized key prefix
255: setKeyPrefix(KEY_PREFIX);
256:
257: addWidget(new CmsWidgetDialogParameter(m_settings, "vfsFolder",
258: "/", PAGES[0], new CmsVfsFileWidget(false, getCms()
259: .getRequestContext().getSiteRoot()), 1, 1));
260:
261: addWidget(new CmsWidgetDialogParameter(m_settings,
262: "includeSubFolders", PAGES[0], new CmsCheckboxWidget()));
263: addWidget(new CmsWidgetDialogParameter(m_settings, "template",
264: PAGES[0], new CmsSelectWidget(
265: getTemplateConfigOptions())));
266:
267: List localeSelections = getLocaleConfigOptions();
268: addWidget(new CmsWidgetDialogParameter(m_settings, "oldLocale",
269: PAGES[0], new CmsSelectWidget(localeSelections)));
270: addWidget(new CmsWidgetDialogParameter(m_settings, "newLocale",
271: PAGES[0], new CmsSelectWidget(localeSelections)));
272: }
273:
274: /**
275: * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
276: */
277: protected String[] getPageArray() {
278:
279: return PAGES;
280: }
281:
282: /**
283: * @see org.opencms.workplace.CmsWorkplace#initMessages()
284: */
285: protected void initMessages() {
286:
287: // add specific dialog resource bundle
288: addMessages(Messages.get().getBundleName());
289: // add workplace messages
290: addMessages("org.opencms.workplace.workplace");
291: // add default resource bundles
292: super .initMessages();
293: }
294:
295: /**
296: * Initializes the settings object to work with depending on the dialog state and request parameters.<p>
297: */
298: protected void initSettingsObject() {
299:
300: Object o;
301:
302: if (CmsStringUtil.isEmpty(getParamAction())) {
303: o = new CmsVfsImportExportHandler();
304: } else {
305: // this is not the initial call, get the job object from session
306: o = getDialogObject();
307: }
308:
309: if (!(o instanceof CmsElementChangeLocaleSettings)) {
310: // create a new export handler object
311: m_settings = new CmsElementChangeLocaleSettings();
312: } else {
313: // reuse export handler object stored in session
314: m_settings = (CmsElementChangeLocaleSettings) o;
315: }
316:
317: }
318:
319: /**
320: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
321: */
322: protected void initWorkplaceRequestValues(
323: CmsWorkplaceSettings settings, HttpServletRequest request) {
324:
325: // initialize parameters and dialog actions in super implementation
326: super .initWorkplaceRequestValues(settings, request);
327:
328: // save the current state of the export handler (may be changed because of the widget values)
329: setDialogObject(m_settings);
330: }
331: }
|