<commit_before>/*
 * Copyright (c) 2010-2017 Evolveum
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.evolveum.midpoint.provisioning.ucf.impl.connid.query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.PrismPropertyValue;

import com.evolveum.midpoint.prism.query.*;
import org.identityconnectors.framework.common.objects.Attribute;
import org.identityconnectors.framework.common.objects.AttributeBuilder;
import org.identityconnectors.framework.common.objects.OperationalAttributes;
import org.identityconnectors.framework.common.objects.filter.Filter;
import org.identityconnectors.framework.common.objects.filter.FilterBuilder;

import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.provisioning.ucf.impl.connid.ConnIdNameMapper;
import com.evolveum.midpoint.provisioning.ucf.impl.connid.ConnIdUtil;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LockoutStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;

public class ValueOperation extends Operation {

	public ValueOperation(FilterInterpreter interpreter) {
		super(interpreter);
	}

	@Override
	public <T> Filter interpret(ObjectFilter objectFilter, ConnIdNameMapper icfNameMapper) throws SchemaException {

		OperationResult parentResult = new OperationResult("interpret");

		ValueFilter valueFilter= (ValueFilter) objectFilter;
		if (valueFilter.getParentPath().isEmpty()) {
			throw new UnsupportedOperationException("Empty path is not supported (filter: " + objectFilter+")");
		}
		if (valueFilter.getParentPath().equivalent(new ItemPath(ShadowType.F_ATTRIBUTES))) {
			try {
				QName propName = valueFilter.getDefinition().getName();
				String icfName = icfNameMapper.convertAttributeNameToIcf(propName, getInterpreter()
						.getObjectClassDefinition(), "(attribute in the filter)");
				
				if (objectFilter instanceof EqualFilter) {
					EqualFilter<T> eq = (EqualFilter<T>) objectFilter;
					
					Collection<Object> convertedValues = convertValues(propName, eq.getValues());
					if (convertedValues == null || convertedValues.isEmpty()) {
						// See MID-1460
						throw new UnsupportedOperationException("Equals filter with a null value is NOT supported by ICF");
					} else {
						Attribute attr = AttributeBuilder.build(icfName, convertedValues);
						if (valueFilter.getDefinition().isSingleValue()) {
							return FilterBuilder.equalTo(attr);
						} else {
							return FilterBuilder.containsAllValues(attr);
						}
					}
					
				} else if (objectFilter instanceof SubstringFilter) {
					SubstringFilter substring = (SubstringFilter) objectFilter;
                    Collection<Object> convertedValues = convertValues(propName, substring.getValues());
                    if (convertedValues.isEmpty()) {
                        throw new IllegalArgumentException("Substring filter with null value makes no sense");
                    } else {
                        if (convertedValues.size() != 1) {
                            throw new IllegalArgumentException("Substring filter with multiple values makes no sense");
                            //maybe it does, through OR clauses
                        }
                        return FilterBuilder.contains(AttributeBuilder.build(icfName, convertedValues.iterator().next()));
                    }
				} else if (objectFilter instanceof ComparativeFilter) {
					ComparativeFilter comparativeFilter = (ComparativeFilter) objectFilter;
					Collection<Object> convertedValues = convertValues(propName, comparativeFilter.getValues());
					if (convertedValues.isEmpty()) {
						throw new IllegalArgumentException("Comparative filter with null value makes no sense");
					} else {
						if (convertedValues.size() != 1) {
							throw new IllegalArgumentException("Comparative filter with multiple values makes no sense");
						}
						Attribute attribute = AttributeBuilder.build(icfName, convertedValues.iterator().next().toString());        // HACK TODO
						if (comparativeFilter instanceof GreaterFilter) {
							if (comparativeFilter.isEquals()) {<commit_msg>Remove toString() hack for attribute comparison<commit_after>/*
 * Copyright (c) 2010-2017 Evolveum
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.evolveum.midpoint.provisioning.ucf.impl.connid.query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.PrismPropertyValue;

import com.evolveum.midpoint.prism.query.*;
import org.identityconnectors.framework.common.objects.Attribute;
import org.identityconnectors.framework.common.objects.AttributeBuilder;
import org.identityconnectors.framework.common.objects.OperationalAttributes;
import org.identityconnectors.framework.common.objects.filter.Filter;
import org.identityconnectors.framework.common.objects.filter.FilterBuilder;

import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.provisioning.ucf.impl.connid.ConnIdNameMapper;
import com.evolveum.midpoint.provisioning.ucf.impl.connid.ConnIdUtil;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LockoutStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;

public class ValueOperation extends Operation {

	public ValueOperation(FilterInterpreter interpreter) {
		super(interpreter);
	}

	@Override
	public <T> Filter interpret(ObjectFilter objectFilter, ConnIdNameMapper icfNameMapper) throws SchemaException {

		OperationResult parentResult = new OperationResult("interpret");

		ValueFilter valueFilter= (ValueFilter) objectFilter;
		if (valueFilter.getParentPath().isEmpty()) {
			throw new UnsupportedOperationException("Empty path is not supported (filter: " + objectFilter+")");
		}
		if (valueFilter.getParentPath().equivalent(new ItemPath(ShadowType.F_ATTRIBUTES))) {
			try {
				QName propName = valueFilter.getDefinition().getName();
				String icfName = icfNameMapper.convertAttributeNameToIcf(propName, getInterpreter()
						.getObjectClassDefinition(), "(attribute in the filter)");
				
				if (objectFilter instanceof EqualFilter) {
					EqualFilter<T> eq = (EqualFilter<T>) objectFilter;
					
					Collection<Object> convertedValues = convertValues(propName, eq.getValues());
					if (convertedValues == null || convertedValues.isEmpty()) {
						// See MID-1460
						throw new UnsupportedOperationException("Equals filter with a null value is NOT supported by ICF");
					} else {
						Attribute attr = AttributeBuilder.build(icfName, convertedValues);
						if (valueFilter.getDefinition().isSingleValue()) {
							return FilterBuilder.equalTo(attr);
						} else {
							return FilterBuilder.containsAllValues(attr);
						}
					}
					
				} else if (objectFilter instanceof SubstringFilter) {
					SubstringFilter substring = (SubstringFilter) objectFilter;
                    Collection<Object> convertedValues = convertValues(propName, substring.getValues());
                    if (convertedValues.isEmpty()) {
                        throw new IllegalArgumentException("Substring filter with null value makes no sense");
                    } else {
                        if (convertedValues.size() != 1) {
                            throw new IllegalArgumentException("Substring filter with multiple values makes no sense");
                            //maybe it does, through OR clauses
                        }
                        return FilterBuilder.contains(AttributeBuilder.build(icfName, convertedValues.iterator().next()));
                    }
				} else if (objectFilter instanceof ComparativeFilter) {
					ComparativeFilter comparativeFilter = (ComparativeFilter) objectFilter;
					Collection<Object> convertedValues = convertValues(propName, comparativeFilter.getValues());
					if (convertedValues.isEmpty()) {
						throw new IllegalArgumentException("Comparative filter with null value makes no sense");
					} else {
						if (convertedValues.size() != 1) {
							throw new IllegalArgumentException("Comparative filter with multiple values makes no sense");
						}
						Attribute attribute = AttributeBuilder.build(icfName, convertedValues.iterator().next());
						if (comparativeFilter instanceof GreaterFilter) {
							if (comparativeFilter.isEquals()) {
{'fraction_non_alphanumeric': 0.09330167210843969, 'fraction_numerical': 0.004353418422875235, 'mean_word_length': 3.8949152542372882, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6726666', 'n_tokens_mistral': 2978, 'n_tokens_neox': 2596, 'n_words': 649}