Revision 1861 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/impl/AbstractPersistentState.java
AbstractPersistentState.java | ||
---|---|---|
25 | 25 |
* AUTHORS (In addition to CIT): |
26 | 26 |
* 2009 IVER T.I {{Task}} |
27 | 27 |
*/ |
28 |
|
|
29 | 28 |
package org.gvsig.tools.persistence.impl; |
30 | 29 |
|
31 | 30 |
import org.gvsig.tools.util.FileTools; |
... | ... | |
75 | 74 |
import org.slf4j.LoggerFactory; |
76 | 75 |
|
77 | 76 |
public abstract class AbstractPersistentState implements |
78 |
PersistentStateServices {
|
|
77 |
PersistentStateServices {
|
|
79 | 78 |
|
80 |
private static final Logger logger = LoggerFactory.getLogger(PersistentState.class);
|
|
79 |
private static final Logger logger = LoggerFactory.getLogger(PersistentState.class);
|
|
81 | 80 |
|
82 |
private static final Pattern VALID_NAME_PATTERN = Pattern
|
|
83 |
.compile("[\\w][\\d\\w_]*" |
|
84 |
); |
|
81 |
private static final Pattern VALID_NAME_PATTERN = Pattern
|
|
82 |
.compile("[\\w][\\d\\w_]*"
|
|
83 |
);
|
|
85 | 84 |
|
86 |
private static final List validStdTypeClasses = Collections.unmodifiableList(
|
|
87 |
Arrays.asList( |
|
88 |
new Class[] { // All need to be immutable
|
|
89 |
Integer.class, Long.class, Float.class, Double.class, |
|
90 |
Boolean.class, String.class, Date.class, |
|
91 |
File.class, URL.class, URI.class, Byte.class
|
|
92 |
} |
|
93 |
) |
|
85 |
private static final List validStdTypeClasses = Collections.unmodifiableList(
|
|
86 |
Arrays.asList(
|
|
87 |
new Class[]{ // All need to be immutable
|
|
88 |
Integer.class, Long.class, Float.class, Double.class,
|
|
89 |
Boolean.class, String.class, Date.class,
|
|
90 |
File.class, URL.class, URI.class, Byte.class
|
|
91 |
}
|
|
92 |
)
|
|
94 | 93 |
); |
95 | 94 |
|
96 |
private Map values = new LinkedHashMap();
|
|
97 |
private final PersistentContextServices context;
|
|
98 |
private final PersistentIdentifier id;
|
|
99 |
private final PersistenceManagerServices manager;
|
|
100 |
private String theClassName = null;
|
|
101 |
private PersistenceFactory factory;
|
|
95 |
private Map values = new LinkedHashMap();
|
|
96 |
private final PersistentContextServices context;
|
|
97 |
private final PersistentIdentifier id;
|
|
98 |
private final PersistenceManagerServices manager;
|
|
99 |
private String theClassName = null;
|
|
100 |
private PersistenceFactory factory;
|
|
102 | 101 |
|
103 |
public AbstractPersistentState(PersistenceManagerServices manager,
|
|
104 |
PersistentContextServices context, PersistentIdentifier id) {
|
|
105 |
this.manager = manager;
|
|
106 |
this.context = context;
|
|
107 |
this.id = id;
|
|
108 |
values = new HashMap();
|
|
109 |
}
|
|
102 |
public AbstractPersistentState(PersistenceManagerServices manager,
|
|
103 |
PersistentContextServices context, PersistentIdentifier id) {
|
|
104 |
this.manager = manager;
|
|
105 |
this.context = context;
|
|
106 |
this.id = id;
|
|
107 |
values = new HashMap();
|
|
108 |
}
|
|
110 | 109 |
|
111 | 110 |
@Override |
112 |
public void setFactory(PersistenceFactory factory) {
|
|
113 |
this.factory = factory;
|
|
114 |
}
|
|
111 |
public void setFactory(PersistenceFactory factory) {
|
|
112 |
this.factory = factory;
|
|
113 |
}
|
|
115 | 114 |
|
116 | 115 |
@Override |
117 |
public DynStruct getDefinition() {
|
|
118 |
if(this.factory == null){
|
|
119 |
return null;
|
|
120 |
}
|
|
121 |
DynStruct definition = this.factory.getDefinition(this.theClassName);
|
|
122 |
return definition;
|
|
123 |
}
|
|
116 |
public DynStruct getDefinition() {
|
|
117 |
if (this.factory == null) {
|
|
118 |
return null;
|
|
119 |
}
|
|
120 |
DynStruct definition = this.factory.getDefinition(this.theClassName);
|
|
121 |
return definition;
|
|
122 |
}
|
|
124 | 123 |
|
125 |
private DynStruct getDefinitionCheckNull() {
|
|
126 |
DynStruct definition = getDefinition();
|
|
127 |
if (definition == null) {
|
|
128 |
throw new NullPointerException(
|
|
129 |
"Can't locate definition for the class '"
|
|
130 |
+ this.theClassName + "' in the factory '"
|
|
131 |
+ this.factory.getClass().getName()
|
|
132 |
+ "', managed classes are '"
|
|
133 |
+ this.factory.getManagedClasses().toString()
|
|
134 |
+ "'.");
|
|
135 |
}
|
|
136 |
return definition;
|
|
137 |
}
|
|
124 |
private DynStruct getDefinitionCheckNull() {
|
|
125 |
DynStruct definition = getDefinition();
|
|
126 |
if (definition == null) {
|
|
127 |
throw new NullPointerException(
|
|
128 |
"Can't locate definition for the class '"
|
|
129 |
+ this.theClassName + "' in the factory '"
|
|
130 |
+ this.factory.getClass().getName()
|
|
131 |
+ "', managed classes are '"
|
|
132 |
+ this.factory.getManagedClasses().toString()
|
|
133 |
+ "'.");
|
|
134 |
}
|
|
135 |
return definition;
|
|
136 |
}
|
|
138 | 137 |
|
139 | 138 |
@Override |
140 |
public Object get(String name) throws PersistenceException { |
|
141 |
Object ret = values.get(name); |
|
142 |
if (ret == null) { |
|
143 |
// We are asked for an attribute not defined in the dynstruct |
|
144 |
if (!values.containsKey(name) && !definitionHasField(name)) { |
|
145 |
throw new PersistenceValueNotFoundException(name); |
|
146 |
} |
|
147 |
return getDefaultValue(name); |
|
148 |
} |
|
149 |
return get(ret); |
|
150 |
} |
|
151 |
|
|
152 |
public boolean isNull(String name) { |
|
153 |
Object ret = values.get(name); |
|
154 |
return ret == null; |
|
139 |
public Object get(String name) throws PersistenceException { |
|
140 |
Object ret = values.get(name); |
|
141 |
if (ret == null) { |
|
142 |
// We are asked for an attribute not defined in the dynstruct |
|
143 |
if (!values.containsKey(name) && !definitionHasField(name)) { |
|
144 |
throw new PersistenceValueNotFoundException(name); |
|
145 |
} |
|
146 |
return getDefaultValue(name); |
|
155 | 147 |
} |
148 |
return get(ret); |
|
149 |
} |
|
156 | 150 |
|
151 |
public boolean isNull(String name) { |
|
152 |
Object ret = values.get(name); |
|
153 |
return ret == null; |
|
154 |
} |
|
155 |
|
|
157 | 156 |
@Override |
158 |
public List getList(String name) throws PersistenceException {
|
|
159 |
return (List) get(name);
|
|
160 |
}
|
|
157 |
public List getList(String name) throws PersistenceException {
|
|
158 |
return (List) get(name);
|
|
159 |
}
|
|
161 | 160 |
|
162 | 161 |
@Override |
163 |
public Map getMap(String name) throws PersistenceException {
|
|
164 |
return (Map) get(name);
|
|
165 |
}
|
|
162 |
public Map getMap(String name) throws PersistenceException {
|
|
163 |
return (Map) get(name);
|
|
164 |
}
|
|
166 | 165 |
|
167 | 166 |
@Override |
168 |
public Set getSet(String name) throws PersistenceException {
|
|
169 |
return (Set) get(name);
|
|
170 |
}
|
|
167 |
public Set getSet(String name) throws PersistenceException {
|
|
168 |
return (Set) get(name);
|
|
169 |
}
|
|
171 | 170 |
|
172 |
private Object get(Object obj) throws PersistenceException {
|
|
173 |
if (obj instanceof ObjectReference) {
|
|
174 |
ObjectReference ref = (ObjectReference) obj;
|
|
175 |
return ref.getObject();
|
|
176 |
}
|
|
177 |
if (obj instanceof Persistent) {
|
|
178 |
return obj;
|
|
179 |
}
|
|
180 |
/*
|
|
171 |
private Object get(Object obj) throws PersistenceException {
|
|
172 |
if (obj instanceof ObjectReference) {
|
|
173 |
ObjectReference ref = (ObjectReference) obj;
|
|
174 |
return ref.getObject();
|
|
175 |
}
|
|
176 |
if (obj instanceof Persistent) {
|
|
177 |
return obj;
|
|
178 |
}
|
|
179 |
/*
|
|
181 | 180 |
* |
182 |
*/
|
|
183 |
if (obj instanceof List) {
|
|
184 |
if ((manager.getFactories().get(obj)) == null) {
|
|
185 |
List result = new ArrayList(((List) obj).size());
|
|
186 |
getValues(result, (((List) obj).iterator()));
|
|
187 |
return result;
|
|
188 |
}
|
|
189 |
}
|
|
190 |
return obj;
|
|
191 |
}
|
|
181 |
*/
|
|
182 |
if (obj instanceof List) {
|
|
183 |
if ((manager.getFactories().get(obj)) == null) {
|
|
184 |
List result = new ArrayList(((List) obj).size());
|
|
185 |
getValues(result, (((List) obj).iterator()));
|
|
186 |
return result;
|
|
187 |
}
|
|
188 |
}
|
|
189 |
return obj;
|
|
190 |
}
|
|
192 | 191 |
|
193 |
private void getValues(List result, Iterator iterator)
|
|
194 |
throws PersistenceException {
|
|
195 |
while (iterator.hasNext()) {
|
|
196 |
result.add(get(iterator.next()));
|
|
197 |
}
|
|
198 |
}
|
|
192 |
private void getValues(List result, Iterator iterator)
|
|
193 |
throws PersistenceException {
|
|
194 |
while (iterator.hasNext()) {
|
|
195 |
result.add(get(iterator.next()));
|
|
196 |
}
|
|
197 |
}
|
|
199 | 198 |
|
200 | 199 |
@Override |
201 |
public boolean getBoolean(String name) throws PersistenceException {
|
|
202 |
return ((Boolean) get(name));
|
|
203 |
}
|
|
200 |
public boolean getBoolean(String name) throws PersistenceException {
|
|
201 |
return ((Boolean) get(name));
|
|
202 |
}
|
|
204 | 203 |
|
205 | 204 |
@Override |
206 |
public double getDouble(String name) throws PersistenceException {
|
|
207 |
return ((Double) get(name));
|
|
208 |
}
|
|
205 |
public double getDouble(String name) throws PersistenceException {
|
|
206 |
return ((Double) get(name));
|
|
207 |
}
|
|
209 | 208 |
|
210 | 209 |
@Override |
211 |
public File getFile(String name) throws PersistenceException {
|
|
210 |
public File getFile(String name) throws PersistenceException {
|
|
212 | 211 |
Object obj = get(name); |
213 |
if(obj instanceof String){
|
|
214 |
return new File((String)obj); |
|
212 |
if (obj instanceof String) {
|
|
213 |
return new File((String) obj);
|
|
215 | 214 |
} |
216 | 215 |
return (File) obj; |
217 |
}
|
|
216 |
}
|
|
218 | 217 |
|
219 | 218 |
@Override |
220 |
public URL getURL(String name) throws PersistenceException {
|
|
219 |
public URL getURL(String name) throws PersistenceException {
|
|
221 | 220 |
Object obj = get(name); |
222 |
if(obj instanceof String){
|
|
221 |
if (obj instanceof String) {
|
|
223 | 222 |
try { |
224 |
return new URL((String)obj); |
|
223 |
return new URL((String) obj);
|
|
225 | 224 |
} catch (MalformedURLException e) { |
226 |
throw new PersistenceException("Can't create URL from '"+(String)obj+"'", e);
|
|
225 |
throw new PersistenceException("Can't create URL from '" + (String) obj + "'", e);
|
|
227 | 226 |
} |
228 | 227 |
} |
229 | 228 |
return (URL) obj; |
230 |
}
|
|
229 |
}
|
|
231 | 230 |
|
232 | 231 |
@Override |
233 |
public URI getURI(String name) throws PersistenceException {
|
|
234 |
Object obj = get(name);
|
|
235 |
if(obj instanceof String){
|
|
236 |
try {
|
|
237 |
return new URI((String)obj); |
|
232 |
public URI getURI(String name) throws PersistenceException {
|
|
233 |
Object obj = get(name);
|
|
234 |
if (obj instanceof String) {
|
|
235 |
try {
|
|
236 |
return new URI((String) obj);
|
|
238 | 237 |
} catch (URISyntaxException e) { |
239 |
throw new PersistenceException("Can't create URI from '"+(String)obj+"'", e);
|
|
238 |
throw new PersistenceException("Can't create URI from '" + (String) obj + "'", e);
|
|
240 | 239 |
} |
241 |
}
|
|
242 |
return (URI) obj;
|
|
243 |
}
|
|
240 |
}
|
|
241 |
return (URI) obj;
|
|
242 |
}
|
|
244 | 243 |
|
245 | 244 |
@Override |
246 |
public Date getDate(String name) throws PersistenceException {
|
|
247 |
return (Date) get(name);
|
|
248 |
}
|
|
245 |
public Date getDate(String name) throws PersistenceException {
|
|
246 |
return (Date) get(name);
|
|
247 |
}
|
|
249 | 248 |
|
250 | 249 |
@Override |
251 |
public float getFloat(String name) throws PersistenceException {
|
|
252 |
return ((Float) get(name));
|
|
253 |
}
|
|
250 |
public float getFloat(String name) throws PersistenceException {
|
|
251 |
return ((Float) get(name));
|
|
252 |
}
|
|
254 | 253 |
|
255 | 254 |
@Override |
256 |
public int getInt(String name) throws PersistenceException {
|
|
257 |
return ((Integer) get(name));
|
|
258 |
}
|
|
255 |
public int getInt(String name) throws PersistenceException {
|
|
256 |
return ((Integer) get(name));
|
|
257 |
}
|
|
259 | 258 |
|
260 | 259 |
@Override |
261 |
public Iterator getIterator(String name) throws PersistenceException {
|
|
262 |
Collection collection = (Collection) get(name);
|
|
263 |
if( collection==null ) {
|
|
264 |
return null;
|
|
265 |
}
|
|
266 |
return collection.iterator();
|
|
267 |
}
|
|
260 |
public Iterator getIterator(String name) throws PersistenceException {
|
|
261 |
Collection collection = (Collection) get(name); |
|
262 |
if (collection == null) {
|
|
263 |
return null; |
|
264 |
} |
|
265 |
return collection.iterator();
|
|
266 |
}
|
|
268 | 267 |
|
269 | 268 |
@Override |
270 |
public long getLong(String name) throws PersistenceException {
|
|
271 |
return ((Long) get(name));
|
|
272 |
}
|
|
269 |
public long getLong(String name) throws PersistenceException {
|
|
270 |
return ((Long) get(name));
|
|
271 |
}
|
|
273 | 272 |
|
274 | 273 |
@Override |
275 |
public Object[] getArray(String name, Class valueClass)
|
|
276 |
throws PersistenceException {
|
|
277 |
List valueList = getList(name);
|
|
278 |
if (valueList == null) {
|
|
279 |
return null;
|
|
280 |
}
|
|
281 |
Object array = Array.newInstance(valueClass, valueList.size());
|
|
282 |
return valueList.toArray((Object[]) array);
|
|
283 |
}
|
|
274 |
public Object[] getArray(String name, Class valueClass)
|
|
275 |
throws PersistenceException {
|
|
276 |
List valueList = getList(name);
|
|
277 |
if (valueList == null) {
|
|
278 |
return null;
|
|
279 |
}
|
|
280 |
Object array = Array.newInstance(valueClass, valueList.size());
|
|
281 |
return valueList.toArray((Object[]) array);
|
|
282 |
}
|
|
284 | 283 |
|
285 | 284 |
@Override |
286 |
public boolean[] getBooleanArray(String name) throws PersistenceException {
|
|
287 |
List valueList = getList(name);
|
|
288 |
if (valueList == null) {
|
|
289 |
return null;
|
|
290 |
}
|
|
291 |
boolean[] values = new boolean[valueList.size()];
|
|
292 |
for (int i = 0; i < values.length; i++) {
|
|
293 |
values[i] = ((Boolean) valueList.get(i));
|
|
294 |
}
|
|
295 |
return values;
|
|
296 |
}
|
|
285 |
public boolean[] getBooleanArray(String name) throws PersistenceException {
|
|
286 |
List valueList = getList(name);
|
|
287 |
if (valueList == null) {
|
|
288 |
return null;
|
|
289 |
}
|
|
290 |
boolean[] values = new boolean[valueList.size()];
|
|
291 |
for (int i = 0; i < values.length; i++) {
|
|
292 |
values[i] = ((Boolean) valueList.get(i));
|
|
293 |
}
|
|
294 |
return values;
|
|
295 |
}
|
|
297 | 296 |
|
298 | 297 |
@Override |
299 |
public int[] getIntArray(String name) throws PersistenceException {
|
|
300 |
List valueList = getList(name);
|
|
301 |
if (valueList == null) {
|
|
302 |
return null;
|
|
303 |
}
|
|
304 |
int[] values = new int[valueList.size()];
|
|
305 |
for (int i = 0; i < values.length; i++) {
|
|
306 |
values[i] = ((Number) valueList.get(i)).intValue();
|
|
307 |
}
|
|
308 |
return values;
|
|
309 |
}
|
|
298 |
public int[] getIntArray(String name) throws PersistenceException {
|
|
299 |
List valueList = getList(name);
|
|
300 |
if (valueList == null) {
|
|
301 |
return null;
|
|
302 |
}
|
|
303 |
int[] values = new int[valueList.size()];
|
|
304 |
for (int i = 0; i < values.length; i++) {
|
|
305 |
values[i] = ((Number) valueList.get(i)).intValue();
|
|
306 |
}
|
|
307 |
return values;
|
|
308 |
}
|
|
310 | 309 |
|
311 | 310 |
@Override |
312 |
public long[] getLongArray(String name) throws PersistenceException {
|
|
313 |
List valueList = getList(name);
|
|
314 |
if (valueList == null) {
|
|
315 |
return null;
|
|
316 |
}
|
|
317 |
long[] values = new long[valueList.size()];
|
|
318 |
for (int i = 0; i < values.length; i++) {
|
|
319 |
values[i] = ((Number) valueList.get(i)).longValue();
|
|
320 |
}
|
|
321 |
return values;
|
|
322 |
}
|
|
311 |
public long[] getLongArray(String name) throws PersistenceException {
|
|
312 |
List valueList = getList(name);
|
|
313 |
if (valueList == null) {
|
|
314 |
return null;
|
|
315 |
}
|
|
316 |
long[] values = new long[valueList.size()];
|
|
317 |
for (int i = 0; i < values.length; i++) {
|
|
318 |
values[i] = ((Number) valueList.get(i)).longValue();
|
|
319 |
}
|
|
320 |
return values;
|
|
321 |
}
|
|
323 | 322 |
|
324 | 323 |
@Override |
325 |
public float[] getFloatArray(String name) throws PersistenceException {
|
|
326 |
List valueList = getList(name);
|
|
327 |
if (valueList == null) {
|
|
328 |
return null;
|
|
329 |
}
|
|
330 |
float[] values = new float[valueList.size()];
|
|
331 |
for (int i = 0; i < values.length; i++) {
|
|
332 |
values[i] = ((Number) valueList.get(i)).floatValue();
|
|
333 |
}
|
|
334 |
return values;
|
|
335 |
}
|
|
324 |
public float[] getFloatArray(String name) throws PersistenceException {
|
|
325 |
List valueList = getList(name);
|
|
326 |
if (valueList == null) {
|
|
327 |
return null;
|
|
328 |
}
|
|
329 |
float[] values = new float[valueList.size()];
|
|
330 |
for (int i = 0; i < values.length; i++) {
|
|
331 |
values[i] = ((Number) valueList.get(i)).floatValue();
|
|
332 |
}
|
|
333 |
return values;
|
|
334 |
}
|
|
336 | 335 |
|
337 | 336 |
@Override |
338 |
public double[] getDoubleArray(String name) throws PersistenceException {
|
|
339 |
List valueList = getList(name);
|
|
340 |
if (valueList == null) {
|
|
341 |
return null;
|
|
342 |
}
|
|
343 |
double[] values = new double[valueList.size()];
|
|
344 |
for (int i = 0; i < values.length; i++) {
|
|
345 |
values[i] = ((Number) valueList.get(i)).doubleValue();
|
|
346 |
}
|
|
347 |
return values;
|
|
348 |
}
|
|
337 |
public double[] getDoubleArray(String name) throws PersistenceException {
|
|
338 |
List valueList = getList(name);
|
|
339 |
if (valueList == null) {
|
|
340 |
return null;
|
|
341 |
}
|
|
342 |
double[] values = new double[valueList.size()];
|
|
343 |
for (int i = 0; i < values.length; i++) {
|
|
344 |
values[i] = ((Number) valueList.get(i)).doubleValue();
|
|
345 |
}
|
|
346 |
return values;
|
|
347 |
}
|
|
349 | 348 |
|
350 | 349 |
@Override |
351 |
public Date[] getDateArray(String name) throws PersistenceException {
|
|
352 |
return (Date[]) this.getArray(name, Date.class);
|
|
353 |
}
|
|
350 |
public Date[] getDateArray(String name) throws PersistenceException {
|
|
351 |
return (Date[]) this.getArray(name, Date.class);
|
|
352 |
}
|
|
354 | 353 |
|
355 | 354 |
@Override |
356 |
public String[] getStringArray(String name) throws PersistenceException {
|
|
357 |
return (String[]) this.getArray(name, String.class);
|
|
358 |
}
|
|
355 |
public String[] getStringArray(String name) throws PersistenceException {
|
|
356 |
return (String[]) this.getArray(name, String.class);
|
|
357 |
}
|
|
359 | 358 |
|
360 | 359 |
@Override |
361 |
public Iterator getNames() {
|
|
362 |
return values.keySet().iterator();
|
|
363 |
}
|
|
360 |
public Iterator getNames() {
|
|
361 |
return values.keySet().iterator();
|
|
362 |
}
|
|
364 | 363 |
|
365 | 364 |
@Override |
366 |
public String getString(String name) throws PersistenceException {
|
|
367 |
return ((String) get(name));
|
|
368 |
}
|
|
365 |
public String getString(String name) throws PersistenceException {
|
|
366 |
return ((String) get(name));
|
|
367 |
}
|
|
369 | 368 |
|
370 | 369 |
@Override |
371 |
public void setNull(String name) throws PersistenceException {
|
|
372 |
setValue(name, null);
|
|
373 |
}
|
|
370 |
public void setNull(String name) throws PersistenceException {
|
|
371 |
setValue(name, null);
|
|
372 |
}
|
|
374 | 373 |
|
375 | 374 |
@Override |
376 |
public void set(String name, String value) throws PersistenceException {
|
|
377 |
setValue(name, value);
|
|
378 |
}
|
|
375 |
public void set(String name, String value) throws PersistenceException {
|
|
376 |
setValue(name, value);
|
|
377 |
}
|
|
379 | 378 |
|
380 | 379 |
@Override |
381 |
public void set(String name, File value) throws PersistenceException {
|
|
382 |
setValue(name, value);
|
|
383 |
}
|
|
380 |
public void set(String name, File value) throws PersistenceException {
|
|
381 |
setValue(name, value);
|
|
382 |
}
|
|
384 | 383 |
|
385 | 384 |
@Override |
386 |
public void set(String name, URL value) throws PersistenceException {
|
|
387 |
setValue(name, value);
|
|
388 |
}
|
|
385 |
public void set(String name, URL value) throws PersistenceException {
|
|
386 |
setValue(name, value);
|
|
387 |
}
|
|
389 | 388 |
|
390 | 389 |
@Override |
391 |
public void set(String name, URI value) throws PersistenceException {
|
|
392 |
setValue(name, value);
|
|
393 |
}
|
|
390 |
public void set(String name, URI value) throws PersistenceException {
|
|
391 |
setValue(name, value);
|
|
392 |
}
|
|
394 | 393 |
|
395 | 394 |
@Override |
396 |
public void set(String name, int value) throws PersistenceException {
|
|
397 |
setValue(name, value);
|
|
398 |
}
|
|
395 |
public void set(String name, int value) throws PersistenceException {
|
|
396 |
setValue(name, value);
|
|
397 |
}
|
|
399 | 398 |
|
400 | 399 |
@Override |
401 |
public void set(String name, long value) throws PersistenceException {
|
|
402 |
setValue(name, value);
|
|
403 |
}
|
|
400 |
public void set(String name, long value) throws PersistenceException {
|
|
401 |
setValue(name, value);
|
|
402 |
}
|
|
404 | 403 |
|
405 | 404 |
@Override |
406 |
public void set(String name, double value) throws PersistenceException {
|
|
407 |
setValue(name, value);
|
|
405 |
public void set(String name, double value) throws PersistenceException {
|
|
406 |
setValue(name, value);
|
|
408 | 407 |
|
409 |
}
|
|
408 |
}
|
|
410 | 409 |
|
411 | 410 |
@Override |
412 |
public void set(String name, float value) throws PersistenceException {
|
|
413 |
setValue(name, value);
|
|
411 |
public void set(String name, float value) throws PersistenceException {
|
|
412 |
setValue(name, value);
|
|
414 | 413 |
|
415 |
}
|
|
414 |
}
|
|
416 | 415 |
|
417 | 416 |
@Override |
418 |
public void set(String name, boolean value) throws PersistenceException {
|
|
419 |
setValue(name, value);
|
|
420 |
}
|
|
417 |
public void set(String name, boolean value) throws PersistenceException {
|
|
418 |
setValue(name, value);
|
|
419 |
}
|
|
421 | 420 |
|
422 | 421 |
@Override |
423 |
public void set(String name, Persistent obj) throws PersistenceException { |
|
424 |
setValue(name, getObjectToPersist(obj, name)); |
|
425 |
} |
|
422 |
public void set(String name, Persistent obj) throws PersistenceException { |
|
423 |
if (obj == null) { |
|
424 |
setNull(name); |
|
425 |
} else { |
|
426 |
setValue(name, getObjectToPersist(obj, name)); |
|
427 |
} |
|
428 |
} |
|
426 | 429 |
|
427 | 430 |
@Override |
428 |
public void set(String name, Object obj) throws PersistenceException { |
|
429 |
setValue(name, getObjectToPersist(obj, name)); |
|
430 |
} |
|
431 |
public void set(String name, Object obj) throws PersistenceException { |
|
432 |
if (obj == null) { |
|
433 |
setNull(name); |
|
434 |
} else { |
|
435 |
setValue(name, getObjectToPersist(obj, name)); |
|
436 |
} |
|
437 |
} |
|
431 | 438 |
|
432 | 439 |
@Override |
433 |
public void set(String name, Map obj) throws PersistenceException { |
|
434 |
setValue(name, getObjectToPersist(obj, name)); |
|
435 |
} |
|
440 |
public void set(String name, Map obj) throws PersistenceException { |
|
441 |
if (obj == null) { |
|
442 |
setNull(name); |
|
443 |
} else { |
|
444 |
setValue(name, getObjectToPersist(obj, name)); |
|
445 |
} |
|
446 |
} |
|
436 | 447 |
|
437 | 448 |
@Override |
438 |
public void set(String name, List obj) throws PersistenceException { |
|
439 |
setValue(name, getObjectToPersist(obj, name)); |
|
440 |
} |
|
449 |
public void set(String name, List obj) throws PersistenceException { |
|
450 |
if (obj == null) { |
|
451 |
setNull(name); |
|
452 |
} else { |
|
453 |
setValue(name, getObjectToPersist(obj, name)); |
|
454 |
} |
|
455 |
} |
|
441 | 456 |
|
442 | 457 |
@Override |
443 |
public void set(String name, Iterator obj) throws PersistenceException { |
|
444 |
setValue(name, getObjectToPersist(obj, name)); |
|
445 |
} |
|
458 |
public void set(String name, Iterator obj) throws PersistenceException { |
|
459 |
if (obj == null) { |
|
460 |
setNull(name); |
|
461 |
} else { |
|
462 |
setValue(name, getObjectToPersist(obj, name)); |
|
463 |
} |
|
464 |
} |
|
446 | 465 |
|
447 | 466 |
@Override |
448 |
public void set(String name, Object[] values) throws PersistenceException {
|
|
449 |
set(name, values == null ? null : Arrays.asList(values));
|
|
450 |
}
|
|
467 |
public void set(String name, Object[] values) throws PersistenceException {
|
|
468 |
set(name, values == null ? null : Arrays.asList(values));
|
|
469 |
}
|
|
451 | 470 |
|
452 | 471 |
@Override |
453 |
public void set(String name, Date[] values) throws PersistenceException {
|
|
454 |
set(name, values == null ? null : Arrays.asList(values));
|
|
455 |
}
|
|
472 |
public void set(String name, Date[] values) throws PersistenceException {
|
|
473 |
set(name, values == null ? null : Arrays.asList(values));
|
|
474 |
}
|
|
456 | 475 |
|
457 | 476 |
@Override |
458 |
public void set(String name, String[] values) throws PersistenceException {
|
|
459 |
set(name, values == null ? null : Arrays.asList(values));
|
|
460 |
}
|
|
477 |
public void set(String name, String[] values) throws PersistenceException {
|
|
478 |
set(name, values == null ? null : Arrays.asList(values));
|
|
479 |
}
|
|
461 | 480 |
|
462 | 481 |
@Override |
463 |
public void set(String name, boolean[] value) throws PersistenceException {
|
|
464 |
if (value == null) {
|
|
465 |
setValue(name, null);
|
|
466 |
} else {
|
|
467 |
List valueList = new ArrayList(value.length);
|
|
468 |
for (int i = 0; i < value.length; i++) {
|
|
469 |
valueList.add(value[i] ? Boolean.TRUE : Boolean.FALSE);
|
|
470 |
}
|
|
471 |
set(name, valueList);
|
|
472 |
}
|
|
473 |
}
|
|
482 |
public void set(String name, boolean[] value) throws PersistenceException {
|
|
483 |
if (value == null) {
|
|
484 |
setValue(name, null);
|
|
485 |
} else {
|
|
486 |
List valueList = new ArrayList(value.length);
|
|
487 |
for (int i = 0; i < value.length; i++) {
|
|
488 |
valueList.add(value[i] ? Boolean.TRUE : Boolean.FALSE);
|
|
489 |
}
|
|
490 |
set(name, valueList);
|
|
491 |
}
|
|
492 |
}
|
|
474 | 493 |
|
475 | 494 |
@Override |
476 |
public void set(String name, int[] value) throws PersistenceException {
|
|
477 |
if (value == null) {
|
|
478 |
setValue(name, null);
|
|
479 |
} else {
|
|
480 |
List valueList = new ArrayList(value.length);
|
|
481 |
for (int i = 0; i < value.length; i++) {
|
|
482 |
valueList.add(value[i]);
|
|
483 |
}
|
|
484 |
set(name, valueList);
|
|
485 |
}
|
|
486 |
}
|
|
495 |
public void set(String name, int[] value) throws PersistenceException {
|
|
496 |
if (value == null) {
|
|
497 |
setValue(name, null);
|
|
498 |
} else {
|
|
499 |
List valueList = new ArrayList(value.length);
|
|
500 |
for (int i = 0; i < value.length; i++) {
|
|
501 |
valueList.add(value[i]);
|
|
502 |
}
|
|
503 |
set(name, valueList);
|
|
504 |
}
|
|
505 |
}
|
|
487 | 506 |
|
488 | 507 |
@Override |
489 |
public void set(String name, long[] value) throws PersistenceException {
|
|
490 |
if (value == null) {
|
|
491 |
setValue(name, null);
|
|
492 |
} else {
|
|
493 |
List valueList = new ArrayList(value.length);
|
|
494 |
for (int i = 0; i < value.length; i++) {
|
|
495 |
valueList.add(value[i]);
|
|
496 |
}
|
|
497 |
set(name, valueList);
|
|
498 |
}
|
|
499 |
}
|
|
508 |
public void set(String name, long[] value) throws PersistenceException {
|
|
509 |
if (value == null) {
|
|
510 |
setValue(name, null);
|
|
511 |
} else {
|
|
512 |
List valueList = new ArrayList(value.length);
|
|
513 |
for (int i = 0; i < value.length; i++) {
|
|
514 |
valueList.add(value[i]);
|
|
515 |
}
|
|
516 |
set(name, valueList);
|
|
517 |
}
|
|
518 |
}
|
|
500 | 519 |
|
501 | 520 |
@Override |
502 |
public void set(String name, float[] value) throws PersistenceException {
|
|
503 |
if (value == null) {
|
|
504 |
setValue(name, null);
|
|
505 |
} else {
|
|
506 |
List valueList = new ArrayList(value.length);
|
|
507 |
for (int i = 0; i < value.length; i++) {
|
|
508 |
valueList.add(value[i]);
|
|
509 |
}
|
|
510 |
set(name, valueList);
|
|
511 |
}
|
|
512 |
}
|
|
521 |
public void set(String name, float[] value) throws PersistenceException {
|
|
522 |
if (value == null) {
|
|
523 |
setValue(name, null);
|
|
524 |
} else {
|
|
525 |
List valueList = new ArrayList(value.length);
|
|
526 |
for (int i = 0; i < value.length; i++) {
|
|
527 |
valueList.add(value[i]);
|
|
528 |
}
|
|
529 |
set(name, valueList);
|
|
530 |
}
|
|
531 |
}
|
|
513 | 532 |
|
514 | 533 |
@Override |
515 |
public void set(String name, double[] value) throws PersistenceException {
|
|
516 |
if (value == null) {
|
|
517 |
setValue(name, null);
|
|
518 |
} else {
|
|
519 |
List valueList = new ArrayList(value.length);
|
|
520 |
for (int i = 0; i < value.length; i++) {
|
|
521 |
valueList.add(value[i]);
|
|
522 |
}
|
|
523 |
set(name, valueList);
|
|
524 |
}
|
|
525 |
}
|
|
534 |
public void set(String name, double[] value) throws PersistenceException {
|
|
535 |
if (value == null) {
|
|
536 |
setValue(name, null);
|
|
537 |
} else {
|
|
538 |
List valueList = new ArrayList(value.length);
|
|
539 |
for (int i = 0; i < value.length; i++) {
|
|
540 |
valueList.add(value[i]);
|
|
541 |
}
|
|
542 |
set(name, valueList);
|
|
543 |
}
|
|
544 |
}
|
|
526 | 545 |
|
527 | 546 |
@Override |
528 |
public void set(String name, Set obj) throws PersistenceException { |
|
529 |
setValue(name, getObjectToPersist(obj, name)); |
|
530 |
} |
|
547 |
public void set(String name, Set obj) throws PersistenceException { |
|
548 |
if (obj == null) { |
|
549 |
setNull(name); |
|
550 |
} else { |
|
551 |
setValue(name, getObjectToPersist(obj, name)); |
|
552 |
} |
|
553 |
} |
|
531 | 554 |
|
532 | 555 |
@Override |
533 |
public void set(String name, Boolean obj) throws PersistenceException { |
|
534 |
setValue(name, getObjectToPersist(obj, name)); |
|
535 |
} |
|
556 |
public void set(String name, Boolean obj) throws PersistenceException { |
|
557 |
if (obj == null) { |
|
558 |
setNull(name); |
|
559 |
} else { |
|
560 |
setValue(name, getObjectToPersist(obj, name)); |
|
561 |
} |
|
562 |
} |
|
536 | 563 |
|
537 | 564 |
@Override |
538 |
public void set(String name, Integer obj) throws PersistenceException { |
|
539 |
setValue(name, getObjectToPersist(obj, name)); |
|
540 |
} |
|
565 |
public void set(String name, Integer obj) throws PersistenceException { |
|
566 |
if (obj == null) { |
|
567 |
setNull(name); |
|
568 |
} else { |
|
569 |
setValue(name, getObjectToPersist(obj, name)); |
|
570 |
} |
|
571 |
} |
|
541 | 572 |
|
542 | 573 |
@Override |
543 |
public void set(String name, Long obj) throws PersistenceException { |
|
544 |
setValue(name, getObjectToPersist(obj, name)); |
|
545 |
} |
|
574 |
public void set(String name, Long obj) throws PersistenceException { |
|
575 |
if (obj == null) { |
|
576 |
setNull(name); |
|
577 |
} else { |
|
578 |
setValue(name, getObjectToPersist(obj, name)); |
|
579 |
} |
|
580 |
} |
|
546 | 581 |
|
547 | 582 |
@Override |
548 |
public void set(String name, Float obj) throws PersistenceException { |
|
549 |
setValue(name, getObjectToPersist(obj, name)); |
|
550 |
} |
|
583 |
public void set(String name, Float obj) throws PersistenceException { |
|
584 |
if (obj == null) { |
|
585 |
setNull(name); |
|
586 |
} else { |
|
587 |
setValue(name, getObjectToPersist(obj, name)); |
|
588 |
} |
|
589 |
} |
|
551 | 590 |
|
552 | 591 |
@Override |
553 |
public void set(String name, Double obj) throws PersistenceException { |
|
554 |
setValue(name, getObjectToPersist(obj, name)); |
|
555 |
} |
|
592 |
public void set(String name, Double obj) throws PersistenceException { |
|
593 |
if (obj == null) { |
|
594 |
setNull(name); |
|
595 |
} else { |
|
596 |
setValue(name, getObjectToPersist(obj, name)); |
|
597 |
} |
|
598 |
} |
|
556 | 599 |
|
557 | 600 |
@Override |
558 |
public void set(String name, Date obj) throws PersistenceException { |
|
559 |
setValue(name, getObjectToPersist(obj, name)); |
|
560 |
} |
|
601 |
public void set(String name, Date obj) throws PersistenceException { |
|
602 |
if (obj == null) { |
|
603 |
setNull(name); |
|
604 |
} else { |
|
605 |
setValue(name, getObjectToPersist(obj, name)); |
|
606 |
} |
|
607 |
} |
|
561 | 608 |
|
562 |
protected Object getObjectToPersist(Object theOriginal)
|
|
563 |
throws PersistenceException {
|
|
564 |
return getObjectToPersist(theOriginal, null);
|
|
565 |
}
|
|
609 |
protected Object getObjectToPersist(Object theOriginal)
|
|
610 |
throws PersistenceException {
|
|
611 |
return getObjectToPersist(theOriginal, null);
|
|
612 |
}
|
|
566 | 613 |
|
567 |
/**
|
|
568 |
* Checks class of <code>theOriginal</code> and transforms it if is
|
|
569 |
* necessary.<br>
|
|
570 |
*
|
|
571 |
* @param theOriginal
|
|
572 |
* @return
|
|
573 |
* @throws PersistenceException
|
|
574 |
*/
|
|
575 |
private Object getObjectToPersist(Object theOriginal, String propName)
|
|
576 |
throws PersistenceException {
|
|
577 |
DynStruct definition;
|
|
578 |
DynField field = null;
|
|
579 |
try {
|
|
580 |
if (theOriginal == null) {
|
|
581 |
return null;
|
|
582 |
} else if (validStdTypeClasses.contains(theOriginal.getClass())) {
|
|
583 |
// FIXME: No esta comprobando que el tipo sea el correcto
|
|
584 |
return theOriginal;
|
|
585 |
}
|
|
586 |
if (theOriginal instanceof File) {
|
|
587 |
// File no es una clase final, asi que convertimos
|
|
588 |
// a File para curarnos en salud.
|
|
589 |
// FIXME: No esta comprobando que el tipo sea el correcto
|
|
590 |
return new File(((File) theOriginal).toURI());
|
|
591 |
}
|
|
592 |
definition = this.getDefinitionCheckNull();
|
|
593 |
if (propName != null) {
|
|
594 |
field = definition.getDynField(propName);
|
|
595 |
}
|
|
596 |
if (theOriginal instanceof Persistent) {
|
|
597 |
if (field != null) {
|
|
598 |
Class classOfValue = field.getClassOfValue();
|
|
599 |
if (classOfValue == null) {
|
|
600 |
throw new NullPointerException(
|
|
601 |
"Class of value for field '"
|
|
602 |
+ definition.getFullName() + "' of '"
|
|
603 |
+ field.getName() + "' can't be null.");
|
|
604 |
}
|
|
605 |
if (!classOfValue.isAssignableFrom(theOriginal.getClass())) {
|
|
606 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
607 |
propName, definition, field, theOriginal);
|
|
608 |
}
|
|
609 |
}
|
|
610 |
ObjectReference ref = context.get(theOriginal);
|
|
611 |
if (ref == null) {
|
|
612 |
PersistentStateServices state = manager.createState(
|
|
613 |
theOriginal, context);
|
|
614 |
ref = context.get(state);
|
|
615 |
}
|
|
616 |
return ref;
|
|
614 |
/**
|
|
615 |
* Checks class of <code>theOriginal</code> and transforms it if is
|
|
616 |
* necessary.<br>
|
|
617 |
*
|
|
618 |
* @param theOriginal
|
|
619 |
* @return
|
|
620 |
* @throws PersistenceException
|
|
621 |
*/
|
|
622 |
private Object getObjectToPersist(Object theOriginal, String propName)
|
|
623 |
throws PersistenceException {
|
|
624 |
DynStruct definition;
|
|
625 |
DynField field = null;
|
|
626 |
try {
|
|
627 |
if (theOriginal == null) {
|
|
628 |
return null;
|
|
629 |
} else if (validStdTypeClasses.contains(theOriginal.getClass())) {
|
|
630 |
// FIXME: No esta comprobando que el tipo sea el correcto
|
|
631 |
return theOriginal;
|
|
632 |
}
|
|
633 |
if (theOriginal instanceof File) {
|
|
634 |
// File no es una clase final, asi que convertimos
|
|
635 |
// a File para curarnos en salud.
|
|
636 |
// FIXME: No esta comprobando que el tipo sea el correcto
|
|
637 |
return new File(((File) theOriginal).toURI());
|
|
638 |
}
|
|
639 |
definition = this.getDefinitionCheckNull();
|
|
640 |
if (propName != null) {
|
|
641 |
field = definition.getDynField(propName);
|
|
642 |
}
|
|
643 |
if (theOriginal instanceof Persistent) {
|
|
644 |
if (field != null) {
|
|
645 |
Class classOfValue = field.getClassOfValue();
|
|
646 |
if (classOfValue == null) {
|
|
647 |
throw new NullPointerException(
|
|
648 |
"Class of value for field '"
|
|
649 |
+ definition.getFullName() + "' of '"
|
|
650 |
+ field.getName() + "' can't be null.");
|
|
651 |
}
|
|
652 |
if (!classOfValue.isAssignableFrom(theOriginal.getClass())) {
|
|
653 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
654 |
propName, definition, field, theOriginal);
|
|
655 |
}
|
|
656 |
}
|
|
657 |
ObjectReference ref = context.get(theOriginal);
|
|
658 |
if (ref == null) {
|
|
659 |
PersistentStateServices state = manager.createState(
|
|
660 |
theOriginal, context);
|
|
661 |
ref = context.get(state);
|
|
662 |
}
|
|
663 |
return ref;
|
|
617 | 664 |
|
618 |
} else if ((manager.getFactories().get(theOriginal)) != null) {
|
|
619 |
if (field != null) {
|
|
620 |
Class classOfValue = field.getClassOfValue();
|
|
621 |
if (classOfValue == null) {
|
|
622 |
throw new NullPointerException(
|
|
623 |
"Class of value for field '"
|
|
624 |
+ definition.getFullName() + "' of '"
|
|
625 |
+ field.getName() + "' can't be null.");
|
|
626 |
}
|
|
627 |
if (!classOfValue.isAssignableFrom(theOriginal.getClass())) {
|
|
628 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
629 |
propName, definition, field, theOriginal);
|
|
630 |
}
|
|
631 |
}
|
|
632 |
ObjectReference ref = context.get(theOriginal);
|
|
633 |
if (ref == null) {
|
|
634 |
PersistentStateServices state = manager.createState(
|
|
635 |
theOriginal, context);
|
|
636 |
ref = context.get(state);
|
|
637 |
}
|
|
638 |
return ref;
|
|
665 |
} else if ((manager.getFactories().get(theOriginal)) != null) {
|
|
666 |
if (field != null) {
|
|
667 |
Class classOfValue = field.getClassOfValue();
|
|
668 |
if (classOfValue == null) {
|
|
669 |
throw new NullPointerException(
|
|
670 |
"Class of value for field '"
|
|
671 |
+ definition.getFullName() + "' of '"
|
|
672 |
+ field.getName() + "' can't be null.");
|
|
673 |
}
|
|
674 |
if (!classOfValue.isAssignableFrom(theOriginal.getClass())) {
|
|
675 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
676 |
propName, definition, field, theOriginal);
|
|
677 |
}
|
|
678 |
}
|
|
679 |
ObjectReference ref = context.get(theOriginal);
|
|
680 |
if (ref == null) {
|
|
681 |
PersistentStateServices state = manager.createState(
|
|
682 |
theOriginal, context);
|
|
683 |
ref = context.get(state);
|
|
684 |
}
|
|
685 |
return ref;
|
|
639 | 686 |
|
640 |
} else if (theOriginal instanceof Map) {
|
|
641 |
if (field != null && field.getType() != DataTypes.MAP) {
|
|
642 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
643 |
propName, definition, field, theOriginal);
|
|
644 |
}
|
|
645 |
Map result = new LinkedHashMap(((Map) theOriginal).size());
|
|
646 |
storeValues(result, (Map) theOriginal);
|
|
647 |
return result;
|
|
687 |
} else if (theOriginal instanceof Map) {
|
|
688 |
if (field != null && field.getType() != DataTypes.MAP) {
|
|
689 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
690 |
propName, definition, field, theOriginal);
|
|
691 |
}
|
|
692 |
Map result = new LinkedHashMap(((Map) theOriginal).size());
|
|
693 |
storeValues(result, (Map) theOriginal);
|
|
694 |
return result;
|
|
648 | 695 |
|
649 |
} else if (theOriginal instanceof Set) {
|
|
650 |
if (field != null && field.getType() != DataTypes.SET) {
|
|
651 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
652 |
propName, definition, field, theOriginal);
|
|
653 |
}
|
|
654 |
Set result = new LinkedHashSet(((Set) theOriginal).size());
|
|
655 |
storeValues(result, ((Set) theOriginal).iterator());
|
|
656 |
return result;
|
|
696 |
} else if (theOriginal instanceof Set) {
|
|
697 |
if (field != null && field.getType() != DataTypes.SET) {
|
|
698 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
699 |
propName, definition, field, theOriginal);
|
|
700 |
}
|
|
701 |
Set result = new LinkedHashSet(((Set) theOriginal).size());
|
|
702 |
storeValues(result, ((Set) theOriginal).iterator());
|
|
703 |
return result;
|
|
657 | 704 |
|
658 |
} else if (theOriginal instanceof List) {
|
|
659 |
if (field != null
|
|
660 |
&& !(field.getType() == DataTypes.LIST || field
|
|
661 |
.getType() == DataTypes.ARRAY)) {
|
|
662 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
663 |
propName, definition, field, theOriginal);
|
|
664 |
}
|
|
665 |
List result = new ArrayList(((List) theOriginal).size());
|
|
666 |
storeValues(result, (((List) theOriginal).iterator()));
|
|
667 |
return result;
|
|
705 |
} else if (theOriginal instanceof List) {
|
|
706 |
if (field != null
|
|
707 |
&& !(field.getType() == DataTypes.LIST || field
|
|
708 |
.getType() == DataTypes.ARRAY)) {
|
|
709 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
710 |
propName, definition, field, theOriginal);
|
|
711 |
}
|
|
712 |
List result = new ArrayList(((List) theOriginal).size());
|
|
713 |
storeValues(result, (((List) theOriginal).iterator()));
|
|
714 |
return result;
|
|
668 | 715 |
|
669 |
} else if (theOriginal instanceof Iterator) {
|
|
670 |
if (field != null
|
|
671 |
&& !(field.getType() == DataTypes.LIST || field
|
|
672 |
.getType() == DataTypes.ARRAY)) {
|
|
673 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
674 |
propName, definition, field, theOriginal);
|
|
675 |
}
|
|
676 |
List result = new ArrayList();
|
|
716 |
} else if (theOriginal instanceof Iterator) {
|
|
717 |
if (field != null
|
|
718 |
&& !(field.getType() == DataTypes.LIST || field
|
|
719 |
.getType() == DataTypes.ARRAY)) {
|
|
720 |
throw new PersistenceInvalidTypeForPropertyException(
|
|
721 |
propName, definition, field, theOriginal);
|
|
722 |
}
|
|
723 |
List result = new ArrayList();
|
|
677 | 724 |
|
678 |
storeValues(result, (Iterator) theOriginal);
|
|
679 |
return result;
|
|
725 |
storeValues(result, (Iterator) theOriginal);
|
|
726 |
return result;
|
|
680 | 727 |
|
681 |
} else {
|
|
682 |
if (this.manager.getAutoValidation() == PersistenceManager.MANDATORY) {
|
|
683 |
throw new PersistenceTypeNotSupportedException(
|
|
684 |
theOriginal.getClass(), propName);
|
|
685 |
} else {
|
|
686 |
logger.error("Persistence type '"
|
|
687 |
+ theOriginal.getClass().getName()
|
|
688 |
+ "' not supported.");
|
|
689 |
return null;
|
|
690 |
}
|
|
691 |
}
|
|
692 |
} catch (PersistenceException ex) {
|
|
693 |
PersistentContextServices context = (PersistentContextServices) this
|
|
694 |
.getContext();
|
|
695 |
if (!context.getCollectErrors()) {
|
|
696 |
throw ex;
|
|
697 |
}
|
|
698 |
context.addError(ex);
|
|
699 |
return null;
|
|
700 |
} catch (Exception ex) {
|
|
701 |
PersistentContextServices context = (PersistentContextServices) this
|
|
702 |
.getContext();
|
|
703 |
if (!context.getCollectErrors()) {
|
|
704 |
throw new PersistenceException(ex);
|
|
705 |
}
|
|
706 |
context.addError(ex);
|
|
707 |
return null;
|
|
708 |
}
|
|
709 |
}
|
|
728 |
} else {
|
|
729 |
if (this.manager.getAutoValidation() == PersistenceManager.MANDATORY) {
|
|
730 |
throw new PersistenceTypeNotSupportedException(
|
|
731 |
theOriginal.getClass(), propName);
|
|
732 |
} else {
|
|
733 |
logger.error("Persistence type '"
|
|
734 |
+ theOriginal.getClass().getName()
|
|
735 |
+ "' not supported.");
|
|
736 |
return null;
|
|
737 |
}
|
|
738 |
}
|
|
739 |
} catch (PersistenceException ex) {
|
|
740 |
PersistentContextServices context = (PersistentContextServices) this
|
|
741 |
.getContext();
|
|
742 |
if (!context.getCollectErrors()) {
|
|
743 |
throw ex;
|
|
744 |
}
|
|
745 |
context.addError(ex);
|
|
746 |
return null;
|
|
747 |
} catch (Exception ex) {
|
|
748 |
PersistentContextServices context = (PersistentContextServices) this
|
|
749 |
.getContext();
|
|
750 |
if (!context.getCollectErrors()) {
|
|
751 |
throw new PersistenceException(ex);
|
|
752 |
}
|
|
753 |
context.addError(ex);
|
|
754 |
return null;
|
|
755 |
}
|
|
756 |
}
|
|
710 | 757 |
|
711 | 758 |
@Override |
712 |
public PersistentIdentifier getId() {
|
|
713 |
return id;
|
|
714 |
}
|
|
759 |
public PersistentIdentifier getId() {
|
|
760 |
return id;
|
|
761 |
}
|
|
715 | 762 |
|
716 | 763 |
@Override |
717 |
public void setValue(String name, Object value) throws PersistenceException {
|
|
718 |
try {
|
|
719 |
if (this.theClassName == null) {
|
|
720 |
throw new PersistenceIllegalStateTheClassNameNotSetException();
|
|
721 |
}
|
|
722 |
if (!VALID_NAME_PATTERN.matcher(name).matches()) {
|
|
723 |
throw new PersistenceInvalidPropertyNameException(name);
|
|
724 |
}
|
|
725 |
this.values.put(name, value);
|
|
764 |
public void setValue(String name, Object value) throws PersistenceException {
|
|
765 |
try {
|
|
766 |
if (this.theClassName == null) {
|
|
767 |
throw new PersistenceIllegalStateTheClassNameNotSetException();
|
|
768 |
}
|
|
769 |
if (!VALID_NAME_PATTERN.matcher(name).matches()) {
|
|
770 |
throw new PersistenceInvalidPropertyNameException(name);
|
|
771 |
}
|
|
772 |
this.values.put(name, value);
|
|
726 | 773 |
|
727 |
} catch (PersistenceException ex) {
|
|
728 |
PersistentContextServices context = (PersistentContextServices) this
|
|
729 |
.getContext();
|
|
730 |
if (!context.getCollectErrors()) {
|
|
731 |
throw ex;
|
|
732 |
}
|
|
733 |
context.addError(ex);
|
|
734 |
} catch (Exception ex) {
|
|
735 |
PersistentContextServices context = (PersistentContextServices) this
|
|
736 |
.getContext();
|
|
737 |
if (!context.getCollectErrors()) {
|
|
738 |
throw new PersistenceException(ex);
|
|
739 |
}
|
|
740 |
context.addError(ex);
|
|
741 |
}
|
|
742 |
}
|
|
774 |
} catch (PersistenceException ex) {
|
|
775 |
PersistentContextServices context = (PersistentContextServices) this
|
|
776 |
.getContext();
|
|
777 |
if (!context.getCollectErrors()) {
|
|
778 |
throw ex;
|
|
779 |
}
|
|
780 |
context.addError(ex);
|
|
781 |
} catch (Exception ex) {
|
|
782 |
PersistentContextServices context = (PersistentContextServices) this
|
|
783 |
.getContext();
|
|
784 |
if (!context.getCollectErrors()) {
|
|
785 |
throw new PersistenceException(ex);
|
|
786 |
}
|
|
787 |
context.addError(ex);
|
|
788 |
}
|
|
789 |
}
|
|
743 | 790 |
|
744 | 791 |
@Override |
745 |
public String getTheClassName() {
|
|
746 |
return theClassName;
|
|
747 |
}
|
|
792 |
public String getTheClassName() {
|
|
793 |
return theClassName;
|
|
794 |
}
|
|
748 | 795 |
|
749 |
/*
|
|
796 |
/*
|
|
750 | 797 |
* (non-Javadoc) |
751 | 798 |
* |
752 | 799 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistentState# |
753 | 800 |
* setTheClassName(java.lang.String) |
754 |
*/
|
|
801 |
*/
|
|
755 | 802 |
@Override |
756 |
public void setTheClassName(String className) {
|
|
757 |
theClassName = className;
|
|
758 |
}
|
|
803 |
public void setTheClassName(String className) {
|
|
804 |
theClassName = className;
|
|
805 |
}
|
|
759 | 806 |
|
760 |
private void storeValues(Collection storage, Iterator iter)
|
|
761 |
throws PersistenceException {
|
|
762 |
while (iter.hasNext()) {
|
|
763 |
storage.add(getObjectToPersist(iter.next()));
|
|
764 |
}
|
|
807 |
private void storeValues(Collection storage, Iterator iter)
|
|
808 |
throws PersistenceException {
|
|
809 |
while (iter.hasNext()) {
|
|
810 |
storage.add(getObjectToPersist(iter.next()));
|
|
811 |
}
|
|
765 | 812 |
|
766 |
}
|
|
813 |
}
|
|
767 | 814 |
|
768 |
private void storeValues(Map storage, Map originalStorage)
|
|
769 |
throws PersistenceException {
|
|
770 |
Iterator iter = originalStorage.entrySet().iterator();
|
|
771 |
Entry orgEntry;
|
|
772 |
Object key;
|
|
773 |
Object value;
|
|
815 |
private void storeValues(Map storage, Map originalStorage)
|
|
816 |
throws PersistenceException {
|
|
817 |
Iterator iter = originalStorage.entrySet().iterator();
|
|
818 |
Entry orgEntry;
|
|
819 |
Object key;
|
|
820 |
Object value;
|
|
774 | 821 |
|
775 |
while (iter.hasNext()) {
|
|
776 |
orgEntry = (Entry) iter.next();
|
|
777 |
key = orgEntry.getKey();
|
|
822 |
while (iter.hasNext()) {
|
|
823 |
orgEntry = (Entry) iter.next();
|
|
824 |
key = orgEntry.getKey();
|
|
778 | 825 |
|
779 |
if (key == null) {
|
|
780 |
// Nothing to do
|
|
781 |
} else if (key instanceof String) {
|
|
782 |
// Nothing to do
|
|
783 |
} else if (key instanceof Number) {
|
|
784 |
// Nothing to do
|
|
785 |
} else if (key instanceof Boolean) {
|
|
786 |
// Nothing to do
|
|
787 |
} else if (key instanceof Persistent) {
|
|
788 |
key = getObjectToPersist(key);
|
|
789 |
} else {
|
|
790 |
throw new PersistenceUnsuportedMapKeyTypeException(orgEntry
|
|
791 |
.getKey().getClass());
|
|
792 |
}
|
|
826 |
if (key == null) {
|
|
827 |
// Nothing to do
|
|
828 |
} else if (key instanceof String) {
|
|
829 |
// Nothing to do
|
|
830 |
} else if (key instanceof Number) {
|
|
831 |
// Nothing to do
|
|
832 |
} else if (key instanceof Boolean) {
|
|
833 |
// Nothing to do
|
|
834 |
} else if (key instanceof Persistent) {
|
|
835 |
key = getObjectToPersist(key);
|
|
836 |
} else {
|
|
837 |
throw new PersistenceUnsuportedMapKeyTypeException(orgEntry
|
|
838 |
.getKey().getClass());
|
|
839 |
}
|
|
793 | 840 |
|
794 |
value = getObjectToPersist(orgEntry.getValue());
|
|
841 |
value = getObjectToPersist(orgEntry.getValue());
|
|
795 | 842 |
|
796 |
storage.put(key, value);
|
|
797 |
}
|
|
843 |
storage.put(key, value);
|
|
844 |
}
|
|
798 | 845 |
|
799 |
}
|
|
846 |
}
|
|
800 | 847 |
|
801 | 848 |
@Override |
802 | 849 |
public PersistentContext getContext() { |
803 |
return context;
|
|
804 |
}
|
|
850 |
return context;
|
|
851 |
}
|
|
805 | 852 |
|
806 | 853 |
@Override |
807 |
public boolean hasValue(String name) {
|
|
808 |
return this.values.containsKey(name);
|
|
809 |
}
|
|
854 |
public boolean hasValue(String name) {
|
|
855 |
return this.values.containsKey(name);
|
|
856 |
}
|
|
810 | 857 |
|
811 | 858 |
@Override |
812 |
public Object getValue(String name) {
|
|
813 |
return values.get(name);
|
|
814 |
}
|
|
859 |
public Object getValue(String name) {
|
|
860 |
return values.get(name);
|
|
861 |
}
|
|
815 | 862 |
|
816 |
/**
|
|
817 |
* Returns if the definition related to this state has a defined field
|
|
818 |
*/
|
|
819 |
private boolean definitionHasField(String name) {
|
|
820 |
DynStruct definition = manager.getDefinition(getTheClassName());
|
|
821 |
return definition != null && definition.getDynField(name) != null;
|
|
822 |
}
|
|
863 |
/**
|
|
864 |
* Returns if the definition related to this state has a defined field
|
|
865 |
*/
|
|
866 |
private boolean definitionHasField(String name) {
|
|
867 |
DynStruct definition = manager.getDefinition(getTheClassName());
|
|
868 |
return definition != null && definition.getDynField(name) != null;
|
|
869 |
}
|
|
823 | 870 |
|
824 | 871 |
private Object getDefaultValue(String name) { |
825 | 872 |
DynStruct definition = manager.getDefinition(getTheClassName()); |
826 |
if( definition == null ) {
|
|
873 |
if (definition == null) {
|
|
827 | 874 |
return null; |
828 | 875 |
} |
829 |
if( definition.getDynField(name) == null ) {
|
|
876 |
if (definition.getDynField(name) == null) {
|
|
830 | 877 |
return null; |
831 | 878 |
} |
832 | 879 |
return definition.getDynField(name).getDefaultValue(); |
833 | 880 |
} |
834 | 881 |
|
835 | 882 |
@Override |
836 |
public void relativizeFiles(File rootFolder) {
|
|
837 |
PersistentContext context = this.getContext();
|
|
883 |
public void relativizeFiles(File rootFolder) {
|
|
884 |
PersistentContext context = this.getContext();
|
|
838 | 885 |
|
839 |
URI cwd = new File(System.getProperty("user.dir")).toURI();
|
|
840 |
Iterator statesIterator = context.iterator();
|
|
841 |
while (statesIterator.hasNext()) {
|
|
842 |
PersistentState aState = (PersistentState) statesIterator.next();
|
|
843 |
DynStruct definition = aState.getDefinition();
|
|
844 |
DynField[] fields = definition.getDynFields();
|
|
845 |
for( int i=0; i<fields.length; i++) {
|
|
846 |
DynField field = fields[i];
|
|
847 |
if (field.getType() == DataTypes.FILE
|
|
848 |
|| field.getType() == DataTypes.FOLDER) {
|
|
849 |
try {
|
|
850 |
File value = aState.getFile(field.getName());
|
|
851 |
value = FileTools.relativizeFile(rootFolder, value);
|
|
852 |
aState.set(field.getName(), value);
|
|
853 |
} catch (PersistenceException e) {
|
|
854 |
logger.warn(
|
|
855 |
"Can't relativice field '" + field.getName()
|
|
856 |
+ "' for class '"
|
|
857 |
+ definition.getName() + "'.", e);
|
|
858 |
}
|
|
859 |
} else if (field.getType() == DataTypes.URL) {
|
|
860 |
try {
|
|
861 |
URL value = aState.getURL(field.getName());
|
|
862 |
if (value!=null && "FILE".equalsIgnoreCase(value.getProtocol())) {
|
|
863 |
File file = FileUtils.toFile(value);
|
|
864 |
if (FileTools.isInDifferentDrive(rootFolder,file)) {
|
|
886 |
URI cwd = new File(System.getProperty("user.dir")).toURI();
|
|
887 |
Iterator statesIterator = context.iterator();
|
|
888 |
while (statesIterator.hasNext()) {
|
|
889 |
PersistentState aState = (PersistentState) statesIterator.next();
|
|
890 |
DynStruct definition = aState.getDefinition();
|
|
891 |
DynField[] fields = definition.getDynFields();
|
|
892 |
for (int i = 0; i < fields.length; i++) {
|
|
893 |
DynField field = fields[i];
|
|
894 |
if (field.getType() == DataTypes.FILE
|
|
895 |
|| field.getType() == DataTypes.FOLDER) {
|
|
896 |
try {
|
|
897 |
File value = aState.getFile(field.getName());
|
|
898 |
value = FileTools.relativizeFile(rootFolder, value);
|
|
899 |
aState.set(field.getName(), value);
|
|
900 |
} catch (PersistenceException e) {
|
|
901 |
logger.warn(
|
|
902 |
"Can't relativice field '" + field.getName()
|
|
903 |
+ "' for class '"
|
|
904 |
+ definition.getName() + "'.", e);
|
|
905 |
}
|
|
906 |
} else if (field.getType() == DataTypes.URL) {
|
|
907 |
try {
|
|
908 |
URL value = aState.getURL(field.getName());
|
|
909 |
if (value != null && "FILE".equalsIgnoreCase(value.getProtocol())) {
|
|
910 |
File file = FileUtils.toFile(value);
|
|
911 |
if (FileTools.isInDifferentDrive(rootFolder, file)) {
|
|
865 | 912 |
/* |
866 | 913 |
* Different drives |
867 | 914 |
* (should happen only in Windows) |
868 | 915 |
*/ |
869 | 916 |
value = file.toURI().toURL(); |
870 |
} else {
|
|
917 |
} else {
|
|
871 | 918 |
/* |
872 | 919 |
* Both in the Same drive (C: D: etc) or Unix-style |
873 |
*/ |
|
874 |
file = FileTools.relativizeFile(rootFolder,file); |
|
875 |
value = new URL("file","",file.toURI().toString().substring(cwd.toString().length()));
|
|
876 |
}
|
|
877 |
aState.set(field.getName(), value);
|
|
878 |
}
|
|
879 |
} catch (PersistenceException | MalformedURLException e) {
|
|
880 |
// do nothind
|
|
881 |
}
|
|
882 |
|
|
883 |
} else if (field.getType() == DataTypes.URI) {
|
|
884 |
try {
|
|
885 |
URI value = aState.getURI(field.getName());
|
|
886 |
if (value!=null&&
|
|
887 |
(value.getScheme() == null || "FILE".equalsIgnoreCase(value.getScheme()))) {
|
|
888 |
File file = new File(value.getPath());
|
|
889 |
if (FileTools.isInDifferentDrive(rootFolder,file)) {
|
|
920 |
*/
|
|
921 |
file = FileTools.relativizeFile(rootFolder, file);
|
|
922 |
value = new URL("file", "", file.toURI().toString().substring(cwd.toString().length()));
|
|
923 |
}
|
|
924 |
aState.set(field.getName(), value);
|
|
925 |
}
|
|
926 |
} catch (PersistenceException | MalformedURLException e) {
|
|
927 |
// do nothind
|
|
928 |
}
|
|
929 |
|
|
930 |
} else if (field.getType() == DataTypes.URI) {
|
|
931 |
try {
|
|
932 |
URI value = aState.getURI(field.getName());
|
|
933 |
if (value != null
|
|
934 |
&& (value.getScheme() == null || "FILE".equalsIgnoreCase(value.getScheme()))) {
|
|
935 |
File file = new File(value.getPath());
|
|
936 |
if (FileTools.isInDifferentDrive(rootFolder, file)) {
|
|
890 | 937 |
/* |
891 | 938 |
* Different drives |
892 | 939 |
* (should happen only in Windows) |
893 | 940 |
*/ |
894 | 941 |
value = file.toURI(); |
895 |
} else { |
Also available in: Unified diff