001: /*
002: * $Id: AbstractBaseTable.java,v 1.4 2005/12/20 18:32:28 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.tables;
042:
043: import java.util.Iterator;
044: import java.util.List;
045:
046: import org.axiondb.AxionException;
047: import org.axiondb.Constraint;
048: import org.axiondb.ConstraintViolationException;
049: import org.axiondb.Literal;
050: import org.axiondb.RowDecorator;
051: import org.axiondb.RowIterator;
052: import org.axiondb.Selectable;
053: import org.axiondb.Table;
054: import org.axiondb.engine.rowiterators.FilteringRowIterator;
055: import org.axiondb.event.BaseTableModificationPublisher;
056: import org.axiondb.event.RowEvent;
057: import org.axiondb.functions.AndFunction;
058: import org.axiondb.functions.EqualFunction;
059:
060: /**
061: * An abstract implementation of {@link Table}, code common between TransactableTableImpl
062: * and BaseTable
063: *
064: * @version $Revision: 1.4 $ $Date: 2005/12/20 18:32:28 $
065: * @author Ahimanikya Satapathy
066: */
067: public abstract class AbstractBaseTable extends
068: BaseTableModificationPublisher implements Table {
069:
070: public RowIterator getMatchingRows(List selectables, List values,
071: boolean readOnly) throws AxionException {
072: if (null == selectables || selectables.isEmpty()) {
073: return getRowIterator(readOnly);
074: }
075:
076: RowIterator baseIterator = null;
077: Selectable filter = null;
078: for (int i = 0, I = selectables.size(); i < I; i++) {
079: Selectable sel = (Selectable) selectables.get(i);
080: Object val = values.get(i);
081:
082: EqualFunction function = new EqualFunction();
083: function.addArgument(sel);
084: function.addArgument(new Literal(val));
085:
086: if (null == baseIterator) {
087: baseIterator = getIndexedRows(function, readOnly);
088: if (baseIterator != null) {
089: function = null;
090: }
091: }
092:
093: if (function != null) {
094: if (null == filter) {
095: filter = function;
096: } else {
097: AndFunction fn = new AndFunction();
098: fn.addArgument(filter);
099: fn.addArgument(function);
100: filter = fn;
101: }
102: }
103: }
104:
105: if (null == baseIterator) {
106: baseIterator = getRowIterator(readOnly);
107: }
108:
109: if (null != filter) {
110: return new FilteringRowIterator(baseIterator,
111: makeRowDecorator(), filter);
112: }
113: return baseIterator;
114: }
115:
116: public void migrate() throws AxionException {
117: }
118:
119: protected void checkConstraints(RowEvent event, RowDecorator dec)
120: throws AxionException {
121: if (isDeferAll()) {
122: return;
123: }
124: checkConstraints(event, false, dec);
125: }
126:
127: protected void checkConstraints(RowEvent event, boolean deferred,
128: RowDecorator dec) throws AxionException {
129: for (Iterator iter = getConstraints(); iter.hasNext();) {
130: Constraint c = (Constraint) iter.next();
131: if (c.isDeferred() == deferred) {
132: if (!c.evaluate(event, dec)) {
133: throw new ConstraintViolationException(c);
134: }
135: }
136: }
137: }
138:
139: protected void checkConstraints(RowIterator oldRows,
140: RowIterator newRows) throws AxionException {
141: for (Iterator iter = getConstraints(); iter.hasNext();) {
142: Constraint c = (Constraint) iter.next();
143: if (!c.evaluate(oldRows, newRows, this )) {
144: throw new ConstraintViolationException(c);
145: }
146: }
147: }
148:
149: protected boolean hasDeferredConstraint() {
150: if (isDeferAll()) {
151: return true;
152: }
153:
154: for (Iterator iter = getConstraints(); iter.hasNext();) {
155: Constraint c = (Constraint) iter.next();
156: if (c.isDeferred()) {
157: return true;
158: }
159: }
160: return false;
161: }
162:
163: protected boolean isDeferAll() {
164: return false;
165: }
166:
167: }
|