~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/core/src/main/java/org/hibernate/type/CustomCollectionType.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Hibernate, Relational Persistence for Idiomatic Java
3
 
 *
4
 
 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5
 
 * indicated by the @author tags or express copyright attribution
6
 
 * statements applied by the authors.  All third-party contributions are
7
 
 * distributed under license by Red Hat Middleware LLC.
8
 
 *
9
 
 * This copyrighted material is made available to anyone wishing to use, modify,
10
 
 * copy, or redistribute it subject to the terms and conditions of the GNU
11
 
 * Lesser General Public License, as published by the Free Software Foundation.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
16
 
 * for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Lesser General Public License
19
 
 * along with this distribution; if not, write to:
20
 
 * Free Software Foundation, Inc.
21
 
 * 51 Franklin Street, Fifth Floor
22
 
 * Boston, MA  02110-1301  USA
23
 
 *
24
 
 */
25
 
package org.hibernate.type;
26
 
 
27
 
import java.io.Serializable;
28
 
import java.util.Iterator;
29
 
import java.util.Map;
30
 
 
31
 
import org.hibernate.HibernateException;
32
 
import org.hibernate.MappingException;
33
 
import org.hibernate.collection.PersistentCollection;
34
 
import org.hibernate.engine.SessionImplementor;
35
 
import org.hibernate.engine.SessionFactoryImplementor;
36
 
import org.hibernate.persister.collection.CollectionPersister;
37
 
import org.hibernate.usertype.UserCollectionType;
38
 
import org.hibernate.usertype.LoggableUserType;
39
 
 
40
 
/**
41
 
 * A custom type for mapping user-written classes that implement <tt>PersistentCollection</tt>
42
 
 * 
43
 
 * @see org.hibernate.collection.PersistentCollection
44
 
 * @see org.hibernate.usertype.UserCollectionType
45
 
 * @author Gavin King
46
 
 */
47
 
public class CustomCollectionType extends CollectionType {
48
 
 
49
 
        private final UserCollectionType userType;
50
 
        private final boolean customLogging;
51
 
 
52
 
        public CustomCollectionType(Class userTypeClass, String role, String foreignKeyPropertyName, boolean isEmbeddedInXML) {
53
 
                super(role, foreignKeyPropertyName, isEmbeddedInXML);
54
 
 
55
 
                if ( !UserCollectionType.class.isAssignableFrom( userTypeClass ) ) {
56
 
                        throw new MappingException( "Custom type does not implement UserCollectionType: " + userTypeClass.getName() );
57
 
                }
58
 
 
59
 
                try {
60
 
                        userType = ( UserCollectionType ) userTypeClass.newInstance();
61
 
                }
62
 
                catch ( InstantiationException ie ) {
63
 
                        throw new MappingException( "Cannot instantiate custom type: " + userTypeClass.getName() );
64
 
                }
65
 
                catch ( IllegalAccessException iae ) {
66
 
                        throw new MappingException( "IllegalAccessException trying to instantiate custom type: " + userTypeClass.getName() );
67
 
                }
68
 
 
69
 
                customLogging = LoggableUserType.class.isAssignableFrom( userTypeClass );
70
 
        }
71
 
 
72
 
        public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key)
73
 
        throws HibernateException {
74
 
                return userType.instantiate(session, persister);
75
 
        }
76
 
 
77
 
        public PersistentCollection wrap(SessionImplementor session, Object collection) {
78
 
                return userType.wrap(session, collection);
79
 
        }
80
 
 
81
 
        public Class getReturnedClass() {
82
 
                return userType.instantiate( -1 ).getClass();
83
 
        }
84
 
 
85
 
        public Object instantiate(int anticipatedType) {
86
 
                return userType.instantiate( anticipatedType );
87
 
        }
88
 
 
89
 
        public Iterator getElementsIterator(Object collection) {
90
 
                return userType.getElementsIterator(collection);
91
 
        }
92
 
        public boolean contains(Object collection, Object entity, SessionImplementor session) {
93
 
                return userType.contains(collection, entity);
94
 
        }
95
 
        public Object indexOf(Object collection, Object entity) {
96
 
                return userType.indexOf(collection, entity);
97
 
        }
98
 
 
99
 
        public Object replaceElements(Object original, Object target, Object owner, Map copyCache, SessionImplementor session)
100
 
        throws HibernateException {
101
 
                CollectionPersister cp = session.getFactory().getCollectionPersister( getRole() );
102
 
                return userType.replaceElements(original, target, cp, owner, copyCache, session);
103
 
        }
104
 
 
105
 
        protected String renderLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException {
106
 
                if ( customLogging ) {
107
 
                        return ( ( LoggableUserType ) userType ).toLoggableString( value, factory );
108
 
                }
109
 
                else {
110
 
                        return super.renderLoggableString( value, factory );
111
 
                }
112
 
        }
113
 
 
114
 
        public UserCollectionType getUserType() {
115
 
                return userType;
116
 
        }
117
 
}