KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > map > DbKeyGenerator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.map;
22
23 import java.io.Serializable JavaDoc;
24
25 import org.apache.cayenne.util.CayenneMapEntry;
26 import org.apache.cayenne.util.XMLEncoder;
27 import org.apache.cayenne.util.XMLSerializable;
28
29 /**
30  * DbKeyGenerator is an abstraction of a primary key generator It configures the primary
31  * key generation per DbEntity in a RDBMS independent manner. DbAdapter generates actual
32  * key values based on the configuration. For more details see data-map.dtd
33  *
34  * @author Andriy Shapochka
35  */

36
37 public class DbKeyGenerator implements CayenneMapEntry, XMLSerializable, Serializable JavaDoc {
38
39     public static final String JavaDoc ORACLE_TYPE = "ORACLE";
40     public static final String JavaDoc NAMED_SEQUENCE_TABLE_TYPE = "NAMED_SEQUENCE_TABLE";
41
42     protected String JavaDoc name;
43     protected DbEntity dbEntity;
44     protected String JavaDoc generatorType;
45     protected Integer JavaDoc keyCacheSize;
46     protected String JavaDoc generatorName;
47
48     public DbKeyGenerator() {
49     }
50
51     public DbKeyGenerator(String JavaDoc name) {
52         this.name = name;
53     }
54
55     public String JavaDoc getName() {
56         return name;
57     }
58
59     public void setName(String JavaDoc name) {
60         this.name = name;
61     }
62
63     public Object JavaDoc getParent() {
64         return getDbEntity();
65     }
66
67     public void setParent(Object JavaDoc parent) {
68         if (parent != null && !(parent instanceof DbEntity)) {
69             throw new IllegalArgumentException JavaDoc("Expected null or DbEntity, got: "
70                     + parent);
71         }
72
73         setDbEntity((DbEntity) parent);
74     }
75
76     /**
77      * Prints itself as XML to the provided XMLEncoder.
78      *
79      * @since 1.1
80      */

81     public void encodeAsXML(XMLEncoder encoder) {
82         if (getGeneratorType() == null) {
83             return;
84         }
85
86         encoder.println("<db-key-generator>");
87         encoder.indent(1);
88
89         encoder.print("<db-generator-type>");
90         encoder.print(getGeneratorType());
91         encoder.println("</db-generator-type>");
92
93         if (getGeneratorName() != null) {
94             encoder.print("<db-generator-name>");
95             encoder.print(getGeneratorName());
96             encoder.println("</db-generator-name>");
97         }
98
99         if (getKeyCacheSize() != null) {
100             encoder.print("<db-key-cache-size>");
101             encoder.print(String.valueOf(getKeyCacheSize()));
102             encoder.println("</db-key-cache-size>");
103         }
104
105         encoder.indent(-1);
106         encoder.println("</db-key-generator>");
107     }
108
109     public DbEntity getDbEntity() {
110         return dbEntity;
111     }
112
113     public void setDbEntity(DbEntity dbEntity) {
114         this.dbEntity = dbEntity;
115     }
116
117     public void setGeneratorType(String JavaDoc generatorType) {
118         this.generatorType = generatorType;
119         if (this.generatorType != null) {
120             this.generatorType = this.generatorType.trim().toUpperCase();
121             if (!(ORACLE_TYPE.equals(this.generatorType) || NAMED_SEQUENCE_TABLE_TYPE
122                     .equals(this.generatorType)))
123                 this.generatorType = null;
124         }
125     }
126
127     public String JavaDoc getGeneratorType() {
128         return generatorType;
129     }
130
131     public void setKeyCacheSize(Integer JavaDoc keyCacheSize) {
132         this.keyCacheSize = keyCacheSize;
133         if (this.keyCacheSize != null && this.keyCacheSize.intValue() < 1) {
134             this.keyCacheSize = null;
135         }
136     }
137
138     public Integer JavaDoc getKeyCacheSize() {
139         return keyCacheSize;
140     }
141
142     public void setGeneratorName(String JavaDoc generatorName) {
143         this.generatorName = generatorName;
144         if (this.generatorName != null) {
145             this.generatorName = this.generatorName.trim();
146             if (this.generatorName.length() == 0)
147                 this.generatorName = null;
148         }
149     }
150
151     public String JavaDoc getGeneratorName() {
152         return generatorName;
153     }
154
155     public String JavaDoc toString() {
156         return "{Type="
157                 + generatorType
158                 + ", Name="
159                 + generatorName
160                 + ", Cache="
161                 + keyCacheSize
162                 + "}";
163     }
164 }
165
Popular Tags