001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.metadata;
027:
028: import java.util.List;
029:
030: import org.objectweb.speedo.api.SpeedoException;
031:
032: /**
033: * This class corresponds to the description of the policy management for a
034: * given identity.
035: * @author S.Chassande-Barrioz
036: */
037: public final class SpeedoIdentity extends SpeedoElement {
038: public final static byte NO_ID = 0;
039: public final static byte USER_ID = 1;
040: public final static byte DATASTORE_OLONG = 2;
041: public final static byte DATASTORE_LONG = 3;
042: public final static byte DATASTORE_NATIVE = DATASTORE_LONG;
043: public final static byte DATASTORE_POLYMORPHID = 4;
044: public final static byte DATASTORE_SEQUENCE = 5;
045: public final static byte DATASTORE_AUTO_ASSIGN = 6;
046: public final static byte DATASTORE_INCREMENT = 7;
047: public final static byte DATASTORE_UUID_STRING = 8;
048: public final static byte DATASTORE_UUID_HEX = 9;
049:
050: /**
051: * identifier strategy
052: */
053: public byte strategy = DATASTORE_NATIVE;
054:
055: /**
056: * the class used as identifier
057: */
058: public String objectidClass;
059: public Class objectidJClass;
060:
061: public boolean oidClassAutoCalculated = false;
062:
063: /**
064: * the name of the sequence in case of the strategy is #DATASTORE_SEQUENCE
065: */
066: public String sequenceName;
067:
068: /**
069: * is the columns used for the identifier which have not been mapped in
070: * memory by visible persistent fields.
071: */
072: public SpeedoNoFieldColumn[] columns;
073:
074: public SpeedoIdentity() {
075: SpeedoDefaults.setDefaults(this );
076: }
077:
078: /**
079: * Transforms a String into a Byte. The String must corresponds to local variables.
080: * It returns the byte associated with the variable.
081: * @param s String to transform.
082: * @return the byte associated to the String.
083: */
084: public static byte getStrategy(String s) {
085: if (s.equalsIgnoreCase("application"))
086: return USER_ID;
087: else if (s.equalsIgnoreCase("datastore"))
088: return DATASTORE_NATIVE;
089: else if (s.equalsIgnoreCase("sequence"))
090: return DATASTORE_SEQUENCE;
091: else if (s.equalsIgnoreCase("native"))
092: return DATASTORE_NATIVE;
093: else if (s.equalsIgnoreCase("autoassign"))
094: return DATASTORE_AUTO_ASSIGN;
095: else if (s.equalsIgnoreCase("increment"))
096: return DATASTORE_INCREMENT;
097: else if (s.equalsIgnoreCase("uuid-string"))
098: return DATASTORE_UUID_STRING;
099: else if (s.equalsIgnoreCase("uuid-hex"))
100: return DATASTORE_UUID_HEX;
101: else if (s.equalsIgnoreCase("polymorph2l"))
102: return DATASTORE_POLYMORPHID;
103: else
104: return DATASTORE_NATIVE;
105: }
106:
107: public static String getStrategyName(byte s) {
108: switch (s) {
109: case NO_ID:
110: return "NO_ID";
111: case USER_ID:
112: return "USER_ID";
113: case DATASTORE_OLONG:
114: return "DATASTORE_OLONG";
115: case DATASTORE_POLYMORPHID:
116: return "DATASTORE_POLYMORPHID";
117: case DATASTORE_SEQUENCE:
118: return "DATASTORE_SEQUENCE";
119: case DATASTORE_AUTO_ASSIGN:
120: return "DATASTORE_AUTO_ASSIGN";
121: case DATASTORE_INCREMENT:
122: return "DATASTORE_INCREMENT";
123: case DATASTORE_UUID_STRING:
124: return "DATASTORE_UUID_STRING";
125: case DATASTORE_UUID_HEX:
126: return "DATASTORE_UUID_HEX";
127:
128: case DATASTORE_NATIVE:
129: default:
130: return "DATASTORE_NATIVE";
131: }
132: }
133:
134: public void setDatastoreIdSequenceName(String sequencename) {
135: strategy = DATASTORE_SEQUENCE;
136: sequenceName = sequencename;
137: }
138:
139: public boolean isDataStore() {
140: return strategy > USER_ID;
141: }
142:
143: public String getStrategyName() {
144: return getStrategyName(strategy);
145: }
146:
147: /**
148: * Assignes SpeedoColumn for the identifier. This list of SpeedoColumn is
149: * transformed into an array of SpeedoNoFieldColumn
150: * @param columns is a list of SpeedoColumn
151: * @throws SpeedoException if the strategy does not correspond to the
152: * specified column (too many column, no strategy defined,
153: * application staregy choosen, ...)
154: * @see #columns
155: */
156: public void setColumns(List columns) throws SpeedoException {
157: switch (strategy) {
158: case NO_ID:
159: throw new SpeedoException(
160: "No identifier strategy defined !");
161: case USER_ID:
162: throw new SpeedoException(
163: "Application identifier cannot have hidden column(s): "
164: + columns);
165: case DATASTORE_LONG:
166: setNoFieldColumn(columns, new Class[] { Long.TYPE });
167: break;
168: case DATASTORE_OLONG:
169: case DATASTORE_SEQUENCE:
170: case DATASTORE_AUTO_ASSIGN:
171: case DATASTORE_INCREMENT:
172: setNoFieldColumn(columns, new Class[] { Long.class });
173: break;
174: case DATASTORE_POLYMORPHID:
175: setNoFieldColumn(columns, new Class[] { Long.TYPE,
176: Long.TYPE });
177: break;
178:
179: case DATASTORE_UUID_STRING:
180: case DATASTORE_UUID_HEX:
181: setNoFieldColumn(columns, new Class[] { String.class });
182: break;
183: }
184: }
185:
186: /**
187: * Assignes SpeedoColumn for the identifier. This list of SpeedoColumn is
188: * transformed into an array of SpeedoNoFieldColumn
189: * @param columns is a list of SpeedoColumn
190: * @param memoryTypes is a list of expected memory types corresponding to
191: * the column.
192: * @throws SpeedoException if the strategy does not correspond to the
193: * specified column (too many column, no strategy defined,
194: * application staregy choosen, ...)
195: * @see #columns
196: */
197: private void setNoFieldColumn(List cols, Class[] memoryTypes)
198: throws SpeedoException {
199: if (cols.size() != memoryTypes.length) {
200: throw new SpeedoException("The identity strategy '"
201: + getStrategyName(strategy) + "' requires "
202: + memoryTypes.length + " column(s): " + columns);
203: }
204: SpeedoNoFieldColumn[] nfcs = new SpeedoNoFieldColumn[cols
205: .size()];
206: for (int i = 0; i < nfcs.length; i++) {
207: nfcs[i] = new SpeedoNoFieldColumn();
208: nfcs[i].column = (SpeedoColumn) cols.get(i);
209: if (memoryTypes[i] != null) {
210: nfcs[i].type = memoryTypes[i].getName();
211: } else {
212:
213: }
214: }
215: this .columns = nfcs;
216: }
217:
218: public String toString() {
219: StringBuffer sb = new StringBuffer(super .toString());
220: final String sep = " / ";
221: sb.append(sep).append("strategy=").append(getStrategyName());
222: switch (strategy) {
223: case USER_ID:
224: sb.append(sep).append("objectidClass=").append(
225: objectidClass);
226: break;
227: case DATASTORE_SEQUENCE:
228: sb.append(sep).append("sequenceName=").append(sequenceName);
229: break;
230: }
231: return sb.toString();
232: }
233:
234: public void merge(SpeedoIdentity id) {
235: this.jdoExtension = id.jdoExtension;
236: this.objectidClass = id.objectidClass;
237: this.sequenceName = id.sequenceName;
238: this.strategy = id.strategy;
239: if (this.columns == null) {
240: this.columns = id.columns;
241: }
242: }
243: }
|