KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > mivisitor > KeyFieldChecker


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.generation.mivisitor;
19
20 import org.objectweb.speedo.api.SpeedoException;
21 import org.objectweb.speedo.api.SpeedoProperties;
22 import org.objectweb.speedo.metadata.SpeedoClass;
23 import org.objectweb.speedo.metadata.SpeedoExtension;
24 import org.objectweb.speedo.metadata.SpeedoField;
25 import org.objectweb.speedo.metadata.SpeedoMap;
26 import org.objectweb.util.monolog.api.BasicLevel;
27
28 /**
29  * Checks the keyField speedo extension. It cheks that the key field exists
30  * and has the rigth type.
31  *
32  * @author S.Chassande-Barrioz
33  */

34 public class KeyFieldChecker
35     extends AbstractMetaInfoVisitor
36     implements SpeedoProperties {
37
38     public KeyFieldChecker() {
39     }
40
41     public KeyFieldChecker(MetaInfoVisitor mim) {
42         super(mim);
43     }
44
45     protected String JavaDoc getLoggerName() {
46         return AbstractMetaInfoVisitor.LOGGER_NAME + ".keyField";
47     }
48
49     public void visitExtension(SpeedoExtension se) throws SpeedoException {
50         debug = logger.isLoggable(BasicLevel.DEBUG);
51         if (!VENDOR_NAME.equals(se.vendorName)
52                 || !KEY_FIELD.equals(se.key)) {
53             super.visitExtension(se);
54             return;
55         }
56
57         //check that the extension KEY_FIELD is specified for a SpeedoField
58
if (!(se.jdoElement instanceof SpeedoField)) {
59             SpeedoClass sc = getSpeedoClass(se.jdoElement);
60             logger.log(BasicLevel.ERROR, "You have specified the key field ('"
61                     + se.value + "') in other thing than a field tag ('"
62                     + se.jdoElement.toString() + "')" +
63                     (sc == null
64                     ? ""
65                     : " in class '" + sc.getFQName() + "' of the descriptor '"
66                     + sc.jdoPackage.jdoXMLDescriptor.xmlFile));
67             super.visitExtension(se);
68             return;
69         }
70
71         //check that the SpeedoField is like a map
72
SpeedoField sf = (SpeedoField) se.jdoElement;
73         if (!(sf.jdoTuple instanceof SpeedoMap)) {
74             SpeedoClass sc = getSpeedoClass(se.jdoElement);
75             logger.log(BasicLevel.ERROR, "You have specified the key field ('"
76                     + se.value + "') for the field '" + sf.name
77                     + "' which is not Map" +
78                     (sc == null
79                     ? ""
80                     : ", in class '" + sc.getFQName() + "' of the descriptor '"
81                     + sc.jdoPackage.jdoXMLDescriptor.xmlFile));
82             super.visitExtension(se);
83             return;
84         }
85         //Search the key field
86
SpeedoField keyfield = ReverseFieldAdder.getReverseField(sf, se.value);
87         if (keyfield == null) {
88             ReverseFieldAdder.throwError("No key field '" + se.value + "' found for the", sf);
89             super.visitExtension(se);
90             return;
91         }
92         SpeedoMap sm = (SpeedoMap) sf.jdoTuple;
93         if (sm.keyType != null && ((String JavaDoc) sm.keyType).length() > 0) {
94             //check the key field type and the
95
if (keyfield.type().equalsIgnoreCase((String JavaDoc) sm.keyType)) {
96                 ReverseFieldAdder.throwError("No key field '" + se.value + "' found for the", sf);
97                 super.visitExtension(se);
98                 return;
99             }
100         } else {
101             //specify the map key type from the keyField type
102
sm.keyType = keyfield.type();
103         }
104
105         // Add the SQL_NAME extension for the index field if it is specified
106
// for the key field
107
SpeedoExtension _se = keyfield.getExtensionByKey(SQL_NAME);
108         if (_se != null) {
109             _se = new SpeedoExtension(_se.vendorName, INDEX, _se.value, sf);
110             sf.addExtension(_se);
111         }
112
113         // Add the SQL_TYPE extension for the index field if it is specified
114
// for the key field
115
_se = keyfield.getExtensionByKey(SQL_TYPE);
116         if (_se != null) {
117             _se = new SpeedoExtension(_se.vendorName, INDEX_TYPE, _se.value, sf);
118             sf.addExtension(_se);
119         }
120         super.visitExtension(se);
121     }
122 }
123
Popular Tags