1 19 20 package org.apache.cayenne.map; 21 22 import java.io.Serializable ; 23 24 import org.apache.cayenne.dba.TypesMapping; 25 import org.apache.cayenne.util.CayenneMapEntry; 26 import org.apache.cayenne.util.Util; 27 import org.apache.cayenne.util.XMLEncoder; 28 import org.apache.cayenne.util.XMLSerializable; 29 30 35 public class ProcedureParameter implements CayenneMapEntry, XMLSerializable, Serializable { 36 37 public static final int IN_OUT_PARAMETER = 3; 38 public static final int IN_PARAMETER = 1; 39 public static final int OUT_PARAMETER = 2; 40 41 protected String name; 42 protected Procedure procedure; 43 44 protected int direction = -1; 45 46 protected int maxLength = -1; 48 49 protected int precision = -1; 51 protected int type = TypesMapping.NOT_DEFINED; 52 53 56 public ProcedureParameter() { 57 } 58 59 public ProcedureParameter(String name) { 60 setName(name); 61 } 62 63 public ProcedureParameter(String name, int type, int direction) { 64 this(name); 65 setType(type); 66 setDirection(direction); 67 } 68 69 public String getName() { 70 return name; 71 } 72 73 public void setName(String name) { 74 this.name = name; 75 } 76 77 public Object getParent() { 78 return getProcedure(); 79 } 80 81 public void setParent(Object parent) { 82 if (parent != null && !(parent instanceof Procedure)) { 83 throw new IllegalArgumentException ("Expected null or Procedure, got: " 84 + parent); 85 } 86 87 setProcedure((Procedure) parent); 88 } 89 90 95 public void encodeAsXML(XMLEncoder encoder) { 96 encoder.print("<procedure-parameter name=\"" 97 + Util.encodeXmlAttribute(getName()) 98 + '\"'); 99 100 String type = TypesMapping.getSqlNameByType(getType()); 101 if (type != null) { 102 encoder.print(" type=\"" + type + '\"'); 103 } 104 105 if (getMaxLength() > 0) { 106 encoder.print(" length=\""); 107 encoder.print(getMaxLength()); 108 encoder.print('\"'); 109 } 110 111 if (getPrecision() > 0) { 112 encoder.print(" precision=\""); 113 encoder.print(getPrecision()); 114 encoder.print('\"'); 115 } 116 117 int direction = getDirection(); 118 if (direction == ProcedureParameter.IN_PARAMETER) { 119 encoder.print(" direction=\"in\""); 120 } 121 else if (direction == ProcedureParameter.IN_OUT_PARAMETER) { 122 encoder.print(" direction=\"in_out\""); 123 } 124 else if (direction == ProcedureParameter.OUT_PARAMETER) { 125 encoder.print(" direction=\"out\""); 126 } 127 128 encoder.println("/>"); 129 } 130 131 135 public int getDirection() { 136 return direction; 137 } 138 139 public int getMaxLength() { 140 return maxLength; 141 } 142 143 public int getPrecision() { 144 return precision; 145 } 146 147 public int getType() { 148 return type; 149 } 150 151 154 public boolean isInParameter() { 155 return direction == IN_PARAMETER || direction == IN_OUT_PARAMETER; 156 } 157 158 161 public boolean isOutParam() { 162 return direction == OUT_PARAMETER || direction == IN_OUT_PARAMETER; 163 } 164 165 170 public void setDirection(int direction) { 171 if (direction != IN_PARAMETER 172 && direction != OUT_PARAMETER 173 && direction != IN_OUT_PARAMETER) { 174 throw new IllegalArgumentException ("Unknown parameter type: " + direction); 175 } 176 177 this.direction = direction; 178 } 179 180 public void setMaxLength(int i) { 181 maxLength = i; 182 } 183 184 public void setPrecision(int i) { 185 precision = i; 186 } 187 188 public void setType(int i) { 189 type = i; 190 } 191 192 195 public Procedure getProcedure() { 196 return procedure; 197 } 198 199 202 public void setProcedure(Procedure procedure) { 203 this.procedure = procedure; 204 } 205 } 206 | Popular Tags |