001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Arnaud Kervern <akervern@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.directory.sql.filter;
021
022import org.apache.commons.lang.builder.EqualsBuilder;
023import org.apache.commons.lang.builder.HashCodeBuilder;
024import org.nuxeo.ecm.core.storage.sql.jdbc.db.Column;
025import org.nuxeo.ecm.directory.DirectoryException;
026
027import java.io.Serializable;
028import java.sql.PreparedStatement;
029import java.sql.SQLException;
030
031/**
032 * Simple class to provide a complex filter that handles right side part and operator to use while querying
033 * org.nuxeo.ecm.directory.sql.SQLDirectory Warning, when using a complex filter fulltext is ignored on the field.
034 * 
035 * @since 5.7
036 * @see org.nuxeo.ecm.directory.sql.SQLSession#query(java.util.Map, java.util.Set, java.util.Map, boolean, int, int)
037 */
038public abstract class SQLComplexFilter implements Serializable {
039
040    protected String operator;
041
042    protected Serializable value;
043
044    public SQLComplexFilter(String operator) {
045        this.operator = operator;
046    }
047
048    public int setFieldValue(PreparedStatement ps, int index, Column column) throws DirectoryException {
049        try {
050            return doSetFieldValue(ps, index, column);
051        } catch (SQLException e) {
052            throw new DirectoryException("SQLComplexFilter setFieldValue failed", e);
053        }
054
055    }
056
057    public abstract int doSetFieldValue(PreparedStatement ps, int index, Column column) throws SQLException;
058
059    public String getRightSide() {
060        return "?";
061    }
062
063    public String getOperator() {
064        return " " + operator + " ";
065    }
066
067    @Override
068    public int hashCode() {
069        return HashCodeBuilder.reflectionHashCode(this);
070    }
071
072    @Override
073    public boolean equals(Object obj) {
074        return EqualsBuilder.reflectionEquals(this, obj);
075    }
076}