001: /*
002: * User: mrettig
003: * Date: Jun 24, 2002
004: * Time: 5:25:33 PM
005: */
006: package net.sourceforge.jaxor.mappers;
007:
008: import net.sourceforge.jaxor.MetaField;
009: import net.sourceforge.jaxor.SQLValidationException;
010: import net.sourceforge.jaxor.api.FieldAdapter;
011: import net.sourceforge.jaxor.api.Mapper;
012: import net.sourceforge.jaxor.api.ValueChangeListener;
013: import net.sourceforge.jaxor.api.MutationInterceptor;
014: import net.sourceforge.jaxor.util.ObjectUtils;
015: import net.sourceforge.jaxor.util.ValueHolderImpl;
016: import net.sourceforge.jaxor.util.JaxorDate;
017: import net.sourceforge.jaxor.util.JaxorTimestamp;
018:
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Date;
023: import java.sql.Timestamp;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026:
027: public class FieldAdapterImpl extends ValueHolderImpl implements
028: FieldAdapter {
029:
030: private final MetaField _meta;
031: private final ArrayList _listeners = new ArrayList();
032: private final Mapper _mapper;
033:
034: public FieldAdapterImpl(MetaField metaData, Object value) {
035: _meta = metaData;
036: _mapper = metaData.getMapperInstance();
037: super .setValue(value);
038: }
039:
040: /**
041: * Does a null check first before mapping sql object
042: * @return
043: */
044: public Object getMappedSqlObject() {
045: if (isNull())
046: return null;
047: return _mapper.mapToSqlObject(getValue());
048: }
049:
050: public String getName() {
051: return _meta.getColumn();
052: }
053:
054: public void setValue(Object obj) {
055: Object oldValue = getValue();
056: obj = wrapIfMutable(obj);
057: if (oldValue instanceof MutationInterceptor) {
058: MutationInterceptor interceptor = (MutationInterceptor) oldValue;
059: oldValue = interceptor.getDecoratedObject();
060: interceptor.setFieldAdapter(null);
061: }
062: super .setValue(obj);
063: registerChange(obj, oldValue);
064: }
065:
066: private Object wrapIfMutable(Object obj) {
067: if (obj == null)
068: return null;
069:
070: //unwrap object so we can work with the original
071: if (obj instanceof MutationInterceptor)
072: obj = ((MutationInterceptor) obj).getDecoratedObject();
073:
074: if (obj.getClass().equals(java.sql.Date.class))
075: return new JaxorDate((Date) obj, this );
076: if (obj.getClass().equals(Timestamp.class))
077: return new JaxorTimestamp(((Timestamp) obj).getTime(), this );
078:
079: return obj;
080: }
081:
082: public void registerChange(Object obj, Object oldValue) {
083: for (Iterator i = _listeners.iterator(); i.hasNext();) {
084: ValueChangeListener l = (ValueChangeListener) i.next();
085: l.registerChange(obj, oldValue, this );
086: }
087: }
088:
089: public void validate() {
090: validateNulls();
091: _mapper.validate(getValue(), _meta);
092: }
093:
094: public void validateLoad() {
095: validateNulls();
096: if (isNull() && _meta.isPrimaryKey())
097: throw new SQLValidationException(
098: "Primary Key cannot be null " + _meta.getColumn());
099: }
100:
101: private void validateNulls() {
102: if (isNull() && !_meta.canBeNull())
103: throw new SQLValidationException(
104: "Unexpected null in column " + _meta.getColumn());
105: }
106:
107: public String toString() {
108: return _meta.getColumn() + " = " + getValue();
109: }
110:
111: public void addToStatement(PreparedStatement statement,
112: int parameterIndex) throws SQLException {
113: addToStatement(statement, parameterIndex, _mapper
114: .mapToSqlObject(getValue()));
115: }
116:
117: public void addToStatement(PreparedStatement statement,
118: int parameterIndex, Object value) throws SQLException {
119: if (isNull()) {
120: statement.setNull(parameterIndex, _mapper.getSQLType());
121: } else {
122: statement.setObject(parameterIndex, value);
123: }
124: }
125:
126: public boolean isNull() {
127: return getValue() == null;
128: }
129:
130: public int hashCode() {
131: return _meta.getColumn().hashCode();
132: }
133:
134: public boolean equals(Object obj) {
135: if (obj instanceof FieldAdapterImpl)
136: return equals((FieldAdapterImpl) obj);
137: return false;
138: }
139:
140: public MetaField getMetaField() {
141: return _meta;
142: }
143:
144: public boolean equals(FieldAdapterImpl field) {
145: if (!ObjectUtils.equals(getValue(), field.getValue()))
146: return false;
147: return _meta.equals(field.getMetaField());
148: }
149:
150: public void addChangeListener(ValueChangeListener listener) {
151: if (listener != null)
152: _listeners.add(listener);
153: }
154:
155: public void removeChangeListener(ValueChangeListener listener) {
156: if (listener != null)
157: _listeners.remove(listener);
158: }
159:
160: public void removeChangeListeners() {
161: _listeners.clear();
162: }
163:
164: public void setValueFromResultSet(ResultSet rs) throws SQLException {
165: setValue(_mapper.getValueFromResultSet(rs, this ));
166: }
167:
168: public boolean isPrimaryKey() {
169: return _meta.isPrimaryKey();
170: }
171:
172: public boolean isMatchOnUpdate() {
173: return getMetaField().isMatchOnUpdate();
174: }
175:
176: }
|