Source code: org/jdbf/engine/keygen/HighLowKeyGenerator.java
1 /*
2 * 20/01/2003 - 10:17:56
3 *
4 * HighLowKeyGenerator.java - JDBF Object Relational mapping system
5 * Copyright (C) 2002 Giovanni Martone
6 * giovannimartone@hotmail.com
7 * http://jdbf.sourceforge.net
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 package org.jdbf.engine.keygen;
24
25 import java.util.Map;
26 import java.util.HashMap;
27 import java.util.Collections;
28 import java.sql.Connection;
29
30 import org.jdbf.castor.Messages;
31 import org.jdbf.engine.database.DatabaseImpl;
32 import org.jdbf.engine.mapping.*;
33 import org.jdbf.engine.repository.RepositoryView;
34 import org.jdbf.engine.sql.SqlInterface;
35
36
37 /**
38 * This is not the HighLow approach described by Scott Ambler in
39 * <a href="http://www.ambysoft.com/mappingObjects.pdf">
40 * Mapping Objects To Relational Databases</a>.
41 *
42 * Scott Ambler calls this the key-values approach, but others
43 * use "HighLow" to describe a modified key-values approach which
44 * does not use database access for each key.
45 *
46 * @author Giovanni Martone
47 * @version $id$
48 *
49 */
50 public class HighLowKeyGenerator implements KeyGenerator{
51
52 /**
53 * Holds a KeyKeeper for each table. The table's name
54 * is the key and the value is a KeyKeeper.
55 */
56 private Map keys = Collections.synchronizedMap(new HashMap());
57
58
59 /**
60 * Generation of key is before insert.
61 */
62 public boolean isBeforeInsert(){
63 return true;
64 }
65
66
67 /**
68 * Returns a generated key.
69 *
70 * @param view RepositoryView object
71 * @param conn Connection the databas
72 * @param vendor name of database vendor may be null
73 * @return the key
74 * @exception MappingException if an error occurs
75 */
76 public Object generateKey(RepositoryView view, Connection conn,String vendor)
77 throws MappingException{
78
79 HighLowMap hiloMap = (HighLowMap)
80 view.getBeanDescriptor().getGeneratorMap();
81 String tableName = view.getBeanDescriptor().getTableName();
82 KeyKeeper keyKeeper;
83 Object key = null;
84 synchronized (keys) {
85 keyKeeper = (KeyKeeper)keys.get(tableName);
86 if (keyKeeper == null) {
87 keyKeeper = new KeyKeeper(tableName,hiloMap);
88 }
89
90 key = keyKeeper.nextKey(conn,vendor);
91 keys.put(tableName, keyKeeper);
92 }
93 return key;
94 }
95 }