001/*
002 * (C) Copyright 2006-2018 Nuxeo (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 java.io.Serializable;
023import java.sql.PreparedStatement;
024import java.sql.SQLException;
025
026import org.apache.commons.lang3.builder.EqualsBuilder;
027import org.apache.commons.lang3.builder.HashCodeBuilder;
028import org.nuxeo.ecm.core.storage.sql.jdbc.db.Column;
029import org.nuxeo.ecm.directory.DirectoryException;
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 * @deprecated since 10.3, use {@link org.nuxeo.ecm.directory.sql.SQLSession#query(org.nuxeo.ecm.core.query.sql.model.QueryBuilder)} instead
038 */
039@Deprecated
040public abstract class SQLComplexFilter implements Serializable {
041
042    protected String operator;
043
044    protected Serializable value;
045
046    public SQLComplexFilter(String operator) {
047        this.operator = operator;
048    }
049
050    public int setFieldValue(PreparedStatement ps, int index, Column column) {
051        try {
052            return doSetFieldValue(ps, index, column);
053        } catch (SQLException e) {
054            throw new DirectoryException("SQLComplexFilter setFieldValue failed", e);
055        }
056
057    }
058
059    public abstract int doSetFieldValue(PreparedStatement ps, int index, Column column) throws SQLException;
060
061    public String getRightSide() {
062        return "?";
063    }
064
065    public String getOperator() {
066        return " " + operator + " ";
067    }
068
069    @Override
070    public int hashCode() {
071        return HashCodeBuilder.reflectionHashCode(this);
072    }
073
074    @Override
075    public boolean equals(Object obj) {
076        return EqualsBuilder.reflectionEquals(this, obj);
077    }
078}