1 13 package info.magnolia.cms.core; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.security.AccessDeniedException; 17 import info.magnolia.cms.security.AccessManager; 18 import info.magnolia.cms.security.Permission; 19 20 import java.util.Calendar ; 21 import java.util.GregorianCalendar ; 22 import java.util.TimeZone ; 23 24 import javax.jcr.Node; 25 import javax.jcr.PathNotFoundException; 26 import javax.jcr.PropertyIterator; 27 import javax.jcr.RepositoryException; 28 29 import org.apache.commons.lang.StringUtils; 30 import org.apache.commons.lang.builder.ToStringBuilder; 31 import org.slf4j.Logger; 32 import org.slf4j.LoggerFactory; 33 34 35 38 public class MetaData { 39 40 44 public static final String TITLE = "title"; 46 public static final String CREATION_DATE = "creationdate"; 48 public static final String LAST_MODIFIED = "lastmodified"; 50 public static final String LAST_ACTION = "lastaction"; 52 public static final String AUTHOR_ID = "authorid"; 54 public static final String ACTIVATOR_ID = "activatorid"; 56 public static final String TEMPLATE = "template"; 58 public static final String TEMPLATE_TYPE = "templatetype"; 60 public static final String ACTIVATED = "activated"; 62 public static final String SEQUENCE_POS = "sequenceposition"; 64 public static final String DEFAULT_META_NODE = "MetaData"; 66 public static final long SEQUENCE_POS_COEFFICIENT = 1000; 67 68 71 private static Logger log = LoggerFactory.getLogger(MetaData.class); 72 73 76 private Node node; 77 78 private AccessManager accessManager; 79 80 84 MetaData(Node workingNode, AccessManager manager) { 85 this.setMetaNode(workingNode, DEFAULT_META_NODE); 86 this.setAccessManager(manager); 87 } 88 89 public String getHandle() throws RepositoryException { 90 return this.node.getPath(); 91 } 92 93 public void setAccessManager(AccessManager manager) { 94 this.accessManager = manager; 95 } 96 97 private void allowUpdate() throws AccessDeniedException { 98 if (node == null) { 100 return; 101 } 102 try { 103 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.node.getPath()), Permission.WRITE); 104 } 105 catch (RepositoryException re) { 106 log.error(re.getMessage(), re); 107 throw new AccessDeniedException(re.getMessage()); 108 } 109 } 110 111 116 private void setMetaNode(Node workingNode, String name) { 117 try { 118 this.node = workingNode.getNode(name); 119 } 120 catch (PathNotFoundException e) { 121 if (log.isDebugEnabled()) { 122 try { 123 log.debug(workingNode.getPath() + " does not support MetaData"); 124 log.debug("check node type definition of " + workingNode.getPrimaryNodeType().getName()); 125 } 126 catch (RepositoryException re) { 127 } 129 } 130 } 131 catch (RepositoryException re) { 132 log.error(re.getMessage(), re); 133 } 134 } 135 136 140 public PropertyIterator getProperties() { 141 if (node == null) { 142 return null; 143 } 144 try { 145 return this.node.getProperties(); 146 } 147 catch (RepositoryException re) { 148 log.error(re.getMessage(), re); 149 } 150 return null; 151 } 152 153 157 public String getLabel() { 158 try { 159 return this.node.getName(); 160 } 161 catch (NullPointerException e) { 162 if (log.isDebugEnabled()) { 163 log.debug("MetaData has not been created or this node does not support MetaData"); } 165 } 166 catch (RepositoryException e) { 167 log.error(e.getMessage(), e); 168 } 169 return StringUtils.EMPTY; 170 } 171 172 177 private String getInternalPropertyName(String name) { 178 if (StringUtils.indexOf(name, ContentRepository.NAMESPACE_PREFIX + ":") != 0) { 179 return ContentRepository.NAMESPACE_PREFIX + ":" + name; 180 } 181 return name; 182 } 183 184 188 public String getTitle() { 189 return getStringProperty(this.getInternalPropertyName(TITLE)); 190 } 191 192 196 public void setTitle(String value) throws AccessDeniedException { 197 allowUpdate(); 198 setProperty(this.getInternalPropertyName(TITLE), value); 199 } 200 201 204 public void setCreationDate() throws AccessDeniedException { 205 allowUpdate(); 206 Calendar value = new GregorianCalendar (TimeZone.getDefault()); 207 setProperty(this.getInternalPropertyName(CREATION_DATE), value); 208 } 209 210 214 public Calendar getCreationDate() { 215 return this.getDateProperty(this.getInternalPropertyName(CREATION_DATE)); 216 } 217 218 221 public void setActivated() throws AccessDeniedException { 222 allowUpdate(); 223 setProperty(this.getInternalPropertyName(ACTIVATED), true); 224 } 225 226 229 public void setUnActivated() throws AccessDeniedException { 230 allowUpdate(); 231 setProperty(this.getInternalPropertyName(ACTIVATED), false); 232 } 233 234 238 public boolean getIsActivated() { 239 return getBooleanProperty(this.getInternalPropertyName(ACTIVATED)); 240 } 241 242 245 public void setLastActivationActionDate() throws AccessDeniedException { 246 allowUpdate(); 247 Calendar value = new GregorianCalendar (TimeZone.getDefault()); 248 setProperty(this.getInternalPropertyName(LAST_ACTION), value); 249 } 250 251 255 public Calendar getLastActionDate() { 256 return getDateProperty(this.getInternalPropertyName(LAST_ACTION)); 257 } 258 259 262 public void setModificationDate() throws AccessDeniedException { 263 allowUpdate(); 264 Calendar value = new GregorianCalendar (TimeZone.getDefault()); 265 setProperty(this.getInternalPropertyName(LAST_MODIFIED), value); 266 } 267 268 272 public Calendar getModificationDate() { 273 return getDateProperty(this.getInternalPropertyName(LAST_MODIFIED)); 274 } 275 276 280 public String getAuthorId() { 281 return getStringProperty(this.getInternalPropertyName(AUTHOR_ID)); 282 } 283 284 288 public void setAuthorId(String value) throws AccessDeniedException { 289 allowUpdate(); 290 setProperty(this.getInternalPropertyName(AUTHOR_ID), value); 291 } 292 293 297 public String getActivatorId() { 298 return getStringProperty(this.getInternalPropertyName(ACTIVATOR_ID)); 299 } 300 301 305 public void setActivatorId(String value) throws AccessDeniedException { 306 allowUpdate(); 307 setProperty(this.getInternalPropertyName(ACTIVATOR_ID), value); 308 } 309 310 314 public String getTemplate() { 315 return getStringProperty(this.getInternalPropertyName(TEMPLATE)); 316 } 317 318 322 public void setTemplate(String value) throws AccessDeniedException { 323 allowUpdate(); 324 setProperty(this.getInternalPropertyName(TEMPLATE), value); 325 } 326 327 331 public void setTemplateType(String value) throws AccessDeniedException { 332 allowUpdate(); 333 setProperty(this.getInternalPropertyName(TEMPLATE_TYPE), value); 334 } 335 336 340 public void setProperty(String name, String value) throws AccessDeniedException { 341 allowUpdate(); 342 name = this.getInternalPropertyName(name); 343 try { 344 this.node.getProperty(name).setValue(value); 345 } 346 catch (PathNotFoundException e) { 347 try { 348 this.node.setProperty(name, value); 349 } 350 catch (RepositoryException re) { 351 log.error(re.getMessage(), re); 352 } 353 } 354 catch (RepositoryException re) { 355 throw new AccessDeniedException(re.getMessage(), re); 356 } 357 catch (NullPointerException e) { 358 if (log.isDebugEnabled()) { 359 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot set property - " + name); } 362 } 363 } 364 365 369 public void setProperty(String name, long value) throws AccessDeniedException { 370 allowUpdate(); 371 name = this.getInternalPropertyName(name); 372 try { 373 this.node.getProperty(name).setValue(value); 374 } 375 catch (PathNotFoundException e) { 376 try { 377 this.node.setProperty(name, value); 378 } 379 catch (RepositoryException re) { 380 log.error(re.getMessage(), re); 381 } 382 } 383 catch (RepositoryException re) { 384 log.error(re.getMessage(), re); 385 throw new AccessDeniedException(re.getMessage()); 386 } 387 catch (NullPointerException e) { 388 if (log.isDebugEnabled()) { 389 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot set property - " + name); } 392 } 393 } 394 395 399 public void setProperty(String name, double value) throws AccessDeniedException { 400 allowUpdate(); 401 name = this.getInternalPropertyName(name); 402 try { 403 this.node.getProperty(name).setValue(value); 404 } 405 catch (PathNotFoundException e) { 406 try { 407 this.node.setProperty(name, value); 408 } 409 catch (RepositoryException re) { 410 log.error(re.getMessage(), re); 411 } 412 } 413 catch (RepositoryException re) { 414 log.error(re.getMessage(), re); 415 throw new AccessDeniedException(re.getMessage()); 416 } 417 catch (NullPointerException e) { 418 if (log.isDebugEnabled()) { 419 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot set property - " + name); } 422 } 423 } 424 425 429 public void setProperty(String name, boolean value) throws AccessDeniedException { 430 allowUpdate(); 431 name = this.getInternalPropertyName(name); 432 try { 433 this.node.getProperty(name).setValue(value); 434 } 435 catch (PathNotFoundException e) { 436 try { 437 this.node.setProperty(name, value); 438 } 439 catch (RepositoryException re) { 440 log.error(re.getMessage(), re); 441 } 442 } 443 catch (RepositoryException re) { 444 log.error(re.getMessage(), re); 445 throw new AccessDeniedException(re.getMessage()); 446 } 447 catch (NullPointerException e) { 448 if (log.isDebugEnabled()) { 449 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot set property - " + name); } 452 } 453 } 454 455 459 public void setProperty(String name, Calendar value) throws AccessDeniedException { 460 allowUpdate(); 461 name = this.getInternalPropertyName(name); 462 try { 463 this.node.getProperty(name).setValue(value); 464 } 465 catch (PathNotFoundException e) { 466 try { 467 this.node.setProperty(name, value); 468 } 469 catch (RepositoryException re) { 470 log.error(re.getMessage(), re); 471 } 472 } 473 catch (RepositoryException re) { 474 log.error(re.getMessage(), re); 475 throw new AccessDeniedException(re.getMessage()); 476 } 477 catch (NullPointerException e) { 478 if (log.isDebugEnabled()) { 479 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot set property - " + name); } 482 } 483 } 484 485 488 public Calendar getDateProperty(String name) { 489 name = this.getInternalPropertyName(name); 490 try { 491 return this.node.getProperty(name).getDate(); 492 } 493 catch (PathNotFoundException re) { 494 if (log.isDebugEnabled()) { 495 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); } 497 } 498 catch (RepositoryException re) { 499 log.error(re.getMessage(), re); 500 } 501 catch (NullPointerException e) { 502 if (log.isDebugEnabled()) { 503 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot get property - " + name); } 506 } 507 return null; 508 } 509 510 513 public boolean getBooleanProperty(String name) { 514 name = this.getInternalPropertyName(name); 515 try { 516 return this.node.getProperty(name).getBoolean(); 517 } 518 catch (PathNotFoundException re) { 519 if (log.isDebugEnabled()) { 520 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); } 522 } 523 catch (RepositoryException re) { 524 log.error(re.getMessage(), re); 525 } 526 catch (NullPointerException e) { 527 if (log.isDebugEnabled()) { 528 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot get property - " + name); } 531 } 532 return false; 533 } 534 535 538 public double getDoubleProperty(String name) { 539 name = this.getInternalPropertyName(name); 540 try { 541 return this.node.getProperty(name).getDouble(); 542 } 543 catch (PathNotFoundException re) { 544 if (log.isDebugEnabled()) { 545 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); } 547 } 548 catch (RepositoryException re) { 549 log.error(re.getMessage(), re); 550 } 551 catch (NullPointerException e) { 552 if (log.isDebugEnabled()) { 553 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot get property - " + name); } 556 } 557 return 0d; 558 } 559 560 563 public long getLongProperty(String name) { 564 name = this.getInternalPropertyName(name); 565 try { 566 return this.node.getProperty(name).getLong(); 567 } 568 catch (PathNotFoundException re) { 569 if (log.isDebugEnabled()) { 570 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); } 572 } 573 catch (RepositoryException re) { 574 log.error(re.getMessage(), re); 575 } 576 catch (NullPointerException e) { 577 if (log.isDebugEnabled()) { 578 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot get property - " + name); } 581 } 582 return 0L; 583 } 584 585 590 public String getStringProperty(String name) { 591 name = this.getInternalPropertyName(name); 592 try { 593 return this.node.getProperty(name).getString(); 594 } 595 catch (PathNotFoundException re) { 596 if (log.isDebugEnabled()) { 597 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); } 599 } 600 catch (RepositoryException re) { 601 log.error(re.getMessage(), re); 602 } 603 catch (NullPointerException e) { 604 if (log.isDebugEnabled()) { 605 log.debug("MetaData has not been created or this node does not support MetaData"); log.debug("cannot get property - " + name); } 608 } 609 return StringUtils.EMPTY; 610 } 611 612 618 public void removeProperty(String name) throws PathNotFoundException, RepositoryException { 619 this.node.getProperty(this.getInternalPropertyName(name)).remove(); 620 } 621 622 627 public boolean hasProperty(String name) { 628 try { 629 return this.node.hasProperty(this.getInternalPropertyName(name)); 630 } 631 catch (RepositoryException re) { 632 log.error(re.getMessage(), re); 633 } 634 return false; 635 } 636 637 640 public String toString() { 641 return new ToStringBuilder(this).append("title", this.getTitle()) .append("template", this.getTemplate()) .append("authorId", this.getAuthorId()) .append("label", this.getLabel()) .append("activatorId", this.getActivatorId()) .append("isActivated", this.getIsActivated()) .append("creationDate", this.getCreationDate()) .append("lastActionDate", this.getLastActionDate()) .append("modificationDate", this.getModificationDate()) .toString(); 651 } 652 653 } 654 | Popular Tags |