001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.generation.mivisitor;
018:
019: import org.objectweb.speedo.api.SpeedoException;
020: import org.objectweb.speedo.api.SpeedoProperties;
021: import org.objectweb.speedo.lib.Personality;
022: import org.objectweb.speedo.metadata.SpeedoClass;
023: import org.objectweb.speedo.metadata.SpeedoExtension;
024: import org.objectweb.speedo.metadata.SpeedoField;
025: import org.objectweb.speedo.metadata.SpeedoMap;
026: import org.objectweb.util.monolog.api.BasicLevel;
027:
028: /**
029: * Checks the keyField speedo extension. It cheks that the key field exists
030: * and has the rigth type.
031: *
032: * @author S.Chassande-Barrioz
033: */
034: public class KeyFieldChecker extends AbstractMetaInfoVisitor implements
035: SpeedoProperties {
036:
037: public KeyFieldChecker(Personality p) {
038: super (p);
039: }
040:
041: public KeyFieldChecker(MetaInfoVisitor mim, Personality p) {
042: super (mim, p);
043: }
044:
045: protected String getLoggerName() {
046: return AbstractMetaInfoVisitor.LOGGER_NAME + ".keyField";
047: }
048:
049: public void visitExtension(SpeedoExtension se)
050: throws SpeedoException {
051: debug = logger.isLoggable(BasicLevel.DEBUG);
052: if (!VENDOR_NAME.equals(se.vendorName)
053: || !KEY_FIELD.equals(se.key)) {
054: super .visitExtension(se);
055: return;
056: }
057:
058: //check that the extension KEY_FIELD is specified for a SpeedoField
059: if (!(se.owner instanceof SpeedoField)) {
060: SpeedoClass sc = getSpeedoClass(se.owner);
061: logger
062: .log(
063: BasicLevel.ERROR,
064: "You have specified the key field ('"
065: + se.value
066: + "') in other thing than a field tag ('"
067: + se.owner.toString()
068: + "')"
069: + (sc == null ? ""
070: : " in class '"
071: + sc.getFQName()
072: + "' of the descriptor '"
073: + sc.moPackage.xmlDescriptor.xmlFile));
074: super .visitExtension(se);
075: return;
076: }
077:
078: //check that the SpeedoField is like a map
079: SpeedoField sf = (SpeedoField) se.owner;
080: if (!(sf.jdoTuple instanceof SpeedoMap)) {
081: SpeedoClass sc = getSpeedoClass(se.owner);
082: logger
083: .log(
084: BasicLevel.ERROR,
085: "You have specified the key field ('"
086: + se.value
087: + "') for the field '"
088: + sf.name
089: + "' which is not Map"
090: + (sc == null ? ""
091: : ", in class '"
092: + sc.getFQName()
093: + "' of the descriptor '"
094: + sc.moPackage.xmlDescriptor.xmlFile));
095: super .visitExtension(se);
096: return;
097: }
098: //Search the key field
099: SpeedoField keyfield = sf
100: .getFieldOfTheReferencedClass(se.value);
101: if (keyfield == null) {
102: throw new SpeedoException("No key field '" + se.value
103: + "' found for the" + sf.getSourceDesc());
104: }
105: SpeedoMap sm = (SpeedoMap) sf.jdoTuple;
106: if (sm.keyType != null && ((String) sm.keyType).length() > 0) {
107: //check the key field type and the
108: if (keyfield.type().equalsIgnoreCase((String) sm.keyType)) {
109: throw new SpeedoException(
110: "Bad type for the key field '" + se.value
111: + "' found for the"
112: + sf.getSourceDesc());
113: }
114: } else {
115: //specify the map key type from the keyField type
116: sm.keyType = keyfield.type();
117: }
118:
119: // Add the SQL_NAME extension for the index field if it is specified
120: // for the key field
121: SpeedoExtension _se = keyfield.getExtensionByKey(SQL_NAME);
122: if (_se != null) {
123: _se = new SpeedoExtension(_se.vendorName, INDEX, _se.value,
124: sf);
125: sf.addExtension(_se);
126: }
127:
128: // Add the SQL_TYPE extension for the index field if it is specified
129: // for the key field
130: _se = keyfield.getExtensionByKey(SQL_TYPE);
131: if (_se != null) {
132: _se = new SpeedoExtension(_se.vendorName, INDEX_TYPE,
133: _se.value, sf);
134: sf.addExtension(_se);
135: }
136: super.visitExtension(se);
137: }
138: }
|