1 package org.apache.ojb.broker.metadata; 2 3 17 18 import org.apache.commons.lang.builder.ToStringBuilder; 19 import org.apache.commons.lang.builder.ToStringStyle; 20 import org.apache.ojb.broker.accesslayer.conversions.FieldConversion; 21 import java.io.Serializable ; 22 23 34 public final class ArgumentDescriptor extends DescriptorBase implements XmlCapable, Serializable 35 { 36 private static final long serialVersionUID = 5205304260023247711L; 37 38 private static final int SOURCE_NULL = 0; 39 private static final int SOURCE_FIELD = 1; 40 private static final int SOURCE_VALUE = 2; 41 private int fieldSource = SOURCE_NULL; 42 private String constantValue = null; 43 private String fieldRefName = null; 44 private boolean returnedByProcedure = false; 45 46 50 private ProcedureDescriptor procedureDescriptor; 51 52 62 public ArgumentDescriptor(ProcedureDescriptor procedureDescriptor) 63 { 64 this.procedureDescriptor = procedureDescriptor; 65 this.setValue(); 66 } 67 68 71 public void setValue() 72 { 73 this.fieldSource = SOURCE_NULL; 74 this.fieldRefName = null; 75 this.returnedByProcedure = false; 76 this.constantValue = null; 77 } 78 79 94 public void setValue(String fieldRefName, boolean returnedByProcedure) 95 { 96 this.fieldSource = SOURCE_FIELD; 97 this.fieldRefName = fieldRefName; 98 this.returnedByProcedure = returnedByProcedure; 99 this.constantValue = null; 100 101 if (this.getFieldRef() == null) 104 { 105 this.returnedByProcedure = false; 106 } 107 108 if (this.getFieldRef() == null) 111 { 112 this.returnedByProcedure = false; 113 } 114 } 115 116 122 public void setValue(String constantValue) 123 { 124 this.fieldSource = SOURCE_VALUE; 125 this.fieldRefName = null; 126 this.returnedByProcedure = false; 127 this.constantValue = constantValue; 128 } 129 130 public boolean getIsReturnedByProcedure() 131 { 132 return this.returnedByProcedure; 133 } 134 135 public Object getValue(Object objekt) 136 { 137 switch (this.fieldSource) 138 { 139 case SOURCE_FIELD : 140 if (objekt == null) 141 { 142 return null; 143 } 144 else 145 { 146 FieldDescriptor fd = this.getFieldRef(); 147 Object value = null; 148 FieldConversion conversion = null; 149 if (fd != null) 150 { 151 conversion = fd.getFieldConversion(); 152 value = fd.getPersistentField().get(objekt); 153 if (conversion != null) 154 { 155 value = conversion.javaToSql(value); 156 } 157 } 158 return value; 159 } 160 case SOURCE_VALUE : 161 return this.constantValue; 162 case SOURCE_NULL : 163 return null; 164 default : 165 return null; 166 } 167 } 168 169 public void saveValue(Object objekt, Object value) 170 { 171 if ((this.fieldSource == SOURCE_FIELD) && (this.returnedByProcedure)) 172 { 173 FieldDescriptor fd = this.getFieldRef(); 174 FieldConversion conversion = null; 175 if (fd != null) 176 { 177 conversion = fd.getFieldConversion(); 178 if (conversion == null) 179 { 180 fd.getPersistentField().set(objekt, value); 181 } 182 else 183 { 184 fd.getPersistentField().set(objekt, conversion.sqlToJava(value)); 185 } 186 } 187 } 188 } 189 190 198 public final FieldDescriptor getFieldRef() 199 { 200 if (this.fieldSource == SOURCE_FIELD) 201 { 202 return this.getProcedureDescriptor().getClassDescriptor().getFieldDescriptorByName( 203 this.fieldRefName); 204 } 205 else 206 { 207 return null; 208 } 209 } 210 211 215 public final int getJdbcType() 216 { 217 switch (this.fieldSource) 218 { 219 case SOURCE_FIELD : 220 return this.getFieldRef().getJdbcType().getType(); 221 case SOURCE_NULL : 222 return java.sql.Types.NULL; 223 case SOURCE_VALUE : 224 return java.sql.Types.VARCHAR; 225 default : 226 return java.sql.Types.NULL; 227 } 228 } 229 230 236 public final ProcedureDescriptor getProcedureDescriptor() 237 { 238 return this.procedureDescriptor; 239 } 240 241 244 public String toXML() 245 { 246 String eol = System.getProperty("line.separator"); 247 RepositoryTags tags = RepositoryTags.getInstance(); 248 249 String result = " "; 251 252 switch (this.fieldSource) 253 { 254 case SOURCE_FIELD : 255 result += " " + tags.getOpeningTagNonClosingById(RUNTIME_ARGUMENT); 256 result += " " + tags.getAttribute(FIELD_REF, this.fieldRefName); 257 result += " " + tags.getAttribute(RETURN, String.valueOf(this.returnedByProcedure)); 258 result += "/>"; 259 break; 260 case SOURCE_VALUE : 261 result += " " + tags.getOpeningTagNonClosingById(CONSTANT_ARGUMENT); 262 result += " " + tags.getAttribute(VALUE, this.constantValue); 263 result += "/>"; 264 break; 265 case SOURCE_NULL : 266 result += " " + tags.getOpeningTagNonClosingById(RUNTIME_ARGUMENT); 267 result += "/>"; 268 break; 269 default : 270 break; 271 } 272 273 return (result + eol); 275 } 276 277 283 public String toString() 284 { 285 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE); 286 switch (this.fieldSource) 287 { 288 case SOURCE_FIELD : 289 { 290 buf.append("fieldRefName", this.fieldRefName); 291 buf.append("returnedByProcedure", this.returnedByProcedure); 292 break; 293 } 294 case SOURCE_NULL : 295 { 296 break; 297 } 298 case SOURCE_VALUE : 299 { 300 buf.append("constantValue", this.constantValue); 301 break; 302 } 303 } 304 return buf.toString(); 305 } 306 } 307 | Popular Tags |