KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > core > PropertyImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.jcr.impl.core;
7
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.util.Calendar JavaDoc;
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 /**
40  * Created by The eXo Platform SARL .
41  *
42  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
43  * @version $Id: PropertyImpl.java,v 1.17 2004/11/02 18:36:33 geaz Exp $
44  */

45
46 public class PropertyImpl extends ItemImpl implements Property {
47
48
49 // protected int type;
50
protected Value[] values = new Value[1];
51
52   public PropertyImpl(String JavaDoc 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 // this.type = type;
60

61   }
62
63   public PropertyImpl(String JavaDoc 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 // this.values = values;
71
// this.type = type;
72

73   }
74
75   // Deep copy
76
public PropertyImpl(PropertyImpl prop) throws PathNotFoundException, RepositoryException {
77
78     super(prop.getPath());
79 // System.out.println(">>>>>>>>>> "+prop);
80

81     this.values = PropertyConvertor.convert(prop.getValues(), prop.getType());
82 // this.type = prop.type;
83
}
84
85   /**
86    * 6.2.3
87    * Returns the value of the Property as a generic Value
88    * object or null if the Property has no value.
89    */

90   public Value getValue() {
91     if (values.length == 0)
92       return null;
93     return this.values[0];
94   }
95
96   /**
97    * Returns an array of all the values of this property. Used to access
98    * multi-value properties.
99    *
100    * @return a <code>Value</code> array
101    * @throws RepositoryException if an error occurs.
102    */

103   public Value[] getValues() throws RepositoryException {
104     return this.values;
105   }
106
107   /**
108    * 6.2.3
109    * Returns a String representation of the value of this
110    * Property. If conversion is not possible or the
111    * property has no value, an empty string is returned.
112    * This behavior is in contrast to Value.getString(),
113    * which throws an exception in these circumstances.
114    */

115   public String JavaDoc getString() {
116     try {
117       return values[0].getString();
118     } catch (Exception JavaDoc e) {
119       return "";
120     }
121   }
122
123   /**
124    * 6.3.2
125    * Returns a double representation of the value of this
126    * Property. If conversion is not possible or the
127    * property has no value, 0.0 is returned. This behavior
128    * is in contrast to Value.getDouble(), which throws an
129    * exception in these circumstances.
130    */

131   public double getDouble() {
132     try {
133       return values[0].getDouble();
134     } catch (Exception JavaDoc e) {
135       return 0.0;
136     }
137   }
138
139   /**
140    * 6.3.2
141    * Returns a long representation of the value of this
142    * Property. If conversion is not possible or the
143    * property has no value, 0 is returned. This behavior is
144    * in contrast to Value.getLong(), which throws an
145    * exception in these circumstances.
146    */

147   public long getLong() {
148     try {
149       return values[0].getLong();
150     } catch (Exception JavaDoc e) {
151       return 0l;
152     }
153
154   }
155
156   /**
157    * 6.3.2
158    * Returns an InputStream representation of the value
159    * of this Property. If conversion is not possible or the
160    * property has no value, an empty stream is returned.
161    */

162   public InputStream JavaDoc getStream() {
163     try {
164       return values[0].getStream();
165     } catch (Exception JavaDoc e) {
166       return new ByteArrayInputStream JavaDoc(new byte[0]);
167     }
168
169   }
170
171   /**
172    * 6.3.2
173    * Returns a Calendar representation of the value of this
174    * Property. If conversion is not possible or the
175    * property has no value, a Calendar value of 1970-01-01T00:00:00Z is returned.
176    */

177   public Calendar JavaDoc getDate() {
178     try {
179       return values[0].getDate();
180     } catch (Exception JavaDoc e) {
181       return ISO8601.parse("1970-01-01T00:00:00.00Z");
182     }
183
184   }
185
186   /**
187    * 6.3.2
188    * Returns a boolean representation of the value of this
189    * Property. If conversion is not possible or the
190    * property has no value, a value of false is returned.
191    */

192   public boolean getBoolean() {
193     try {
194       return values[0].getBoolean();
195     } catch (Exception JavaDoc e) {
196       return false;
197     }
198   }
199
200   /**
201    * 6.3.2
202    * Returns true if this property has a value. Returns false otherwise.
203    */

204   public boolean hasValue() {
205     if (values.length == 0 || values[0] == null || getLength() == 0)
206       return false;
207     return true;
208   }
209
210   /**
211    * 6.3.2
212    * Returns the length of the value of this property in
213    * bytes if the value is a PropertyType.BINARY,
214    * otherwise it returns the number of characters needed
215    * to display the value (for strings this is the string
216    * length, for numeric types it is the number of
217    * characters needed to display the number). Returns �1
218    * if the implementation cannot determine the length.
219    * Returns 0 if the property has no value.
220    */

221   public long getLength() {
222     try {
223       return values[0].getString().length();
224     } catch (IllegalStateException JavaDoc e) {
225       try {
226         return getStream().available();
227       } catch (IOException JavaDoc ioe) {
228         return -1;
229       }
230     } catch (Exception JavaDoc e) {
231       return -1;
232     }
233   }
234
235   /**
236    * 6.3.2
237    * Sets the value of this Property to value. If this property's
238    * property type is not constrained by the node type of its
239    * parent node, then the property type is changed to that of the
240    * supplied value. If the property type is constrained, then a
241    * best-effort conversion is attempted. If conversion fails, a
242    * ConstraintViolationException is thrown on the next save.
243    */

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   /**
252    * Sets the value of this <code>Property</code> to the array <code>values</code>.
253    * If this property's property type is not constrained by the node type of
254    * its parent node, then the property type is changed to that of the supplied
255    * <code>value</code>. If the property type is constrained, then a
256    * best-effort conversion is attempted. If conversion fails, a
257    * <code>ValueFormatException</code> is thrown immediately (not on <code>save</code>).
258    * If this property is not a multi-valued then a ValueFormatException is
259    * thrown immediately. The change will be persisted (if valid) on <code>save</code>
260    *
261    * @param values The new value to set the property to.
262    * @throws ValueFormatException if the type or format of the specified value
263    * is incompatible with the <code>type</code> of this property.
264    * @throws RepositoryException if another error occurs.
265    */

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   /**
287    * 6.3.2
288    * Same as setValue(Value value) except that the value is specified as a String.
289    */

290   public void setValue(String JavaDoc value) throws ValueFormatException, RepositoryException {
291     setValue(new StringValue(value));
292   }
293
294   /**
295    * 6.3.2
296    * Same as setValue(Value value) except that the value is specified as a InputStream.
297    */

298   public void setValue(InputStream JavaDoc stream) throws ValueFormatException, RepositoryException {
299     setValue(new BinaryValue(stream));
300   }
301
302   /**
303    * 6.3.2
304    */

305   public void setValue(double number) throws ValueFormatException, RepositoryException {
306     setValue(new DoubleValue(number));
307   }
308
309   /**
310    * 6.3.2
311    */

312   public void setValue(long number) throws ValueFormatException, RepositoryException {
313     setValue(new LongValue(number));
314   }
315
316   /**
317    * 6.3.2
318    */

319   public void setValue(Calendar JavaDoc date) throws ValueFormatException, RepositoryException {
320     setValue(new DateValue(date));
321   }
322
323   /**
324    * 6.3.2
325    */

326   public void setValue(boolean b) throws ValueFormatException, RepositoryException {
327     setValue(new BooleanValue(b));
328   }
329
330   public PropertyDef getDefinition() /*throws ItemNotFoundException , AccessDeniedException*/ {
331
332     NodeType parentType;
333     try {
334       parentType = getParent().getPrimaryNodeType();
335     } catch (Exception JavaDoc e) {
336       e.printStackTrace();
337       throw new RuntimeException JavaDoc(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     // Residual def
345
return new PropertyDefImpl(parentType.getName());
346   }
347
348
349  //////////////////// Item implementation ////////////////////
350
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 JavaDoc e) {
380         log.error("Property.getPaths() Error while obtaining Parent for "+this+" Reason:"+e);
381         e.printStackTrace();
382         throw new RuntimeException JavaDoc("Property.getPaths() Error while obtaining Parent for "+this+" Reason:"+e);
383 // return new EntityCollection();
384
}
385
386   }
387
388
389   //////////////////////////////////
390

391   public int getType() {
392 // return type;
393
if(values[0] == null)
394         return PropertyType.UNDEFINED;
395     else
396         return values[0].getType();
397   }
398
399
400   public String JavaDoc toString() {
401     String JavaDoc 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 JavaDoc e) { strValues = e.getMessage(); }
408
409     String JavaDoc strType;
410     try {
411        strType = PropertyType.nameFromValue(getType());
412     } catch (Exception JavaDoc e) {
413        strType = "Undefined";
414     }
415
416     return getName()+"("+strType+")=["+strValues+"]";
417   }
418
419 }
420
Popular Tags