001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.core.framework.persistence.jpa;
017    
018    import java.io.Serializable;
019    import java.util.Properties;
020    
021    import org.hibernate.MappingException;
022    import org.hibernate.dialect.Dialect;
023    import org.hibernate.engine.SessionImplementor;
024    import org.hibernate.id.enhanced.SequenceStyleGenerator;
025    import org.hibernate.type.LongType;
026    import org.hibernate.type.Type;
027    
028    /**
029     * This is a subclass of Hibernate's SequenceStyleGenerator that will convert sequence-generated or table-generated numeric IDs into Strings.
030     * 
031     * TODO: Make sure this generator works with MySQL, and verify that the correct numeric Hibernate Type is being used in the configure() method override.
032     * 
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class RiceNumericStringSequenceStyleGenerator extends SequenceStyleGenerator {
036    
037            /**
038             * Performs the same configuration operations as those of the superclass, except that the provided Type will be replaced by Hibernate's LongType instead.
039             * 
040             * TODO: Should we add a configuration parameter for allowing users to specify a different supported Hibernate numeric Type if desired?
041             * 
042             * @see org.hibernate.id.enhanced.SequenceStyleGenerator#configure(org.hibernate.type.Type, java.util.Properties, org.hibernate.dialect.Dialect)
043             */
044            @Override
045            public void configure(Type type, Properties params, Dialect dialect)
046                            throws MappingException {
047                    super.configure(new LongType(), params, dialect);
048            }
049            
050            /**
051             * Takes the ID generated by the superclass's version of this method and returns its toString() representation instead.
052             * 
053             * @see org.hibernate.id.enhanced.SequenceStyleGenerator#generate(org.hibernate.engine.SessionImplementor, java.lang.Object)
054             */
055            @Override
056            public Serializable generate(SessionImplementor session, Object object) {
057                    return super.generate(session, object).toString();
058            }
059    }