KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > generator > objectid > ObjectIdGenerator


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.generator.objectid;
19
20 import org.objectweb.speedo.generation.generator.lib.AbstractVelocityGenerator;
21 import org.objectweb.speedo.api.SpeedoException;
22 import org.objectweb.speedo.generation.generator.api.SpeedoGenerationException;
23 import org.objectweb.speedo.api.SpeedoProperties;
24 import org.objectweb.speedo.metadata.SpeedoClass;
25 import org.objectweb.speedo.metadata.SpeedoField;
26 import org.objectweb.util.monolog.wrapper.velocity.VelocityLogger;
27 import org.objectweb.jorm.type.api.PType;
28 import org.objectweb.jorm.metainfo.api.TypedElement;
29 import org.apache.velocity.context.Context;
30 import org.apache.velocity.app.Velocity;
31 import org.apache.velocity.VelocityContext;
32
33 import java.io.FileWriter JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 /**
38  *
39  * @author S.Chassande-Barrioz
40  */

41 public class ObjectIdGenerator extends AbstractVelocityGenerator {
42
43     public final static String JavaDoc LOGGER_NAME
44             = SpeedoProperties.LOGGER_NAME + ".generation.generator.objectid";
45
46     public final static String JavaDoc TEMPLATE_NAME = TEMPLATE_DIR + ".objectid.ObjectId";
47
48     public final static String JavaDoc OBJECTID_SUFFIX = "_Id";
49
50
51     // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
52
//----------------------------------------------------//
53

54     public boolean init() throws SpeedoException {
55         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
56         return !scp.getXmldescriptor().isEmpty();
57     }
58
59     // IMPLEMENTATION OF THE VelocityGenerator INTERFACE //
60
//---------------------------------------------------//
61

62     /**
63      * This method generates the a file since a SpeedoClass meta object.
64      * @param sClass is the speedo meta object.
65      * @param fileName name of the new file.
66      * @exception org.objectweb.speedo.generation.generator.api.SpeedoGenerationException If there is a problem during writing
67      * the new file.
68      */

69     public void generate(SpeedoClass sClass, String JavaDoc fileName)
70             throws SpeedoException {
71         computeTemplate(TEMPLATE_NAME.replace('.', '/') + ".vm");
72         try {
73             Context ctx = getContext(sClass);
74             FileWriter JavaDoc fw = new FileWriter JavaDoc(fileName);
75             ve.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM,
76                     new VelocityLogger(logger));
77             template.merge(ctx, fw);
78             fw.flush();
79             fw.close();
80         } catch (Exception JavaDoc e) {
81             throw new SpeedoGenerationException(
82                     "Error during the generation of the file " + fileName, e);
83         }
84     }
85
86     private Context getContext(SpeedoClass sc) {
87         //creation of the Velocity context
88
Context ctx = new VelocityContext();
89         ctx.put("tools", this);
90         ctx.put("jdoClass", sc);
91         ctx.put("package", sc.jdoPackage.name);
92         ctx.put("classNameObjectId", sc.name + OBJECTID_SUFFIX);
93         ArrayList JavaDoc fields = new ArrayList JavaDoc();
94         Iterator JavaDoc it = sc.jdoField.values().iterator();
95         while(it.hasNext()) {
96             SpeedoField sf = (SpeedoField) it.next();
97             if (sf.primaryKey) {
98                 TypedElement te = sc.jormclass.getTypedElement(sf.name);
99                 Field f = new Field(sf.name, te.getType().getJavaName());
100                 String JavaDoc encode = f.name;
101                 String JavaDoc decode = "current.substring("
102                         + (f.name.length() + 1) + " ,(idx = current.indexOf(\";\")))";
103                 f.next = "current = current.substring(idx+1);";
104                 switch(te.getType().getTypeCode()) {
105                 case PType.TYPECODE_BOOLEAN:
106                     f.defaultValue = "false";
107                     f.decode = "Boolean.getBoolean(" + decode + ")";
108                     break;
109                 case PType.TYPECODE_CHAR:
110                     f.defaultValue = "(char) 0";
111                     f.decode = decode + ".charAt(0)";
112                     break;
113                 case PType.TYPECODE_BYTE:
114                     f.defaultValue = "(byte) -1";
115                     f.decode = "Byte.parseByte(" + decode + ")";
116                     break;
117                 case PType.TYPECODE_SHORT:
118                     f.defaultValue = "(short) -1";
119                     f.decode = "Short.parseShort(" + decode + ")";
120                     break;
121                 case PType.TYPECODE_INT:
122                     f.defaultValue = "-1";
123                     f.decode = "Integer.parseInt(" + decode + ")";
124                     break;
125                 case PType.TYPECODE_LONG:
126                     f.defaultValue = "-1";
127                     f.decode = "Long.parseLong(" + decode + ")";
128                     break;
129                 case PType.TYPECODE_FLOAT:
130                     f.defaultValue = "-1";
131                     f.decode = "Float.parseFloat(" + decode + ")";
132                     break;
133                 case PType.TYPECODE_DOUBLE:
134                     f.defaultValue = "0";
135                     f.decode = "Double.parseDouble(" + decode + ")";
136                     break;
137                 case PType.TYPECODE_OBJBOOLEAN:
138                     f.defaultValue = "null";
139                     f.decode = "Boolean.valueOf(" + decode + ")";
140                     break;
141                 case PType.TYPECODE_OBJCHAR:
142                     f.defaultValue = "null";
143                     f.decode = "new Character(" + decode + ".charAt(0))";
144                     break;
145                 case PType.TYPECODE_OBJBYTE:
146                     f.defaultValue = "null";
147                     f.decode = "Byte.valueOf(" + decode + ")";
148                     break;
149                 case PType.TYPECODE_OBJSHORT:
150                     f.defaultValue = "null";
151                     f.decode = "Short.valueOf(" + decode + ")";
152                     break;
153                 case PType.TYPECODE_OBJINT:
154                     f.defaultValue = "null";
155                     f.decode = "Integer.valueOf(" + decode + ")";
156                     break;
157                 case PType.TYPECODE_OBJLONG:
158                     f.defaultValue = "null";
159                     f.decode = "Long.valueOf(" + decode + ")";
160                     break;
161                 case PType.TYPECODE_OBJFLOAT:
162                     f.defaultValue = "null";
163                     f.decode = "Float.valueOf(" + decode + ")";
164                     break;
165                 case PType.TYPECODE_OBJDOUBLE:
166                     f.defaultValue = "null";
167                     f.decode = "Double.valueOf(" + decode + ")";
168                     break;
169                 case PType.TYPECODE_STRING:
170                     f.defaultValue = "null";
171                     f.decode = decode;
172                     break;
173                 case PType.TYPECODE_DATE:
174                     f.defaultValue = "null";
175                     encode = f.name + ".getTime()";
176                     decode = "new java.util.Date(Long.parseLong(" + decode + "))";
177                     break;
178                 case PType.TYPECODE_CHARARRAY:
179                     f.defaultValue = "null";
180                     f.decode = decode + ".toCharArray()";
181                     encode = "new String(" + f.name + ")";
182                     break;
183                 case PType.TYPECODE_BYTEARRAY:
184                     f.defaultValue = "null";
185                     f.decode = decode + ".getBytes()";
186                     encode = "new String(" + f.name + ")";
187                     break;
188                 default:
189                     continue;
190                 }
191                 f.encode = "\"" + f.name + ":\" + " + encode + " + \";\"";
192                 fields.add(f);
193             }
194             ctx.put("fields", fields);
195         }
196
197         return ctx;
198     }
199
200     public class Field {
201         public String JavaDoc name;
202         public String JavaDoc type;
203         public String JavaDoc defaultValue;
204         public String JavaDoc encode;
205         public String JavaDoc decode;
206         public String JavaDoc next;
207
208         public Field(String JavaDoc name, String JavaDoc type) {
209             this.name = name;
210             this.type = type;
211         }
212
213         public String JavaDoc getName() {
214             return name;
215         }
216
217         public String JavaDoc getType() {
218             return type;
219         }
220
221         public String JavaDoc getDefaultValue() {
222             return defaultValue;
223         }
224
225         public String JavaDoc getEncode() {
226             return encode;
227         }
228
229         public String JavaDoc getDecode() {
230             return decode;
231         }
232
233         public String JavaDoc getNext() {
234             return next;
235         }
236     }
237 }
238
Popular Tags