1 5 6 package org.exoplatform.services.jcr.impl.core; 7 8 9 import java.io.ByteArrayInputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.util.Calendar ; 13 14 import javax.jcr.BinaryValue; 15 import javax.jcr.BooleanValue; 16 import javax.jcr.DateValue; 17 import javax.jcr.DoubleValue; 18 import javax.jcr.Item; 19 import javax.jcr.ItemVisitor; 20 import javax.jcr.LongValue; 21 import javax.jcr.PathNotFoundException; 22 import javax.jcr.Property; 23 import javax.jcr.PropertyType; 24 import javax.jcr.RepositoryException; 25 import javax.jcr.StringIterator; 26 import javax.jcr.StringValue; 27 import javax.jcr.Value; 28 import javax.jcr.ValueFormatException; 29 import javax.jcr.nodetype.NodeType; 30 import javax.jcr.nodetype.PropertyDef; 31 import javax.jcr.util.ISO8601; 32 33 import org.exoplatform.services.jcr.core.ItemLocation; 34 import org.exoplatform.services.jcr.impl.core.nodetype.NodeTypeImpl; 35 import org.exoplatform.services.jcr.impl.core.nodetype.PropertyDefImpl; 36 import org.exoplatform.services.jcr.impl.util.EntityCollection; 37 import org.exoplatform.services.jcr.impl.util.PropertyConvertor; 38 39 45 46 public class PropertyImpl extends ItemImpl implements Property { 47 48 49 protected Value[] values = new Value[1]; 51 52 public PropertyImpl(String path, Value value, int type) 53 throws PathNotFoundException, RepositoryException { 54 super(path); 55 56 this.values[0] = value; 57 this.values = PropertyConvertor.convert(values, type); 58 59 61 } 62 63 public PropertyImpl(String path, Value[] values, int type) 64 throws PathNotFoundException, RepositoryException { 65 66 super(path); 67 log.debug("PropertyImpl()-- "+path+" type = "+type); 68 this.values = PropertyConvertor.convert(values, type); 69 70 73 } 74 75 public PropertyImpl(PropertyImpl prop) throws PathNotFoundException, RepositoryException { 77 78 super(prop.getPath()); 79 81 this.values = PropertyConvertor.convert(prop.getValues(), prop.getType()); 82 } 84 85 90 public Value getValue() { 91 if (values.length == 0) 92 return null; 93 return this.values[0]; 94 } 95 96 103 public Value[] getValues() throws RepositoryException { 104 return this.values; 105 } 106 107 115 public String getString() { 116 try { 117 return values[0].getString(); 118 } catch (Exception e) { 119 return ""; 120 } 121 } 122 123 131 public double getDouble() { 132 try { 133 return values[0].getDouble(); 134 } catch (Exception e) { 135 return 0.0; 136 } 137 } 138 139 147 public long getLong() { 148 try { 149 return values[0].getLong(); 150 } catch (Exception e) { 151 return 0l; 152 } 153 154 } 155 156 162 public InputStream getStream() { 163 try { 164 return values[0].getStream(); 165 } catch (Exception e) { 166 return new ByteArrayInputStream (new byte[0]); 167 } 168 169 } 170 171 177 public Calendar getDate() { 178 try { 179 return values[0].getDate(); 180 } catch (Exception e) { 181 return ISO8601.parse("1970-01-01T00:00:00.00Z"); 182 } 183 184 } 185 186 192 public boolean getBoolean() { 193 try { 194 return values[0].getBoolean(); 195 } catch (Exception e) { 196 return false; 197 } 198 } 199 200 204 public boolean hasValue() { 205 if (values.length == 0 || values[0] == null || getLength() == 0) 206 return false; 207 return true; 208 } 209 210 221 public long getLength() { 222 try { 223 return values[0].getString().length(); 224 } catch (IllegalStateException e) { 225 try { 226 return getStream().available(); 227 } catch (IOException ioe) { 228 return -1; 229 } 230 } catch (Exception e) { 231 return -1; 232 } 233 } 234 235 244 public void setValue(Value value) throws ValueFormatException, RepositoryException { 245 log.debug("setValue for " + getPath()+" "+value); 246 Value[] newValues = new Value[1]; 247 newValues[0] = value; 248 setValue(newValues); 249 } 250 251 266 public void setValue(Value[] values) throws ValueFormatException, RepositoryException { 267 log.debug("setValues for " + getPath()+" length="+values.length+" propType="+getType()+" valueType="+values[0].getType()); 268 269 NodeImpl parent = (NodeImpl) getParent(); 270 if (parent == null) 271 throw new RepositoryException("Parent for <" + getPath() + "> is null!"); 272 273 if(values.length > 1) 274 if (!((NodeTypeImpl) parent.getPrimaryNodeType()).getPropertyDef(getName()).isMultiple()) 275 throw new ValueFormatException("Can not add multiple value to this property"); 276 277 if ((getType() != PropertyType.UNDEFINED) && (getType() != values[0].getType())) 278 this.values = PropertyConvertor.convert(values, getType()); 279 else 280 this.values = values; 281 282 parent.updateProperty(this.getName(), getType(), values); 283 284 } 285 286 290 public void setValue(String value) throws ValueFormatException, RepositoryException { 291 setValue(new StringValue(value)); 292 } 293 294 298 public void setValue(InputStream stream) throws ValueFormatException, RepositoryException { 299 setValue(new BinaryValue(stream)); 300 } 301 302 305 public void setValue(double number) throws ValueFormatException, RepositoryException { 306 setValue(new DoubleValue(number)); 307 } 308 309 312 public void setValue(long number) throws ValueFormatException, RepositoryException { 313 setValue(new LongValue(number)); 314 } 315 316 319 public void setValue(Calendar date) throws ValueFormatException, RepositoryException { 320 setValue(new DateValue(date)); 321 } 322 323 326 public void setValue(boolean b) throws ValueFormatException, RepositoryException { 327 setValue(new BooleanValue(b)); 328 } 329 330 public PropertyDef getDefinition() { 331 332 NodeType parentType; 333 try { 334 parentType = getParent().getPrimaryNodeType(); 335 } catch (Exception e) { 336 e.printStackTrace(); 337 throw new RuntimeException (e.getMessage()); 338 } 339 PropertyDef[] propDefs = parentType.getPropertyDefs(); 340 for (int i = 0; i < propDefs.length; i++) 341 if (propDefs[i].getName() == null || propDefs[i].getName().equals(getName())) 342 return propDefs[i]; 343 344 return new PropertyDefImpl(parentType.getName()); 346 } 347 348 349 public void accept(ItemVisitor visitor) throws RepositoryException { 351 visitor.visit(this); 352 } 353 354 public boolean isNode() { 355 return false; 356 } 357 358 protected boolean isItemIdentical(Item otherItem) { 359 360 Property otherProperty = (Property) otherItem; 361 if (this.getName().equals(otherProperty.getName())) 362 return toString().equals(otherProperty.toString()); 363 else 364 return false; 365 } 366 367 public StringIterator getPaths() { 368 369 try { 370 371 NodeImpl parent = (NodeImpl) this.getParent(); 372 StringIterator parentPaths = parent.getPaths(); 373 EntityCollection paths = new EntityCollection(); 374 while(parentPaths.hasNext()) 375 paths.add(new ItemLocation(parentPaths.nextString()+"/"+getName()).getPath()); 376 377 return paths; 378 379 } catch (Exception e) { 380 log.error("Property.getPaths() Error while obtaining Parent for "+this+" Reason:"+e); 381 e.printStackTrace(); 382 throw new RuntimeException ("Property.getPaths() Error while obtaining Parent for "+this+" Reason:"+e); 383 } 385 386 } 387 388 389 391 public int getType() { 392 if(values[0] == null) 394 return PropertyType.UNDEFINED; 395 else 396 return values[0].getType(); 397 } 398 399 400 public String toString() { 401 String strValues = ""; 402 try { 403 Value[] newValues = PropertyConvertor.convert(values, PropertyType.STRING); 404 for(int i=0; i<values.length; i++) { 405 strValues+=newValues[i].getString()+" "; 406 } 407 } catch (Exception e) { strValues = e.getMessage(); } 408 409 String strType; 410 try { 411 strType = PropertyType.nameFromValue(getType()); 412 } catch (Exception e) { 413 strType = "Undefined"; 414 } 415 416 return getName()+"("+strType+")=["+strValues+"]"; 417 } 418 419 } 420 | Popular Tags |