/**
* 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.generator.objectid;
import org.objectweb.speedo.generation.generator.lib.AbstractVelocityGenerator;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.generation.generator.api.SpeedoGenerationException;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.metadata.SpeedoClass;
import org.objectweb.speedo.metadata.SpeedoField;
import org.objectweb.util.monolog.wrapper.velocity.VelocityLogger;
import org.objectweb.jorm.type.api.PType;
import org.objectweb.jorm.metainfo.api.TypedElement;
import org.apache.velocity.context.Context;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.ArrayList;
/**
*
* @author S.Chassande-Barrioz
*/
public class ObjectIdGenerator extends AbstractVelocityGenerator {
public final static String LOGGER_NAME
= SpeedoProperties.LOGGER_NAME + ".generation.generator.objectid";
public final static String TEMPLATE_NAME = TEMPLATE_DIR + ".objectid.ObjectId";
public final static String OBJECTID_SUFFIX = "_Id";
public ObjectIdGenerator(Personality p) {
super(p);
}
// IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
//----------------------------------------------------//
public boolean init() throws SpeedoException {
logger = scp.loggerFactory.getLogger(LOGGER_NAME);
return !scp.getXmldescriptor().isEmpty();
}
// IMPLEMENTATION OF THE VelocityGenerator INTERFACE //
//---------------------------------------------------//
/**
* This method generates the a file since a SpeedoClass meta object.
* @param sClass is the speedo meta object.
* @param fileName name of the new file.
* @exception org.objectweb.speedo.generation.generator.api.SpeedoGenerationException If there is a problem during writing
* the new file.
*/
public void generate(SpeedoClass sClass, String fileName)
throws SpeedoException {
computeTemplate(TEMPLATE_NAME.replace('.', '/') + ".vm");
try {
Context ctx = getContext(sClass);
FileWriter fw = new FileWriter(fileName);
ve.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM,
new VelocityLogger(logger));
template.merge(ctx, fw);
fw.flush();
fw.close();
} catch (Exception e) {
throw new SpeedoGenerationException(
"Error during the generation of the file " + fileName, e);
}
}
private Context getContext(SpeedoClass sc) {
//creation of the Velocity context
Context ctx = new VelocityContext();
ctx.put("tools", this);
ctx.put("jdoClass", sc);
ctx.put("package", sc.moPackage.name);
ctx.put("classNameObjectId", sc.name + OBJECTID_SUFFIX);
ArrayList fields = new ArrayList();
Iterator it = sc.fields.values().iterator();
while(it.hasNext()) {
SpeedoField sf = (SpeedoField) it.next();
if (sf.primaryKey) {
TypedElement te = sc.jormclass.getTypedElement(sf.name);
Field f = new Field(sf.name, te.getType().getJavaName());
String encode = f.name;
String decode = "current.substring("
+ (f.name.length() + 1) + " ,(idx = current.indexOf(\";\")))";
f.next = "current = current.substring(idx+1);";
switch(te.getType().getTypeCode()) {
case PType.TYPECODE_BOOLEAN:
f.defaultValue = "false";
f.decode = "Boolean.getBoolean(" + decode + ")";
break;
case PType.TYPECODE_CHAR:
f.defaultValue = "(char) 0";
f.decode = decode + ".charAt(0)";
break;
case PType.TYPECODE_BYTE:
f.defaultValue = "(byte) -1";
f.decode = "Byte.parseByte(" + decode + ")";
break;
case PType.TYPECODE_SHORT:
f.defaultValue = "(short) -1";
f.decode = "Short.parseShort(" + decode + ")";
break;
case PType.TYPECODE_INT:
f.defaultValue = "-1";
f.decode = "Integer.parseInt(" + decode + ")";
break;
case PType.TYPECODE_LONG:
f.defaultValue = "-1";
f.decode = "Long.parseLong(" + decode + ")";
break;
case PType.TYPECODE_FLOAT:
f.defaultValue = "-1";
f.decode = "Float.parseFloat(" + decode + ")";
break;
case PType.TYPECODE_DOUBLE:
f.defaultValue = "0";
f.decode = "Double.parseDouble(" + decode + ")";
break;
case PType.TYPECODE_OBJBOOLEAN:
f.defaultValue = "null";
f.decode = "Boolean.valueOf(" + decode + ")";
break;
case PType.TYPECODE_OBJCHAR:
f.defaultValue = "null";
f.decode = "new Character(" + decode + ".charAt(0))";
break;
case PType.TYPECODE_OBJBYTE:
f.defaultValue = "null";
f.decode = "Byte.valueOf(" + decode + ")";
break;
case PType.TYPECODE_OBJSHORT:
f.defaultValue = "null";
f.decode = "Short.valueOf(" + decode + ")";
break;
case PType.TYPECODE_OBJINT:
f.defaultValue = "null";
f.decode = "Integer.valueOf(" + decode + ")";
break;
case PType.TYPECODE_OBJLONG:
f.defaultValue = "null";
f.decode = "Long.valueOf(" + decode + ")";
break;
case PType.TYPECODE_OBJFLOAT:
f.defaultValue = "null";
f.decode = "Float.valueOf(" + decode + ")";
break;
case PType.TYPECODE_OBJDOUBLE:
f.defaultValue = "null";
f.decode = "Double.valueOf(" + decode + ")";
break;
case PType.TYPECODE_STRING:
f.defaultValue = "null";
f.decode = decode;
break;
case PType.TYPECODE_DATE:
f.defaultValue = "null";
encode = f.name + ".getTime()";
decode = "new java.util.Date(Long.parseLong(" + decode + "))";
break;
case PType.TYPECODE_CHARARRAY:
f.defaultValue = "null";
f.decode = decode + ".toCharArray()";
encode = "new String(" + f.name + ")";
break;
case PType.TYPECODE_BYTEARRAY:
f.defaultValue = "null";
f.decode = decode + ".getBytes()";
encode = "new String(" + f.name + ")";
break;
default:
continue;
}
f.encode = "\"" + f.name + ":\" + " + encode + " + \";\"";
fields.add(f);
}
ctx.put("fields", fields);
}
return ctx;
}
public class Field {
public String name;
public String type;
public String defaultValue;
public String encode;
public String decode;
public String next;
public Field(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getDefaultValue() {
return defaultValue;
}
public String getEncode() {
return encode;
}
public String getDecode() {
return decode;
}
public String getNext() {
return next;
}
}
}