All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yuehuanghun.mybatis.milu.generic.AbstractGenericExampleProviderSql Maven / Gradle / Ivy

There is a newer version: 1.18.0
Show newest version
/*
 * Copyright 2020-2022 the original author or authors.
 *
 * 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
 *
 *      https://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.yuehuanghun.mybatis.milu.generic;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.scripting.xmltags.OgnlCache;

import com.yuehuanghun.mybatis.milu.metamodel.Entity.Attribute;
import com.yuehuanghun.mybatis.milu.metamodel.Entity.RangeCondition;
import com.yuehuanghun.mybatis.milu.metamodel.KeyType;
import com.yuehuanghun.mybatis.milu.tool.Constants;
import com.yuehuanghun.mybatis.milu.tool.InstanceUtils;
import com.yuehuanghun.mybatis.milu.tool.StringUtils;
import com.yuehuanghun.mybatis.milu.tool.converter.ExampleQueryConverter;

public abstract class AbstractGenericExampleProviderSql extends GenericCachingProviderSql {
	@Override
	public String provideSql(GenericProviderContext context, Object params) {
		String sql =  super.provideSql(context, params);
		convertRangQueryValue(context, params);
		return sql;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected void convertRangQueryValue(GenericProviderContext context, Object params) {
		Object example = ((Map)params).get(Constants.EXAMPLE);
		if(example == null) {
			return;
		}
		for(Attribute attr : context.getEntity().getAttributes()) {
			for(RangeCondition condition : attr.getRangeList()) {
				if(!condition.getKeyName().contains(".")) { //实体类直接属性不处理
					continue;
				}
				
				int index = condition.getKeyName().lastIndexOf(".");
				String preKey = condition.getKeyName().substring(0, index);
				Object containerObj = OgnlCache.getValue(preKey, example);
				if(!(containerObj instanceof Map)) { //非Map不处理,只有Map的元素可以转换后覆盖
					continue;
				}
				
				Map container = (Map)containerObj;
				String lastKey = condition.getKeyName().substring(index + 1);
				Object value = container.get(lastKey);
				if(value == null) {
					continue;
				}
				
				ExampleQueryConverter converter = InstanceUtils.getSigleton(condition.getValueConverter());
	
				if(condition.getKeyType() == KeyType.IN) {
					Object collection = StringUtils.toCollection(value);
					List values = new ArrayList<>();
					if(collection.getClass().isArray()) {
						for(int i = 0; i < Array.getLength(collection); i++) {
							values.add(converter.convert(Array.get(collection, i), condition.getAttrJavaType(), condition.getKeyName(), condition.getKeyType()));
						}
						collection = values;
					} else if(collection instanceof Collection) {
						Iterator it = ((Collection)collection).iterator();
						while(it.hasNext()) {
							values.add(converter.convert(it.next(), condition.getAttrJavaType(), condition.getKeyName(), condition.getKeyType()));
						}
						collection = values;
					}
					container.put(lastKey, collection);
					continue;
				}
				
				Object convertedValue = converter.convert(value, condition.getAttrJavaType(), condition.getKeyName(), condition.getKeyType());
				
				container.put(lastKey, convertedValue);
			}
		}
	}
	
	
}