1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.portals.graffito.jcr.persistence.atomictypeconverter.impl;
19
20 import java.util.Calendar;
21
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Value;
24 import javax.jcr.ValueFactory;
25
26 import org.apache.portals.graffito.jcr.exception.IncorrectAtomicTypeException;
27 import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
28
29 /***
30 * Calendar Type Converter
31 *
32 * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
33 * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
34 */
35 public class CalendarTypeConverterImpl implements AtomicTypeConverter
36 {
37 /***
38 *
39 * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter#getValue(java.lang.Object)
40 */
41 public Value getValue(ValueFactory valueFactory, Object propValue)
42 {
43 if (propValue == null)
44 {
45 return null;
46 }
47
48 return valueFactory.createValue(((Calendar) propValue).getTimeInMillis());
49 }
50
51 /***
52 *
53 * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter#getObject(javax.jcr.Value)
54 */
55 public Object getObject(Value value)
56 {
57 try
58 {
59 Calendar calendar = Calendar.getInstance();
60 calendar.setTimeInMillis(value.getLong());
61 return calendar;
62 }
63 catch (RepositoryException e)
64 {
65 throw new IncorrectAtomicTypeException("Impossible to convert the value : " + value.toString(), e);
66 }
67 }
68
69 /***
70 *
71 * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter#getStringValue(java.lang.Object)
72 */
73 public String getStringValue(Object object)
74 {
75 Calendar calendar = (Calendar) object;
76 return new Long(calendar.getTimeInMillis()).toString();
77 }
78 }