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

org.teasoft.beex.mongodb.ParaConvertUtil Maven / Gradle / Ivy

/*
 * Copyright 2016-2023 the original author.All rights reserved.
 * Kingstar([email protected])
 * The license,see the LICENSE file.
 */

package org.teasoft.beex.mongodb;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.teasoft.bee.osql.Condition;
import org.teasoft.bee.osql.OrderType;
import org.teasoft.bee.osql.annotation.customizable.Json;
import org.teasoft.bee.osql.type.SetParaTypeConvert;
import org.teasoft.bee.sharding.ShardingSortStruct;
import org.teasoft.honey.osql.constant.NullEmpty;
import org.teasoft.honey.osql.core.ConditionImpl;
import org.teasoft.honey.osql.core.HoneyUtil;
import org.teasoft.honey.osql.core.NameTranslateHandle;
import org.teasoft.honey.osql.type.SetParaTypeConverterRegistry;
import org.teasoft.honey.sharding.ShardingUtil;

import com.mongodb.client.model.Sorts;

/**
 * @author Jade
 * @since  2.0
 */
public class ParaConvertUtil {

	public static Map toMap(Object entity) throws Exception {
		return  toMap(entity, -1);
		
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map toMap(Object entity,int includeType) throws Exception {
		Map documentAsMap = null;
		Field fields[] = entity.getClass().getDeclaredFields();
		boolean isFirst = true;
		int len = fields.length;
		String column = "";
		Object value = null;
		for (int i = 0; i < len; i++) {
			fields[i].setAccessible(true);
			if (HoneyUtil.isContinue(includeType, fields[i].get(entity), fields[i])) {
				continue;
			} else {
				if (isFirst) {
					isFirst = false;
					documentAsMap = new LinkedHashMap();
				}
				column = _toColumnName(fields[i].getName(), entity.getClass());
				if ("id".equalsIgnoreCase(column)) {// 替换id为_id
					column = "_id";
				}
				value = fields[i].get(entity); // value
				// 是实体的,要转成Json; null不转
				if(value!=null &&  fields[i].isAnnotationPresent(Json.class)) {
					SetParaTypeConvert converter = SetParaTypeConverterRegistry.getConverter(Json.class);
					if (converter != null) {
						value=(String)converter.convert(value);
					}
				}
				
				if ("_id".equalsIgnoreCase(column) && value == null) {
					// ignore
				} else {
					documentAsMap.put(column, value);
				}
			}
		}

		return documentAsMap;
	}
	
	private static boolean isExcludeField(String excludeFieldList, String checkField) {
		String excludeFields[] = excludeFieldList.split(",");
		for (String f : excludeFields) {
			if (f.equals(checkField)) return true;
		}
		return false;
	}
	
	public static Map toMapExcludeSome(Object entity,String excludeFieldList) throws Exception {
		Map documentAsMap = null;
		Field fields[] = entity.getClass().getDeclaredFields();
		boolean isFirst = true;
		int len = fields.length;
		String column = "";
		Object value = null;
		for (int i = 0; i < len; i++) {
			fields[i].setAccessible(true);
//			if (HoneyUtil.isContinue(-1, fields[i].get(entity), fields[i])) {
			if (HoneyUtil.isContinue(NullEmpty.EMPTY_STRING, fields[i].get(entity), fields[i])) {// mongodb,批量插入,不处理null,但会插入是空字符的
				continue;
			} else {
				if (!"".equals(excludeFieldList) && isExcludeField(excludeFieldList, fields[i].getName())) continue;
				
				if (isFirst) {
					isFirst = false;
					documentAsMap = new LinkedHashMap();
				}
				column = _toColumnName(fields[i].getName(), entity.getClass());
				if ("id".equalsIgnoreCase(column)) {// 替换id为_id
					column = "_id";
				}
				value = fields[i].get(entity); // value
				documentAsMap.put(column, value);
			}
		}

		return documentAsMap;
	}
	

	public static List> toListBson(List objs) throws Exception {
		List> list = new ArrayList<>();
		for (int i = 0; null != objs && i < objs.size(); i++) {
			list.add(toMap(objs.get(i)));
		}
		return list;
	}

	public static Bson toSortBson(String orderFields[], OrderType[] orderTypes) {
		if (orderFields.length < 1) return null;
		
		int len=orderFields.length;
		
		if (len == 1) {
			int order = -1;
			if (orderTypes!=null && orderTypes.length==1 && orderTypes[0] == OrderType.ASC) order = 1;
			return new Document(tranferId(orderFields[0]), order);
		}

		Bson b[] = new Bson[len];
		int order;
		for (int i = 0; i < len; i++) {
			if (orderTypes==null || orderTypes[i] == OrderType.ASC)
				order = 1;
			else
				order = -1;
			b[i] = new Document(tranferId(orderFields[i]), order);
		}

//		return Filters.and(b);
		return Sorts.orderBy(b);
	}
	
	private static String tranferId(String fieldName) {
		if ("id".equalsIgnoreCase(fieldName))
			return "_id";
		else
			return fieldName;
	}
	
	public static Bson toSortBson(Condition condition) {
		
		if(condition==null) return null;
		
		ConditionImpl conditionImpl = (ConditionImpl) condition;
		ShardingSortStruct struct=ShardingUtil.parseOrderByMap(conditionImpl.getOrderBy());
		
		return toSortBson(struct.getOrderFields(), struct.getOrderTypes());
	}
	

	@SuppressWarnings("rawtypes")
	private static String _toColumnName(String fieldName, Class entityClass) {
		return NameTranslateHandle.toColumnName(fieldName, entityClass);
	}

}