KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Date JavaDoc;
22
23 import javax.jcr.RepositoryException;
24 import javax.jcr.ValueFormatException;
25
26 import org.alfresco.jcr.session.SessionImpl;
27 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
28 import org.alfresco.service.cmr.repository.NodeRef;
29 import org.alfresco.service.cmr.repository.Path;
30 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
31 import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
32 import org.alfresco.service.cmr.repository.datatype.TypeConverter;
33 import org.alfresco.service.namespace.NamespaceException;
34 import org.alfresco.service.namespace.QName;
35
36
37 /**
38  * Responsible for converting Alfresco values to JCR values.
39  *
40  * @author David Caruana
41  *
42  */

43 public class JCRTypeConverter
44 {
45     private SessionTypeConverter jcrTypeConverter;
46     
47     /**
48      * Construct
49      *
50      * @param session
51      */

52     public JCRTypeConverter(SessionImpl session)
53     {
54         this.jcrTypeConverter = new SessionTypeConverter(session);
55     }
56     
57     /**
58      * Get the underlying Converter
59      *
60      * @return type converter
61      */

62     public TypeConverter getConverter()
63     {
64         return jcrTypeConverter;
65     }
66     
67     
68     /**
69      * Convert to JCR Reference Value
70      *
71      * @param session
72      * @param value
73      * @return
74      * @throws ValueFormatException
75      * @throws RepositoryException
76      */

77     public NodeImpl referenceValue(Object JavaDoc value) throws ValueFormatException, RepositoryException
78     {
79         NodeRef nodeRef = (NodeRef)convert(NodeRef.class, value);
80         return new NodeImpl(jcrTypeConverter.getSession(), nodeRef);
81     }
82         
83     /**
84      * Convert to JCR String Value
85      *
86      * @param value
87      * @return
88      * @throws ValueFormatException
89      * @throws RepositoryException
90      */

91     public String JavaDoc stringValue(Object JavaDoc value) throws ValueFormatException, RepositoryException
92     {
93         return (String JavaDoc)convert(String JavaDoc.class, value);
94     }
95
96     /**
97      * Convert to JCR Stream Value
98      *
99      * @param value
100      * @return
101      * @throws IllegalStateException
102      * @throws RepositoryException
103      */

104     public InputStream JavaDoc streamValue(Object JavaDoc value) throws IllegalStateException JavaDoc, RepositoryException
105     {
106         return (InputStream JavaDoc)convert(InputStream JavaDoc.class, value);
107     }
108
109     /**
110      * Convert to JCR Long Value
111      *
112      * @param value
113      * @return
114      * @throws ValueFormatException
115      * @throws IllegalStateException
116      * @throws RepositoryException
117      */

118     public long longValue(Object JavaDoc value) throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
119     {
120         try
121         {
122             return jcrTypeConverter.longValue(value);
123         }
124         catch(Exception JavaDoc e)
125         {
126             translateException(e);
127             throw new RepositoryException(e);
128         }
129     }
130
131     /**
132      * Convert to JCR Double Value
133      *
134      * @param value
135      * @return
136      * @throws ValueFormatException
137      * @throws IllegalStateException
138      * @throws RepositoryException
139      */

140     public double doubleValue(Object JavaDoc value) throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
141     {
142         try
143         {
144             return jcrTypeConverter.doubleValue(value);
145         }
146         catch(Exception JavaDoc e)
147         {
148             translateException(e);
149             throw new RepositoryException(e);
150         }
151     }
152
153     /**
154      * Convert to JCR Date Value
155      *
156      * @param value
157      * @return
158      * @throws ValueFormatException
159      * @throws IllegalStateException
160      * @throws RepositoryException
161      */

162     public Calendar JavaDoc dateValue(Object JavaDoc value) throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
163     {
164         Date JavaDoc date = (Date JavaDoc)convert(Date JavaDoc.class, value);
165         Calendar JavaDoc calendar = Calendar.getInstance();
166         calendar.setTime(date);
167         return calendar;
168     }
169
170     /**
171      * Convert to JCR Boolean Value
172      *
173      * @param value
174      * @return
175      * @throws ValueFormatException
176      * @throws IllegalStateException
177      * @throws RepositoryException
178      */

179     public boolean booleanValue(Object JavaDoc value) throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
180     {
181         try
182         {
183             return jcrTypeConverter.booleanValue(value);
184         }
185         catch(Exception JavaDoc e)
186         {
187             translateException(e);
188             throw new RepositoryException(e);
189         }
190     }
191     
192     /**
193      * Convert to JCR Name Value
194      *
195      * @param value
196      * @return
197      * @throws ValueFormatException
198      * @throws IllegalStateException
199      * @throws RepositoryException
200      */

201     public QName nameValue(Object JavaDoc value) throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
202     {
203         return convert(QName.class, value);
204     }
205
206     /**
207      * Convert to JCR Path Value
208      *
209      * @param value
210      * @return
211      * @throws ValueFormatException
212      * @throws IllegalStateException
213      * @throws RepositoryException
214      */

215     public Path pathValue(Object JavaDoc value) throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException
216     {
217         return convert(Path.class, value);
218     }
219
220     
221     /**
222      * General conversion method using JCR converters
223      *
224      * @param propertyType datatype to convert to
225      * @param value the value to convert
226      * @return the converted value
227      * @throws RepositoryException
228      */

229     public final Object JavaDoc convert(DataTypeDefinition propertyType, Object JavaDoc value)
230         throws RepositoryException
231     {
232         try
233         {
234             return jcrTypeConverter.convert(propertyType, value);
235         }
236         catch(Exception JavaDoc e)
237         {
238             translateException(e);
239             throw new RepositoryException(e);
240         }
241     }
242     
243     /**
244      * General conversion method using JCR converters
245      *
246      * @param <T> class
247      * @param c class
248      * @param value value to convert
249      * @return converted value
250      * @throws RepositoryException
251      */

252     public final <T> T convert(Class JavaDoc<T> c, Object JavaDoc value)
253         throws RepositoryException
254     {
255         try
256         {
257             return jcrTypeConverter.convert(c, value);
258         }
259         catch(Exception JavaDoc e)
260         {
261             translateException(e);
262             throw new RepositoryException(e);
263         }
264     }
265     
266     /**
267      * Catch and translate value conversion errors
268      *
269      * @param e exception to translate
270      * @throws ValueFormatException value formatting exception
271      */

272     private static void translateException(Exception JavaDoc e) throws ValueFormatException
273     {
274         if (e instanceof TypeConversionException ||
275             e instanceof NumberFormatException JavaDoc)
276         {
277             throw new ValueFormatException(e);
278         }
279     }
280
281
282     /**
283      * Data Type Converter that takes into account JCR session context
284      *
285      * @author David Caruana
286      */

287     private static class SessionTypeConverter extends TypeConverter
288     {
289         private SessionImpl session;
290         
291         /**
292          * Construct
293          *
294          * @param session session context
295          */

296         public SessionTypeConverter(SessionImpl session)
297         {
298             this.session = session;
299             
300             
301             /**
302              * Converter to translating string to QName as prefix:localName
303              */

304             addConverter(String JavaDoc.class, QName.class, new TypeConverter.Converter<String JavaDoc, QName>()
305             {
306                 public QName convert(String JavaDoc source)
307                 {
308                     try
309                     {
310                         return QName.createQName(source, SessionTypeConverter.this.session.getNamespaceResolver());
311                     }
312                     catch(NamespaceException e)
313                     {
314                         throw new TypeConversionException("Cannot convert " + source + " to qualified name", e);
315                     }
316                 }
317             });
318
319             /**
320              * Converter to translating string to QName as prefix:localName
321              */

322             addConverter(String JavaDoc.class, Path.class, new TypeConverter.Converter<String JavaDoc, Path>()
323             {
324                 public Path convert(String JavaDoc source)
325                 {
326                     try
327                     {
328                         return new JCRPath(SessionTypeConverter.this.session.getNamespaceResolver(), source).getPath();
329                     }
330                     catch(NamespaceException e)
331                     {
332                         throw new TypeConversionException("Cannot convert " + source + " to qualified name", e);
333                     }
334                 }
335             });
336
337             /**
338              * Converter for translating QName to string as prefix:localName
339              */

340             addConverter(QName.class, String JavaDoc.class, new TypeConverter.Converter<QName, String JavaDoc>()
341             {
342                 public String JavaDoc convert(QName source)
343                 {
344                     try
345                     {
346                         return source.toPrefixString(SessionTypeConverter.this.session.getNamespaceResolver());
347                     }
348                     catch(NamespaceException e)
349                     {
350                         throw new TypeConversionException("Cannot convert " + source + " to qualified name", e);
351                     }
352                 }
353             });
354
355             /**
356              * Converter for translating Path to string as prefix:localName
357              */

358             addConverter(Path.class, String JavaDoc.class, new TypeConverter.Converter<Path, String JavaDoc>()
359             {
360                 public String JavaDoc convert(Path source)
361                 {
362                     try
363                     {
364                         return source.toPrefixString(SessionTypeConverter.this.session.getNamespaceResolver());
365                     }
366                     catch(NamespaceException e)
367                     {
368                         throw new TypeConversionException("Cannot convert " + source + " to qualified name", e);
369                     }
370                 }
371             });
372             
373             /**
374              * Converter for translating Node Ref to JCR Id
375              */

376             addConverter(NodeRef.class, String JavaDoc.class, new TypeConverter.Converter<NodeRef, String JavaDoc>()
377             {
378                 public String JavaDoc convert(NodeRef source)
379                 {
380                     return source.getId();
381                 }
382             });
383         }
384
385         /**
386          * Get the session
387          *
388          * @return session
389          */

390         public SessionImpl getSession()
391         {
392             return session;
393         }
394         
395         /* (non-Javadoc)
396          * @see org.alfresco.service.cmr.repository.datatype.TypeConverter#getConverter(java.lang.Class, java.lang.Class)
397          */

398         @Override JavaDoc
399         @SuppressWarnings JavaDoc("unchecked")
400         public <F, T> Converter getConverter(Class JavaDoc<F> source, Class JavaDoc<T> dest)
401         {
402             Converter converter = super.getConverter(source, dest);
403             if (converter == null)
404             {
405                 converter = DefaultTypeConverter.INSTANCE.getConverter(source, dest);
406                 if (converter instanceof DynamicTwoStageConverter)
407                 {
408                     DynamicTwoStageConverter dynamic = (DynamicTwoStageConverter)converter;
409                     converter = addDynamicTwoStageConverter(dynamic.getFrom(), dynamic.getIntermediate(), dynamic.getTo());
410                 }
411             }
412             return converter;
413         }
414     }
415     
416 }
417
Popular Tags