Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
eobjects.org AnalyzerBeans Copyright (C) 2010 eobjects.org This copyrighted material is made available to anyone wishing to use, modify, copy, or redistribute it subject to the terms and conditions of the GNU Lesser General Public License, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this distribution; if not, write to: Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
 
 package org.eobjects.analyzer.beans;
 
 import java.util.List;
 
An analyzer that performs a check on a weak foreign key / reference.

Author(s):
Kasper Sørensen
 
 @AnalyzerBean("Referential Integrity validator")
 public class ReferentialIntegrityValidator implements Explorer<DataSetResult> {
 
 	private Logger logger = LoggerFactory.getLogger(getClass());
 	private List<RowinvalidRows;
 
 	@Configured(value = "Primary key column", order = 1)
 
 	@Configured(value = "Foreign key column", order = 2)
 
 	@Configured(value = "Accept NULL foreign keys?", order = 3)
Returns a query with the following select items:
  1. the primary key value
  2. the foreign key value
  3. the remaining "informational" values from the columns of the foreign table

Returns:
 
 	public Query createQuery() {
 		Table primaryTable = .getTable();
 		Table foreignTable = .getTable();
 		List<ColumninformationalForeignColumns = new ArrayList<Column>();
 		for (Column column : foreignTable.getColumns()) {
 			if (column != ) {
 				informationalForeignColumns.add(column);
 			}
 		}
 
 		Query leftQuery = new Query().select()
 				.select(informationalForeignColumns.toArray(new Column[informationalForeignColumns.size()]))
 				.from(foreignTable);
 		Query rightQuery = new Query().select().from(primaryTable);
 		if (.isDebugEnabled()) {
 			.debug("Left query: " + leftQuery);
 			.debug("Right query: " + rightQuery);
 		}
 
 		FromItem leftSide = new FromItem(leftQuery).setAlias("a");
 		FromItem rightSide = new FromItem(rightQuery).setAlias("b");
 
 		SelectClause leftSelectClause = leftQuery.getSelectClause();
 		SelectClause rightSelectClause = rightQuery.getSelectClause();
 
 		SelectItem leftOn = leftSelectClause.getItem(0);
 		SelectItem rightOn = rightSelectClause.getItem(0);
		// Create master query
		Query q = new Query().from(new FromItem(.leftSiderightSidenew SelectItem[] { leftOn },
				new SelectItem[] { rightOn }));
		for (SelectItem si : rightSelectClause.getItems()) {
			q.select(new SelectItem(sirightSide));
		}
		for (SelectItem si : leftSelectClause.getItems()) {
			q.select(new SelectItem(sileftSide));
		}
		return q;
	}
	public void run(DataContext dc) {
		SelectItem foreignKeySelectItem = q.getSelectClause().getItem(1);
		SelectItem primaryKeySelectItem = q.getSelectClause().getItem(0);
		DataSet dataSet = dc.executeQuery(q);
		while (dataSet.next()) {
			Row row = dataSet.getRow();
			Object foreignKey = row.getValue(foreignKeySelectItem);
			if (foreignKey == null) {
						.info("Accepting row with NULL primary key: " + row);
					}
else {
				}
else {
				Object primaryKey = row.getValue(primaryKeySelectItem);
				if (primaryKey == null) {
else if (!primaryKey.equals(foreignKey)) {
						.warn("Unexpected state! Primary and foreign key values are not null and different! PK="
primaryKey + ", FK=" + foreignKey);
					}
				}
			}
		}
		dataSet.close();
	}
	public void setAcceptNullForeignKey(boolean acceptNullForeignKey) {
		this. = acceptNullForeignKey;
	}
	public void setPrimaryKeyColumn(Column idColumn) {
		this. = idColumn;
	}
	public void setForeignKeyColumn(Column parentIdColumn) {
		this. = parentIdColumn;
	}
		// TODO: Should probably use validation result?
	}
New to GrepCode? Check out our FAQ X