1 31 32 package org.opencms.workplace.explorer; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsFolder; 36 import org.opencms.file.CmsObject; 37 import org.opencms.file.CmsProperty; 38 import org.opencms.file.CmsPropertyDefinition; 39 import org.opencms.file.CmsResource; 40 import org.opencms.file.CmsResourceFilter; 41 import org.opencms.file.types.CmsResourceTypeXmlPage; 42 import org.opencms.jsp.CmsJspActionElement; 43 import org.opencms.main.CmsException; 44 import org.opencms.main.CmsLog; 45 import org.opencms.main.OpenCms; 46 import org.opencms.util.CmsStringUtil; 47 import org.opencms.workplace.CmsWorkplace; 48 import org.opencms.workplace.CmsWorkplaceSettings; 49 import org.opencms.workplace.commons.CmsPropertyAdvanced; 50 51 import java.io.IOException ; 52 import java.util.ArrayList ; 53 import java.util.HashMap ; 54 import java.util.Iterator ; 55 import java.util.List ; 56 import java.util.Map ; 57 import java.util.TreeMap ; 58 59 import javax.servlet.ServletException ; 60 import javax.servlet.http.HttpServletRequest ; 61 import javax.servlet.http.HttpServletResponse ; 62 import javax.servlet.jsp.JspException ; 63 import javax.servlet.jsp.PageContext ; 64 65 import org.apache.commons.logging.Log; 66 67 82 public class CmsNewResourceXmlPage extends CmsNewResource { 83 84 85 public static final String PARAM_BODYFILE = "bodyfile"; 86 87 88 public static final String PARAM_SUFFIXCHECK = "suffixcheck"; 89 90 91 public static final String PARAM_TEMPLATE = "template"; 92 93 94 private static final Log LOG = CmsLog.getLog(CmsNewResourceXmlPage.class); 95 private String m_paramBodyFile; 96 private String m_paramDialogMode; 97 private String m_paramSuffixCheck; 98 private String m_paramTemplate; 99 100 105 public CmsNewResourceXmlPage(CmsJspActionElement jsp) { 106 107 super(jsp); 108 } 109 110 117 public CmsNewResourceXmlPage(PageContext context, HttpServletRequest req, HttpServletResponse res) { 118 119 this(new CmsJspActionElement(context, req, res)); 120 } 121 122 130 public static TreeMap getBodies(CmsObject cms, String currWpPath) throws CmsException { 131 132 return getElements(cms, CmsWorkplace.VFS_DIR_DEFAULTBODIES, currWpPath, true); 133 } 134 135 144 public static TreeMap getBodies(CmsObject cms, String currWpPath, boolean emptyMap) throws CmsException { 145 146 return getElements(cms, CmsWorkplace.VFS_DIR_DEFAULTBODIES, currWpPath, emptyMap); 147 } 148 149 157 public static TreeMap getTemplates(CmsObject cms, String currWpPath) throws CmsException { 158 159 return getElements(cms, CmsWorkplace.VFS_DIR_TEMPLATES, currWpPath, true); 160 } 161 162 171 public static TreeMap getTemplates(CmsObject cms, String currWpPath, boolean emptyMap) throws CmsException { 172 173 return getElements(cms, CmsWorkplace.VFS_DIR_TEMPLATES, currWpPath, emptyMap); 174 } 175 176 186 protected static TreeMap getElements(CmsObject cms, String elementFolder, String currWpPath, boolean emptyMap) 187 throws CmsException { 188 189 TreeMap elements = new TreeMap (); 190 TreeMap allElements = new TreeMap (); 191 192 if (CmsStringUtil.isNotEmpty(currWpPath)) { 193 currWpPath = cms.getRequestContext().addSiteRoot(currWpPath); 195 } 196 197 List modules = cms.getSubFolders(CmsWorkplace.VFS_PATH_MODULES, CmsResourceFilter.IGNORE_EXPIRATION); 199 for (int i = 0; i < modules.size(); i++) { 200 List moduleTemplateFiles = new ArrayList (); 201 String folder = cms.getSitePath((CmsFolder)modules.get(i)); 202 try { 203 moduleTemplateFiles = cms.getFilesInFolder( 204 folder + elementFolder, 205 CmsResourceFilter.DEFAULT.addRequireVisible()); 206 } catch (CmsException e) { 207 if (LOG.isDebugEnabled()) { 209 LOG.debug(e.getMessage(), e); 210 } 211 } 212 for (int j = 0; j < moduleTemplateFiles.size(); j++) { 213 CmsFile templateFile = (CmsFile)moduleTemplateFiles.get(j); 215 String title = null; 216 String folderProp = null; 217 try { 218 title = cms.readPropertyObject( 219 cms.getSitePath(templateFile), 220 CmsPropertyDefinition.PROPERTY_TITLE, 221 false).getValue(); 222 folderProp = cms.readPropertyObject( 223 templateFile, 224 CmsPropertyDefinition.PROPERTY_FOLDERS_AVAILABLE, 225 false).getValue(); 226 } catch (CmsException e) { 227 if (LOG.isInfoEnabled()) { 229 LOG.info(e); 230 } 231 } 232 233 boolean isInFolder = false; 234 if (CmsStringUtil.isNotEmpty(currWpPath) && CmsStringUtil.isNotEmpty(folderProp)) { 236 List folders = CmsStringUtil.splitAsList(folderProp, DELIM_PROPERTYVALUES); 238 for (int k = 0; k < folders.size(); k++) { 239 String checkFolder = (String )folders.get(k); 240 if (currWpPath.startsWith(checkFolder)) { 241 isInFolder = true; 242 break; 243 } 244 } 245 } else { 246 isInFolder = true; 247 } 248 249 if (title == null) { 250 title = templateFile.getName(); 252 } 253 String path = cms.getSitePath(templateFile); 254 if (isInFolder) { 255 elements.put(title, path); 257 } 258 allElements.put(title, path); 260 } 261 } 262 if (!emptyMap && elements.size() < 1) { 263 return allElements; 265 } 266 return elements; 268 } 269 270 282 public void actionCloseDialog() throws JspException { 283 284 if (isCreateIndexMode()) { 285 String updateFolder = CmsResource.getParentFolder(getSettings().getExplorerResource()); 287 getSettings().setExplorerResource(updateFolder); 288 List folderList = new ArrayList (1); 289 if (updateFolder != null) { 290 folderList.add(updateFolder); 291 } 292 getJsp().getRequest().setAttribute(REQUEST_ATTRIBUTE_RELOADTREE, folderList); 293 } 294 super.actionCloseDialog(); 295 } 296 297 302 public void actionCreateResource() throws JspException { 303 304 try { 305 String title = computeNewTitleProperty(); 307 String fullResourceName = computeFullResourceName(); 309 310 boolean forceSuffix = false; 312 if (CmsStringUtil.isEmpty(getParamSuffixCheck())) { 313 forceSuffix = true; 315 } 316 fullResourceName = appendSuffixHtml(fullResourceName, forceSuffix); 317 318 byte[] bodyFileBytes = null; 320 if (CmsStringUtil.isEmpty(getParamBodyFile())) { 321 bodyFileBytes = ("").getBytes(); 323 } else { 324 bodyFileBytes = getCms().readFile(getParamBodyFile(), CmsResourceFilter.IGNORE_EXPIRATION).getContents(); 326 } 327 328 List properties = new ArrayList (4); 330 properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE, getParamTemplate(), null)); 332 properties.addAll(createResourceProperties( 333 fullResourceName, 334 CmsResourceTypeXmlPage.getStaticTypeName(), 335 title)); 336 getCms().createResource( 337 fullResourceName, 338 CmsResourceTypeXmlPage.getStaticTypeId(), 339 bodyFileBytes, 340 properties); 341 342 setParamResource(fullResourceName); 344 setResourceCreated(true); 345 } catch (Throwable e) { 346 includeErrorpage(this, e); 348 } 349 } 350 351 360 public void actionEditProperties() throws IOException , JspException , ServletException { 361 362 boolean editProps = Boolean.valueOf(getParamNewResourceEditProps()).booleanValue(); 363 if (editProps) { 364 Map params = new HashMap (); 366 params.put(PARAM_RESOURCE, getParamResource()); 367 if (isCreateIndexMode()) { 368 params.put(CmsPropertyAdvanced.PARAM_DIALOGMODE, CmsPropertyAdvanced.MODE_WIZARD_INDEXCREATED); 369 } else { 370 params.put(CmsPropertyAdvanced.PARAM_DIALOGMODE, CmsPropertyAdvanced.MODE_WIZARD); 371 } 372 sendForward(CmsPropertyAdvanced.URI_PROPERTY_DIALOG_HANDLER, params); 373 } else { 374 actionCloseDialog(); 376 } 377 } 378 379 385 public String buildSelectBodyFile(String attributes) { 386 387 List options = new ArrayList (); 388 List values = new ArrayList (); 389 TreeMap bodies = null; 390 try { 391 bodies = getBodies(getCms(), getParamCurrentFolder(), false); 393 } catch (CmsException e) { 394 if (LOG.isInfoEnabled()) { 396 LOG.info(e); 397 } 398 } 399 if (bodies == null) { 400 return ""; 402 } else { 403 Iterator i = bodies.keySet().iterator(); 405 int counter = 0; 406 while (i.hasNext()) { 407 String key = (String )i.next(); 408 String path = (String )bodies.get(key); 409 410 options.add(key); 411 values.add(path); 412 counter++; 413 } 414 } 415 return buildSelect(attributes, options, values, -1, false); 416 } 417 418 424 public String buildSelectTemplates(String attributes) { 425 426 List options = new ArrayList (); 427 List values = new ArrayList (); 428 TreeMap templates = null; 429 try { 430 templates = getTemplates(getCms(), getParamCurrentFolder(), false); 432 } catch (CmsException e) { 433 if (LOG.isInfoEnabled()) { 435 LOG.info(e); 436 } 437 } 438 if (templates == null) { 439 return ""; 441 } else { 442 Iterator i = templates.keySet().iterator(); 444 int counter = 0; 445 while (i.hasNext()) { 446 String key = (String )i.next(); 447 String path = (String )templates.get(key); 448 449 options.add(key); 450 values.add(path); 451 counter++; 452 } 453 } 454 return buildSelect(attributes, options, values, -1, false); 455 } 456 457 462 public String getParamBodyFile() { 463 464 return m_paramBodyFile; 465 } 466 467 476 public String getParamDialogmode() { 477 478 return m_paramDialogMode; 479 } 480 481 486 public String getParamSuffixCheck() { 487 488 return m_paramSuffixCheck; 489 } 490 491 496 public String getParamTemplate() { 497 498 return m_paramTemplate; 499 } 500 501 506 public boolean isCreateIndexMode() { 507 508 return CmsPropertyAdvanced.MODE_WIZARD_CREATEINDEX.equals(getParamDialogmode()); 509 } 510 511 516 public String paramsAsHidden() { 517 518 String resourceName = getParamResource(); 519 setParamResource(null); 521 String params = super.paramsAsHidden(); 522 setParamResource(resourceName); 524 return params; 525 } 526 527 532 public void setParamBodyFile(String bodyFile) { 533 534 m_paramBodyFile = bodyFile; 535 } 536 537 542 public void setParamDialogmode(String value) { 543 544 m_paramDialogMode = value; 545 } 546 547 552 public void setParamSuffixCheck(String paramSuffixCheck) { 553 554 m_paramSuffixCheck = paramSuffixCheck; 555 } 556 557 562 public void setParamTemplate(String template) { 563 564 m_paramTemplate = template; 565 } 566 567 570 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 571 572 fillParamValues(request); 574 setParamDialogtype(DIALOG_TYPE); 576 if (DIALOG_OK.equals(getParamAction())) { 578 setAction(ACTION_OK); 579 } else if (DIALOG_CANCEL.equals(getParamAction())) { 580 setAction(ACTION_CANCEL); 581 } else { 582 setInitialResourceName(); 584 setAction(ACTION_DEFAULT); 585 setParamTitle(key(Messages.GUI_NEWRESOURCE_XMLPAGE_0)); 587 } 588 } 589 590 596 private void setInitialResourceName() { 597 598 if (isCreateIndexMode()) { 599 String defaultFile = ""; 601 try { 602 defaultFile = (String )OpenCms.getDefaultFiles().get(0); 603 } catch (IndexOutOfBoundsException e) { 604 } 606 if (CmsStringUtil.isEmpty(defaultFile)) { 607 defaultFile = "index.html"; 609 } 610 setParamResource(defaultFile); 611 } else { 612 setParamResource(""); 613 } 614 } 615 616 } | Popular Tags |