/**
* 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
*/
package org.objectweb.speedo.generation.mivisitor;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.metadata.SpeedoClass;
import org.objectweb.speedo.metadata.SpeedoExtension;
import org.objectweb.speedo.metadata.SpeedoField;
import org.objectweb.speedo.metadata.SpeedoMap;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* Checks the keyField speedo extension. It cheks that the key field exists
* and has the rigth type.
*
* @author S.Chassande-Barrioz
*/
public class KeyFieldChecker
extends AbstractMetaInfoVisitor
implements SpeedoProperties {
public KeyFieldChecker(Personality p) {
super(p);
}
public KeyFieldChecker(MetaInfoVisitor mim, Personality p) {
super(mim, p);
}
protected String getLoggerName() {
return AbstractMetaInfoVisitor.LOGGER_NAME + ".keyField";
}
public void visitExtension(SpeedoExtension se) throws SpeedoException {
debug = logger.isLoggable(BasicLevel.DEBUG);
if (!VENDOR_NAME.equals(se.vendorName)
|| !KEY_FIELD.equals(se.key)) {
super.visitExtension(se);
return;
}
//check that the extension KEY_FIELD is specified for a SpeedoField
if (!(se.owner instanceof SpeedoField)) {
SpeedoClass sc = getSpeedoClass(se.owner);
logger.log(BasicLevel.ERROR, "You have specified the key field ('"
+ se.value + "') in other thing than a field tag ('"
+ se.owner.toString() + "')" +
(sc == null
? ""
: " in class '" + sc.getFQName() + "' of the descriptor '"
+ sc.moPackage.xmlDescriptor.xmlFile));
super.visitExtension(se);
return;
}
//check that the SpeedoField is like a map
SpeedoField sf = (SpeedoField) se.owner;
if (!(sf.jdoTuple instanceof SpeedoMap)) {
SpeedoClass sc = getSpeedoClass(se.owner);
logger.log(BasicLevel.ERROR, "You have specified the key field ('"
+ se.value + "') for the field '" + sf.name
+ "' which is not Map" +
(sc == null
? ""
: ", in class '" + sc.getFQName() + "' of the descriptor '"
+ sc.moPackage.xmlDescriptor.xmlFile));
super.visitExtension(se);
return;
}
//Search the key field
SpeedoField keyfield = sf.getFieldOfTheReferencedClass(se.value);
if (keyfield == null) {
throw new SpeedoException("No key field '" + se.value
+ "' found for the" + sf.getSourceDesc());
}
SpeedoMap sm = (SpeedoMap) sf.jdoTuple;
if (sm.keyType != null && ((String) sm.keyType).length() > 0) {
//check the key field type and the
if (keyfield.type().equalsIgnoreCase((String) sm.keyType)) {
throw new SpeedoException("Bad type for the key field '" + se.value
+ "' found for the" + sf.getSourceDesc());
}
} else {
//specify the map key type from the keyField type
sm.keyType = keyfield.type();
}
// Add the SQL_NAME extension for the index field if it is specified
// for the key field
SpeedoExtension _se = keyfield.getExtensionByKey(SQL_NAME);
if (_se != null) {
_se = new SpeedoExtension(_se.vendorName, INDEX, _se.value, sf);
sf.addExtension(_se);
}
// Add the SQL_TYPE extension for the index field if it is specified
// for the key field
_se = keyfield.getExtensionByKey(SQL_TYPE);
if (_se != null) {
_se = new SpeedoExtension(_se.vendorName, INDEX_TYPE, _se.value, sf);
sf.addExtension(_se);
}
super.visitExtension(se);
}
}