1
|
/**
|
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
|
package eu.etaxonomy.cdm.model.common;
|
11
|
|
12
|
import java.util.Iterator;
|
13
|
import java.util.Map;
|
14
|
|
15
|
import org.hibernate.HibernateException;
|
16
|
import org.hibernate.collection.spi.PersistentCollection;
|
17
|
import org.hibernate.engine.spi.SessionImplementor;
|
18
|
import org.hibernate.persister.collection.CollectionPersister;
|
19
|
import org.hibernate.usertype.UserCollectionType;
|
20
|
|
21
|
/**
|
22
|
* TODO move to eu.etaxonomy.cdm.hibernate
|
23
|
*
|
24
|
* @author unknown
|
25
|
*
|
26
|
*
|
27
|
*/
|
28
|
public class PersistentMultiLanguageTextType implements UserCollectionType {
|
29
|
|
30
|
public PersistentMultiLanguageTextType() {
|
31
|
}
|
32
|
|
33
|
public boolean contains(Object collection, Object obj) {
|
34
|
Map<?,?> map = (Map<?,?>) collection;
|
35
|
return map.containsValue(obj);
|
36
|
}
|
37
|
|
38
|
public Iterator getElementsIterator(Object collection) {
|
39
|
return ( (Map<?,?>) collection ).values().iterator();
|
40
|
}
|
41
|
|
42
|
public Object indexOf(Object collection, Object element) {
|
43
|
Iterator<?> iter = ( (Map<?,?>) collection ).entrySet().iterator();
|
44
|
while ( iter.hasNext() ) {
|
45
|
Map.Entry<?,?> me = (Map.Entry<?,?>) iter.next();
|
46
|
//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
|
Map<Object,Object> result = (Map<Object,Object>) target;
|
66
|
result.clear();
|
67
|
|
68
|
Iterator<?> iter = ( (Map<?,?>) original ).entrySet().iterator();
|
69
|
while ( iter.hasNext() ) {
|
70
|
Map.Entry<?,?> me = (Map.Entry<?,?>) iter.next();
|
71
|
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
|
}
|