1 17 package org.alfresco.jcr.item; 18 19 import java.io.InputStream ; 20 import java.io.Serializable ; 21 import java.util.ArrayList ; 22 import java.util.Calendar ; 23 import java.util.Collection ; 24 import java.util.List ; 25 26 import javax.jcr.AccessDeniedException; 27 import javax.jcr.Item; 28 import javax.jcr.ItemNotFoundException; 29 import javax.jcr.ItemVisitor; 30 import javax.jcr.Node; 31 import javax.jcr.Property; 32 import javax.jcr.RepositoryException; 33 import javax.jcr.Value; 34 import javax.jcr.ValueFormatException; 35 import javax.jcr.lock.LockException; 36 import javax.jcr.nodetype.ConstraintViolationException; 37 import javax.jcr.nodetype.PropertyDefinition; 38 import javax.jcr.version.VersionException; 39 40 import org.alfresco.jcr.api.JCRNodeRef; 41 import org.alfresco.jcr.dictionary.DataTypeMap; 42 import org.alfresco.jcr.dictionary.PropertyDefinitionImpl; 43 import org.alfresco.jcr.util.JCRProxyFactory; 44 import org.alfresco.repo.content.MimetypeMap; 45 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 46 import org.alfresco.service.cmr.dictionary.DictionaryService; 47 import org.alfresco.service.cmr.dictionary.InvalidTypeException; 48 import org.alfresco.service.cmr.repository.ContentReader; 49 import org.alfresco.service.cmr.repository.ContentService; 50 import org.alfresco.service.cmr.repository.ContentWriter; 51 import org.alfresco.service.cmr.repository.NodeService; 52 import org.alfresco.service.cmr.repository.Path; 53 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 54 import org.alfresco.service.cmr.repository.datatype.TypeConversionException; 55 import org.alfresco.service.namespace.QName; 56 57 58 63 public class PropertyImpl extends ItemImpl implements Property 64 { 65 66 private NodeImpl node; 67 private QName name; 68 private Property proxy = null; 69 70 71 76 public PropertyImpl(NodeImpl node, QName name) 77 { 78 super(node.session); 79 this.node = node; 80 this.name = name; 81 } 82 83 88 @Override 89 public Property getProxy() 90 { 91 if (proxy == null) 92 { 93 proxy = (Property)JCRProxyFactory.create(this, Property.class, session); 94 } 95 return proxy; 96 } 97 98 101 public void remove() throws VersionException, LockException, ConstraintViolationException, RepositoryException 102 { 103 setValue((Value)null); 104 } 105 106 109 public void setValue(Value value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 110 { 111 setPropertyValue(value, -1); 112 } 113 114 117 public void setValue(Value[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 118 { 119 setPropertyValue(values, -1); 120 } 121 122 125 public void setValue(String value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 126 { 127 setPropertyValue(value, -1); 128 } 129 130 133 public void setValue(String [] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 134 { 135 setPropertyValue(values, -1); 136 } 137 138 141 public void setValue(InputStream value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 142 { 143 setPropertyValue(value, -1); 144 } 145 146 149 public void setValue(long value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 150 { 151 setPropertyValue(value, -1); 152 } 153 154 157 public void setValue(double value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 158 { 159 setPropertyValue(value, -1); 160 } 161 162 165 public void setValue(Calendar value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 166 { 167 setPropertyValue((value == null) ? null : value.getTime(), -1); 168 } 169 170 173 public void setValue(boolean value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 174 { 175 setPropertyValue(value, -1); 176 } 177 178 181 public void setValue(Node value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException 182 { 183 setPropertyValue((value == null) ? null : JCRNodeRef.getNodeRef(value), -1); 184 } 185 186 189 public Value getValue() throws ValueFormatException, RepositoryException 190 { 191 checkSingleValued(); 192 ValueImpl valueImpl = new ValueImpl(session, getType(), getPropertyValue()); 193 return valueImpl; 195 } 196 197 200 public Value[] getValues() throws ValueFormatException, RepositoryException 201 { 202 checkMultiValued(); 204 Collection values = (Collection )getPropertyValue(); 205 int type = getType(); 206 207 List <Value> jcrValues = new ArrayList <Value>(values.size()); 209 for (Object value : values) 210 { 211 if (value != null) 213 { 214 jcrValues.add(new ValueImpl(session, type, value)); 216 } 217 } 218 219 return jcrValues.toArray(new Value[jcrValues.size()]); 220 } 221 222 225 public String getString() throws ValueFormatException, RepositoryException 226 { 227 checkSingleValued(); 228 return session.getTypeConverter().stringValue(getPropertyValue()); 229 } 230 231 234 public InputStream getStream() throws ValueFormatException, RepositoryException 235 { 236 checkSingleValued(); 237 return session.getTypeConverter().streamValue(getPropertyValue()); 238 } 239 240 243 public long getLong() throws ValueFormatException, RepositoryException 244 { 245 checkSingleValued(); 246 return session.getTypeConverter().longValue(getPropertyValue()); 247 } 248 249 252 public double getDouble() throws ValueFormatException, RepositoryException 253 { 254 checkSingleValued(); 255 return session.getTypeConverter().doubleValue(getPropertyValue()); 256 } 257 258 261 public Calendar getDate() throws ValueFormatException, RepositoryException 262 { 263 checkSingleValued(); 264 return session.getTypeConverter().dateValue(getPropertyValue()); 265 } 266 267 270 public boolean getBoolean() throws ValueFormatException, RepositoryException 271 { 272 checkSingleValued(); 273 return session.getTypeConverter().booleanValue(getPropertyValue()); 274 } 275 276 279 public Node getNode() throws ValueFormatException, RepositoryException 280 { 281 checkSingleValued(); 282 return session.getTypeConverter().referenceValue(getPropertyValue()).getProxy(); 283 } 284 285 288 public long getLength() throws ValueFormatException, RepositoryException 289 { 290 checkSingleValued(); 291 return getPropertyLength(getPropertyValue()); 292 } 293 294 297 public long[] getLengths() throws ValueFormatException, RepositoryException 298 { 299 checkMultiValued(); 300 Collection values = (Collection )getPropertyValue(); 301 long[] lengths = new long[values.size()]; 302 int i = 0; 303 for (Object value : values) 304 { 305 lengths[i++] = getPropertyLength(value); 306 } 307 return lengths; 308 } 309 310 313 public PropertyDefinition getDefinition() throws RepositoryException 314 { 315 PropertyDefinitionImpl propDefImpl = new PropertyDefinitionImpl(session.getTypeManager(), getPropertyDefinition()); 316 return propDefImpl; 317 } 318 319 322 public int getType() throws RepositoryException 323 { 324 return DataTypeMap.convertDataTypeToPropertyType(getPropertyDefinition().getDataType().getName()); 326 } 327 328 331 public String getName() throws RepositoryException 332 { 333 return name.toPrefixString(session.getNamespaceResolver()); 334 } 335 336 339 public boolean isNode() 340 { 341 return false; 342 } 343 344 347 public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException 348 { 349 return node.getProxy(); 350 } 351 352 355 public String getPath() throws RepositoryException 356 { 357 NodeService nodeService = session.getRepositoryImpl().getServiceRegistry().getNodeService(); 358 Path path = nodeService.getPath(node.getNodeRef()); 359 path.append(new JCRPath.SimpleElement(name)); 360 return path.toPrefixString(session.getNamespaceResolver()); 361 } 362 363 366 public int getDepth() throws RepositoryException 367 { 368 NodeService nodeService = session.getRepositoryImpl().getServiceRegistry().getNodeService(); 369 Path path = nodeService.getPath(node.getNodeRef()); 370 return path.size(); 372 } 373 374 377 public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException 378 { 379 int propertyDepth = getDepth(); 380 if (depth < 0 || depth > propertyDepth) 381 { 382 throw new ItemNotFoundException("Ancestor at depth " + depth + " not found for property " + name); 383 } 384 385 if (depth == propertyDepth) 386 { 387 return this.getProxy(); 388 } 389 else 390 { 391 return node.getAncestor(depth -1); 392 } 393 } 394 395 398 public boolean isSame(Item otherItem) throws RepositoryException 399 { 400 return getProxy().equals(otherItem); 401 } 402 403 406 public void accept(ItemVisitor visitor) throws RepositoryException 407 { 408 visitor.visit(getProxy()); 409 } 410 411 416 protected NodeImpl getNodeImpl() 417 { 418 return node; 419 } 420 421 426 protected QName getPropertyName() 427 { 428 return name; 429 } 430 431 436 protected Object getPropertyValue() 437 throws RepositoryException 438 { 439 Object value = null; 440 441 if (getPropertyDefinition().getDataType().getName().equals(DataTypeDefinition.CONTENT)) 442 { 443 ContentService contentService = node.session.getRepositoryImpl().getServiceRegistry().getContentService(); 445 value = contentService.getReader(node.getNodeRef(), name); 446 if (value == null) 447 { 448 throw new RepositoryException("Property " + name + " has been removed."); 450 } 451 } 452 else 453 { 454 NodeService nodeService = node.session.getRepositoryImpl().getServiceRegistry().getNodeService(); 456 value = nodeService.getProperty(node.getNodeRef(), name); 457 if (value == null) 458 { 459 throw new RepositoryException("Property " + name + " has been removed."); 461 } 462 463 boolean multiValued = getPropertyDefinition().isMultiValued(); 465 if (multiValued != (value instanceof Collection )) 466 { 467 throw new RepositoryException("Alfresco value does not match multi-valued definition of " + multiValued); 468 } 469 } 470 471 return value; 472 } 473 474 482 private long getPropertyLength(Object value) throws ValueFormatException, RepositoryException 483 { 484 if (value instanceof ContentReader) 486 { 487 return ((ContentReader)value).getSize(); 488 } 489 if (value instanceof InputStream ) 490 { 491 return -1; 492 } 493 494 String strValue = (String )DefaultTypeConverter.INSTANCE.convert(String .class, value); 496 return strValue.length(); 497 } 498 499 506 protected void setPropertyValue(Object value, int type) 507 throws RepositoryException 508 { 509 checkSingleValued(); 510 Object castValue = castValue(value, type); 511 writeValue(castValue); 512 } 513 514 521 protected void setPropertyValue(Object [] values, int type) 522 throws RepositoryException 523 { 524 checkMultiValued(); 525 526 List <Object > castValues = null; 528 if (values != null) 529 { 530 castValues = new ArrayList <Object >(values.length); 531 for (Object value : values) 532 { 533 Object castValue = castValue(value, type); 534 castValues.add(castValue); 535 } 536 } 537 538 writeValue(castValues); 539 } 540 541 549 private Object castValue(Object value, int type) 550 throws RepositoryException 551 { 552 if (value instanceof Value) 554 { 555 value = ValueImpl.getValue((Value)value); 556 } 557 558 DataTypeDefinition dataTypeDef = getPropertyDefinition().getDataType(); 560 if (type != -1 && dataTypeDef.getName().equals(DataTypeDefinition.ANY)) 561 { 562 QName dataTypeName = DataTypeMap.convertPropertyTypeToDataType(type); 564 DictionaryService dictionaryService = session.getRepositoryImpl().getServiceRegistry().getDictionaryService(); 565 dataTypeDef = dictionaryService.getDataType(dataTypeName); 566 if (!dataTypeName.equals(DataTypeDefinition.CONTENT)) 567 { 568 value = session.getTypeConverter().convert(dataTypeDef, value); 569 } 570 } 571 572 if (dataTypeDef.getName().equals(DataTypeDefinition.CONTENT)) 574 { 575 value = session.getTypeConverter().streamValue(value); 576 } 577 578 return value; 579 } 580 581 587 private void writeValue(Object value) 588 throws ValueFormatException 589 { 590 if (value instanceof InputStream ) 592 { 593 try 595 { 596 ContentService contentService = session.getRepositoryImpl().getServiceRegistry().getContentService(); 597 ContentWriter writer = contentService.getWriter(node.getNodeRef(), name, true); 598 writer.setMimetype(MimetypeMap.MIMETYPE_BINARY); 599 writer.putContent((InputStream )value); 600 } 601 catch(InvalidTypeException e) 602 { 603 throw new ValueFormatException(e); 604 } 605 } 606 else 607 { 608 try 611 { 612 NodeService nodeService = session.getRepositoryImpl().getServiceRegistry().getNodeService(); 613 nodeService.setProperty(node.getNodeRef(), name, (Serializable )value); 614 } 615 catch(TypeConversionException e) 616 { 617 throw new ValueFormatException(e); 618 } 619 } 620 } 621 622 627 private void checkSingleValued() 628 throws ValueFormatException 629 { 630 if (getPropertyDefinition().isMultiValued()) 631 { 632 throw new ValueFormatException("Property " + name + " is multi-valued."); 634 } 635 } 636 637 642 private void checkMultiValued() 643 throws ValueFormatException 644 { 645 if (!getPropertyDefinition().isMultiValued()) 646 { 647 throw new ValueFormatException("Property " + name + " is single-valued."); 649 } 650 } 651 652 657 private org.alfresco.service.cmr.dictionary.PropertyDefinition getPropertyDefinition() 658 { 659 DictionaryService dictionary = session.getRepositoryImpl().getServiceRegistry().getDictionaryService(); 660 return dictionary.getProperty(name); 661 } 662 663 @Override 664 public boolean equals(Object obj) 665 { 666 if (obj == this) 667 { 668 return true; 669 } 670 if (!(obj instanceof PropertyImpl)) 671 { 672 return false; 673 } 674 PropertyImpl other = (PropertyImpl)obj; 675 return this.name.equals(other.name); 676 } 677 678 @Override 679 public int hashCode() 680 { 681 return name.hashCode(); 682 } 683 684 } 685 | Popular Tags |