1 30 package com.genimen.djeneric.tools.strongtyper; 31 32 import com.genimen.djeneric.repository.DjDomain; 33 import com.genimen.djeneric.repository.DjExtent; 34 import com.genimen.djeneric.repository.DjPersistenceManager; 35 import com.genimen.djeneric.repository.DjProperty; 36 37 43 public class QbeGenerator extends Generator 44 { 45 DjExtent _extent; 46 DjPersistenceManager _mgr; 47 48 53 public QbeGenerator(DjPersistenceManager mgr) 54 { 55 _mgr = mgr; 56 } 57 58 63 public DjExtent getExtent() 64 { 65 return _extent; 66 } 67 68 73 public void setExtent(DjExtent extent) 74 { 75 _extent = extent; 76 } 77 78 83 public String getQbeInterfaceName() 84 { 85 return getQbeInterfaceName(getExtent()); 86 } 87 88 93 public String getClassName() 94 { 95 return getClassName(getExtent()) + (isAbstract() ? getGeneratedSuffix() : ""); 96 } 97 98 104 public String getClassName(DjExtent extent) 105 { 106 return getQbeClassName(extent); 107 } 108 109 114 public String getPackageName() 115 { 116 return getImplPackageName(); 117 } 118 119 125 public String getCode() throws Exception 126 { 127 StringBuffer code = new StringBuffer (5000); 128 if (getPackageName().trim().length() > 0) code.append("package " + getPackageName() + ";\n\n"); 129 130 if (containsBigDecimal(getExtent())) code.append("import java.math.*;\n"); 131 code.append("import " + getItfPackageName() + ".*;\n"); 132 code.append("import com.genimen.djeneric.repository.*;\n\n"); 133 code.append("import com.genimen.djeneric.repository.exceptions.*;\n\n"); 134 code.append(StrongTyper.getRegenerationTags(1)); 135 136 code.append("public class " + getClassName()); 137 if (getExtent().getSuper() != null) 138 { 139 code.append(" extends " + getClassName(getExtent().getSuper())); 140 } 141 else 142 { 143 code.append(" extends DjQueryByExample"); 144 } 145 146 code.append(" implements " + getQbeInterfaceName()); 147 148 code.append("\n{\n"); 149 150 code.append(" private static boolean propidx_set = false;\n"); 151 152 for (int i = 0; i < _extent.getPropertyCount(); i++) 153 { 154 DjProperty prop = _extent.getProperty(i); 155 if (_extent.isInherited(prop)) continue; 156 157 code.append(" private static int PROPIDX_" + prop.getName().toUpperCase() + ";\n"); 158 } 159 160 code.append("\n protected " + getClassName() + "(DjSession session, DjExtent extent) throws DjenericException\n" 161 + " {\n" + " super(session, extent);\n" + " if(!propidx_set)\n" + " {\n" 162 + " propidx_set = true;\n"); 163 164 for (int i = 0; i < _extent.getPropertyCount(); i++) 165 { 166 DjProperty prop = _extent.getProperty(i); 167 if (_extent.isInherited(prop)) continue; 168 code.append(" PROPIDX_" + prop.getName().toUpperCase() + " = extent.getPropertyIndex(\"" + prop.getName() 169 + "\");\n"); 170 } 171 172 code.append(" }\n" + " }\n\n"); 173 174 code.append(" public void setQueryOperator(String propertyName, String operator) throws " 175 + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" 176 + " setOperator(propertyName, operator);\n" + " }\n" + " catch(Exception x)\n" + " {\n" 177 + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n"); 178 179 for (int i = 0; i < _extent.getPropertyCount(); i++) 180 { 181 DjProperty prop = _extent.getProperty(i); 182 if (_extent.isInherited(prop)) continue; 183 184 String propInitcap = initCap(prop.getName()); 185 String typeInitcap = initCap(prop.getNativeType()); 186 187 if (prop.getTypeCode() == DjDomain.BYTE_TYPE) typeInitcap = "Bytes"; 189 190 if (prop.getType() instanceof DjDomain) 191 { 192 code.append(" public " + translateNativeType(prop.getNativeType()) + " get" + propInitcap + "()\n" + " {\n" 193 + " return get" + typeInitcap + "(PROPIDX_" + prop.getName().toUpperCase() + ");\n" + " }\n\n"); 194 195 code.append(" public void set" + propInitcap + "(" + translateNativeType(prop.getNativeType()) 196 + " value) throws " + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" 197 + " set" + typeInitcap + "(PROPIDX_" + prop.getName().toUpperCase() + ", value);\n" 198 + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName() 199 + "(x);\n" + " }\n" + " }\n\n"); 200 } 201 else 202 { 203 DjExtent extent = (DjExtent) prop.getType(); 204 code.append(" public " + getInterfaceName(extent) + " get" + propInitcap + "() throws " 205 + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" + " return " + "(" 206 + getInterfaceName(extent) + ")get(PROPIDX_" + prop.getName().toUpperCase() + ");\n" + " }\n" 207 + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName() + "(x);\n" 208 + " }\n" + " }\n\n"); 209 210 code.append(" public void set" + propInitcap + "(" + getInterfaceName(extent) + " value) throws " 211 + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" + 212 " set(PROPIDX_" + prop.getName().toUpperCase() + ", (" + super.getClassName(extent) 214 + ")value);\n" + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new " 215 + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n"); 216 } 217 } 218 219 code.append(StrongTyper.getRegenerationTags(0)); 220 code.append("}\n"); 221 222 return code.toString(); 223 } 224 225 } | Popular Tags |