1 31 32 package org.opencms.workplace.explorer; 33 34 import org.opencms.db.CmsDbUtil; 35 import org.opencms.db.CmsUserSettings; 36 import org.opencms.file.CmsFolder; 37 import org.opencms.file.CmsObject; 38 import org.opencms.file.CmsProject; 39 import org.opencms.file.CmsPropertyDefinition; 40 import org.opencms.file.CmsResource; 41 import org.opencms.file.CmsResourceFilter; 42 import org.opencms.file.collectors.I_CmsResourceCollector; 43 import org.opencms.file.types.CmsResourceTypePlain; 44 import org.opencms.i18n.CmsEncoder; 45 import org.opencms.jsp.CmsJspActionElement; 46 import org.opencms.lock.CmsLock; 47 import org.opencms.main.CmsException; 48 import org.opencms.main.CmsLog; 49 import org.opencms.main.CmsRuntimeException; 50 import org.opencms.main.OpenCms; 51 import org.opencms.util.CmsStringUtil; 52 import org.opencms.workplace.CmsWorkplace; 53 import org.opencms.workplace.CmsWorkplaceSettings; 54 import org.opencms.workplace.commons.CmsTouch; 55 import org.opencms.workplace.galleries.A_CmsGallery; 56 57 import java.util.ArrayList ; 58 import java.util.Collections ; 59 import java.util.List ; 60 61 import javax.servlet.http.HttpServletRequest ; 62 63 import org.apache.commons.logging.Log; 64 65 82 public class CmsExplorer extends CmsWorkplace { 83 84 85 public static final int LAYOUTSTYLE_AFTEREXPIRE = 2; 86 87 88 public static final int LAYOUTSTYLE_BEFORERELEASE = 1; 89 90 91 public static final int LAYOUTSTYLE_INRANGE = 0; 92 93 94 public static final String PARAMETER_MODE = "mode"; 95 96 97 public static final String VIEW_LIST = "listview"; 98 99 100 private static final String LOCATION_SIBLING = "siblings:"; 101 102 103 private static final Log LOG = CmsLog.getLog(CmsExplorer.class); 104 105 106 private static final String PARAMETER_FLATURL = "flaturl"; 107 108 109 private static final String PARAMETER_PAGE = "page"; 110 111 112 private static final String PARAMETER_RESOURCE = "resource"; 113 114 115 private static final String PARAMETER_SHOWLINKS = "showlinks"; 116 117 118 public static final String VIEW_EXPLORER = "explorerview"; 119 120 121 public static final String VIEW_GALLERY = "galleryview"; 122 123 128 public CmsExplorer(CmsJspActionElement jsp) { 129 130 super(jsp); 131 } 132 133 138 public String getFileList() { 139 140 boolean galleryView = VIEW_GALLERY.equals(getSettings().getExplorerMode()); 142 boolean listView = VIEW_LIST.equals(getSettings().getExplorerMode()); 144 boolean showVfsLinks = getSettings().getExplorerShowLinks(); 146 147 CmsResource currentResource = null; 148 149 String currentFolder = getSettings().getExplorerResource(); 150 boolean found = true; 151 try { 152 currentResource = getCms().readResource(currentFolder, CmsResourceFilter.ALL); 153 } catch (CmsException e) { 154 found = false; 156 } 157 if (found) { 158 if (showVfsLinks) { 159 currentFolder = LOCATION_SIBLING + currentFolder; 161 } 162 } else { 163 currentFolder = "/"; 165 showVfsLinks = false; 166 try { 167 currentResource = getCms().readResource(currentFolder, CmsResourceFilter.ALL); 168 } catch (CmsException e) { 169 LOG.error(e); 171 throw new CmsRuntimeException(e.getMessageContainer(), e); 172 } 173 } 174 175 StringBuffer content = new StringBuffer (2048); 177 content.append("function initialize() {\n"); 178 179 content.append("top.setRootFolder(\""); 180 String rootFolder = getRootFolder(); 181 content.append(rootFolder); 182 content.append("\");\n"); 183 184 content.append("top.mode=\""); 185 content.append(getSettings().getExplorerMode()); 186 content.append("\";\n"); 187 188 content.append("top.showlinks="); 189 content.append(showVfsLinks); 190 content.append(";\n"); 191 192 content.append("top.plainresid="); 194 content.append(CmsResourceTypePlain.getStaticTypeId()); 195 content.append(";\n"); 196 197 content.append("top.autolock="); 199 content.append(OpenCms.getWorkplaceManager().autoLockResources()); 200 content.append(";\n"); 201 202 content.append("top.buttonType="); 204 content.append(getSettings().getUserSettings().getExplorerButtonStyle()); 205 content.append(";\n"); 206 207 content.append("top.head.helpUrl='explorer/index.html';\n"); 209 content.append("top.setProject("); 211 if (!listView) { 212 content.append(getSettings().getProject()); 213 } else { 214 content.append(getSettings().getExplorerProjectId()); 215 } 216 content.append(");\n"); 217 content.append("top.setOnlineProject("); 219 content.append(CmsProject.ONLINE_PROJECT_ID); 220 content.append(");\n"); 221 boolean writeAccess = VIEW_EXPLORER.equals(getSettings().getExplorerMode()); 223 if (writeAccess && (!showVfsLinks)) { 224 writeAccess = getCms().isInsideCurrentProject(currentFolder); 225 } 226 content.append("top.enableNewButton("); 227 content.append(writeAccess); 228 content.append(");\n"); 229 content.append("top.setDirectory(\""); 231 content.append(CmsResource.getFolderPath(currentResource.getRootPath())); 232 content.append("\",\""); 233 if (showVfsLinks) { 234 content.append(LOCATION_SIBLING); 235 content.append(getSettings().getExplorerResource()); 236 } else { 237 content.append(CmsResource.getFolderPath(getSettings().getExplorerResource())); 238 } 239 content.append("\");\n"); 240 content.append("top.rD();\n"); 241 List reloadTreeFolders = (List )getJsp().getRequest().getAttribute(REQUEST_ATTRIBUTE_RELOADTREE); 242 if (reloadTreeFolders != null) { 243 String reloadFolder = ""; 245 for (int i = 0; i < reloadTreeFolders.size(); i++) { 246 reloadFolder = (String )reloadTreeFolders.get(i); 247 if (getSettings().getUserSettings().getRestrictExplorerView()) { 248 reloadFolder = reloadFolder.substring(rootFolder.length() - 1); 250 } 251 content.append("top.addNodeToLoad(\"" + reloadFolder + "\");\n"); 252 } 253 content.append("top.reloadNodeList();\n"); 254 } 255 content.append("\n"); 256 257 int preferences = getUserPreferences(); 259 260 boolean showTitle = (preferences & CmsUserSettings.FILELIST_TITLE) > 0; 261 boolean showPermissions = (preferences & CmsUserSettings.FILELIST_PERMISSIONS) > 0; 262 boolean showDateLastModified = (preferences & CmsUserSettings.FILELIST_DATE_LASTMODIFIED) > 0; 263 boolean showUserWhoLastModified = (preferences & CmsUserSettings.FILELIST_USER_LASTMODIFIED) > 0; 264 boolean showDateCreated = (preferences & CmsUserSettings.FILELIST_DATE_CREATED) > 0; 265 boolean showUserWhoCreated = (preferences & CmsUserSettings.FILELIST_USER_CREATED) > 0; 266 boolean showDateReleased = (preferences & CmsUserSettings.FILELIST_DATE_RELEASED) > 0; 267 boolean showDateExpired = (preferences & CmsUserSettings.FILELIST_DATE_EXPIRED) > 0; 268 269 List resources = getResources(getSettings().getExplorerResource()); 271 272 int startat = 0; 274 int stopat = resources.size(); 275 int selectedPage = 1; 276 int numberOfPages = 0; 277 int maxEntrys = getSettings().getUserSettings().getExplorerFileEntries(); 278 279 if (!(galleryView || showVfsLinks)) { 280 selectedPage = getSettings().getExplorerPage(); 281 if (stopat > maxEntrys) { 282 numberOfPages = (stopat / maxEntrys) + 1; 284 if (selectedPage > numberOfPages) { 285 selectedPage = 1; 287 } 288 startat = (selectedPage - 1) * maxEntrys; 289 if ((startat + maxEntrys) < stopat) { 290 stopat = startat + maxEntrys; 291 } 292 } 293 } 294 CmsProject project; 296 try { 297 if (!listView) { 298 project = getCms().readProject(getSettings().getProject()); 299 } else { 300 project = getCms().readProject(getSettings().getExplorerProjectId()); 301 } 302 } catch (CmsException ex) { 303 project = getCms().getRequestContext().currentProject(); 304 } 305 306 List projectResources; 308 try { 309 projectResources = getCms().readProjectResources(project); 310 } catch (CmsException e) { 311 if (LOG.isInfoEnabled()) { 313 LOG.info(e); 314 } 315 projectResources = new ArrayList (); 316 } 317 318 for (int i = startat; i < stopat; i++) { 319 CmsResource res = (CmsResource)resources.get(i); 320 CmsLock lock = null; 321 String path = getCms().getSitePath(res); 322 323 try { 324 lock = getCms().getLock(res); 325 } catch (CmsException e) { 326 lock = CmsLock.getNullLock(); 327 328 LOG.error(e); 329 } 330 331 content.append("top.aF("); 332 333 content.append("\""); 335 content.append(res.getName()); 336 content.append("\","); 337 338 if (showVfsLinks || galleryView || listView) { 340 content.append("\""); 341 content.append(path); 342 content.append("\","); 343 } else { 344 content.append("\"\","); 346 } 347 348 if (showTitle) { 350 String title = ""; 351 try { 352 title = getCms().readPropertyObject( 353 getCms().getSitePath(res), 354 CmsPropertyDefinition.PROPERTY_TITLE, 355 false).getValue(); 356 } catch (CmsException e) { 357 getCms().getRequestContext().saveSiteRoot(); 358 try { 359 getCms().getRequestContext().setSiteRoot("/"); 360 title = getCms().readPropertyObject( 361 res.getRootPath(), 362 CmsPropertyDefinition.PROPERTY_TITLE, 363 false).getValue(); 364 } catch (Exception e1) { 365 if (LOG.isInfoEnabled()) { 367 LOG.info(e); 368 } 369 } finally { 370 getCms().getRequestContext().restoreSiteRoot(); 371 } 372 } 373 content.append("\""); 374 if (title != null) { 375 content.append(CmsEncoder.escapeWBlanks(title, CmsEncoder.ENCODING_UTF_8)); 376 } 377 content.append("\","); 378 379 } else { 380 content.append("\"\","); 381 } 382 383 content.append(res.getTypeId()); 385 content.append(","); 386 387 if (res.getSiblingCount() > 1) { 389 if (res.isLabeled()) { 391 content.append("2"); 393 } else { 394 content.append("1"); 396 } 397 } else { 398 content.append("0"); 400 } 401 content.append(","); 402 403 content.append(res.getLength()); 405 content.append(","); 406 407 content.append(res.getState()); 409 content.append(","); 410 411 int layoutstyle = CmsExplorer.LAYOUTSTYLE_INRANGE; 413 if (res.getDateReleased() > getCms().getRequestContext().getRequestTime()) { 414 layoutstyle = CmsExplorer.LAYOUTSTYLE_BEFORERELEASE; 415 } else if ((res.getDateExpired() < getCms().getRequestContext().getRequestTime())) { 416 layoutstyle = CmsExplorer.LAYOUTSTYLE_AFTEREXPIRE; 417 } 418 content.append(layoutstyle); 419 content.append(','); 420 421 int projectId = res.getProjectLastModified(); 423 if (!lock.isNullLock() 424 && lock.getType() != CmsLock.TYPE_INHERITED 425 && lock.getType() != CmsLock.TYPE_SHARED_INHERITED) { 426 projectId = lock.getProjectId(); 428 } 429 content.append(projectId); 430 content.append(","); 431 432 if (showDateLastModified) { 434 content.append("\""); 435 content.append(getMessages().getDateTime(res.getDateLastModified())); 436 content.append("\","); 437 438 } else { 439 content.append("\"\","); 440 } 441 442 if (showUserWhoLastModified) { 444 content.append("\""); 445 try { 446 content.append(getCms().readUser(res.getUserLastModified()).getName()); 447 } catch (CmsException e) { 448 content.append(res.getUserLastModified().toString()); 449 } 450 content.append("\","); 451 } else { 452 content.append("\"\","); 453 } 454 455 if (showDateCreated) { 457 content.append("\""); 458 content.append(getMessages().getDateTime(res.getDateCreated())); 459 content.append("\","); 460 461 } else { 462 content.append("\"\","); 463 } 464 465 if (showUserWhoCreated) { 467 content.append("\""); 468 try { 469 content.append(getCms().readUser(res.getUserCreated()).getName()); 470 } catch (CmsException e) { 471 content.append(res.getUserCreated().toString()); 472 } 473 content.append("\","); 474 } else { 475 content.append("\"\","); 476 } 477 478 if (showDateReleased) { 480 content.append("\""); 481 long release = res.getDateReleased(); 482 if (release != CmsResource.DATE_RELEASED_DEFAULT) { 483 content.append(getMessages().getDateTime(release)); 484 } else { 485 content.append(CmsTouch.DEFAULT_DATE_STRING); 486 } 487 content.append("\","); 488 489 } else { 490 content.append("\"\","); 491 } 492 493 if (showDateExpired) { 495 content.append("\""); 496 long expire = res.getDateExpired(); 497 if (expire != CmsResource.DATE_EXPIRED_DEFAULT) { 498 content.append(getMessages().getDateTime(expire)); 499 } else { 500 content.append(CmsTouch.DEFAULT_DATE_STRING); 501 } 502 content.append("\","); 503 504 } else { 505 content.append("\"\","); 506 } 507 508 if (showPermissions) { 510 content.append("\""); 511 try { 512 content.append(getCms().getPermissions(getCms().getSitePath(res)).getPermissionString()); 513 } catch (CmsException e) { 514 getCms().getRequestContext().saveSiteRoot(); 515 try { 516 getCms().getRequestContext().setSiteRoot("/"); 517 content.append(getCms().getPermissions(res.getRootPath()).getPermissionString()); 518 } catch (Exception e1) { 519 content.append(CmsStringUtil.escapeJavaScript(e1.getMessage())); 520 } finally { 521 getCms().getRequestContext().restoreSiteRoot(); 522 } 523 } 524 content.append("\","); 525 } else { 526 content.append("\"\","); 527 } 528 529 if (lock.isNullLock()) { 531 content.append("\"\","); 532 } else { 533 content.append("\""); 534 try { 535 content.append(getCms().readUser(lock.getUserId()).getName()); 536 } catch (CmsException e) { 537 content.append(CmsStringUtil.escapeJavaScript(e.getMessage())); 538 } 539 content.append("\","); 540 } 541 542 content.append(lock.getType()); 544 content.append(","); 545 546 int lockedInProject = CmsDbUtil.UNKNOWN_ID; 548 if (lock.isNullLock() && res.getState() != CmsResource.STATE_UNCHANGED) { 549 lockedInProject = res.getProjectLastModified(); 551 } else { 552 if (res.getState() != CmsResource.STATE_UNCHANGED) { 553 lockedInProject = projectId; 555 } else { 556 lockedInProject = lock.getProjectId(); 558 } 559 } 560 String lockedInProjectName; 561 try { 562 if (lockedInProject == CmsDbUtil.UNKNOWN_ID) { 563 lockedInProjectName = ""; 565 } else { 566 lockedInProjectName = getCms().readProject(lockedInProject).getName(); 567 } 568 } catch (CmsException exc) { 569 if (LOG.isInfoEnabled()) { 571 LOG.info(exc); 572 } 573 lockedInProjectName = ""; 574 } 575 content.append("\""); 576 content.append(lockedInProjectName); 577 content.append("\","); 578 579 content.append(lockedInProject); 581 content.append(",\""); 582 583 if (CmsProject.isInsideProject(projectResources, res)) { 585 content.append("I"); 586 } else { 587 content.append("O"); 588 } 589 content.append("\""); 590 content.append(");\n"); 591 } 592 593 content.append("top.dU(document,"); 594 content.append(numberOfPages); 595 content.append(","); 596 content.append(selectedPage); 597 content.append("); \n"); 598 599 if (getSettings().getErrorMessage() != null) { 601 content.append("alert(\""); 603 content.append(CmsStringUtil.escapeJavaScript(getSettings().getErrorMessage().key(getLocale()))); 604 content.append("\");\n"); 605 getSettings().setErrorMessage(null); 607 } 608 609 String message = getBroadcastMessageString(); 611 if (CmsStringUtil.isNotEmpty(message)) { 612 content.append("alert(decodeURIComponent(\""); 614 content.append(CmsEncoder.escapeWBlanks(message, CmsEncoder.ENCODING_UTF_8)); 616 content.append("\"));\n"); 617 } 618 619 content.append("}\n"); 620 return content.toString(); 621 } 622 623 628 public String getRootFolder() { 629 630 String folder = "/"; 631 if (getSettings().getUserSettings().getRestrictExplorerView()) { 632 folder = getSettings().getUserSettings().getStartFolder(); 633 } 634 try { 635 getCms().readFolder(folder, CmsResourceFilter.IGNORE_EXPIRATION); 636 return folder; 637 } catch (CmsException e) { 638 if (LOG.isInfoEnabled()) { 640 LOG.info(e); 641 } 642 return "/"; 643 } 644 } 645 646 649 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 650 651 String currentResource = request.getParameter(PARAMETER_RESOURCE); 652 String mode = request.getParameter(PARAMETER_MODE); 653 if (CmsStringUtil.isNotEmpty(mode)) { 654 settings.setExplorerMode(mode); 655 } else { 656 if (!(VIEW_GALLERY.equals(settings.getExplorerMode()) || VIEW_LIST.equals(settings.getExplorerMode()))) { 658 settings.setExplorerMode(VIEW_EXPLORER); 659 } 660 } 661 662 boolean showLinks = Boolean.valueOf(request.getParameter(PARAMETER_SHOWLINKS)).booleanValue(); 663 664 if (showLinks) { 665 settings.setExplorerResource(currentResource); 667 } else { 668 if (currentResource != null && currentResource.startsWith(LOCATION_SIBLING)) { 670 showLinks = true; 672 settings.setExplorerResource(currentResource.substring(LOCATION_SIBLING.length())); 673 } else { 674 if (CmsStringUtil.isNotEmpty(currentResource) && folderExists(getCms(), currentResource)) { 675 settings.setExplorerResource(currentResource); 677 } else { 678 showLinks = settings.getExplorerShowLinks(); 680 currentResource = settings.getExplorerResource(); 681 if (!resourceExists(getCms(), currentResource)) { 682 settings.setExplorerResource("/"); 684 showLinks = false; 685 } 686 } 687 } 688 } 689 settings.setExplorerShowLinks(showLinks); 690 691 String selectedPage = request.getParameter(PARAMETER_PAGE); 692 if (selectedPage != null) { 693 int page = 1; 694 try { 695 page = Integer.parseInt(selectedPage); 696 } catch (NumberFormatException e) { 697 if (LOG.isInfoEnabled()) { 699 LOG.info(e); 700 } 701 } 702 settings.setExplorerPage(page); 703 } 704 705 settings.setExplorerFlaturl(request.getParameter(PARAMETER_FLATURL)); 707 } 708 709 716 private boolean folderExists(CmsObject cms, String folder) { 717 718 try { 719 CmsFolder test = cms.readFolder(folder, CmsResourceFilter.IGNORE_EXPIRATION); 720 if (test.isFile()) { 721 return false; 722 } 723 return true; 724 } catch (Exception e) { 725 return false; 726 } 727 } 728 729 739 private List getResources(String resource) { 740 741 if (getSettings().getExplorerShowLinks()) { 742 try { 744 return getCms().readSiblings(resource, CmsResourceFilter.ALL); 746 } catch (CmsException e) { 747 if (LOG.isInfoEnabled()) { 749 LOG.info(e); 750 } 751 return Collections.EMPTY_LIST; 752 } 753 } else if (VIEW_LIST.equals(getSettings().getExplorerMode())) { 754 755 I_CmsResourceCollector collector = getSettings().getCollector(); 757 if (collector != null) { 758 try { 760 return collector.getResults(getCms()); 761 } catch (CmsException e) { 762 if (LOG.isInfoEnabled()) { 763 LOG.info(e); 764 } 765 } 766 } 767 768 return Collections.EMPTY_LIST; 769 } else if (VIEW_GALLERY.equals(getSettings().getExplorerMode())) { 770 771 A_CmsGallery gallery = A_CmsGallery.createInstance(getSettings().getGalleryType(), getJsp()); 773 return gallery.getGalleries(); 774 } else { 775 try { 777 return getCms().getResourcesInFolder(resource, CmsResourceFilter.ONLY_VISIBLE); 778 } catch (CmsException e) { 779 if (LOG.isInfoEnabled()) { 781 LOG.info(e); 782 } 783 return Collections.EMPTY_LIST; 784 } 785 } 786 } 787 788 793 private int getUserPreferences() { 794 795 CmsUserSettings settings = new CmsUserSettings(getCms()); 796 return settings.getExplorerSettings(); 797 } 798 799 806 private boolean resourceExists(CmsObject cms, String resource) { 807 808 try { 809 cms.readResource(resource, CmsResourceFilter.ALL); 810 return true; 811 } catch (CmsException e) { 812 return false; 813 } 814 } 815 }
| Popular Tags
|