|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.mongodb.DBCollection;
|
|
import com.mongodb.DBCollection;
|
|
|
import com.mongodb.DuplicateKeyException;
|
|
import com.mongodb.DuplicateKeyException;
|
|
|
import com.mongodb.WriteResult;
|
|
import com.mongodb.WriteResult;
|
|
|
|
|
+import com.uas.kanban.annotation.CollectionProperty;
|
|
|
import com.uas.kanban.annotation.FieldProperty;
|
|
import com.uas.kanban.annotation.FieldProperty;
|
|
|
import com.uas.kanban.annotation.NotEmpty;
|
|
import com.uas.kanban.annotation.NotEmpty;
|
|
|
import com.uas.kanban.exception.OperationException;
|
|
import com.uas.kanban.exception.OperationException;
|
|
@@ -66,12 +67,6 @@ public class BaseDao<T extends BaseEntity> {
|
|
|
public static <K> void checkFields(@NotEmpty("k") K k) throws IllegalArgumentException, IllegalStateException {
|
|
public static <K> void checkFields(@NotEmpty("k") K k) throws IllegalArgumentException, IllegalStateException {
|
|
|
Class<?> clazz = k.getClass();
|
|
Class<?> clazz = k.getClass();
|
|
|
Field[] declaredFields = clazz.getDeclaredFields();
|
|
Field[] declaredFields = clazz.getDeclaredFields();
|
|
|
- String collectionName;
|
|
|
|
|
- if (k instanceof BaseEntity) {
|
|
|
|
|
- collectionName = ((BaseEntity) k).collectionSimpleName();
|
|
|
|
|
- } else {
|
|
|
|
|
- collectionName = clazz.getSimpleName();
|
|
|
|
|
- }
|
|
|
|
|
for (Field field : declaredFields) {
|
|
for (Field field : declaredFields) {
|
|
|
int modifiers = field.getModifiers();
|
|
int modifiers = field.getModifiers();
|
|
|
// 不处理static修饰的变量
|
|
// 不处理static修饰的变量
|
|
@@ -90,7 +85,7 @@ public class BaseDao<T extends BaseEntity> {
|
|
|
|
|
|
|
|
// 如果使用 {@link FieldProperty} 指定不可为空,但是值为空,则抛出异常
|
|
// 如果使用 {@link FieldProperty} 指定不可为空,但是值为空,则抛出异常
|
|
|
if (fieldProperty != null && !fieldProperty.nullable() && ObjectUtils.isEmpty(value)) {
|
|
if (fieldProperty != null && !fieldProperty.nullable() && ObjectUtils.isEmpty(value)) {
|
|
|
- throw new IllegalArgumentException(collectionName + "字段为空:" + field.getName());
|
|
|
|
|
|
|
+ throw new IllegalArgumentException(getCollectionSimpleName(clazz) + "字段为空:" + field.getName());
|
|
|
}
|
|
}
|
|
|
// 如果是嵌入或引用对象,并且不为空,则递归检测
|
|
// 如果是嵌入或引用对象,并且不为空,则递归检测
|
|
|
if ((embedded != null || reference != null) && !ObjectUtils.isEmpty(value)) {
|
|
if ((embedded != null || reference != null) && !ObjectUtils.isEmpty(value)) {
|
|
@@ -115,6 +110,20 @@ public class BaseDao<T extends BaseEntity> {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取指定类的集合简称(默认为类名)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param clazz 指定类
|
|
|
|
|
+ * @return 集合简称
|
|
|
|
|
+ */
|
|
|
|
|
+ protected static String getCollectionSimpleName(@NotEmpty("clazz") Class<?> clazz) {
|
|
|
|
|
+ CollectionProperty annotation = clazz.getAnnotation(CollectionProperty.class);
|
|
|
|
|
+ if (annotation != null && !StringUtils.isEmpty(annotation.simpleName())) {
|
|
|
|
|
+ return annotation.simpleName();
|
|
|
|
|
+ }
|
|
|
|
|
+ return clazz.getSimpleName();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Returns a new query bound to the collection (a specific {@link DBCollection})
|
|
* Returns a new query bound to the collection (a specific {@link DBCollection})
|
|
|
*
|
|
*
|
|
@@ -397,12 +406,7 @@ public class BaseDao<T extends BaseEntity> {
|
|
|
UpdateOperations<T> operations = createUpdateOperations();
|
|
UpdateOperations<T> operations = createUpdateOperations();
|
|
|
Set<String> keySet = jsonObject.keySet();
|
|
Set<String> keySet = jsonObject.keySet();
|
|
|
for (String key : keySet) {
|
|
for (String key : keySet) {
|
|
|
- Field field;
|
|
|
|
|
- try {
|
|
|
|
|
- field = entityClass.getDeclaredField(key);
|
|
|
|
|
- } catch (NoSuchFieldException | SecurityException e1) {
|
|
|
|
|
- throw new IllegalArgumentException("表中不存在该字段:" + key);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ Field field = ObjectUtils.getField(key, entityClass);
|
|
|
int modifiers = field.getModifiers();
|
|
int modifiers = field.getModifiers();
|
|
|
// 不处理static、final、transient修饰的变量(只处理普通成员变量)
|
|
// 不处理static、final、transient修饰的变量(只处理普通成员变量)
|
|
|
if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers)) {
|
|
if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers)) {
|
|
@@ -483,7 +487,7 @@ public class BaseDao<T extends BaseEntity> {
|
|
|
public T checkExist(@NotEmpty("code") String code) throws IllegalStateException {
|
|
public T checkExist(@NotEmpty("code") String code) throws IllegalStateException {
|
|
|
T t = findOne(code);
|
|
T t = findOne(code);
|
|
|
if (t == null) {
|
|
if (t == null) {
|
|
|
- throw new IllegalStateException(t.collectionSimpleName() + "不存在:" + code);
|
|
|
|
|
|
|
+ throw new IllegalStateException(getCollectionSimpleName(entityClass) + "不存在:" + code);
|
|
|
}
|
|
}
|
|
|
return t;
|
|
return t;
|
|
|
}
|
|
}
|
|
@@ -558,26 +562,22 @@ public class BaseDao<T extends BaseEntity> {
|
|
|
* @param clazz type of the retrieved field
|
|
* @param clazz type of the retrieved field
|
|
|
* @param <K> type of the retrieved field
|
|
* @param <K> type of the retrieved field
|
|
|
* @return The results
|
|
* @return The results
|
|
|
- * @throws NoSuchFieldException
|
|
|
|
|
|
|
+ * @throws IllegalStateException
|
|
|
*/
|
|
*/
|
|
|
- public <K> List<K> findField(@NotEmpty("query") Query<T> query, @NotEmpty("field") String field, Class<K> clazz) throws NoSuchFieldException {
|
|
|
|
|
|
|
+ public <K> List<K> findField(@NotEmpty("query") Query<T> query, @NotEmpty("field") String field, Class<K> clazz) throws IllegalStateException {
|
|
|
query.project(field, true);
|
|
query.project(field, true);
|
|
|
List<T> list = query.asList();
|
|
List<T> list = query.asList();
|
|
|
List<K> result = new ArrayList<>();
|
|
List<K> result = new ArrayList<>();
|
|
|
if (!CollectionUtils.isEmpty(list)) {
|
|
if (!CollectionUtils.isEmpty(list)) {
|
|
|
for (T t : list) {
|
|
for (T t : list) {
|
|
|
- try {
|
|
|
|
|
- Object value = ObjectUtils.recursivelyGetValue(field, t);
|
|
|
|
|
- if (value != null) {
|
|
|
|
|
- Class<?> valueClass = value.getClass();
|
|
|
|
|
- if (clazz.isAssignableFrom(valueClass)) {
|
|
|
|
|
- result.add((K) value);
|
|
|
|
|
- } else {
|
|
|
|
|
- throw new ClassCastException("字段 " + field + " 的类型为 " + valueClass + " , 无法转为 " + clazz);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ Object value = ObjectUtils.recursivelyGetValue(field, t);
|
|
|
|
|
+ if (value != null) {
|
|
|
|
|
+ Class<?> valueClass = value.getClass();
|
|
|
|
|
+ if (clazz.isAssignableFrom(valueClass)) {
|
|
|
|
|
+ result.add((K) value);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new ClassCastException("字段 " + field + " 的类型为 " + valueClass + " , 无法转为 " + clazz);
|
|
|
}
|
|
}
|
|
|
- } catch (NoSuchFieldException e) {
|
|
|
|
|
- throw new NoSuchFieldException(entityClass + " 中不存在字段:" + field);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
@@ -585,6 +585,29 @@ public class BaseDao<T extends BaseEntity> {
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Execute the query and get the result (Limits the field retrieved and only one row)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param code the code to query
|
|
|
|
|
+ * @param field retrieved field
|
|
|
|
|
+ * @param clazz type of the retrieved field
|
|
|
|
|
+ * @param <K> type of the retrieved field
|
|
|
|
|
+ * @return The result, may be null
|
|
|
|
|
+ * @throws IllegalStateException
|
|
|
|
|
+ */
|
|
|
|
|
+ public <K> K findField(@NotEmpty("code") String code, @NotEmpty("field") String field, Class<K> clazz) throws IllegalStateException {
|
|
|
|
|
+ Query<T> query = createQuery(code);
|
|
|
|
|
+ List<K> fields = findField(query, field, clazz);
|
|
|
|
|
+ int size = fields.size();
|
|
|
|
|
+ if (size < 1) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ } else if (size > 1) {
|
|
|
|
|
+ throw new IllegalStateException("存在多个值:" + fields);
|
|
|
|
|
+ }
|
|
|
|
|
+ return fields.get(0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 分页获取数据
|
|
* 分页获取数据
|
|
|
*
|
|
*
|