| 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.PersistentCollection; |
|---|
| 17 | import org.hibernate.engine.SessionImplementor; |
|---|
| 18 | import org.hibernate.persister.collection.CollectionPersister; |
|---|
| 19 | import org.hibernate.usertype.UserCollectionType; |
|---|
| 20 | |
|---|
| 21 | public class PersistentMultiLanguageTextType implements UserCollectionType { |
|---|
| 22 | |
|---|
| 23 | public PersistentMultiLanguageTextType() { |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | public boolean contains(Object collection, Object obj) { |
|---|
| 27 | Map map = (Map) collection; |
|---|
| 28 | return map.containsValue(obj); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | public Iterator getElementsIterator(Object collection) { |
|---|
| 32 | return ( (java.util.Map) collection ).values().iterator(); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | public Object indexOf(Object collection, Object element) { |
|---|
| 36 | Iterator iter = ( (Map) collection ).entrySet().iterator(); |
|---|
| 37 | while ( iter.hasNext() ) { |
|---|
| 38 | Map.Entry me = (Map.Entry) iter.next(); |
|---|
| 39 | //TODO: proxies! |
|---|
| 40 | if ( me.getValue()==element ) return me.getKey(); |
|---|
| 41 | } |
|---|
| 42 | return null; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | public Object instantiate(int anticipatedSize) { |
|---|
| 46 | return anticipatedSize <= 0 |
|---|
| 47 | ? new MultilanguageText() |
|---|
| 48 | : new MultilanguageText( anticipatedSize + (int)( anticipatedSize * .75f ), .75f ); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister) throws HibernateException { |
|---|
| 52 | return new PersistentMultiLanguageText(); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public Object replaceElements(Object original, Object target, CollectionPersister collectionPersister, |
|---|
| 56 | Object owner,Map copyCache, SessionImplementor sessionImplementor) throws HibernateException { |
|---|
| 57 | |
|---|
| 58 | java.util.Map result = (java.util.Map) target; |
|---|
| 59 | result.clear(); |
|---|
| 60 | |
|---|
| 61 | Iterator iter = ( (java.util.Map) original ).entrySet().iterator(); |
|---|
| 62 | while ( iter.hasNext() ) { |
|---|
| 63 | java.util.Map.Entry me = (java.util.Map.Entry) iter.next(); |
|---|
| 64 | Object key = collectionPersister.getIndexType().replace( me.getKey(), null, sessionImplementor, owner, copyCache ); |
|---|
| 65 | Object value = collectionPersister.getElementType().replace( me.getValue(), null, sessionImplementor, owner, copyCache ); |
|---|
| 66 | result.put(key, value); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | return result; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public PersistentCollection wrap(SessionImplementor sessionImplementor, Object collection) { |
|---|
| 73 | return new PersistentMultiLanguageText( sessionImplementor, (MultilanguageText) collection ); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | } |
|---|