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, boolean)} instead
038 */
039@Deprecated
040public abstract class SQLComplexFilter implements Serializable {
041
042    private static final long serialVersionUID = 1L;
043
044    protected String operator;
045
046    protected Serializable value;
047
048    public SQLComplexFilter(String operator) {
049        this.operator = operator;
050    }
051
052    public int setFieldValue(PreparedStatement ps, int index, Column column) {
053        try {
054            return doSetFieldValue(ps, index, column);
055        } catch (SQLException e) {
056            throw new DirectoryException("SQLComplexFilter setFieldValue failed", e);
057        }
058
059    }
060
061    public abstract int doSetFieldValue(PreparedStatement ps, int index, Column column) throws SQLException;
062
063    public String getRightSide() {
064        return "?";
065    }
066
067    public String getOperator() {
068        return " " + operator + " ";
069    }
070
071    @Override
072    public int hashCode() {
073        return HashCodeBuilder.reflectionHashCode(this);
074    }
075
076    @Override
077    public boolean equals(Object obj) {
078        return EqualsBuilder.reflectionEquals(this, obj);
079    }
080}