001: /*
002: * $Id: NotNullConstraint.java,v 1.16 2005/12/20 18:32:46 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 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.constraints;
042:
043: import java.util.Iterator;
044:
045: import org.axiondb.AxionException;
046: import org.axiondb.Row;
047: import org.axiondb.RowDecorator;
048: import org.axiondb.RowIterator;
049: import org.axiondb.Selectable;
050: import org.axiondb.Table;
051: import org.axiondb.event.RowEvent;
052:
053: /**
054: * A NOT NULL constraint, which is violated whenever one or more of the specified
055: * {@link Selectable}s is <code>null</code>.
056: *
057: * @version $Revision: 1.16 $ $Date: 2005/12/20 18:32:46 $
058: * @author Rodney Waldhoff
059: * @author Ahimanikya Satapathy
060: */
061: public class NotNullConstraint extends BaseSelectableBasedConstraint {
062: public NotNullConstraint() {
063: super (null, "NOT NULL");
064: }
065:
066: public NotNullConstraint(String name) {
067: super (name, "NOT NULL");
068: }
069:
070: public boolean evaluate(RowEvent event) throws AxionException {
071: return evaluate(event, event.getTable().makeRowDecorator());
072: }
073:
074: public boolean evaluate(RowEvent event, RowDecorator dec)
075: throws AxionException {
076: Row row = event.getNewRow();
077: if (null == row) {
078: return true;
079: }
080: return noneNull(dec, row, getSelectables());
081: }
082:
083: protected static boolean noneNull(RowDecorator dec, Row row,
084: Iterator selectables) throws AxionException {
085: dec.setRow(row);
086: while (selectables.hasNext()) {
087: Object value = ((Selectable) selectables.next())
088: .evaluate(dec);
089: if (null == value) {
090: return false;
091: }
092: }
093: return true;
094: }
095:
096: public boolean evaluate(RowIterator oldRows, RowIterator newRows,
097: Table table) throws AxionException {
098: if (null == newRows || newRows.isEmpty()) {
099: return true;
100: }
101: return noneNull(newRows, table, getSelectables());
102: }
103:
104: protected static boolean noneNull(RowIterator newRows, Table table,
105: Iterator selectables) throws AxionException {
106: RowDecorator dec = table.makeRowDecorator();
107: newRows.reset();
108: while (newRows.hasNext()) {
109: if (!noneNull(dec, newRows.next(), selectables)) {
110: return false;
111: }
112: }
113: return true;
114: }
115:
116: private static final long serialVersionUID = -5339792357598429782L;
117: }
|