001/* 002 * File : $Source$ 003 * Date : $Date$ 004 * Version: $Revision$ 005 * 006 * This library is part of OpenCms - 007 * the Open Source Content Management System 008 * 009 * Copyright (C) 2002 - 2009 Alkacon Software (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, 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 032package org.opencms.workplace.tools.searchindex.sourcesearch; 033 034import org.opencms.file.CmsProject; 035import org.opencms.jsp.CmsJspActionElement; 036import org.opencms.main.CmsException; 037import org.opencms.main.OpenCms; 038import org.opencms.search.CmsSearchIndex; 039import org.opencms.search.solr.CmsSolrIndex; 040import org.opencms.util.CmsStringUtil; 041import org.opencms.widgets.CmsCheckboxWidget; 042import org.opencms.widgets.CmsComboWidget; 043import org.opencms.widgets.CmsInputWidget; 044import org.opencms.widgets.CmsSelectWidget; 045import org.opencms.widgets.CmsSelectWidgetOption; 046import org.opencms.widgets.CmsTypeComboWidget; 047import org.opencms.widgets.CmsVfsFileWidget; 048import org.opencms.workplace.CmsWidgetDialog; 049import org.opencms.workplace.CmsWidgetDialogParameter; 050import org.opencms.workplace.CmsWorkplace; 051import org.opencms.workplace.CmsWorkplaceSettings; 052import org.opencms.workplace.tools.CmsToolDialog; 053import org.opencms.workplace.tools.CmsToolManager; 054 055import java.io.IOException; 056import java.util.ArrayList; 057import java.util.HashMap; 058import java.util.LinkedList; 059import java.util.List; 060import java.util.Locale; 061import java.util.Map; 062 063import javax.servlet.ServletException; 064import javax.servlet.http.HttpServletRequest; 065import javax.servlet.http.HttpServletResponse; 066import javax.servlet.jsp.PageContext; 067 068/** 069 * This dialog provides search and replace functionality.<p> 070 * 071 * @since 9.0.0 072 */ 073public class CmsSourceSearchDialog extends CmsWidgetDialog { 074 075 /** Localized messages Keys prefix. */ 076 public static final String KEY_PREFIX = "sourcesearch"; 077 078 /** Defines which pages are valid for this dialog. */ 079 public static final String[] PAGES = {"page1"}; 080 081 /** The widget mapped data container. */ 082 private CmsSearchReplaceSettings m_settings; 083 084 /** Signals whether Solr search is enabled or not. */ 085 private boolean m_solrEnabled; 086 087 /** 088 * Public constructor with JSP action element.<p> 089 * 090 * @param jsp an initialized JSP action element 091 */ 092 public CmsSourceSearchDialog(final 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 CmsSourceSearchDialog( 105 final PageContext context, 106 final HttpServletRequest req, 107 final HttpServletResponse res) { 108 109 this(new CmsJspActionElement(context, req, res)); 110 111 } 112 113 /** 114 * @see org.opencms.workplace.CmsWidgetDialog#actionCommit() 115 */ 116 @Override 117 public void actionCommit() throws IOException, ServletException { 118 119 List<Throwable> errors = new ArrayList<Throwable>(); 120 Map<String, String[]> params = new HashMap<String, String[]>(); 121 // set style to display report in correct layout 122 params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW}); 123 // set close link to get back to overview after finishing the import 124 params.put(PARAM_CLOSELINK, new String[] {CmsToolManager.linkForToolPath(getJsp(), "/sourcesearch")}); 125 // redirect to the report output JSP 126 getToolManager().jspForwardPage( 127 this, 128 CmsWorkplace.PATH_WORKPLACE + "admin/searchindex/sourcesearch/sourcesearch.jsp", 129 params); 130 // set the list of errors to display when saving failed 131 setCommitErrors(errors); 132 } 133 134 /** 135 * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String) 136 */ 137 @Override 138 protected String createDialogHtml(final String dialog) { 139 140 StringBuffer result = new StringBuffer(1024); 141 // create table 142 result.append(createWidgetTableStart()); 143 // show error header once if there were validation errors 144 result.append(createWidgetErrorHeader()); 145 // create export file name block 146 result.append(createWidgetBlockStart( 147 key(org.opencms.workplace.tools.searchindex.Messages.GUI_SOURCESEARCH_ADMIN_TOOL_BLOCK_0))); 148 if (m_solrEnabled) { 149 result.append(createDialogRowsHtml(0, 9)); 150 } else { 151 result.append(createDialogRowsHtml(0, 8)); 152 } 153 result.append(createWidgetBlockEnd()); 154 // close table 155 result.append(createWidgetTableEnd()); 156 return result.toString(); 157 } 158 159 /** 160 * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets() 161 */ 162 @Override 163 protected void defineWidgets() { 164 165 m_solrEnabled = isSolrEnabled(); 166 167 // initialize the settings object to use for the dialog 168 initSettingsObject(); 169 setKeyPrefix(KEY_PREFIX); 170 CmsVfsFileWidget vfsw = new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()); 171 addWidget(new CmsWidgetDialogParameter(m_settings, "paths", "/", PAGES[0], vfsw, 1, 50)); 172 addWidget(new CmsWidgetDialogParameter(m_settings, "types", "", PAGES[0], new CmsTypeComboWidget(), 0, 1)); 173 // Get list of available locales 174 List<CmsSelectWidgetOption> options = new ArrayList<CmsSelectWidgetOption>(); 175 for (final Locale locale : OpenCms.getLocaleManager().getAvailableLocales()) { 176 CmsSelectWidgetOption option = new CmsSelectWidgetOption(locale.toString()); 177 options.add(option); 178 } 179 addWidget(new CmsWidgetDialogParameter(m_settings, "locale", "", PAGES[0], new CmsComboWidget(options), 1, 1)); 180 addWidget( 181 new CmsWidgetDialogParameter( 182 m_settings, 183 "onlyContentValues", 184 "false", 185 PAGES[0], 186 new CmsCheckboxWidget(), 187 1, 188 1)); 189 addWidget(new CmsWidgetDialogParameter(m_settings, "xpath", "", PAGES[0], new CmsInputWidget(), 0, 1)); 190 CmsSelectWidget indexOptions = new CmsSelectWidget(getSolrIndexOptions()); 191 addWidget(new CmsWidgetDialogParameter(m_settings, "source", "", PAGES[0], indexOptions, 1, 1)); 192 if (m_solrEnabled) { 193 addWidget(new CmsWidgetDialogParameter(m_settings, "query", "", PAGES[0], new CmsInputWidget(), 1, 1)); 194 } 195 addWidget(new CmsWidgetDialogParameter(m_settings, "searchpattern", "", PAGES[0], new CmsInputWidget(), 1, 1)); 196 addWidget(new CmsWidgetDialogParameter(m_settings, "replacepattern", "", PAGES[0], new CmsInputWidget(), 1, 1)); 197 CmsSelectWidget projectOptions = new CmsSelectWidget(getProjectSelections()); 198 String currProject = getCms().getRequestContext().getCurrentProject().getName(); 199 addWidget(new CmsWidgetDialogParameter(m_settings, "project", currProject, PAGES[0], projectOptions, 1, 1)); 200 } 201 202 /** 203 * @see org.opencms.workplace.CmsWidgetDialog#getPageArray() 204 */ 205 @Override 206 protected String[] getPageArray() { 207 208 return PAGES; 209 } 210 211 /** 212 * @see org.opencms.workplace.CmsWorkplace#initMessages() 213 */ 214 @Override 215 protected void initMessages() { 216 217 // add specific dialog resource bundle 218 addMessages(Messages.get().getBundleName()); 219 super.initMessages(); 220 } 221 222 /** 223 * Initializes the settings object.<p> 224 */ 225 protected void initSettingsObject() { 226 227 Object o; 228 if (CmsStringUtil.isEmpty(getParamAction())) { 229 o = new CmsSearchReplaceSettings(); 230 } else { 231 // this is not the initial call, get the job object from session 232 o = getDialogObject(); 233 } 234 235 if (o == null) { 236 // create a new export handler object 237 m_settings = new CmsSearchReplaceSettings(); 238 } else { 239 // reuse export handler object stored in session 240 m_settings = (CmsSearchReplaceSettings)o; 241 } 242 243 } 244 245 /** 246 * @see org.opencms.workplace.CmsWidgetDialog#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest) 247 */ 248 @Override 249 protected void initWorkplaceRequestValues(final CmsWorkplaceSettings settings, final HttpServletRequest request) { 250 251 // initialize parameters and dialog actions in super implementation 252 super.initWorkplaceRequestValues(settings, request); 253 254 // save the current state of the export handler (may be changed because of the widget values) 255 setDialogObject(m_settings); 256 } 257 258 /** 259 * Returns a list with the available projects of the current user.<p> 260 * 261 * @return a list with the available projects of the current user. 262 */ 263 private List<CmsSelectWidgetOption> getProjectSelections() { 264 265 List<CmsSelectWidgetOption> result = new LinkedList<CmsSelectWidgetOption>(); 266 List<CmsProject> projects = null; 267 try { 268 projects = OpenCms.getOrgUnitManager().getAllAccessibleProjects(getCms(), "", true); 269 } catch (CmsException e) { 270 return result; 271 } 272 273 boolean first = true; 274 for (CmsProject project : projects) { 275 if (!project.getName().equals(CmsProject.ONLINE_PROJECT_NAME)) { 276 first = false; 277 result.add(new CmsSelectWidgetOption(project.getName(), first, project.getName())); 278 } 279 } 280 return result; 281 } 282 283 /** 284 * Returns select options for all configures Solr Offline indexes.<p> 285 * 286 * @return select options for all configures Solr Offline indexes 287 */ 288 private List<CmsSelectWidgetOption> getSolrIndexOptions() { 289 290 List<CmsSelectWidgetOption> result = new ArrayList<CmsSelectWidgetOption>(); 291 result.add( 292 new CmsSelectWidgetOption(CmsSearchReplaceSettings.VFS, true, CmsSearchReplaceSettings.VFS.toUpperCase())); 293 if (OpenCms.getSearchManager().getSolrServerConfiguration().isEnabled()) { 294 for (CmsSearchIndex index : OpenCms.getSearchManager().getAllSolrIndexes()) { 295 if (CmsSearchIndex.REBUILD_MODE_OFFLINE.equals(index.getRebuildMode())) { 296 result.add(new CmsSelectWidgetOption(index.getName(), false, index.getName())); 297 } 298 } 299 } 300 return result; 301 } 302 303 /** 304 * Returns <code>true</code> if Solr search is enabled.<p> 305 * 306 * @return <code>true</code> if Solr search is enabled 307 */ 308 private boolean isSolrEnabled() { 309 310 boolean solrEnabled = OpenCms.getSearchManager().getSolrServerConfiguration().isEnabled(); 311 CmsSolrIndex solrIndex = OpenCms.getSearchManager().getIndexSolr(CmsSolrIndex.DEFAULT_INDEX_NAME_OFFLINE); 312 return solrEnabled && (solrIndex != null); 313 } 314}