1 31 32 package org.opencms.workplace.explorer; 33 34 import org.opencms.file.CmsProperty; 35 import org.opencms.file.CmsPropertyDefinition; 36 import org.opencms.file.CmsResource; 37 import org.opencms.file.CmsResourceFilter; 38 import org.opencms.file.types.I_CmsResourceType; 39 import org.opencms.i18n.CmsEncoder; 40 import org.opencms.jsp.CmsJspActionElement; 41 import org.opencms.jsp.CmsJspNavBuilder; 42 import org.opencms.jsp.CmsJspNavElement; 43 import org.opencms.main.CmsException; 44 import org.opencms.main.CmsIllegalArgumentException; 45 import org.opencms.main.CmsLog; 46 import org.opencms.main.CmsRuntimeException; 47 import org.opencms.main.OpenCms; 48 import org.opencms.security.CmsPermissionSet; 49 import org.opencms.security.CmsRole; 50 import org.opencms.util.CmsRequestUtil; 51 import org.opencms.util.CmsStringUtil; 52 import org.opencms.util.CmsUriSplitter; 53 import org.opencms.workplace.CmsDialog; 54 import org.opencms.workplace.CmsWorkplaceMessages; 55 import org.opencms.workplace.CmsWorkplaceSettings; 56 import org.opencms.workplace.commons.CmsPropertyAdvanced; 57 58 import java.io.IOException ; 59 import java.lang.reflect.Constructor ; 60 import java.util.ArrayList ; 61 import java.util.Collections ; 62 import java.util.HashMap ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 import javax.servlet.ServletException ; 68 import javax.servlet.http.HttpServletRequest ; 69 import javax.servlet.http.HttpServletResponse ; 70 import javax.servlet.jsp.JspException ; 71 import javax.servlet.jsp.PageContext ; 72 73 import org.apache.commons.logging.Log; 74 75 93 public class CmsNewResource extends CmsDialog { 94 95 96 public static final int ACTION_NEWFORM = 100; 97 98 99 public static final int ACTION_SUBMITFORM = 110; 100 101 102 public static final int BUTTON_NEXT = 20; 103 104 105 public static final char DELIM_PROPERTYVALUES = ','; 106 107 108 public static final String DIALOG_ADVANCED = "advanced"; 109 110 111 public static final String DIALOG_NEWFORM = "newform"; 112 113 114 public static final String DIALOG_SUBMITFORM = "submitform"; 115 116 117 public static final String DIALOG_TYPE = "newresource"; 118 119 120 public static final String PARAM_APPENDSUFFIXHTML = "appendsuffixhtml"; 121 122 123 public static final String PARAM_CURRENTFOLDER = "currentfolder"; 124 125 126 public static final String PARAM_NEWRESOURCEEDITPROPS = "newresourceeditprops"; 127 128 129 public static final String PARAM_NEWRESOURCETYPE = "newresourcetype"; 130 131 132 public static final String PARAM_NEWRESOURCEURI = "newresourceuri"; 133 134 135 public static final String VALUE_DEFAULT = "default"; 136 137 138 private static final Log LOG = CmsLog.getLog(CmsNewResource.class); 139 140 private String m_availableResTypes; 141 private boolean m_limitedRestypes; 142 private String m_page; 143 144 private String m_paramAppendSuffixHtml; 145 private String m_paramCurrentFolder; 146 private String m_paramNewResourceEditProps; 147 private String m_paramNewResourceType; 148 private String m_paramNewResourceUri; 149 private String m_paramPage; 150 151 152 private boolean m_resourceCreated; 153 154 159 public CmsNewResource(CmsJspActionElement jsp) { 160 161 super(jsp); 162 } 163 164 171 public CmsNewResource(PageContext context, HttpServletRequest req, HttpServletResponse res) { 172 173 this(new CmsJspActionElement(context, req, res)); 174 } 175 176 187 public static Object getNewResourceHandler( 188 String type, 189 String defaultClassName, 190 PageContext context, 191 HttpServletRequest req, 192 HttpServletResponse res) throws CmsRuntimeException { 193 194 if (CmsStringUtil.isEmpty(type)) { 195 type = req.getParameter(PARAM_NEWRESOURCETYPE); 197 } 198 199 String className = null; 200 CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(type); 201 202 if (CmsStringUtil.isNotEmpty(settings.getNewResourceHandlerClassName())) { 203 className = settings.getNewResourceHandlerClassName(); 204 } else { 205 className = defaultClassName; 206 } 207 208 Class clazz = null; 209 try { 210 clazz = Class.forName(className); 211 } catch (ClassNotFoundException e) { 212 213 if (LOG.isErrorEnabled()) { 214 LOG.error(Messages.get().getBundle().key(Messages.ERR_NEW_RES_HANDLER_CLASS_NOT_FOUND_1, className), e); 215 } 216 throw new CmsIllegalArgumentException(Messages.get().container( 217 Messages.ERR_NEW_RES_HANDLER_CLASS_NOT_FOUND_1, 218 className)); 219 } 220 221 Object handler = null; 222 try { 223 Constructor constructor = clazz.getConstructor(new Class [] { 224 PageContext .class, 225 HttpServletRequest .class, 226 HttpServletResponse .class}); 227 handler = constructor.newInstance(new Object [] {context, req, res}); 228 } catch (Exception e) { 229 230 throw new CmsIllegalArgumentException(Messages.get().container( 231 Messages.ERR_NEW_RES_CONSTRUCTOR_NOT_FOUND_1, 232 className)); 233 } 234 235 return handler; 236 } 237 238 243 public void actionCreateResource() throws JspException { 244 245 try { 246 String title = computeNewTitleProperty(); 248 String fullResourceName = computeFullResourceName(); 250 I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(getParamNewResourceType()); 252 List properties = createResourceProperties(fullResourceName, resType.getTypeName(), title); 253 getCms().createResource(fullResourceName, resType.getTypeId(), null, properties); 255 setParamResource(fullResourceName); 256 257 setResourceCreated(true); 258 } catch (Throwable e) { 259 includeErrorpage(this, e); 261 } 262 } 263 264 273 public void actionEditProperties() throws IOException , JspException , ServletException { 274 275 boolean editProps = Boolean.valueOf(getParamNewResourceEditProps()).booleanValue(); 276 if (editProps) { 277 Map params = new HashMap (); 279 params.put(PARAM_RESOURCE, getParamResource()); 280 params.put(CmsPropertyAdvanced.PARAM_DIALOGMODE, CmsPropertyAdvanced.MODE_WIZARD); 281 sendForward(CmsPropertyAdvanced.URI_PROPERTY_DIALOG_HANDLER, params); 282 } else { 283 actionCloseDialog(); 285 } 286 } 287 288 294 public void actionSelect() throws IOException , ServletException { 295 296 String nextUri = getParamNewResourceUri(); 297 if (!nextUri.startsWith("/")) { 298 nextUri = PATH_DIALOGS + nextUri; 300 } 301 302 setParamAction(DIALOG_NEWFORM); 303 CmsUriSplitter splitter = new CmsUriSplitter(nextUri); 304 Map params = CmsRequestUtil.createParameterMap(splitter.getQuery()); 305 params.putAll(paramsAsParameterMap()); 306 sendForward(splitter.getPrefix(), params); 307 } 308 309 315 public String buildNewList(String attributes) { 316 317 StringBuffer result = new StringBuffer (1024); 318 result.append("<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">"); 319 320 Iterator i; 321 if (m_limitedRestypes) { 322 List newResTypes = CmsStringUtil.splitAsList(m_availableResTypes, DELIM_PROPERTYVALUES); 324 Iterator k = newResTypes.iterator(); 325 List settings = new ArrayList (newResTypes.size()); 326 while (k.hasNext()) { 327 String resType = (String )k.next(); 328 CmsExplorerTypeSettings set = OpenCms.getWorkplaceManager().getExplorerTypeSetting(resType); 330 if (set != null) { 331 settings.add(set); 333 } 334 } 335 Collections.sort(settings); 337 i = settings.iterator(); 338 } else { 339 i = OpenCms.getWorkplaceManager().getExplorerTypeSettings().iterator(); 341 } 342 343 while (i.hasNext()) { 344 CmsExplorerTypeSettings settings = (CmsExplorerTypeSettings)i.next(); 345 346 if (!m_limitedRestypes) { 347 if (m_page == null) { 349 if (CmsStringUtil.isNotEmpty(settings.getNewResourcePage())) { 350 continue; 351 } 352 } else if (!m_page.equals(settings.getNewResourcePage())) { 353 continue; 354 } 355 } 356 357 if (CmsStringUtil.isEmpty(settings.getNewResourceUri())) { 358 continue; 360 } 361 362 CmsPermissionSet permissions = settings.getAccess().getPermissions(getCms()); 364 if (!permissions.requiresControlPermission()) { 365 continue; 367 } 368 369 result.append("<tr>\n"); 370 result.append("\t<td><input type=\"radio\" name=\""); 371 result.append(PARAM_NEWRESOURCEURI); 372 result.append("\""); 373 result.append(" value=\"" + CmsEncoder.encode(settings.getNewResourceUri()) + "\""); 374 if (CmsStringUtil.isNotEmpty(attributes)) { 375 result.append(" " + attributes); 376 } 377 result.append("></td>\n"); 378 result.append("\t<td><img SRC=\"" 379 + getSkinUri() 380 + "filetypes/" 381 + settings.getIcon() 382 + "\" border=\"0\" title=\"" 383 + key(settings.getKey()) 384 + "\"></td>\n"); 385 result.append("\t<td>" + key(settings.getKey()) + "</td>\n"); 386 result.append("</tr>\n"); 387 388 } 389 result.append("</table>\n"); 390 391 return result.toString(); 392 } 393 394 401 public String computeNewTitleProperty() { 402 403 String title = getParamResource(); 404 int lastDot = title.lastIndexOf('.'); 405 if ((lastDot > 0) && (lastDot < (title.length() - 1))) { 407 title = title.substring(0, lastDot); 409 } 410 String resName = CmsResource.getName(getParamResource().replace('\\', '/')); 412 setParamResource(getCms().getRequestContext().getFileTranslator().translateResource(resName)); 413 return title; 414 } 415 416 424 public String dialogButtonsAdvancedNextCancel(String advancedAttrs, String nextAttrs, String cancelAttrs) { 425 426 if (m_limitedRestypes && getCms().hasRole(CmsRole.VFS_MANAGER)) { 427 return dialogButtons(new int[] {BUTTON_ADVANCED, BUTTON_NEXT, BUTTON_CANCEL}, new String [] { 428 advancedAttrs, 429 nextAttrs, 430 cancelAttrs}); 431 } else { 432 return dialogButtons(new int[] {BUTTON_NEXT, BUTTON_CANCEL}, new String [] {nextAttrs, cancelAttrs}); 433 } 434 } 435 436 443 public String dialogButtonsNextCancel(String nextAttrs, String cancelAttrs) { 444 445 return dialogButtons(new int[] {BUTTON_NEXT, BUTTON_CANCEL}, new String [] {nextAttrs, cancelAttrs}); 446 } 447 448 453 public String getParamAppendSuffixHtml() { 454 455 return m_paramAppendSuffixHtml; 456 } 457 458 465 public String getParamCurrentFolder() { 466 467 if (CmsStringUtil.isEmpty(m_paramCurrentFolder)) { 468 return computeCurrentFolder(); 469 } 470 471 return m_paramCurrentFolder; 472 } 473 474 479 public String getParamNewResourceEditProps() { 480 481 return m_paramNewResourceEditProps; 482 } 483 484 489 public String getParamNewResourceType() { 490 491 return m_paramNewResourceType; 492 } 493 494 499 public String getParamNewResourceUri() { 500 501 return m_paramNewResourceUri; 502 } 503 504 509 public String getParamPage() { 510 511 return m_paramPage; 512 } 513 514 519 public boolean isResourceCreated() { 520 521 return m_resourceCreated; 522 } 523 524 529 public void setParamAppendSuffixHtml(String paramAppendSuffixHtml) { 530 531 m_paramAppendSuffixHtml = paramAppendSuffixHtml; 532 } 533 534 539 public void setParamCurrentFolder(String paramCurrentFolder) { 540 541 m_paramCurrentFolder = paramCurrentFolder; 542 } 543 544 549 public void setParamNewResourceEditProps(String newResourceEditProps) { 550 551 m_paramNewResourceEditProps = newResourceEditProps; 552 } 553 554 559 public void setParamNewResourceType(String newResourceType) { 560 561 m_paramNewResourceType = newResourceType; 562 } 563 564 569 public void setParamNewResourceUri(String newResourceUri) { 570 571 m_paramNewResourceUri = newResourceUri; 572 } 573 574 579 public void setParamPage(String paramPage) { 580 581 m_paramPage = paramPage; 582 } 583 584 589 public void setResourceCreated(boolean successfullyCreated) { 590 591 m_resourceCreated = successfullyCreated; 592 } 593 594 601 protected String appendSuffixHtml(String resourceName, boolean forceSuffix) { 602 603 if ((forceSuffix || Boolean.valueOf(getParamAppendSuffixHtml()).booleanValue()) 605 && resourceName.indexOf('.') < 0) { 606 resourceName += ".html"; 607 } 608 return resourceName; 609 } 610 611 616 protected String computeCurrentFolder() { 617 618 String currentFolder = getSettings().getExplorerResource(); 619 if (currentFolder == null) { 620 try { 622 currentFolder = getCms().getSitePath(getCms().readFolder("/", CmsResourceFilter.IGNORE_EXPIRATION)); 623 } catch (CmsException e) { 624 if (LOG.isInfoEnabled()) { 626 LOG.info(e); 627 } 628 currentFolder = "/"; 629 } 630 } 631 if (!currentFolder.endsWith("/")) { 632 currentFolder += "/"; 634 } 635 return currentFolder; 636 } 637 638 643 protected String computeFullResourceName() { 644 645 String currentFolder = getParamCurrentFolder(); 648 if (CmsStringUtil.isEmpty(currentFolder)) { 649 currentFolder = computeCurrentFolder(); 650 } 651 return currentFolder + getParamResource(); 652 } 653 654 661 protected CmsProperty createPropertyObject(String name, String value) { 662 663 CmsProperty prop = new CmsProperty(); 664 prop.setAutoCreatePropertyDefinition(true); 665 prop.setName(name); 666 if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) { 667 prop.setValue(value, CmsProperty.TYPE_INDIVIDUAL); 668 } else { 669 prop.setValue(value, CmsProperty.TYPE_SHARED); 670 } 671 return prop; 672 } 673 674 684 protected List createResourceProperties(String resourceName, String resTypeName, String title) { 685 686 List properties = new ArrayList (3); 688 CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(resTypeName); 690 if (settings.isAutoSetTitle()) { 691 properties.add(createPropertyObject(CmsPropertyDefinition.PROPERTY_TITLE, title)); 693 } 694 if (settings.isAutoSetNavigation()) { 695 properties.add(createPropertyObject(CmsPropertyDefinition.PROPERTY_NAVTEXT, title)); 697 List navList = CmsJspNavBuilder.getNavigationForFolder(getCms(), resourceName); 699 float navPos = 1; 700 if (navList.size() > 0) { 701 CmsJspNavElement nav = (CmsJspNavElement)navList.get(navList.size() - 1); 702 navPos = nav.getNavPosition() + 1; 703 } 704 properties.add(createPropertyObject(CmsPropertyDefinition.PROPERTY_NAVPOS, String.valueOf(navPos))); 706 } 707 return properties; 708 } 709 710 713 protected void dialogButtonsHtml(StringBuffer result, int button, String attribute) { 714 715 attribute = appendDelimiter(attribute); 716 717 switch (button) { 718 case BUTTON_NEXT: 719 result.append("<input name=\"next\" type=\"submit\" value=\""); 720 result.append(key(Messages.GUI_BUTTON_NEXTSCREEN_0)); 721 result.append("\" class=\"dialogbutton\""); 722 result.append(attribute); 723 result.append(">\n"); 724 break; 725 default: 726 super.dialogButtonsHtml(result, button, attribute); 727 } 728 } 729 730 733 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 734 735 fillParamValues(request); 737 setParamDialogtype(DIALOG_TYPE); 739 740 if (CmsStringUtil.isNotEmpty(getParamPage())) { 741 m_page = getParamPage(); 742 setParamAction(null); 743 setParamNewResourceUri(null); 744 setParamPage(null); 745 } 746 747 if (DIALOG_OK.equals(getParamAction())) { 749 setAction(ACTION_OK); 750 } else if (DIALOG_SUBMITFORM.equals(getParamAction())) { 751 setAction(ACTION_SUBMITFORM); 752 } else if (DIALOG_NEWFORM.equals(getParamAction())) { 753 setAction(ACTION_NEWFORM); 754 String title = CmsWorkplaceMessages.getNewResourceTitle(this, getParamNewResourceType()); 755 setParamTitle(title); 756 } else if (DIALOG_CANCEL.equals(getParamAction())) { 757 setAction(ACTION_CANCEL); 758 } else { 759 setAction(ACTION_DEFAULT); 760 setParamTitle(key(Messages.GUI_NEWRESOURCE_0)); 762 763 if (!DIALOG_ADVANCED.equals(getParamAction()) && CmsStringUtil.isEmpty(m_page)) { 764 String newResTypesProperty = ""; 766 try { 767 newResTypesProperty = getCms().readPropertyObject( 768 getParamCurrentFolder(), 769 CmsPropertyDefinition.PROPERTY_RESTYPES_AVAILABLE, 770 true).getValue(); 771 } catch (CmsException e) { 772 } 774 if (CmsStringUtil.isNotEmpty(newResTypesProperty) && !newResTypesProperty.equals(VALUE_DEFAULT)) { 775 m_limitedRestypes = true; 776 m_availableResTypes = newResTypesProperty; 777 } 778 } 779 } 780 } 781 } | Popular Tags |