1 31 32 package org.opencms.workplace.tools.searchindex; 33 34 import org.opencms.configuration.CmsSearchConfiguration; 35 import org.opencms.jsp.CmsJspActionElement; 36 import org.opencms.main.CmsIllegalStateException; 37 import org.opencms.main.OpenCms; 38 import org.opencms.search.CmsSearchIndex; 39 import org.opencms.search.CmsSearchIndexSource; 40 import org.opencms.search.CmsSearchManager; 41 import org.opencms.workplace.CmsWidgetDialog; 42 import org.opencms.workplace.CmsWorkplaceSettings; 43 44 import java.util.ArrayList ; 45 import java.util.HashMap ; 46 import java.util.List ; 47 import java.util.Map ; 48 49 import javax.servlet.http.HttpServletRequest ; 50 import javax.servlet.http.HttpServletResponse ; 51 import javax.servlet.jsp.PageContext ; 52 53 68 public abstract class A_CmsEditSearchIndexDialog extends CmsWidgetDialog { 69 70 71 public static final String KEY_PREFIX = "searchindex"; 72 73 74 public static final String [] PAGES = {"page1"}; 75 76 83 public static final String PARAM_INDEXNAME = "indexname"; 84 85 86 protected CmsSearchIndex m_index; 87 88 89 protected CmsSearchManager m_searchManager; 90 91 92 private String m_paramIndexName; 93 94 99 public A_CmsEditSearchIndexDialog(CmsJspActionElement jsp) { 100 101 super(jsp); 102 } 103 104 111 public A_CmsEditSearchIndexDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) { 112 113 this(new CmsJspActionElement(context, req, res)); 114 } 115 116 120 protected static void writeConfiguration() { 121 122 OpenCms.writeConfiguration(CmsSearchConfiguration.class); 124 } 125 126 129 public void actionCommit() { 130 131 List errors = new ArrayList (); 132 133 try { 134 135 if (!m_searchManager.getSearchIndexes().contains(m_index)) { 137 m_searchManager.addSearchIndex(m_index); 139 } 140 writeConfiguration(); 141 142 } catch (Throwable t) { 143 errors.add(t); 144 } 145 146 setCommitErrors(errors); 148 } 149 150 155 public String getParamIndexName() { 156 157 return m_paramIndexName; 158 } 159 160 165 public void setParamIndexName(String paramSearchIndex) { 166 167 m_paramIndexName = paramSearchIndex; 168 } 169 170 180 protected void defineWidgets() { 181 182 initUserObject(); 183 setKeyPrefix(KEY_PREFIX); 184 185 } 186 187 190 protected String [] getPageArray() { 191 192 return PAGES; 193 } 194 195 200 protected String getToolPath() { 201 202 return "/searchindex"; 203 } 204 205 208 protected void initMessages() { 209 210 addMessages(Messages.get().getBundleName()); 212 super.initMessages(); 214 } 215 216 225 protected void initUserObject() { 226 227 try { 228 m_index = m_searchManager.getIndex(getParamIndexName()); 229 if (m_index == null) { 230 m_index = createDummySearchIndex(); 231 } 232 } catch (Exception e) { 233 m_index = createDummySearchIndex(); 234 } 235 } 236 237 243 protected void initWorkplaceMembers(CmsJspActionElement jsp) { 244 245 m_searchManager = OpenCms.getSearchManager(); 246 super.initWorkplaceMembers(jsp); 247 } 248 249 252 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 253 254 super.initWorkplaceRequestValues(settings, request); 256 257 Map dialogObject = (Map )getDialogObject(); 259 if (dialogObject == null) { 260 dialogObject = new HashMap (); 261 dialogObject.put(PARAM_INDEXNAME, m_index); 262 setDialogObject(dialogObject); 263 } 264 265 } 266 267 272 protected boolean isNewSearchIndex() { 273 274 return DIALOG_INITIAL.equals(getParamAction()); 275 } 276 277 280 protected void validateParamaters() throws Exception { 281 282 if (!isNewSearchIndex()) { 283 if (getParamIndexName() == null && getJsp().getRequest().getParameter("name.0") == null) { 285 throw new CmsIllegalStateException(Messages.get().container( 286 Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1, 287 PARAM_INDEXNAME)); 288 } 289 } 290 } 291 292 private CmsSearchIndexSource createDummyIndexSource() { 293 294 CmsSearchIndexSource result = new CmsSearchIndexSource(); 295 result.setName("default"); 296 result.setIndexerClassName("org.opencms.search.CmsVfsIndexer"); 297 result.addDocumentType("html"); 298 result.addDocumentType("generic"); 299 result.addDocumentType("pdf"); 300 m_searchManager.addSearchIndexSource(result); 302 return result; 303 } 304 305 314 private CmsSearchIndex createDummySearchIndex() { 315 316 CmsSearchIndex result = new CmsSearchIndex(); 317 result.setLocale("en"); 318 result.setProjectName("Online"); 319 result.setRebuildMode("auto"); 320 321 Map sources = m_searchManager.getSearchIndexSources(); 323 if (sources.isEmpty()) { 324 CmsSearchIndexSource source = createDummyIndexSource(); 325 sources.put(source.getName(), source); 326 } 327 result.addSourceName((String )sources.keySet().iterator().next()); 328 329 return result; 330 331 } 332 } | Popular Tags |