1 13 package info.magnolia.cms.core.version; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ItemType; 17 import info.magnolia.cms.core.NodeData; 18 import info.magnolia.cms.security.AccessDeniedException; 19 import info.magnolia.cms.security.AccessManager; 20 import info.magnolia.cms.security.Permission; 21 import info.magnolia.cms.util.Rule; 22 23 import java.util.Calendar ; 24 import java.util.Collection ; 25 26 import javax.jcr.PathNotFoundException; 27 import javax.jcr.RepositoryException; 28 import javax.jcr.Value; 29 import javax.jcr.lock.Lock; 30 import javax.jcr.lock.LockException; 31 import javax.jcr.nodetype.NodeType; 32 import javax.jcr.version.Version; 33 import javax.jcr.version.VersionHistory; 34 import javax.jcr.version.VersionIterator; 35 36 import org.apache.commons.lang.StringUtils; 37 import org.slf4j.Logger; 38 import org.slf4j.LoggerFactory; 39 40 41 45 public class ContentVersion extends Content { 46 47 50 private static Logger log = LoggerFactory.getLogger(ContentVersion.class); 51 52 55 public static final String VERSION_USER = "versionUser"; 57 60 public static final String NAME = "name"; 61 62 65 private Version state; 66 67 70 private Content base; 71 72 75 private Rule rule; 76 77 83 public ContentVersion(Version thisVersion, Content base) throws RepositoryException { 84 if (thisVersion == null) { 85 throw new RepositoryException("Failed to get ContentVersion, version does not exist"); 86 } 87 this.state = thisVersion; 88 this.base = base; 89 this.init(); 90 } 91 92 96 private void init() throws RepositoryException { 97 this.setNode(this.state.getNode(ItemType.JCR_FROZENNODE)); 98 try { 99 if (!StringUtils.equalsIgnoreCase(this.state.getName(), VersionManager.ROOT_VERSION)) { 100 this.rule = VersionManager.getInstance().getUsedFilter(this); 101 } 102 } 103 catch (Exception e) { 104 log.error(e.getMessage(), e); 105 } 106 if (this.rule == null) { 107 log.info("failed to get filter used for creating this version, use open filter"); 108 this.rule = new Rule(); 109 } 110 } 111 112 117 public Calendar getCreated() throws RepositoryException { 118 return this.state.getCreated(); 119 } 120 121 126 public String getVersionLabel() throws RepositoryException { 127 return this.state.getName(); 128 } 129 130 135 public VersionHistory getContainingHistory() throws RepositoryException { 136 return this.state.getContainingHistory(); 137 } 138 139 142 public String getName() { 143 try { 144 return VersionManager.getInstance().getSystemNode(this).getNodeData(NAME).getString(); 145 } 146 catch (RepositoryException re) { 147 log.error("Failed to retrieve name from version system node", re); 148 return ""; 149 } 150 } 151 152 155 public String getUserName() { 156 try { 157 return VersionManager.getInstance().getSystemNode(this).getNodeData(VERSION_USER).getString(); 158 } 159 catch (RepositoryException re) { 160 log.error("Failed to retrieve user from version system node", re); 161 return ""; 162 } 163 } 164 165 168 public String getHandle() { 169 return this.base.getHandle(); 170 } 171 172 179 public Content createContent(String name) throws AccessDeniedException { 180 throw new AccessDeniedException("Not allowed to write on version preview"); 181 } 182 183 191 public Content createContent(String name, String contentType) throws AccessDeniedException { 192 throw new AccessDeniedException("Not allowed to write on version preview"); 193 } 194 195 203 public Content createContent(String name, ItemType contentType) throws AccessDeniedException { 204 throw new AccessDeniedException("Not allowed to write on version preview"); 205 } 206 207 214 public NodeData createNodeData(String name) throws AccessDeniedException { 215 throw new AccessDeniedException("Not allowed to write on version preview"); 216 } 217 218 227 public NodeData createNodeData(String name, Value value, int type) throws AccessDeniedException { 228 throw new AccessDeniedException("Not allowed to write on version preview"); 229 } 230 231 239 public NodeData createNodeData(String name, Value value) throws AccessDeniedException { 240 throw new AccessDeniedException("Not allowed to write on version preview"); 241 } 242 243 251 public NodeData createNodeData(String name, int type) throws AccessDeniedException { 252 throw new AccessDeniedException("Not allowed to write on version preview"); 253 } 254 255 259 public void deleteNodeData(String name) throws RepositoryException { 260 throw new AccessDeniedException("Not allowed to write on version preview"); 261 } 262 263 268 public void updateMetaData() throws AccessDeniedException { 269 throw new AccessDeniedException("Not allowed to write on version preview"); 270 } 271 272 276 public Collection getChildren() { 277 try { 278 if (this.rule.isAllowed(this.base.getNodeTypeName())) { 279 return super.getChildren(); 280 } 281 } 282 catch (RepositoryException re) { 283 log.error(re.getMessage(), re); 284 } 285 return this.base.getChildren(); 286 } 287 288 293 public Collection getChildren(String contentType) { 294 if (this.rule.isAllowed(contentType)) { 295 return super.getChildren(contentType); 296 } 297 return this.base.getChildren(contentType); 298 } 299 300 305 public Collection getChildren(ItemType contentType) { 306 return this.getChildren(contentType.getSystemName()); 307 } 308 309 315 public Collection getChildren(String contentType, String namePattern) { 316 if (this.rule.isAllowed(contentType)) { 317 return super.getChildren(contentType, namePattern); 318 } 319 return this.base.getChildren(contentType, namePattern); 320 } 321 322 325 public boolean hasChildren() { 326 return (this.getChildren().size() > 0); 327 } 328 329 333 public boolean hasChildren(String contentType) { 334 return (this.getChildren(contentType).size() > 0); 335 } 336 337 345 public Content getParent() throws PathNotFoundException, RepositoryException, AccessDeniedException { 346 return this.base.getParent(); 347 } 348 349 357 public Content getAncestor(int digree) throws PathNotFoundException, RepositoryException, AccessDeniedException { 358 return this.base.getAncestor(digree); 359 } 360 361 366 public Collection getAncestors() throws PathNotFoundException, RepositoryException { 367 return this.base.getAncestors(); 368 } 369 370 376 public int getLevel() throws PathNotFoundException, RepositoryException { 377 return this.base.getLevel(); 378 } 379 380 386 public void orderBefore(String srcName, String beforeName) throws RepositoryException { 387 throw new AccessDeniedException("Not allowed to write on version preview"); 388 } 389 390 398 public int getIndex() throws RepositoryException { 399 return this.base.getIndex(); 400 } 401 402 406 public NodeType getNodeType() throws RepositoryException { 407 log.warn("This is a Version node, it will always return NT_FROZEN as node type."); 408 log.warn("Use getNodeTypeName to retrieve base node primary type"); 409 return super.getNodeType(); 410 } 411 412 418 public void restore(String versionName, boolean removeExisting) throws RepositoryException { 419 throw new AccessDeniedException("Not allowed to write on version preview"); 420 } 421 422 428 public void restore(Version version, boolean removeExisting) throws RepositoryException { 429 throw new AccessDeniedException("Not allowed to write on version preview"); 430 } 431 432 439 public void restore(Version version, String relPath, boolean removeExisting) throws RepositoryException { 440 throw new AccessDeniedException("Not allowed to write on version preview"); 441 } 442 443 449 public void restoreByLabel(String versionLabel, boolean removeExisting) throws RepositoryException { 450 throw new AccessDeniedException("Not allowed to write on version preview"); 451 } 452 453 457 public Version addVersion() throws RepositoryException { 458 throw new AccessDeniedException("Not allowed to add version on version preview"); 459 } 460 461 467 public Version addVersion(Rule rule) throws RepositoryException { 468 throw new AccessDeniedException("Not allowed to add version on version preview"); 469 } 470 471 480 public boolean isModified() { 481 log.error("Not valid for version"); 482 return false; 483 } 484 485 489 public VersionHistory getVersionHistory() throws RepositoryException { 490 throw new AccessDeniedException("Not allowed to read VersionHistory of Version"); 491 } 492 493 497 public VersionIterator getAllVersions() throws RepositoryException { 498 throw new AccessDeniedException("Not allowed to get VersionIterator of Version"); 499 } 500 501 507 public ContentVersion getBaseVersion() throws RepositoryException { 508 throw new AccessDeniedException("Not allowed to get base version of Version"); 509 } 510 511 517 public ContentVersion getVersionedContent(Version version) throws RepositoryException { 518 throw new AccessDeniedException("Not allowed to get preview of Version itself"); 519 } 520 521 527 public ContentVersion getVersionedContent(String versionName) throws RepositoryException { 528 throw new AccessDeniedException("Not allowed to get preview of Version itself"); 529 } 530 531 535 public void save() throws RepositoryException { 536 throw new AccessDeniedException("Not allowed to write on version preview"); 537 } 538 539 544 public boolean isGranted(long permissions) { 545 return (permissions & Permission.READ) == permissions; 546 } 547 548 552 public void delete() throws RepositoryException { 553 throw new AccessDeniedException("Not allowed to write on version preview"); 554 } 555 556 560 public void delete(String path) throws RepositoryException { 561 throw new AccessDeniedException("Not allowed to write on version preview"); 562 } 563 564 568 public String getUUID() { 569 return this.base.getUUID(); 570 } 571 572 577 public void addMixin(String type) throws RepositoryException { 578 throw new AccessDeniedException("Not allowed to write on version preview"); 579 } 580 581 587 public void removeMixin(String type) throws RepositoryException { 588 throw new AccessDeniedException("Not allowed to write on version preview"); 589 } 590 591 603 public Lock lock(boolean isDeep, boolean isSessionScoped) throws LockException, RepositoryException { 604 throw new AccessDeniedException("Lock not supported on version preview"); 605 } 606 607 620 public Lock lock(boolean isDeep, boolean isSessionScoped, long yieldFor) throws LockException, RepositoryException { 621 throw new AccessDeniedException("Lock not supported on version preview"); 622 } 623 624 630 public Lock getLock() throws LockException, RepositoryException { 631 throw new AccessDeniedException("Lock not supported on version preview"); 632 } 633 634 641 public void unlock() throws LockException, RepositoryException { 642 throw new AccessDeniedException("Lock not supported on version preview"); 643 } 644 645 652 public boolean holdsLock() throws RepositoryException { 653 throw new AccessDeniedException("Lock not supported on version preview"); 654 } 655 656 662 public boolean isLocked() throws RepositoryException { 663 throw new AccessDeniedException("Lock not supported on version preview"); 664 } 665 666 670 public void setAccessManager(AccessManager manager) { 671 log.error("Not allowed to set access manager on Version preview"); 672 } 673 674 678 public AccessManager getAccessManager() { 679 return this.base.getAccessManager(); 680 } 681 682 } 683 | Popular Tags |