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 java.util.Properties;
019
020 import org.apache.ojb.broker.PersistenceBroker;
021 import org.apache.ojb.broker.accesslayer.JdbcAccess;
022 import org.apache.ojb.broker.metadata.ClassDescriptor;
023 import org.apache.ojb.broker.metadata.FieldDescriptor;
024 import org.apache.ojb.broker.metadata.SequenceDescriptor;
025 import org.apache.ojb.broker.platforms.Platform;
026
027 /**
028 * A base class for sequence manager implementations.
029 * <br/>
030 * All sequence manager implementations need a constructor
031 * with a PersistenceBroker argument used by the
032 * {@link org.apache.ojb.broker.util.sequence.SequenceManagerFactory}.
033 *
034 * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
035 * @version $Id: AbstractSequenceManager.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
036 */
037 public abstract class AbstractSequenceManager implements SequenceManager
038 {
039 // private Logger log = LoggerFactory.getLogger(AbstractSequenceManager.class);
040 public static final String PROPERTY_AUTO_NAMING = "autoNaming";
041 protected static final String GLOBAL_SEQUENCE_NAME = "ojb.global.sequence";
042
043 private PersistenceBroker brokerForClass;
044 private Platform platform;
045 private Properties configurationProperties;
046
047 /**
048 * Constructor used by
049 * {@link org.apache.ojb.broker.util.sequence.SequenceManagerFactory}
050 *
051 * @param broker PB instance to perform the
052 * id generation.
053 */
054 public AbstractSequenceManager(PersistenceBroker broker)
055 {
056 this.brokerForClass = broker;
057 this.configurationProperties = new Properties();
058 this.platform = brokerForClass.serviceConnectionManager().getSupportedPlatform();
059 SequenceDescriptor sd = brokerForClass.serviceConnectionManager().
060 getConnectionDescriptor().getSequenceDescriptor();
061 if (sd != null)
062 {
063 this.configurationProperties.putAll(sd.getConfigurationProperties());
064 }
065 }
066
067 /**
068 * returns a unique long value for field.
069 * the returned number is unique accross all tables in the extent of clazz.
070 */
071 abstract protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException;
072
073
074 public Platform getPlatform()
075 {
076 return platform;
077 }
078
079 public PersistenceBroker getBrokerForClass()
080 {
081 return brokerForClass;
082 }
083
084 public Properties getConfigurationProperties()
085 {
086 return this.configurationProperties;
087 }
088
089 public void setConfigurationProperties(Properties prop)
090 {
091 this.configurationProperties.putAll(prop);
092 }
093
094 public String getConfigurationProperty(String key, String defaultValue)
095 {
096 String result = this.configurationProperties.getProperty(key);
097 return result != null ? result : defaultValue;
098 }
099
100 public void setConfigurationProperty(String key, String value)
101 {
102 this.configurationProperties.setProperty(key, value);
103 }
104
105 public boolean useAutoNaming()
106 {
107 return (Boolean.valueOf(getConfigurationProperty(PROPERTY_AUTO_NAMING, "true"))).booleanValue();
108 }
109
110 public String calculateSequenceName(FieldDescriptor field) throws SequenceManagerException
111 {
112 String seqName;
113 seqName = field.getSequenceName();
114 /*
115 if we found no sequence name for the given field, we try to
116 assign a automatic generated sequence name.
117 */
118 if(seqName == null)
119 {
120 seqName = SequenceManagerHelper.buildSequenceName(getBrokerForClass(), field, useAutoNaming());
121 // already done in method above
122 // if(useAutoNaming()) field.setSequenceName(seqName);
123 }
124 return seqName;
125 }
126
127
128 //****************************************************************
129 // method implementations of SequenceManager interface
130 //****************************************************************
131 /**
132 * Returns a unique object for the given field attribute.
133 * The returned value takes in account the jdbc-type
134 * and the FieldConversion.sql2java() conversion defined for <code>field</code>.
135 * The returned object is unique accross all tables in the extent
136 * of class the field belongs to.
137 */
138 public Object getUniqueValue(FieldDescriptor field) throws SequenceManagerException
139 {
140 Object result = field.getJdbcType().sequenceKeyConversion(new Long(getUniqueLong(field)));
141 // perform a sql to java conversion here, so that clients do
142 // not see any db specific values
143 result = field.getFieldConversion().sqlToJava(result);
144 return result;
145 }
146
147 /**
148 * noop
149 */
150 public void afterStore(JdbcAccess dbAccess, ClassDescriptor cld, Object obj)
151 throws SequenceManagerException
152 {
153 // do nothing
154 }
155
156 /**
157 * noop
158 */
159 public void setReferenceFKs(Object obj, ClassDescriptor cld)
160 throws SequenceManagerException
161 {
162 // do nothing
163 }
164 }