Project

General

Profile

Download (2.82 KB) Statistics
| Branch: | Tag: | Revision:
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
	@Override
34
    public boolean contains(Object collection, Object obj) {
35
		Map<?,?> map = (Map<?,?>) collection;
36
		return map.containsValue(obj);
37
	}
38

    
39
	@Override
40
    public Iterator<?> getElementsIterator(Object collection) {
41
		return ( (Map<?,?>) collection ).values().iterator();
42
	}
43

    
44
	@Override
45
    public Object indexOf(Object collection, Object element) {
46
		Iterator<?> iter = ( (Map<?,?>) collection ).entrySet().iterator();
47
		while ( iter.hasNext() ) {
48
			Map.Entry<?,?> me = (Map.Entry<?,?>) iter.next();
49
			//TODO: proxies!
50
			if ( me.getValue()==element ) {
51
                return me.getKey();
52
            }
53
		}
54
		return null;
55
	}
56

    
57
	@Override
58
	public Object instantiate(int anticipatedSize) {
59
		return anticipatedSize <= 0
60
	       ? new MultilanguageText()
61
	       : new MultilanguageText( anticipatedSize + (int)( anticipatedSize * .75f ), .75f );
62
	}
63

    
64
	@Override
65
	public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister) throws HibernateException {
66
		return new PersistentMultiLanguageText();
67
	}
68

    
69
	@Override
70
	public Object replaceElements(Object original, Object target, CollectionPersister collectionPersister,
71
			Object owner, @SuppressWarnings("rawtypes") Map copyCache,
72
			SessionImplementor sessionImplementor) throws HibernateException {
73

    
74
		@SuppressWarnings("unchecked")
75
        Map<Object,Object> result = (Map<Object,Object>) target;
76
		result.clear();
77

    
78
		Iterator<?> iter = ( (Map<?,?>) original ).entrySet().iterator();
79
		while ( iter.hasNext() ) {
80
			Map.Entry<?,?> me = (Map.Entry<?,?>) iter.next();
81
			Object key = collectionPersister.getIndexType().replace( me.getKey(), null, sessionImplementor, owner, copyCache );
82
			Object value = collectionPersister.getElementType().replace( me.getValue(), null, sessionImplementor, owner, copyCache );
83
			result.put(key, value);
84
		}
85

    
86
		return result;
87
	}
88

    
89
	@Override
90
	public PersistentCollection wrap(SessionImplementor sessionImplementor, Object collection) {
91
		return new PersistentMultiLanguageText( sessionImplementor, (MultilanguageText) collection );
92
	}
93

    
94
}
(46-46/56)