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; 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 org.apache.commons.logging.Log; 058 059import jakarta.servlet.ServletException; 060import jakarta.servlet.http.HttpServletRequest; 061import jakarta.servlet.http.HttpServletResponse; 062import jakarta.servlet.jsp.PageContext; 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( 236 new CmsWidgetDialogParameter( 237 m_settings, 238 "vfsFolder", 239 "/", 240 PAGES[0], 241 new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()), 242 1, 243 1)); 244 245 addWidget(new CmsWidgetDialogParameter(m_settings, "includeSubFolders", PAGES[0], new CmsCheckboxWidget())); 246 addWidget( 247 new CmsWidgetDialogParameter( 248 m_settings, 249 "template", 250 PAGES[0], 251 new CmsSelectWidget(getTemplateConfigOptions()))); 252 253 List localeSelections = getLocaleConfigOptions(); 254 addWidget( 255 new CmsWidgetDialogParameter(m_settings, "oldLocale", PAGES[0], new CmsSelectWidget(localeSelections))); 256 addWidget( 257 new CmsWidgetDialogParameter(m_settings, "newLocale", PAGES[0], new CmsSelectWidget(localeSelections))); 258 } 259 260 /** 261 * @see org.opencms.workplace.CmsWidgetDialog#getPageArray() 262 */ 263 @Override 264 protected String[] getPageArray() { 265 266 return PAGES; 267 } 268 269 /** 270 * @see org.opencms.workplace.CmsWorkplace#initMessages() 271 */ 272 @Override 273 protected void initMessages() { 274 275 // add specific dialog resource bundle 276 addMessages(Messages.get().getBundleName()); 277 // add workplace messages 278 addMessages("org.opencms.workplace.workplace"); 279 // add default resource bundles 280 super.initMessages(); 281 } 282 283 /** 284 * Initializes the settings object to work with depending on the dialog state and request parameters.<p> 285 */ 286 protected void initSettingsObject() { 287 288 Object o; 289 290 if (CmsStringUtil.isEmpty(getParamAction())) { 291 o = new CmsVfsImportExportHandler(); 292 } else { 293 // this is not the initial call, get the job object from session 294 o = getDialogObject(); 295 } 296 297 if (!(o instanceof CmsElementChangeLocaleSettings)) { 298 // create a new export handler object 299 m_settings = new CmsElementChangeLocaleSettings(); 300 } else { 301 // reuse export handler object stored in session 302 m_settings = (CmsElementChangeLocaleSettings)o; 303 } 304 305 } 306 307 /** 308 * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, jakarta.servlet.http.HttpServletRequest) 309 */ 310 @Override 311 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 312 313 // initialize parameters and dialog actions in super implementation 314 super.initWorkplaceRequestValues(settings, request); 315 316 // save the current state of the export handler (may be changed because of the widget values) 317 setDialogObject(m_settings); 318 } 319}