001: /**********************************************************************
002: Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.metadata;
019:
020: import java.io.Serializable;
021:
022: /**
023: * Representation of the values for identity "strategy".
024: *
025: * @since 1.1
026: * @version $Revision: 1.16 $
027: */
028: public class IdentityStrategy implements Serializable {
029: /**
030: * strategy="native" in JDO, and "auto" in JPA
031: *
032: * The value "native" allows the JDO implementation to pick the most
033: * suitable strategy based on the underlying database.
034: */
035: public static final IdentityStrategy NATIVE = new IdentityStrategy(
036: 1);
037:
038: /**
039: * strategy="sequence" in JDO and JPA
040: *
041: * The value "sequence" specifies that a named database sequence is used to
042: * generate key values for the table. If sequence is used, then the
043: * sequence-name attribute is required.
044: */
045: public static final IdentityStrategy SEQUENCE = new IdentityStrategy(
046: 2);
047:
048: /**
049: * strategy="identity" in JDO and JPA
050: *
051: * The value "identity" specifies that the column identified as the key
052: * column is managed by the database as an autoincrementing identity type.
053: */
054: public static final IdentityStrategy IDENTITY = new IdentityStrategy(
055: 3);
056:
057: /**
058: * strategy="increment" in JDO and "table" in JPA
059: *
060: * The value "increment" specifies a strategy that simply finds the largest
061: * key already in the database and increments the key value for new
062: * instances. It can be used with integral column types when the JDO
063: * application is the only database user inserting new instances.
064: */
065: public static final IdentityStrategy INCREMENT = new IdentityStrategy(
066: 4);
067:
068: /**
069: * strategy="uuid-string"
070: *
071: * The value "uuid-string" specifies a strategy that generates a 128-bit
072: * UUID unique within a network (the IP address of the machine running the
073: * application is part of the id) and represents the result as a
074: * 16-character String.
075: */
076: public static final IdentityStrategy UUIDSTRING = new IdentityStrategy(
077: 5);
078:
079: /**
080: * strategy="uuid-hex"
081: *
082: * The value "uuid-hex" specifies a strategy that generates a 128-bit UUID
083: * unique within a network (the IP address of the machine running the
084: * application is part of the id) and represents the result as a
085: * 32-character String.
086: */
087: public static final IdentityStrategy UUIDHEX = new IdentityStrategy(
088: 6);
089:
090: /**
091: * An extension strategy not in the standard JDO/JPA list.
092: * Will have the "customName" set to the chosen strategy.
093: * This object only exists for use in the equals() method to check if something is CUSTOM.
094: */
095: public static final IdentityStrategy CUSTOM = new IdentityStrategy(
096: 7);
097:
098: /** The type id. */
099: private final int typeId;
100:
101: /** The Name of the custom type (if CUSTOM). */
102: private String customName;
103:
104: /**
105: * constructor
106: * @param i type id
107: */
108: private IdentityStrategy(int i) {
109: this .typeId = i;
110: }
111:
112: /**
113: * Accessor for the custom name (if using strategy type of CUSTOM).
114: * @return Custom name
115: */
116: public String getCustomName() {
117: return customName;
118: }
119:
120: /**
121: * Indicates whether some other object is "equal to" this one.
122: * @param o the reference object with which to compare.
123: * @return true if this object is the same as the obj argument;
124: * false otherwise.
125: */
126: public boolean equals(Object o) {
127: if (o instanceof IdentityStrategy) {
128: return ((IdentityStrategy) o).typeId == typeId;
129: }
130: return false;
131: }
132:
133: /**
134: * Returns a string representation of the object.
135: * @return a string representation of the object.
136: */
137: public String toString() {
138: switch (typeId) {
139: case 1:
140: return "native";
141: case 2:
142: return "sequence";
143: case 3:
144: return "identity";
145: case 4:
146: return "increment";
147: case 5:
148: return "uuid-string";
149: case 6:
150: return "uuid-hex";
151: case 7:
152: return "custom";
153: }
154: return "";
155: }
156:
157: /**
158: * Accessor for the type.
159: * @return Type
160: **/
161: public int getType() {
162: return typeId;
163: }
164:
165: /**
166: * Gets an IdentityStrategy for the given value argument.
167: * @param value the String representation of IdentityStrategy
168: * @return the IdentityStrategy corresponding to the value argument. NATIVE
169: * IdentityStrategy is returned if the value argument is null or no
170: * corresponding strategy was found
171: */
172: public static IdentityStrategy getIdentityStrategy(
173: final String value) {
174: if (value == null) {
175: return IdentityStrategy.NATIVE;
176: } else if (IdentityStrategy.NATIVE.toString().equals(value)) {
177: return IdentityStrategy.NATIVE;
178: } else if (IdentityStrategy.SEQUENCE.toString().equals(value)) {
179: return IdentityStrategy.SEQUENCE;
180: } else if (IdentityStrategy.IDENTITY.toString().equals(value)) {
181: return IdentityStrategy.IDENTITY;
182: } else if (IdentityStrategy.INCREMENT.toString().equals(value)) {
183: return IdentityStrategy.INCREMENT;
184: } else if ("TABLE".equalsIgnoreCase(value)) {
185: // JPA "table" strategy equates to JDO "increment"
186: return IdentityStrategy.INCREMENT;
187: } else if (IdentityStrategy.UUIDSTRING.toString().equals(value)) {
188: return IdentityStrategy.UUIDSTRING;
189: } else if (IdentityStrategy.UUIDHEX.toString().equals(value)) {
190: return IdentityStrategy.UUIDHEX;
191: } else {
192: // All other strategies have their own strategy object
193: IdentityStrategy strategy = new IdentityStrategy(7);
194: strategy.customName = value;
195: return strategy;
196: }
197: }
198: }
|