1 19 20 package org.apache.cayenne.map; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.cayenne.dba.TypesMapping; 28 import org.apache.cayenne.util.Util; 29 import org.apache.cayenne.util.XMLEncoder; 30 31 46 public class DerivedDbAttribute extends DbAttribute { 47 48 public static final String ATTRIBUTE_TOKEN = "%@"; 49 50 protected String expressionSpec; 51 protected List params = new ArrayList (); 52 protected boolean groupBy; 53 54 57 public DerivedDbAttribute() { 58 super(); 59 } 60 61 64 public DerivedDbAttribute(String name) { 65 super(name); 66 } 67 68 71 public DerivedDbAttribute(String name, int type, DbEntity entity, String spec) { 72 super(name, type, entity); 73 setExpressionSpec(spec); 74 } 75 76 79 public DerivedDbAttribute(DbEntity entity, DbAttribute parentProto) { 80 setName(parentProto.getName()); 81 setType(parentProto.getType()); 82 setMandatory(parentProto.isMandatory()); 83 setMaxLength(parentProto.getMaxLength()); 84 setAttributePrecision(parentProto.getAttributePrecision()); 85 setScale(parentProto.getScale()); 86 setPrimaryKey(parentProto.isPrimaryKey()); 87 88 setExpressionSpec(ATTRIBUTE_TOKEN); 89 addParam(parentProto); 90 setEntity(entity); 91 } 92 93 98 public void encodeAsXML(XMLEncoder encoder) { 99 encoder.print("<db-attribute-derived name=\"" 100 + Util.encodeXmlAttribute(getName()) 101 + '\"'); 102 103 String type = TypesMapping.getSqlNameByType(getType()); 104 if (type != null) { 105 encoder.print(" type=\"" + type + '\"'); 106 } 107 108 if (isPrimaryKey()) { 110 encoder.print(" isPrimaryKey=\"true\""); 111 } 112 113 if (isMandatory()) 114 encoder.print(" isMandatory=\"true\""); 115 116 if (getMaxLength() > 0) { 117 encoder.print(" length=\""); 118 encoder.print(getMaxLength()); 119 encoder.print('\"'); 120 } 121 122 if (getScale() > 0) { 123 encoder.print(" scale=\""); 124 encoder.print(getScale()); 125 encoder.print('\"'); 126 } 127 128 if (getAttributePrecision() > 0) { 129 encoder.print(" attributePrecision=\""); 130 encoder.print(getAttributePrecision()); 131 encoder.print('\"'); 132 } 133 134 if (((DerivedDbEntity) getEntity()).getGroupByAttributes().contains(this)) { 135 encoder.print(" isGroupBy=\"true\""); 136 } 137 138 String spec = getExpressionSpec(); 139 if (spec != null && spec.trim().length() > 0) { 140 encoder.print(" spec=\""); 141 encoder.print(spec); 142 encoder.print('\"'); 143 } 144 145 List params = getParams(); 146 147 if (params.size() > 0) { 148 encoder.println(">"); 149 150 encoder.indent(1); 151 152 Iterator refs = params.iterator(); 153 while (refs.hasNext()) { 154 DbAttribute ref = (DbAttribute) refs.next(); 155 encoder.println("<db-attribute-ref name=\"" 156 + Util.encodeXmlAttribute(ref.getName()) 157 + "\"/>"); 158 } 159 160 encoder.indent(-1); 161 encoder.println("</db-attribute-derived>"); 162 } 163 else { 164 encoder.println("/>"); 165 } 166 } 167 168 public String getAliasedName(String alias) { 169 if (expressionSpec == null) { 170 return super.getAliasedName(alias); 171 } 172 173 int len = params.size(); 174 StringBuffer buf = new StringBuffer (); 175 int ind = 0; 176 for (int i = 0; i < len; i++) { 177 int match = expressionSpec.indexOf(ATTRIBUTE_TOKEN, ind); 180 DbAttribute at = (DbAttribute) params.get(i); 181 if (match > i) { 182 buf.append(expressionSpec.substring(ind, match)); 183 } 184 buf.append(at.getAliasedName(alias)); 185 ind = match + 2; 186 } 187 188 if (ind < expressionSpec.length()) { 189 buf.append(expressionSpec.substring(ind)); 190 } 191 192 return buf.toString(); 193 } 194 195 198 public boolean isGroupBy() { 199 return groupBy; 200 } 201 202 public void setGroupBy(boolean flag) { 203 groupBy = flag; 204 } 205 206 211 public List getParams() { 212 return Collections.unmodifiableList(params); 213 } 214 215 218 public String getExpressionSpec() { 219 return expressionSpec; 220 } 221 222 225 public void addParam(DbAttribute param) { 226 params.add(param); 227 } 228 229 public void removeParam(DbAttribute param) { 230 params.remove(param); 231 } 232 233 public void clearParams() { 234 params.clear(); 235 } 236 237 240 public void setExpressionSpec(String expressionSpec) { 241 this.expressionSpec = expressionSpec; 242 } 243 } 244 | Popular Tags |