001: /*
002: * User: mrettig
003: * Date: Jun 24, 2002
004: * Time: 5:30:30 PM
005: */
006: package net.sourceforge.jaxor;
007:
008: import net.sourceforge.jaxor.api.FieldAdapter;
009: import net.sourceforge.jaxor.api.FieldLifeCycleListener;
010: import net.sourceforge.jaxor.api.JaxorContext;
011: import net.sourceforge.jaxor.api.ValueChangeListener;
012: import net.sourceforge.jaxor.util.ForeignFieldLazyLoader;
013: import net.sourceforge.jaxor.util.SystemException;
014:
015: import java.io.Serializable;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: /**
022: * This class represents the row data for a concrete instance of the
023: * AbstractEntity. All of the attributes defined in the Jaxor file
024: * are managed by instances of this class.
025: */
026:
027: public class EntityRow implements Serializable {
028: private FieldAdapter[] _list = new FieldAdapter[0];
029: private final List _foreignFields = new ArrayList();
030: private final String _table;
031: private PrimaryKeySet _pkset = new PrimaryKeySet(
032: new FieldAdapter[0]);
033: private List _updateMatchValues;
034: private JaxorContext _context;
035:
036: public EntityRow(String table) {
037: _table = table;
038: }
039:
040: public void setJaxorContext(JaxorContext context) {
041: _context = context;
042: }
043:
044: public JaxorContext getJaxorContext() {
045: return _context;
046: }
047:
048: public void setFields(FieldAdapter[] fields) {
049: _list = fields;
050: }
051:
052: public FieldAdapter[] getList() {
053: return _list;
054: }
055:
056: public void setPrimaryKey(PrimaryKeySet primaryKey) {
057: _pkset = primaryKey;
058: }
059:
060: public void validate() {
061: for (int i = 0; i < _list.length; i++) {
062: FieldAdapter fieldAdapter = _list[i];
063: fieldAdapter.validate();
064: }
065: }
066:
067: /**
068: * This method allows a ValueChangeListener to be added to each of
069: * the mappers in the row.
070: *
071: * @param listener the change listener to registerLoad
072: */
073:
074: public void registerLoad(ValueChangeListener listener) {
075: initUpdateMatchValues();
076: register(listener);
077: }
078:
079: public void register(ValueChangeListener listener) {
080: for (int i = 0; i < _list.length; i++) {
081: FieldAdapter fieldAdapter = _list[i];
082: fieldAdapter.addChangeListener(listener);
083: }
084: }
085:
086: /**
087: * This method allows a ValueChangeListener to be removed from each
088: * of the mappers in the row.
089: *
090: * @param listener the change listener to remove
091: */
092:
093: public void unregisterListener(ValueChangeListener listener) {
094: for (int i = 0; i < _list.length; i++) {
095: FieldAdapter adapter = _list[i];
096: adapter.removeChangeListener(listener);
097: }
098: }
099:
100: public String getTable() {
101: return _table;
102: }
103:
104: public String toString() {
105: StringBuffer buffer = new StringBuffer(_table);
106: for (int i = 0; i < _list.length; i++) {
107: buffer.append("\n");
108: FieldAdapter entityField = _list[i];
109: buffer.append(entityField.toString());
110: }
111: return buffer.toString();
112: }
113:
114: public int hashCode() {
115: return _table.hashCode();
116: }
117:
118: public boolean equals(Object obj) {
119: if (obj instanceof EntityRow)
120: return equals((EntityRow) obj);
121: return false;
122: }
123:
124: public int size() {
125: return _list.length;
126: }
127:
128: public boolean equals(EntityRow otherRow) {
129: if (this == otherRow)
130: return true;
131:
132: if (size() != otherRow.size())
133: return false;
134:
135: if (!getTable().equals(otherRow.getTable()))
136: return false;
137:
138: for (int i = 0; i < _list.length; i++) {
139: FieldAdapter entityField = _list[i];
140: if (!entityField.equals(otherRow.getField(entityField
141: .getName())))
142: return false;
143: }
144: return true;
145: }
146:
147: public FieldAdapter getField(String name) {
148: for (int i = 0; i < _list.length; i++) {
149: FieldAdapter entityField = _list[i];
150: if (entityField.getName().equals(name))
151: return entityField;
152: }
153: return null;
154: }
155:
156: /**
157: * Gets all values as mapped sql objects
158: * @return
159: */
160: public Object[] getAllValues() {
161: Object[] vals = new Object[size()];
162: int count = 0;
163: for (int i = 0; i < _list.length; i++) {
164: FieldAdapter adapter = _list[i];
165: vals[count] = adapter.getMappedSqlObject();
166: count++;
167: }
168: return vals;
169: }
170:
171: /**
172: * This method returns the primary keys for the object based on
173: * the attribute values added during the derived object's
174: * constructor.
175: *
176: * @return the set containing the primary keys for this entity
177: */
178:
179: public PrimaryKeySet getPrimaryKey() {
180: return _pkset;
181: }
182:
183: public List getUpdateMatchValues() {
184: if (_updateMatchValues == null)
185: initUpdateMatchValues();
186: return _updateMatchValues;
187: }
188:
189: private void initUpdateMatchValues() {
190: _updateMatchValues = new ArrayList();
191:
192: for (int i = 0; i < _list.length; i++) {
193: FieldAdapter adapter = _list[i];
194: if (adapter.isMatchOnUpdate()) {
195: SqlAdapter.MatchUpdate matchUpdate = new SqlAdapter.MatchUpdate();
196: matchUpdate.mapper = adapter;
197: matchUpdate.value = matchUpdate.mapper
198: .getMappedSqlObject();
199: _updateMatchValues.add(matchUpdate);
200: }
201: }
202: }
203:
204: public void load(ResultSet rs) throws SQLException {
205: for (int i = 0; i < _list.length; i++) {
206: FieldAdapter adapter = _list[i];
207: adapter.setValueFromResultSet(rs);
208: adapter.validateLoad();
209: }
210: }
211:
212: public void notifyFieldsOfInsert(JaxorContext context) {
213: for (int i = 0; i < _list.length; i++) {
214: FieldAdapter fieldAdapter = _list[i];
215: if (fieldAdapter instanceof FieldLifeCycleListener)
216: ((FieldLifeCycleListener) fieldAdapter)
217: .beforeInsert(context);
218: }
219: }
220:
221: public void notifyFieldsOfUpdate(JaxorContext context) {
222: for (int i = 0; i < _list.length; i++) {
223: FieldAdapter fieldAdapter = _list[i];
224: if (fieldAdapter instanceof FieldLifeCycleListener)
225: ((FieldLifeCycleListener) fieldAdapter)
226: .beforeUpdate(context);
227: }
228: }
229:
230: public void notifyFieldsOfDelete(JaxorContext context) {
231: for (int i = 0; i < _list.length; i++) {
232: FieldAdapter fieldAdapter = _list[i];
233: if (fieldAdapter instanceof FieldLifeCycleListener)
234: ((FieldLifeCycleListener) fieldAdapter)
235: .beforeDelete(context);
236: }
237: }
238:
239: public List getForeignFields() {
240: return _foreignFields;
241: }
242:
243: public void addForeignField(ForeignFieldLazyLoader foreignField) {
244: _foreignFields.add(foreignField);
245: }
246:
247: /**
248: * Returns a value based upon a property name. If the property is not found, throws a system exception.
249: * @param propertyName
250: * @return
251: */
252: public Object getPropertyValue(String propertyName) {
253: for (int i = 0; i < _list.length; i++) {
254: FieldAdapter fieldAdapter = _list[i];
255: if (fieldAdapter.getMetaField().getProperty().equals(
256: propertyName))
257: return fieldAdapter.getValue();
258: }
259: throw new SystemException("Failed to Find property: "
260: + propertyName);
261: }
262:
263: public void setPropertyValue(String property, Object value) {
264: for (int i = 0; i < _list.length; i++) {
265: FieldAdapter fieldAdapter = _list[i];
266: if (fieldAdapter.getMetaField().getProperty().equals(
267: property)) {
268: fieldAdapter.setValue(value);
269: return;
270: }
271: }
272: throw new SystemException("Failed to Find property: "
273: + property);
274: }
275: }
|