1 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 ; 34 import java.util.Iterator ; 35 import java.util.ArrayList ; 36 37 41 public class ObjectIdGenerator extends AbstractVelocityGenerator { 42 43 public final static String LOGGER_NAME 44 = SpeedoProperties.LOGGER_NAME + ".generation.generator.objectid"; 45 46 public final static String TEMPLATE_NAME = TEMPLATE_DIR + ".objectid.ObjectId"; 47 48 public final static String OBJECTID_SUFFIX = "_Id"; 49 50 51 54 public boolean init() throws SpeedoException { 55 logger = scp.loggerFactory.getLogger(LOGGER_NAME); 56 return !scp.getXmldescriptor().isEmpty(); 57 } 58 59 62 69 public void generate(SpeedoClass sClass, String fileName) 70 throws SpeedoException { 71 computeTemplate(TEMPLATE_NAME.replace('.', '/') + ".vm"); 72 try { 73 Context ctx = getContext(sClass); 74 FileWriter fw = new FileWriter (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 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 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 fields = new ArrayList (); 94 Iterator 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 encode = f.name; 101 String 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 name; 202 public String type; 203 public String defaultValue; 204 public String encode; 205 public String decode; 206 public String next; 207 208 public Field(String name, String type) { 209 this.name = name; 210 this.type = type; 211 } 212 213 public String getName() { 214 return name; 215 } 216 217 public String getType() { 218 return type; 219 } 220 221 public String getDefaultValue() { 222 return defaultValue; 223 } 224 225 public String getEncode() { 226 return encode; 227 } 228 229 public String getDecode() { 230 return decode; 231 } 232 233 public String getNext() { 234 return next; 235 } 236 } 237 } 238 | Popular Tags |