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