1 19 20 package org.apache.cayenne.map; 21 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.cayenne.util.CayenneMapEntry; 29 import org.apache.cayenne.util.Util; 30 import org.apache.cayenne.util.XMLEncoder; 31 import org.apache.cayenne.util.XMLSerializable; 32 33 38 public class Procedure implements CayenneMapEntry, XMLSerializable, Serializable { 39 40 protected String name; 41 protected DataMap dataMap; 42 43 protected String catalog; 44 protected String schema; 45 protected boolean returningValue; 46 protected List callParameters = new ArrayList (); 47 48 51 public Procedure() { 52 } 53 54 57 public Procedure(String name) { 58 setName(name); 59 } 60 61 public String getName() { 62 return name; 63 } 64 65 public void setName(String name) { 66 this.name = name; 67 } 68 69 public Object getParent() { 70 return getDataMap(); 71 } 72 73 public void setParent(Object parent) { 74 if (parent != null && !(parent instanceof DataMap)) { 75 throw new IllegalArgumentException ("Expected null or DataMap, got: " + parent); 76 } 77 78 setDataMap((DataMap) parent); 79 } 80 81 86 public void encodeAsXML(XMLEncoder encoder) { 87 encoder.print("<procedure name=\""); 88 encoder.print(Util.encodeXmlAttribute(getName())); 89 encoder.print('\"'); 90 91 if (getSchema() != null && getSchema().trim().length() > 0) { 92 encoder.print(" schema=\""); 93 encoder.print(getSchema().trim()); 94 encoder.print('\"'); 95 } 96 97 if (getCatalog() != null && getCatalog().trim().length() > 0) { 98 encoder.print(" catalog=\""); 99 encoder.print(getCatalog().trim()); 100 encoder.print('\"'); 101 } 102 103 if (isReturningValue()) { 104 encoder.print(" returningValue=\"true\""); 105 } 106 107 encoder.println('>'); 108 109 encoder.indent(1); 110 encoder.print(getCallParameters()); 111 encoder.indent(-1); 112 113 encoder.println("</procedure>"); 114 } 115 116 119 public String getFullyQualifiedName() { 120 return (schema != null) ? schema + '.' + getName() : getName(); 121 } 122 123 126 public DataMap getDataMap() { 127 return dataMap; 128 } 129 130 133 public void setDataMap(DataMap dataMap) { 134 this.dataMap = dataMap; 135 } 136 137 public void setCallParameters(List parameters) { 138 clearCallParameters(); 139 callParameters.addAll(parameters); 140 } 141 142 146 public void addCallParameter(ProcedureParameter param) { 147 if (param.getName() == null) { 148 throw new IllegalArgumentException ("Attempt to add unnamed parameter."); 149 } 150 151 if (callParameters.contains(param)) { 152 throw new IllegalArgumentException ( 153 "Attempt to add the same parameter more than once:" + param); 154 } 155 156 param.setProcedure(this); 157 callParameters.add(param); 158 } 159 160 161 public void removeCallParameter(String name) { 162 for (int i = 0; i < callParameters.size(); i++) { 163 ProcedureParameter nextParam = (ProcedureParameter) callParameters.get(i); 164 if (name.equals(nextParam.getName())) { 165 callParameters.remove(i); 166 break; 167 } 168 } 169 } 170 171 public void clearCallParameters() { 172 callParameters.clear(); 173 } 174 175 178 public List getCallParameters() { 179 return Collections.unmodifiableList(callParameters); 180 } 181 182 186 public List getCallOutParameters() { 187 List outParams = new ArrayList (callParameters.size()); 188 Iterator it = callParameters.iterator(); 189 while (it.hasNext()) { 190 ProcedureParameter param = (ProcedureParameter) it.next(); 191 if (param.isOutParam()) { 192 outParams.add(param); 193 } 194 } 195 196 return outParams; 197 } 198 199 204 public ProcedureParameter getResultParam() { 205 return (returningValue && callParameters.size() > 0) 208 ? (ProcedureParameter) callParameters.get(0) 209 : null; 210 } 211 212 219 public boolean isReturningValue() { 220 return returningValue; 221 } 222 223 public void setReturningValue(boolean returningValue) { 224 this.returningValue = returningValue; 225 } 226 227 public String getCatalog() { 228 return catalog; 229 } 230 231 public String getSchema() { 232 return schema; 233 } 234 235 238 public void setCatalog(String string) { 239 catalog = string; 240 } 241 242 245 public void setSchema(String string) { 246 schema = string; 247 } 248 } 249 | Popular Tags |