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

com.cybermkd.kit.MongoKit Maven / Gradle / Ivy

There is a newer version: 1.0.8.2
Show newest version
package com.cybermkd.kit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cybermkd.common.util.Stringer;
import com.cybermkd.log.Logger;
import com.mongodb.Block;
import com.mongodb.DBRef;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.IndexModel;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.*;


/**
 * 创建人:T-baby
 * 创建日期: 16/4/15
 * 文件描述:MongoDB操作工具类
 */
public enum MongoKit {

    /*
    *枚举实现单例模式
    */
    INSTANCE;
    private static MongoClient client;
    private static MongoDatabase defaultDb;
    private static Logger logger = Logger.getLogger(MongoKit.class);

    public MongoClient getClient() {
        return client;
    }

    public void init(MongoClient client, String database) {
        MongoKit.client = client;
        MongoKit.defaultDb = client.getDatabase(database);
    }

    public MongoCollection getCollection(String collectionName) {
        return defaultDb.getCollection(collectionName);
    }

    public long insert(String collectionName, List docs) {
        long before = getCollection(collectionName).count();
        getCollection(collectionName).insertMany(docs);
        return getCollection(collectionName).count() - before;
    }


    public long insert(String collectionName, Document doc) {
        long before = getCollection(collectionName).count();
        getCollection(collectionName).insertOne(doc);
        return getCollection(collectionName).count() - before;
    }

    public List aggregate(String collectionName, List query, boolean allowDiskUse) {

        final List list = new ArrayList();

        Block block = new Block() {

            public void apply(Document document) {
                document = iding(document);
                list.add(parseObject(document.toJson()));
            }
        };

        getCollection(collectionName).aggregate(query).allowDiskUse(allowDiskUse).forEach(block);

        return list;
    }

    public  List aggregate(String collectionName, List query, boolean allowDiskUse, Class clazz) {

        final List list = new ArrayList();

        Block block = new Block() {

            public void apply(Document document) {
                document = iding(document);
                list.add(parseObject(document, clazz));
            }
        };

        getCollection(collectionName).aggregate(query).allowDiskUse(allowDiskUse).forEach(block);

        return list;
    }

    public List find(String collectionName, Bson projection) {
        return find(collectionName, new BsonDocument(), projection, new BsonDocument(), 0, 0, "");
    }

    public List find(String collectionName, int limit, Bson sort, Bson projection) {
        return find(collectionName, new BsonDocument(), projection, sort, limit, 0, "");
    }

    public List find(String collectionName, int limit, int skip, Bson sort, Bson projection, String join) {
        return find(collectionName, new BsonDocument(), projection, sort, limit, 0, join);
    }

    public  List find(String collectionName, int limit, Bson sort, Bson projection, Class clazz) {
        return find(collectionName, new BsonDocument(), projection, sort, limit, 0, "", clazz);
    }

    public  List find(String collectionName, int limit, int skip, Bson sort, Bson projection, String join, Class clazz) {
        return find(collectionName, new BsonDocument(), projection, sort, limit, skip, join, clazz);
    }

    public List find(String collectionName, Bson query, Bson projection) {
        return find(collectionName, query, projection, new BsonDocument(), 0, 0, "");
    }


    public long count(String collectionName, Bson query) {
        return getCollection(collectionName).count(query);
    }

    public long count(String collectionName) {
        return getCollection(collectionName).count();
    }


    public JSONObject findOne(String collectionName, Bson query, Bson sort, String join) {
        return toJSON(
                iding(jointing(getCollection(collectionName).find(query).sort(sort).first(), join))
        );
    }

    public  T findOne(String collectionName, Bson query, Bson sort, String join, Class clazz) {
        return parseObject(
                iding(jointing(getCollection(collectionName).find(query).sort(sort).first(), join))
                , clazz);
    }

    public List find(String collectionName, Bson query, Bson projection, Bson sort, int limit,
                                 int skip, String join) {

        final List list = new ArrayList();

        Block block = new Block() {

            public void apply(Document document) {
                document = iding(document);
                document = jointing(document, join);
                list.add(toJSON(document));
            }
        };
        getCollection(collectionName).find(query).projection(projection).sort(sort).limit(limit).skip(skip).forEach(block);

        return list;

    }

    public  List find(String collectionName, Bson query, Bson projection, Bson sort, int limit, int skip,
                            String join, Class clazz) {

        final List list = new ArrayList();

        Block block = new Block() {

            public void apply(Document document) {
                document = iding(document);
                document = jointing(document, join);
                list.add(parseObject(document, clazz));
            }
        };

        getCollection(collectionName).find(query).projection(projection).sort(sort).limit(limit).skip(skip).forEach(block);

        return list;

    }


    public long update(String collectionName, Bson queue, Bson data) {
        UpdateResult updateResult = getCollection(collectionName).updateMany(queue, data);
        return updateResult.getModifiedCount();
    }

    public long updateOne(String collectionName, Bson queue, Bson data) {
        UpdateResult updateResult = getCollection(collectionName).updateOne(queue, data);
        return updateResult.getModifiedCount();
    }


    public long delete(String collectionName, Bson queue) {
        DeleteResult deleteResult = getCollection(collectionName).deleteMany(queue);
        return deleteResult.getDeletedCount();
    }

    public long deleteOne(String collectionName, Bson queue) {
        DeleteResult deleteResult = getCollection(collectionName).deleteOne(queue);
        return deleteResult.getDeletedCount();
    }

    public String validation(Object obj) {

        StringBuffer buffer = new StringBuffer(64);//用于存储验证后的错误信息

        Validator validator = Validation.buildDefaultValidatorFactory()
                .getValidator();

        Set> constraintViolations = validator
                .validate(obj);//验证某个对象,其实也可以只验证其中的某一个属性的

        constraintViolations.forEach((ConstraintViolation c) -> buffer.append(c.getMessage()));

        return buffer.toString();
    }

    //校验单个属性
    public String validation(Object obj, String[] keys) {

        StringBuffer buffer = new StringBuffer(64);//用于存储验证后的错误信息

        Validator validator = Validation.buildDefaultValidatorFactory()
                .getValidator();

        Set> constraintViolations = new HashSet<>();

        for (String key : keys) {
            Iterator> it = validator.validateProperty(obj, key).iterator();
            if (it.hasNext()) {
                constraintViolations.add(it.next());
            }

        }


        constraintViolations.forEach((ConstraintViolation c) -> buffer.append(c.getMessage()));

        return buffer.toString();
    }

    public String setIndex(String collectionName, Bson bson) {
        return getCollection(collectionName).createIndex(bson);
    }

    public List setIndex(String collectionName, List list) {
        return getCollection(collectionName).createIndexes(list);
    }

    public List getIndex(String collectionName) {

        List list = new ArrayList();

        Block block = new Block() {

            public void apply(final Document document) {
                list.add(parseObject(document.toJson()));
            }
        };

        getCollection(collectionName).listIndexes().forEach(block);

        return list;
    }

    public void deleteIndex(String collectionName, Bson bson) {

        getCollection(collectionName).dropIndex(bson);

    }

    public void deleteIndex(String collectionName) {
        getCollection(collectionName).dropIndexes();
    }

    private Document iding(Document document) {
        try {
            if (document == null || document.get("_id") == null) {
                return document;
            } else {
                document.put("id", document.get("_id").toString());
                document.remove("_id");
            }
        } catch (ClassCastException e) {
                /*如果转换出错直接返回原本的值,不做任何处理*/

        }
        return document;
    }

    private Document jointing(Document document, String join) {
        if (join != null && !join.isEmpty()) {
            try {
                DBRef dbRef = document.get(join, DBRef.class);
                Document joinDoc = getCollection(dbRef.getCollectionName())
                        .find(new Document("_id", dbRef.getId())).first();
                joinDoc = iding(joinDoc);
                document.put(join, joinDoc);
            } catch (ClassCastException e) {
                /*用于避免如果key对应的值并不是DBRef,如果转换出错直接返回原本的值,不做任何处理*/
            }
        }
        return document;

    }

    /*由于fastjson转换空对象时就会直接抛出异常,而在实际查询中查不到东西是很正常的
    * ,所以为了避免会有空异常,特别做了异常处理*/

    private JSONObject parseObject(String json) {
        try {
            if (Stringer.notBlank(json)) {
                return JSON.parseObject(json);
            }
            return new JSONObject();
        } catch (NullPointerException e) {
            error("parseObject", json);
            return new JSONObject();
        }


    }

    private  T parseObject(Document doc, Class clazz) {
        try {
            if (doc == null) {
                return JSON.parseObject(new JSONObject().toJSONString(), clazz);
            }
            return JSON.parseObject(JSON.toJSONString(doc), clazz);
        } catch (NullPointerException e) {
            error("parseObject", clazz.getName());
            return JSON.parseObject(new JSONObject().toJSONString(), clazz);
        }
    }

    private JSONObject toJSON(Object obj) {
        try {
            return (JSONObject) JSON.toJSON(obj);
        } catch (NullPointerException e) {
            error("toJSON", obj.getClass().getName());
            return new JSONObject();
        }
    }

    private void error(String funName, String text) {
        logger.error("MongKit tips: (づ ̄ 3 ̄)づ " + funName + " is error ! " + text);
    }


}







© 2015 - 2024 Weber Informatics LLC | Privacy Policy