1 56 package org.objectstyle.cayenne.map; 57 58 import java.util.ArrayList ; 59 import java.util.Collections ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 63 import org.objectstyle.cayenne.dba.TypesMapping; 64 import org.objectstyle.cayenne.util.Util; 65 import org.objectstyle.cayenne.util.XMLEncoder; 66 67 82 public class DerivedDbAttribute extends DbAttribute { 83 84 public static final String ATTRIBUTE_TOKEN = "%@"; 85 86 protected String expressionSpec; 87 protected List params = new ArrayList (); 88 protected boolean groupBy; 89 90 93 public DerivedDbAttribute() { 94 super(); 95 } 96 97 100 public DerivedDbAttribute(String name) { 101 super(name); 102 } 103 104 107 public DerivedDbAttribute(String name, int type, DbEntity entity, String spec) { 108 super(name, type, entity); 109 setExpressionSpec(spec); 110 } 111 112 115 public DerivedDbAttribute(DbEntity entity, DbAttribute parentProto) { 116 setName(parentProto.getName()); 117 setType(parentProto.getType()); 118 setMandatory(parentProto.isMandatory()); 119 setMaxLength(parentProto.getMaxLength()); 120 setPrecision(parentProto.getPrecision()); 121 setPrimaryKey(parentProto.isPrimaryKey()); 122 123 setExpressionSpec(ATTRIBUTE_TOKEN); 124 addParam(parentProto); 125 setEntity(entity); 126 } 127 128 133 public void encodeAsXML(XMLEncoder encoder) { 134 encoder.print("<db-attribute-derived name=\"" 135 + Util.encodeXmlAttribute(getName()) 136 + '\"'); 137 138 String type = TypesMapping.getSqlNameByType(getType()); 139 if (type != null) { 140 encoder.print(" type=\"" + type + '\"'); 141 } 142 143 if (isPrimaryKey()) { 145 encoder.print(" isPrimaryKey=\"true\""); 146 } 147 148 if (isMandatory()) 149 encoder.print(" isMandatory=\"true\""); 150 151 if (getMaxLength() > 0) { 152 encoder.print(" length=\""); 153 encoder.print(getMaxLength()); 154 encoder.print('\"'); 155 } 156 157 if (getPrecision() > 0) { 158 encoder.print(" precision=\""); 159 encoder.print(getPrecision()); 160 encoder.print('\"'); 161 } 162 163 if (((DerivedDbEntity) getEntity()).getGroupByAttributes().contains(this)) { 164 encoder.print(" isGroupBy=\"true\""); 165 } 166 167 String spec = getExpressionSpec(); 168 if (spec != null && spec.trim().length() > 0) { 169 encoder.print(" spec=\""); 170 encoder.print(spec); 171 encoder.print('\"'); 172 } 173 174 List params = getParams(); 175 176 if (params.size() > 0) { 177 encoder.println(">"); 178 179 encoder.indent(1); 180 181 Iterator refs = params.iterator(); 182 while (refs.hasNext()) { 183 DbAttribute ref = (DbAttribute) refs.next(); 184 encoder.println("<db-attribute-ref name=\"" 185 + Util.encodeXmlAttribute(ref.getName()) 186 + "\"/>"); 187 } 188 189 encoder.indent(-1); 190 encoder.println("</db-attribute-derived>"); 191 } 192 else { 193 encoder.println("/>"); 194 } 195 } 196 197 public String getAliasedName(String alias) { 198 if (expressionSpec == null) { 199 return super.getAliasedName(alias); 200 } 201 202 int len = params.size(); 203 StringBuffer buf = new StringBuffer (); 204 int ind = 0; 205 for (int i = 0; i < len; i++) { 206 int match = expressionSpec.indexOf(ATTRIBUTE_TOKEN, ind); 209 DbAttribute at = (DbAttribute) params.get(i); 210 if (match > i) { 211 buf.append(expressionSpec.substring(ind, match)); 212 } 213 buf.append(at.getAliasedName(alias)); 214 ind = match + 2; 215 } 216 217 if (ind < expressionSpec.length()) { 218 buf.append(expressionSpec.substring(ind)); 219 } 220 221 return buf.toString(); 222 } 223 224 227 public boolean isGroupBy() { 228 return groupBy; 229 } 230 231 public void setGroupBy(boolean flag) { 232 groupBy = flag; 233 } 234 235 240 public List getParams() { 241 return Collections.unmodifiableList(params); 242 } 243 244 247 public String getExpressionSpec() { 248 return expressionSpec; 249 } 250 251 254 public void addParam(DbAttribute param) { 255 params.add(param); 256 } 257 258 public void removeParam(DbAttribute param) { 259 params.remove(param); 260 } 261 262 public void clearParams() { 263 params.clear(); 264 } 265 266 269 public void setExpressionSpec(String expressionSpec) { 270 this.expressionSpec = expressionSpec; 271 } 272 } | Popular Tags |