001 /*
002  * 20/01/2003 - 10:17:56
003  *
004  * $RCSfile: HighLowKeyGenerator.java,v $ - JDBF Object Relational mapping system
005  * Copyright (C) 2002 JDBF Development Team
006  
007  * http://jdbf.sourceforge.net
008  *
009  * This program is free software; you can redistribute it and/or
010  * modify it under the terms of the GNU Lesser General Public License
011  * as published by the Free Software Foundation; either version 2
012  * of the License, or (at your option) any later version.
013  *
014  * This program is distributed in the hope that it will be useful,
015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017  * GNU Lesser General Public License for more details.
018  *
019  * You should have received a copy of the GNU Lesser General Public License
020  * along with this program; if not, write to the Free Software
021  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022  */
023 /*
024  $Id: HighLowKeyGenerator.java,v 1.8 2004/05/20 22:39:38 gmartone Exp $
025 */
026 package org.jdbf.engine.keygen;
027 import java.util.Map;
028 import java.util.HashMap;
029 import java.util.Collections;
030 import java.sql.Connection;
031 import java.util.logging.Logger;
032 import java.util.logging.Level;
033     
034 import org.jdbf.castor.Messages;
035 import org.jdbf.engine.mapping.*;
036 import org.jdbf.engine.repository.RepositoryView;
037 import org.jdbf.engine.sql.SqlInterface;
038 /**
039  
040  <code>HighLowKeyGenerator</code> generates a key generator composed by
041  * a high value and by a low value. High value is fixed to same value and low 
042  * value is incremented.<br>
043  
044  * This is not the HighLow approach described by Scott Ambler in 
045  * <a href="http://www.ambysoft.com/mappingObjects.pdf">
046  * Mapping Objects To Relational Databases</a>.
047  *
048  * Scott Ambler calls this the key-values approach, but others
049  * use "HighLow" to describe a modified key-values approach which 
050  * does not use database access for each key. 
051  *
052  @author Giovanni Martone
053  @version $Revision: 1.8 $
054  * last changed by $Author: gmartone $
055  
056  */
057 public class HighLowKeyGenerator implements KeyGenerator{
058     
059     /**
060      * Holds a KeyKeeper for each table.  The table's name 
061      * is the key and the value is a  KeyKeeper.
062      */
063     private Map keys = Collections.synchronizedMap(new HashMap());
064   /**
065    * Class name
066    */
067     private static final String CLASS_NAME = "org.jdbf.engine.keygen.HighLowKeyGenerator";
068     /**
069      * Logger object
070      */
071     private Logger logger = Logger.getLogger(CLASS_NAME);
072     
073     /**
074      * Generation of key is before insert.
075      
076      @return boolean
077      */
078     public boolean isBeforeInsert(){
079         return true;
080     }
081     
082     /**
083      * Returns a generated key.
084      *
085      @param view RepositoryView object
086      @param conn Connection the databas
087      @param vendor name of database vendor may be null
088      @param sqlInterface
089      @return Object that represents the key
090      @exception KeyGenerationException if an error occurs
091      
092      */
093     public Object generateKey(RepositoryView view, Connection conn,String vendor,
094                     SqlInterface sqlInterface)
095         throws KeyGenerationException{
096   
097     logger.log(Level.INFO,Messages.message("HighLow.generateKey"));
098     HighLowMap hiloMap = (HighLowMap)
099     view.getBeanDescriptor().getGeneratorMap();
100     String tableName = view.getBeanDescriptor().getTableName();
101     KeyKeeper keyKeeper;
102     Object key = null;
103     synchronized (keys) {
104         keyKeeper = (KeyKeeper)keys.get(tableName);
105         if (keyKeeper == null) {
106             keyKeeper = new KeyKeeper(tableName,hiloMap,sqlInterface);
107           }
108                
109         key = keyKeeper.nextKey(conn,vendor);
110         keys.put(tableName, keyKeeper);             
111     }
112     logger.log(Level.INFO,Messages.format("HighLow.generatedKey",key));
113     return key;
114     }
115 }
116 /*
117   $Log: HighLowKeyGenerator.java,v $
118   Revision 1.8  2004/05/20 22:39:38  gmartone
119   Changed for task 99073 (Coverage Javadocs) and resolved some errors in compilation
120   Revision 1.7  2004/04/29 23:00:04  gmartone
121   Changed MappingException in KeyGenerationException in generateKey method signature
122   Revision 1.6  2004/04/19 23:48:43  gmartone
123   changed type of Exception thrown in all methods from MappingException to KeyGenerationException
124   Revision 1.5  2004/04/05 22:05:39  gmartone
125   *** empty log message ***
126   
127 */