KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > item > ValueImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.jcr.item;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.Calendar JavaDoc;
21
22 import javax.jcr.PropertyType;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.Value;
25 import javax.jcr.ValueFormatException;
26
27 import org.alfresco.jcr.session.SessionImpl;
28 import org.alfresco.jcr.util.JCRProxyFactory;
29 import org.alfresco.service.cmr.repository.ContentReader;
30
31
32 /**
33  * Alfresco implementation of JCR Value
34  *
35  * @author David Caruana
36  */

37 public class ValueImpl implements Value
38 {
39     private enum ValueState {Stream, Value, None};
40     private ValueState state = ValueState.None;
41     
42     private SessionImpl session;
43     private int datatype;
44     private Object JavaDoc value;
45     private InputStream JavaDoc stream = null;
46     
47     private Value proxy;
48     
49
50     /**
51      * Constuct
52      *
53      * @param value value to wrap
54      */

55     public ValueImpl(SessionImpl session, int datatype, Object JavaDoc value)
56     {
57         this.session = session;
58         this.datatype = datatype;
59         this.value = value;
60     }
61     
62     /**
63      * Create a proxied JCR Value
64      *
65      * @return the proxied value
66      */

67     public Value getProxy()
68     {
69         if (proxy == null)
70         {
71             proxy = (Value)JCRProxyFactory.create(this, Value.class, session);
72         }
73         return proxy;
74     }
75     
76     /* (non-Javadoc)
77      * @see javax.jcr.Value#getString()
78      */

79     public String JavaDoc getString() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
80     {
81         isValidState(ValueState.Value);
82         String JavaDoc typedValue = session.getTypeConverter().stringValue(getInternalValue());
83         value = typedValue;
84         enterState(ValueState.Value);
85         return typedValue;
86     }
87
88     /* (non-Javadoc)
89      * @see javax.jcr.Value#getStream()
90      */

91     public InputStream JavaDoc getStream() throws IllegalStateException JavaDoc, RepositoryException
92     {
93         isValidState(ValueState.Stream);
94         if (stream == null)
95         {
96             stream = session.getTypeConverter().streamValue(value);
97         }
98         enterState(ValueState.Stream);
99         return stream;
100     }
101
102     /* (non-Javadoc)
103      * @see javax.jcr.Value#getLong()
104      */

105     public long getLong() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
106     {
107         isValidState(ValueState.Value);
108         long typedValue = session.getTypeConverter().longValue(getInternalValue());
109         value = typedValue;
110         enterState(ValueState.Value);
111         return typedValue;
112     }
113
114     /* (non-Javadoc)
115      * @see javax.jcr.Value#getDouble()
116      */

117     public double getDouble() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
118     {
119         isValidState(ValueState.Value);
120         double typedValue = session.getTypeConverter().doubleValue(getInternalValue());
121         value = typedValue;
122         enterState(ValueState.Value);
123         return typedValue;
124     }
125
126     /* (non-Javadoc)
127      * @see javax.jcr.Value#getDate()
128      */

129     public Calendar JavaDoc getDate() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
130     {
131         isValidState(ValueState.Value);
132         Calendar JavaDoc typedValue = session.getTypeConverter().dateValue(getInternalValue());
133         value = typedValue.getTime();
134         enterState(ValueState.Value);
135         return typedValue;
136     }
137
138     /* (non-Javadoc)
139      * @see javax.jcr.Value#getBoolean()
140      */

141     public boolean getBoolean() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
142     {
143         isValidState(ValueState.Value);
144         boolean typedValue = session.getTypeConverter().booleanValue(getInternalValue());
145         value = typedValue;
146         enterState(ValueState.Value);
147         return typedValue;
148     }
149
150     /* (non-Javadoc)
151      * @see javax.jcr.Value#getType()
152      */

153     public int getType()
154     {
155         return datatype;
156     }
157     
158     /**
159      * Get value
160      *
161      * @param value the value wrapper to extract from
162      * @return the value
163      */

164     public static Object JavaDoc getValue(Value value) throws RepositoryException
165     {
166         Object JavaDoc objValue = null;
167         int valueType = value.getType();
168         
169         switch(valueType)
170         {
171             case PropertyType.STRING:
172             case PropertyType.NAME:
173             case PropertyType.PATH:
174                 objValue = value.getString();
175                 break;
176             case PropertyType.LONG:
177                 objValue = value.getLong();
178                 break;
179             case PropertyType.DOUBLE:
180                 objValue = value.getDouble();
181                 break;
182             case PropertyType.BOOLEAN:
183                 objValue = value.getBoolean();
184                 break;
185             case PropertyType.DATE:
186                 objValue = value.getDate();
187                 break;
188             case PropertyType.BINARY:
189                 objValue = value.getStream();
190                 break;
191             default:
192                 // Note: just take the internal value
193
objValue = ((ValueImpl)value).value;
194                 break;
195         }
196         
197         return objValue;
198     }
199     
200     /**
201      * Get typed value
202      *
203      * @param value the value to extract from
204      * @return the wrapped object
205      */

206     public static Object JavaDoc getValue(JCRTypeConverter typeConverter, int requiredType, Value value) throws RepositoryException
207     {
208         Object JavaDoc objValue = null;
209         
210         switch(requiredType)
211         {
212             case PropertyType.STRING:
213                 objValue = value.getString();
214                 break;
215             case PropertyType.LONG:
216                 objValue = value.getLong();
217                 break;
218             case PropertyType.DOUBLE:
219                 objValue = value.getDouble();
220                 break;
221             case PropertyType.BOOLEAN:
222                 objValue = value.getBoolean();
223                 break;
224             case PropertyType.DATE:
225                 objValue = value.getDate();
226                 break;
227             case PropertyType.BINARY:
228                 objValue = value.getStream();
229                 break;
230             case PropertyType.NAME:
231                 objValue = typeConverter.nameValue(ValueImpl.getValue(value));
232                 break;
233             case PropertyType.PATH:
234                 objValue = typeConverter.pathValue(ValueImpl.getValue(value));
235                 break;
236             default:
237                 throw new ValueFormatException("Unsupported Value Type " + requiredType);
238         }
239         
240         return objValue;
241     }
242     
243     /**
244      * Retrieve Value
245      *
246      * Note: When retrieving non stream values against a backed stream, the content reader
247      * has to be re-created.
248      *
249      * @return the value
250      */

251     private Object JavaDoc getInternalValue()
252     {
253         if (value instanceof ContentReader && state == ValueState.Value)
254         {
255             value = ((ContentReader)value).getReader();
256         }
257         return value;
258     }
259     
260     /**
261      * Check for valid state
262      *
263      * @param state the state to check
264      * @throws IllegalStateException state is not valid
265      */

266     private void isValidState(ValueState state)
267     {
268         if (this.state != ValueState.None && this.state != state)
269         {
270             throw new IllegalStateException JavaDoc("This value has already been retrieved as a " + state + " and cannot be retrieved as a " + ValueState.Stream + ".");
271         }
272     }
273     
274     /**
275      * Enter state
276      *
277      * @param state the state to enter
278      */

279     private void enterState(ValueState state)
280     {
281         this.state = state;
282     }
283
284     @Override JavaDoc
285     public boolean equals(Object JavaDoc obj)
286     {
287         if (obj == this)
288         {
289             return true;
290         }
291         if (!(obj instanceof ValueImpl))
292         {
293             return false;
294         }
295         ValueImpl other = (ValueImpl)obj;
296
297         // check data type first
298
if (datatype != other.datatype)
299         {
300             return false;
301         }
302         
303         // handle case where values are content streams
304
if (value instanceof ContentReader)
305         {
306             String JavaDoc thisUrl = ((ContentReader)value).getContentUrl();
307             String JavaDoc otherUrl = ((ContentReader)other).getContentUrl();
308             return thisUrl.equals(otherUrl);
309         }
310
311         // handle other value types
312
return value.equals(other.value);
313     }
314
315     @Override JavaDoc
316     public int hashCode()
317     {
318         return value.hashCode() * 32 + datatype;
319     }
320
321 }
322
Popular Tags