001    package org.apache.ojb.broker.util.sequence;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.PersistenceBroker;
019    import org.apache.ojb.broker.metadata.FieldDescriptor;
020    
021    /**
022     * For internal use only!
023     * This class is used to create transient primary key values for transient 
024     * {@link org.apache.ojb.broker.Identity} objects.
025     *
026     * @version $Id: SequenceManagerTransientImpl.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
027     */
028    public class SequenceManagerTransientImpl extends AbstractSequenceManager
029    {
030        /*
031         Use keyword 'volatile' to make decrement of a long value an
032         atomic operation
033         */
034        private static volatile long tempKey = 1000;
035    
036        public SequenceManagerTransientImpl(PersistenceBroker broker)
037        {
038            super(broker);
039        }
040    
041        protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException
042        {
043            /*
044            arminw:
045            We need unique 'dummy keys' for new objects before storing.
046            Variable 'tempKey' is declared volatile, thus increment should be atomic
047            */
048            return ++tempKey;
049        }
050        
051            /*
052             * abyrne: KULRNE-4545 when the maintenance framework triggers a refresh
053             * of references on a new bank account, and ojb goes to retrieve the bank, it uses this class
054             * to initialize any null keys on bank account with a unique value.  however, after setting
055             * the bank account to a number > 1000, it calls the sqlToJava method on the field converter.
056             * bank account number is an encrypted field, and the field converter complains because the value passed in
057             * is not encrypted.  so, we removing the field conversion call, which should not impact uniqueness
058             * or anything else since the unique long did not come from the db in this case
059             */
060        /**
061         * Returns a unique object for the given field attribute.
062         * The returned value takes in account the jdbc-type
063         * and the FieldConversion.sql2java() conversion defined for <code>field</code>.
064         * The returned object is unique accross all tables in the extent
065         * of class the field belongs to.
066         */
067        public Object getUniqueValue(FieldDescriptor field) throws SequenceManagerException
068        {
069            return field.getJdbcType().sequenceKeyConversion(new Long(getUniqueLong(field)));
070            // perform a sql to java conversion here, so that clients do
071            // not see any db specific values
072            // abyrne commented out: result = field.getFieldConversion().sqlToJava(result);
073            // abyrne commented out: return result;
074        }
075    }