1 23 24 package org.objectweb.jorm.mapper.rdb.generator; 25 26 import org.apache.velocity.context.Context; 27 import org.objectweb.jorm.api.PException; 28 import org.objectweb.jorm.generator.api.MOP; 29 import org.objectweb.jorm.generator.lib.CommonGenerator; 30 import org.objectweb.jorm.generator.lib.NamingFilterExpressionHelper; 31 import org.objectweb.jorm.mapper.rdb.lib.RdbExtentGenInfos; 32 import org.objectweb.jorm.metainfo.api.Class; 33 import org.objectweb.jorm.metainfo.api.NameDef; 34 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping; 35 import org.objectweb.jorm.type.api.PType; 36 import org.objectweb.medor.expression.api.Expression; 37 import org.objectweb.medor.expression.api.ExpressionException; 38 import org.objectweb.medor.expression.api.ParameterOperand; 39 import org.objectweb.util.monolog.api.BasicLevel; 40 import org.objectweb.util.monolog.api.Logger; 41 42 public class RdbMappingMOP extends RdbCommonHelper implements MOP { 43 44 public RdbMappingMOP(Logger logger) { 45 super(logger); 46 } 47 48 public boolean isInPK(PrimitiveElementMapping pem) { 49 return false; 50 } 51 52 54 public void initContext(Context ctx, Class clazz, 55 String projectName, 56 String mapperName) throws PException { 57 ctx.put("rdbTools", this); 58 String mn = (String ) ctx.get("mappername"); 59 ctx.put("adapter", getRdbAdapter(mn)); 60 ctx.put("G", "\""); 61 RdbGenInfos rdbGenInfos = getRdbGenInfos(clazz, projectName, mapperName); 62 ctx.put("genInfos", rdbGenInfos); 63 ctx.put("extentGenInfos", new RdbExtentGenInfos(logger)); 64 65 if (clazz.isPolymorphic()) { 66 NamingFilterExpressionHelper mi = null; 67 try { 68 NameDef nd = clazz.getClassMapping(projectName, mapperName).getIdentifierMapping().getNameDef(); 70 Class classToLookForExpression = (Class ) nd.getParent(); 72 Expression exp = classToLookForExpression.getInheritanceFilter(nd); 74 exp.compileExpression(); 76 mi = new PolymorphicFilterExpressionHelper(exp, logger); 78 } catch (ExpressionException e) { 79 throw new PException(e); 80 } 81 ctx.put("mi", mi); 82 } 83 } 84 85 public String [] getTemplateLibraries() { 86 String [] libs = { 87 CommonGenerator.MAPPER_TEMPLATE_DIR + "rdb/generator/RdbMapping.vm", 88 CommonGenerator.MAPPER_TEMPLATE_DIR + "rdb/generator/RdbExtentDefinition.vm"}; 89 return libs; 90 } 91 92 private String getStrAsObject(String str, PType type) { 93 switch (type.getTypeCode()) { 94 case PType.TYPECODE_BYTE: 95 return "new Byte(" + str +")"; 96 case PType.TYPECODE_CHAR: 97 return "new Character(" + str +")"; 98 case PType.TYPECODE_SHORT: 99 return "new Short(" + str +")"; 100 case PType.TYPECODE_INT: 101 return "new Integer(" + str +")"; 102 case PType.TYPECODE_LONG: 103 return "new Long(" + str +")"; 104 case PType.TYPECODE_OBJBYTE: 105 case PType.TYPECODE_OBJCHAR: 106 case PType.TYPECODE_OBJSHORT: 107 case PType.TYPECODE_OBJINT: 108 case PType.TYPECODE_OBJLONG: 109 case PType.TYPECODE_DATE: 110 case PType.TYPECODE_CHARARRAY: 111 case PType.TYPECODE_BYTEARRAY: 112 case PType.TYPECODE_STRING: 113 case PType.TYPECODE_BIGDECIMAL: 114 case PType.TYPECODE_BIGINTEGER: 115 default: 116 return str; 117 } 118 } 119 120 private String getTupleType(PType type) { 121 switch (type.getTypeCode()) { 122 case PType.TYPECODE_DATE: 123 return "Date"; 124 case PType.TYPECODE_STRING: 125 return "String"; 126 case PType.TYPECODE_CHARARRAY: 127 return "CharArray"; 128 case PType.TYPECODE_BYTEARRAY: 129 return "ByteArray"; 130 case PType.TYPECODE_BIGDECIMAL: 131 return "BigDecimal"; 132 case PType.TYPECODE_BIGINTEGER: 133 return "BigInteger"; 134 case PType.TYPECODE_BYTE: 135 return "Byte"; 136 case PType.TYPECODE_CHAR: 137 return "Char"; 138 case PType.TYPECODE_SHORT: 139 return "Short"; 140 case PType.TYPECODE_INT: 141 return "Int"; 142 case PType.TYPECODE_LONG: 143 return "Long"; 144 case PType.TYPECODE_OBJBYTE: 145 case PType.TYPECODE_OBJBOOLEAN: 146 case PType.TYPECODE_OBJDOUBLE: 147 case PType.TYPECODE_OBJFLOAT: 148 case PType.TYPECODE_OBJCHAR: 149 case PType.TYPECODE_OBJSHORT: 150 case PType.TYPECODE_OBJINT: 151 case PType.TYPECODE_OBJLONG: 152 default: 153 return "Object"; 154 } 155 } 156 161 public class PolymorphicFilterExpressionHelper extends NamingFilterExpressionHelper { 162 163 private PType filterType; 164 public PolymorphicFilterExpressionHelper(Expression e, 165 Logger logger) 166 throws PException, ExpressionException { 167 super(logger); 168 this.filterType = e.getType(); 169 fillMatchInfo(e); 170 } 171 172 public String getFilter() { 173 return getStrAsObject(super.getFilter(), filterType); 174 } 175 176 180 protected void fillMatchInfo(ParameterOperand po) throws PException, ExpressionException { 181 logger.log(BasicLevel.DEBUG, "ParameterOperand " + po.getName()); 182 if (field2declarations.get(po.getName()) == null) { 183 StringBuffer declaration = new StringBuffer (); 184 declaration.append(po.getType().getJavaName()); 185 declaration.append(' '); 186 declaration.append(po.getName()); 187 declaration.append(" = "); 188 declaration.append("tuple.get"); 189 declaration.append(getTupleType(po.getType())); 190 declaration.append("(fieldRank"); 191 declaration.append(")"); 192 declaration.append(";"); 193 field2declarations.put(po.getName(), declaration); 194 if (logger.isLoggable(BasicLevel.DEBUG)) { 195 logger.log(BasicLevel.DEBUG, "Add declaration: " + declaration); 196 } 197 } 198 filter.append(po.getName()); 199 } 200 201 } 202 } 203 | Popular Tags |