1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.portals.graffito.jcr.persistence.collectionconverter.impl; |
18 |
|
|
19 |
|
|
20 |
|
import java.util.Iterator; |
21 |
|
import java.util.Map; |
22 |
|
|
23 |
|
import javax.jcr.Node; |
24 |
|
import javax.jcr.Property; |
25 |
|
import javax.jcr.RepositoryException; |
26 |
|
import javax.jcr.Session; |
27 |
|
import javax.jcr.Value; |
28 |
|
import javax.jcr.ValueFactory; |
29 |
|
import javax.jcr.ValueFormatException; |
30 |
|
|
31 |
|
import org.apache.portals.graffito.jcr.exception.PersistenceException; |
32 |
|
import org.apache.portals.graffito.jcr.mapper.Mapper; |
33 |
|
import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor; |
34 |
|
import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter; |
35 |
|
import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection; |
36 |
|
import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollectionUtil; |
37 |
|
import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter; |
38 |
|
import org.apache.portals.graffito.jcr.reflection.ReflectionUtils; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
public class MultiValueCollectionConverterImpl extends AbstractCollectionConverterImpl { |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
public MultiValueCollectionConverterImpl(Map atomicTypeConverters, |
58 |
|
ObjectConverter objectConverter, |
59 |
|
Mapper mapper) { |
60 |
0 |
super(atomicTypeConverters, objectConverter, mapper); |
61 |
0 |
} |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
protected void doInsertCollection(Session session, |
68 |
|
Node parentNode, |
69 |
|
CollectionDescriptor collectionDescriptor, |
70 |
|
ManageableCollection collection) throws RepositoryException { |
71 |
|
try { |
72 |
0 |
if (collection == null) { |
73 |
0 |
return; |
74 |
|
} |
75 |
|
|
76 |
0 |
String jcrName = getCollectionJcrName(collectionDescriptor); |
77 |
0 |
Value[] values = new Value[collection.getSize()]; |
78 |
0 |
ValueFactory valueFactory = session.getValueFactory(); |
79 |
0 |
Iterator collectionIterator = collection.getIterator(); |
80 |
0 |
for (int i = 0; i < collection.getSize(); i++) { |
81 |
0 |
Object fieldValue = collectionIterator.next(); |
82 |
0 |
AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters |
83 |
|
.get(fieldValue.getClass()); |
84 |
0 |
values[i] = atomicTypeConverter.getValue(valueFactory, fieldValue); |
85 |
|
} |
86 |
|
|
87 |
0 |
parentNode.setProperty(jcrName, values); |
88 |
|
} |
89 |
0 |
catch(ValueFormatException vfe) { |
90 |
0 |
throw new PersistenceException("Cannot insert collection field : " |
91 |
|
+ collectionDescriptor.getFieldName() |
92 |
|
+ " of class " |
93 |
|
+ collectionDescriptor.getClassDescriptor().getClassName(), vfe); |
94 |
0 |
} |
95 |
0 |
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
protected void doUpdateCollection(Session session, |
102 |
|
Node parentNode, |
103 |
|
CollectionDescriptor collectionDescriptor, |
104 |
|
ManageableCollection collection) throws RepositoryException { |
105 |
0 |
String jcrName = getCollectionJcrName(collectionDescriptor); |
106 |
|
|
107 |
|
|
108 |
0 |
if (parentNode.hasProperty(jcrName)) { |
109 |
0 |
parentNode.setProperty(jcrName, (Value[]) null); |
110 |
|
} |
111 |
|
|
112 |
0 |
if (collection == null) { |
113 |
0 |
return; |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
0 |
Value[] values = new Value[collection.getSize()]; |
119 |
0 |
ValueFactory valueFactory = session.getValueFactory(); |
120 |
0 |
int i = 0; |
121 |
0 |
for (Iterator collectionIterator = collection.getIterator(); collectionIterator.hasNext(); i++) { |
122 |
0 |
Object fieldValue = collectionIterator.next(); |
123 |
0 |
AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters |
124 |
|
.get(fieldValue.getClass()); |
125 |
0 |
values[i] = atomicTypeConverter.getValue(valueFactory, fieldValue); |
126 |
|
} |
127 |
|
|
128 |
0 |
parentNode.setProperty(jcrName, values); |
129 |
0 |
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
protected ManageableCollection doGetCollection(Session session, |
135 |
|
Node parentNode, |
136 |
|
CollectionDescriptor collectionDescriptor, |
137 |
|
Class collectionFieldClass) throws RepositoryException { |
138 |
|
try { |
139 |
0 |
String jcrName = getCollectionJcrName(collectionDescriptor); |
140 |
0 |
if (!parentNode.hasProperty(jcrName)) { |
141 |
0 |
return null; |
142 |
|
} |
143 |
0 |
Property property = parentNode.getProperty(jcrName); |
144 |
0 |
Value[] values = property.getValues(); |
145 |
|
|
146 |
0 |
ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass); |
147 |
0 |
String elementClassName = collectionDescriptor.getElementClassName(); |
148 |
0 |
Class elementClass = ReflectionUtils.forName(elementClassName); |
149 |
0 |
for (int i = 0; i < values.length; i++) { |
150 |
0 |
AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters |
151 |
|
.get(elementClass); |
152 |
0 |
collection.addObject(atomicTypeConverter.getObject(values[i])); |
153 |
|
} |
154 |
|
|
155 |
0 |
return collection; |
156 |
|
} |
157 |
0 |
catch(ValueFormatException vfe) { |
158 |
0 |
throw new PersistenceException("Cannot get the collection field : " |
159 |
|
+ collectionDescriptor.getFieldName() |
160 |
|
+ "for class " + collectionDescriptor.getClassDescriptor().getClassName(), |
161 |
|
vfe); |
162 |
|
} |
163 |
|
} |
164 |
|
} |