1 |
1412a4c4
|
Katja Luther
|
/**
|
2 |
|
|
* Copyright (C) 2009 EDIT
|
3 |
|
|
* European Distributed Institute of Taxonomy
|
4 |
|
|
* http://www.e-taxonomy.eu
|
5 |
|
|
*
|
6 |
|
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7 |
|
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
ee91bcd9
|
ben.clark
|
package eu.etaxonomy.cdm.model.common;
|
11 |
|
|
|
12 |
|
|
import java.util.Iterator;
|
13 |
|
|
import java.util.Map;
|
14 |
|
|
|
15 |
|
|
import org.hibernate.HibernateException;
|
16 |
a13c5f66
|
Andreas Müller
|
import org.hibernate.collection.spi.PersistentCollection;
|
17 |
|
|
import org.hibernate.engine.spi.SessionImplementor;
|
18 |
ee91bcd9
|
ben.clark
|
import org.hibernate.persister.collection.CollectionPersister;
|
19 |
|
|
import org.hibernate.usertype.UserCollectionType;
|
20 |
|
|
|
21 |
95d79c4d
|
Andreas Müller
|
/**
|
22 |
|
|
* TODO move to eu.etaxonomy.cdm.hibernate
|
23 |
|
|
*
|
24 |
|
|
* @author unknown
|
25 |
|
|
*
|
26 |
|
|
*
|
27 |
|
|
*/
|
28 |
ee91bcd9
|
ben.clark
|
public class PersistentMultiLanguageTextType implements UserCollectionType {
|
29 |
|
|
|
30 |
|
|
public PersistentMultiLanguageTextType() {
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
public boolean contains(Object collection, Object obj) {
|
34 |
6460ebf7
|
Andreas Müller
|
Map<?,?> map = (Map<?,?>) collection;
|
35 |
ee91bcd9
|
ben.clark
|
return map.containsValue(obj);
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
public Iterator getElementsIterator(Object collection) {
|
39 |
6460ebf7
|
Andreas Müller
|
return ( (Map<?,?>) collection ).values().iterator();
|
40 |
ee91bcd9
|
ben.clark
|
}
|
41 |
|
|
|
42 |
|
|
public Object indexOf(Object collection, Object element) {
|
43 |
6460ebf7
|
Andreas Müller
|
Iterator<?> iter = ( (Map<?,?>) collection ).entrySet().iterator();
|
44 |
ee91bcd9
|
ben.clark
|
while ( iter.hasNext() ) {
|
45 |
6460ebf7
|
Andreas Müller
|
Map.Entry<?,?> me = (Map.Entry<?,?>) iter.next();
|
46 |
ee91bcd9
|
ben.clark
|
//TODO: proxies!
|
47 |
|
|
if ( me.getValue()==element ) return me.getKey();
|
48 |
|
|
}
|
49 |
|
|
return null;
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
public Object instantiate(int anticipatedSize) {
|
53 |
|
|
return anticipatedSize <= 0
|
54 |
|
|
? new MultilanguageText()
|
55 |
|
|
: new MultilanguageText( anticipatedSize + (int)( anticipatedSize * .75f ), .75f );
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister) throws HibernateException {
|
59 |
|
|
return new PersistentMultiLanguageText();
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
public Object replaceElements(Object original, Object target, CollectionPersister collectionPersister,
|
63 |
|
|
Object owner,Map copyCache, SessionImplementor sessionImplementor) throws HibernateException {
|
64 |
|
|
|
65 |
6460ebf7
|
Andreas Müller
|
Map<Object,Object> result = (Map<Object,Object>) target;
|
66 |
ee91bcd9
|
ben.clark
|
result.clear();
|
67 |
|
|
|
68 |
6460ebf7
|
Andreas Müller
|
Iterator<?> iter = ( (Map<?,?>) original ).entrySet().iterator();
|
69 |
ee91bcd9
|
ben.clark
|
while ( iter.hasNext() ) {
|
70 |
6460ebf7
|
Andreas Müller
|
Map.Entry<?,?> me = (Map.Entry<?,?>) iter.next();
|
71 |
ee91bcd9
|
ben.clark
|
Object key = collectionPersister.getIndexType().replace( me.getKey(), null, sessionImplementor, owner, copyCache );
|
72 |
|
|
Object value = collectionPersister.getElementType().replace( me.getValue(), null, sessionImplementor, owner, copyCache );
|
73 |
|
|
result.put(key, value);
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
return result;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
public PersistentCollection wrap(SessionImplementor sessionImplementor, Object collection) {
|
80 |
|
|
return new PersistentMultiLanguageText( sessionImplementor, (MultilanguageText) collection );
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
}
|