001/*
002 * (C) Copyright 2006-2012 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Arnaud Kervern <akervern@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.directory.sql.filter;
019
020import org.apache.commons.lang.builder.EqualsBuilder;
021import org.apache.commons.lang.builder.HashCodeBuilder;
022import org.nuxeo.ecm.core.storage.sql.jdbc.db.Column;
023import org.nuxeo.ecm.directory.DirectoryException;
024
025import java.io.Serializable;
026import java.sql.PreparedStatement;
027import java.sql.SQLException;
028
029/**
030 * Simple class to provide a complex filter that handles right side part and operator to use while querying
031 * org.nuxeo.ecm.directory.sql.SQLDirectory Warning, when using a complex filter fulltext is ignored on the field.
032 * 
033 * @since 5.7
034 * @see org.nuxeo.ecm.directory.sql.SQLSession#query(java.util.Map, java.util.Set, java.util.Map, boolean, int, int)
035 */
036public abstract class SQLComplexFilter implements Serializable {
037
038    protected String operator;
039
040    protected Serializable value;
041
042    public SQLComplexFilter(String operator) {
043        this.operator = operator;
044    }
045
046    public int setFieldValue(PreparedStatement ps, int index, Column column) throws DirectoryException {
047        try {
048            return doSetFieldValue(ps, index, column);
049        } catch (SQLException e) {
050            throw new DirectoryException("SQLComplexFilter setFieldValue failed", e);
051        }
052
053    }
054
055    public abstract int doSetFieldValue(PreparedStatement ps, int index, Column column) throws SQLException;
056
057    public String getRightSide() {
058        return "?";
059    }
060
061    public String getOperator() {
062        return " " + operator + " ";
063    }
064
065    @Override
066    public int hashCode() {
067        return HashCodeBuilder.reflectionHashCode(this);
068    }
069
070    @Override
071    public boolean equals(Object obj) {
072        return EqualsBuilder.reflectionEquals(this, obj);
073    }
074}