001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency;
010:
011: import com.completex.objective.util.PropertyMap;
012: import com.completex.objective.components.persistency.type.ValueStreamHelper;
013: import com.completex.objective.components.persistency.type.ValueStringResult;
014:
015: import java.util.Map;
016: import java.util.HashMap;
017:
018: /**
019: * @author Gennady Krizhevsky
020: */
021: public class Parameter implements Mappable {
022:
023: public static final String TAG_BINARY = "binary"; // True if value is of unknown binary type
024: public static final String TAG_VALUE = "value";
025: public static final String TAG_COLUMN_TYPE = "columnType";
026: public static final String TAG_COLUMN_TYPE_CLASS = "columnTypeClass";
027:
028: private ColumnType type = ColumnType.OBJECT;
029: private Object value;
030:
031: //
032: // Optional:
033: //
034: private int jdbcType;
035: private String jdbcTypeName;
036:
037: protected Parameter() {
038: }
039:
040: /**
041: * Creates parameter restoring its state from the map
042: *
043: * @param parameter map representation of this parameter
044: */
045: public Parameter(Map parameter) {
046: fromMap(parameter);
047: }
048:
049: /**
050: * @param value value of this parameter.
051: */
052: public Parameter(Object value) {
053: this .value = value;
054: }
055:
056: /**
057: * @param type column type of the field this parameter corresponds to
058: * @param value value of this parameter.
059: */
060: public Parameter(ColumnType type, Object value) {
061: this .type = type;
062: this .value = value;
063: }
064:
065: /**
066: *
067: * @param type column type of the field this parameter corresponds to
068: * @param value value of this parameter
069: * @param jdbcType one of java.sql.Types
070: */
071: public Parameter(ColumnType type, Object value, int jdbcType) {
072: this .type = type;
073: this .value = value;
074: this .jdbcType = jdbcType;
075: }
076:
077: /**
078: *
079: * @param type column type of the field this parameter corresponds to
080: * @param value value of this parameter
081: * @param jdbcType one of java.sql.Types
082: * @param jdbcTypeName jdbc type name
083: */
084: public Parameter(ColumnType type, Object value, int jdbcType,
085: String jdbcTypeName) {
086: this .type = type;
087: this .value = value;
088: this .jdbcType = jdbcType;
089: this .jdbcTypeName = jdbcTypeName;
090:
091: }
092:
093: /**
094: * return column type of the field this parameter corresponds to
095: *
096: * @return column type of the field this parameter corresponds to
097: */
098: public ColumnType getType() {
099: return type;
100: }
101:
102: /**
103: * return value of this parameter.
104: *
105: * @return value of this parameter.
106: */
107: public Object getValue() {
108: return value;
109: }
110:
111: /**
112: * Sets value of this parameter.
113: *
114: * @param value value of this parameter.
115: */
116: protected void setValue(Object value) {
117: this .value = value;
118: }
119:
120: /**
121: * @see com.completex.objective.components.persistency.Mappable#fromMap(java.util.Map)
122: */
123: public void fromMap(Map parameter) {
124: fromMap0(parameter);
125: }
126:
127: /**
128: * Restores object state from map
129: *
130: * @param parameter map representation of this object
131: * @return PropertyMap wrapper of the parameter
132: */
133: protected PropertyMap fromMap0(Map parameter) {
134: PropertyMap parameterMap = PropertyMap.toPropertyMap(parameter);
135: Object value = parameterMap.get(TAG_VALUE, true);
136: String columnTypeName = parameterMap
137: .getProperty(TAG_COLUMN_TYPE);
138: this .type = ColumnType.toColumnType(columnTypeName);
139: Boolean bin = parameterMap.getBooleanObj(TAG_BINARY,
140: Boolean.FALSE, false);
141: boolean binary = bin.booleanValue();
142: if (value instanceof String) {
143: this .value = ValueStreamHelper.string2value(binary,
144: ((String) value), false, type, "");
145: } else {
146: this .value = value;
147: }
148: return parameterMap;
149: }
150:
151: /**
152: * @see com.completex.objective.components.persistency.Mappable#toMap()
153: */
154: public Map toMap() {
155: HashMap map = new HashMap();
156: map.put(TAG_COLUMN_TYPE, type.getName());
157: map.put(TAG_COLUMN_TYPE_CLASS, type.getClass().getName());
158: ValueStringResult result = ValueStreamHelper.value2string(type,
159: value);
160: map.put(TAG_BINARY, Boolean.valueOf(result.isBinary()));
161: map.put(TAG_VALUE, result.getValueString());
162: return map;
163: }
164:
165: public String toString() {
166: return new StringBuffer().append(super .toString()).append(
167: "; value = ").append(value).append("; type = ").append(
168: type).toString();
169: }
170:
171: public int getJdbcType() {
172: return jdbcType;
173: }
174:
175: /**
176: * Sets jdbcType - one of java.sql.Types types
177: *
178: * @param jdbcType one of java.sql.Types types
179: */
180: public void setJdbcType(int jdbcType) {
181: this .jdbcType = jdbcType;
182: }
183:
184: public String getJdbcTypeName() {
185: return jdbcTypeName;
186: }
187:
188: /**
189: * Sets jdbcTypeName - the fully-qualified name of an SQL structured type
190: *
191: * @param jdbcTypeName
192: */
193: public void setJdbcTypeName(String jdbcTypeName) {
194: this.jdbcTypeName = jdbcTypeName;
195: }
196: }
|