/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*
* Contact: speedo@objectweb.org
*
* Authors: S.Chassande-Barrioz.
*
*/
package org.objectweb.speedo.metadata;
import java.util.List;
import org.objectweb.speedo.api.SpeedoException;
/**
* This class corresponds to the description of the policy management for a
* given identity.
* @author S.Chassande-Barrioz
*/
public final class SpeedoIdentity extends SpeedoElement {
public final static byte NO_ID = 0;
public final static byte USER_ID = 1;
public final static byte DATASTORE_OLONG = 2;
public final static byte DATASTORE_LONG = 3;
public final static byte DATASTORE_NATIVE = DATASTORE_LONG;
public final static byte DATASTORE_POLYMORPHID = 4;
public final static byte DATASTORE_SEQUENCE = 5;
public final static byte DATASTORE_AUTO_ASSIGN = 6;
public final static byte DATASTORE_INCREMENT = 7;
public final static byte DATASTORE_UUID_STRING = 8;
public final static byte DATASTORE_UUID_HEX = 9;
/**
* identifier strategy
*/
public byte strategy = DATASTORE_NATIVE;
/**
* the class used as identifier
*/
public String objectidClass;
public Class objectidJClass;
public boolean oidClassAutoCalculated = false;
/**
* the name of the sequence in case of the strategy is #DATASTORE_SEQUENCE
*/
public String sequenceName;
/**
* is the columns used for the identifier which have not been mapped in
* memory by visible persistent fields.
*/
public SpeedoNoFieldColumn[] columns;
public SpeedoIdentity() {
SpeedoDefaults.setDefaults(this);
}
/**
* Transforms a String into a Byte. The String must corresponds to local variables.
* It returns the byte associated with the variable.
* @param s String to transform.
* @return the byte associated to the String.
*/
public static byte getStrategy(String s) {
if (s.equalsIgnoreCase("application"))
return USER_ID;
else if (s.equalsIgnoreCase("datastore"))
return DATASTORE_NATIVE;
else if (s.equalsIgnoreCase("sequence"))
return DATASTORE_SEQUENCE;
else if (s.equalsIgnoreCase("native"))
return DATASTORE_NATIVE;
else if (s.equalsIgnoreCase("autoassign"))
return DATASTORE_AUTO_ASSIGN;
else if (s.equalsIgnoreCase("increment"))
return DATASTORE_INCREMENT;
else if (s.equalsIgnoreCase("uuid-string"))
return DATASTORE_UUID_STRING;
else if (s.equalsIgnoreCase("uuid-hex"))
return DATASTORE_UUID_HEX;
else if (s.equalsIgnoreCase("polymorph2l"))
return DATASTORE_POLYMORPHID;
else
return DATASTORE_NATIVE;
}
public static String getStrategyName(byte s) {
switch(s) {
case NO_ID: return "NO_ID";
case USER_ID: return "USER_ID";
case DATASTORE_OLONG: return "DATASTORE_OLONG";
case DATASTORE_POLYMORPHID: return "DATASTORE_POLYMORPHID";
case DATASTORE_SEQUENCE: return "DATASTORE_SEQUENCE";
case DATASTORE_AUTO_ASSIGN: return "DATASTORE_AUTO_ASSIGN";
case DATASTORE_INCREMENT: return "DATASTORE_INCREMENT";
case DATASTORE_UUID_STRING: return "DATASTORE_UUID_STRING";
case DATASTORE_UUID_HEX: return "DATASTORE_UUID_HEX";
case DATASTORE_NATIVE:
default:
return "DATASTORE_NATIVE";
}
}
public void setDatastoreIdSequenceName(String sequencename) {
strategy = DATASTORE_SEQUENCE;
sequenceName = sequencename;
}
public boolean isDataStore() {
return strategy > USER_ID;
}
public String getStrategyName() {
return getStrategyName(strategy);
}
/**
* Assignes SpeedoColumn for the identifier. This list of SpeedoColumn is
* transformed into an array of SpeedoNoFieldColumn
* @param columns is a list of SpeedoColumn
* @throws SpeedoException if the strategy does not correspond to the
* specified column (too many column, no strategy defined,
* application staregy choosen, ...)
* @see #columns
*/
public void setColumns(List columns) throws SpeedoException {
switch (strategy) {
case NO_ID:
throw new SpeedoException("No identifier strategy defined !");
case USER_ID:
throw new SpeedoException("Application identifier cannot have hidden column(s): " + columns);
case DATASTORE_LONG:
setNoFieldColumn(columns, new Class[]{Long.TYPE});
break;
case DATASTORE_OLONG:
case DATASTORE_SEQUENCE:
case DATASTORE_AUTO_ASSIGN:
case DATASTORE_INCREMENT:
setNoFieldColumn(columns, new Class[]{Long.class});
break;
case DATASTORE_POLYMORPHID:
setNoFieldColumn(columns, new Class[]{Long.TYPE, Long.TYPE});
break;
case DATASTORE_UUID_STRING:
case DATASTORE_UUID_HEX:
setNoFieldColumn(columns, new Class[]{String.class});
break;
}
}
/**
* Assignes SpeedoColumn for the identifier. This list of SpeedoColumn is
* transformed into an array of SpeedoNoFieldColumn
* @param columns is a list of SpeedoColumn
* @param memoryTypes is a list of expected memory types corresponding to
* the column.
* @throws SpeedoException if the strategy does not correspond to the
* specified column (too many column, no strategy defined,
* application staregy choosen, ...)
* @see #columns
*/
private void setNoFieldColumn(List cols, Class[] memoryTypes) throws SpeedoException {
if (cols.size() != memoryTypes.length) {
throw new SpeedoException("The identity strategy '"
+ getStrategyName(strategy) + "' requires "
+ memoryTypes.length + " column(s): " + columns);
}
SpeedoNoFieldColumn[] nfcs = new SpeedoNoFieldColumn[cols.size()];
for (int i = 0; i < nfcs.length; i++) {
nfcs[i] = new SpeedoNoFieldColumn();
nfcs[i].column = (SpeedoColumn) cols.get(i);
if (memoryTypes[i] != null) {
nfcs[i].type = memoryTypes[i].getName();
} else {
}
}
this.columns = nfcs;
}
public String toString() {
StringBuffer sb = new StringBuffer(super.toString());
final String sep = " / ";
sb.append(sep).append("strategy=").append(getStrategyName());
switch(strategy) {
case USER_ID:
sb.append(sep).append("objectidClass=").append(objectidClass);
break;
case DATASTORE_SEQUENCE:
sb.append(sep).append("sequenceName=").append(sequenceName);
break;
}
return sb.toString();
}
public void merge(SpeedoIdentity id) {
this.jdoExtension = id.jdoExtension;
this.objectidClass = id.objectidClass;
this.sequenceName = id.sequenceName;
this.strategy = id.strategy;
if (this.columns == null) {
this.columns = id.columns;
}
}
}