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

com.github.aidensuen.mongo.util.MongoCommandUtil Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.github.aidensuen.mongo.util;

import com.github.aidensuen.mongo.bson.DocumentList;
import com.github.aidensuen.mongo.core.MongoDaoStatement;
import com.github.aidensuen.mongo.mapping.BoundCommand;
import com.github.aidensuen.mongo.mapping.ExampleStr;
import com.github.aidensuen.mongo.mapping.MongoOperatorConst;
import com.github.aidensuen.util.StringUtil;
import org.bson.Document;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.*;


public class MongoCommandUtil extends StringUtil {

    public static String converteJsonToCommandStr(String json) {
        if (isEmpty(json)) {
            return "";
        } else {
            return json.replaceAll("\"", "'")
                    .replaceAll("'\\{", "{")
                    .replaceAll("}'", "}")
                    .replaceAll("\\\\", "");
        }
    }

    public static Query createQuery(BoundCommand boundCommand, MongoDaoStatement ms) {
        BasicQuery query = null;
        if (!StringUtil.isEmpty(ms.getReturnFields())) {
            query = new BasicQuery(boundCommand.getCommand(), ms.getReturnFields());
        } else {
            query = new BasicQuery(boundCommand.getCommand());
        }
        if (!StringUtil.isEmpty(ms.getSortFields())) {
            query.setSortObject(Document.parse(ms.getReturnFields()));
        }
        add_parameterIfNecessary(query, ms);
        return query;
    }

    public static Document createCommand(BoundCommand boundCommand) {
        return Document.parse(boundCommand.getCommand());
    }

    public static List createPipeline(BoundCommand boundCommand) {
        DocumentList documentList = DocumentList.parse(boundCommand.getCommand());
        return documentList.getValues();
    }

    public static boolean add_parameterIfNecessary(Query query, MongoDaoStatement ms) {
        if ((ms.getConfiguration().isSafeDelete() || ms.getConfiguration().isSafeUpdate()) && query.getQueryObject().size() < 1) {
            switch (ms.getOperationType()) {
                case DELETE:
                case UPSERT:
                case UPDATEMULTI:
                case UPDATEONE:
                    query.getQueryObject().append("_parameter", new Date().getTime());
                    return true;
                default:
                    return false;
            }
        }
        return false;
    }

    public static Criteria buildCriteriaByExampleStr(ExampleStr o) {
        int localInteger = 0;
        Criteria localCriteria = new Criteria();
        if (o != null) {
            String localObject1;
            Object localObject2;
            Object localObject3;
            String localObject4;
            String localObject5;
            Iterator> iterator = o.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = iterator.next();
                if (entry.getKey().indexOf(MongoOperatorConst.OR) != -1) {
                    localObject2 = new ArrayList();
                    localObject3 = entry.getValue();
                    for (Object object : (List) localObject3) {
                        if (object instanceof ExampleStr) {
                            ((List) localObject2).add(buildCriteriaByExampleStr((ExampleStr) object));
                        }
                    }
                    localObject3 = new Criteria[((List) localObject2).size()];
                    localCriteria = localCriteria.orOperator((Criteria[]) ((List) localObject2).toArray((Object[]) localObject3));
                } else if (entry.getKey().indexOf(MongoOperatorConst.AND) != -1) {
                    localObject2 = new ArrayList();
                    localObject3 = entry.getValue();
                    for (Object object : (List) localObject3) {
                        if (object instanceof ExampleStr) {
                            ((List) localObject2).add(buildCriteriaByExampleStr((ExampleStr) object));
                        }
                    }
                    localObject3 = new Criteria[((List) localObject2).size()];
                    localCriteria = localCriteria.andOperator((Criteria[]) ((List) localObject2).toArray((Object[]) localObject3));
                } else if (entry.getKey().indexOf(MongoOperatorConst.IN) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.IN));
                    localObject2 = entry.getValue();
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).in((Collection) localObject2) : localCriteria.and(localObject1).in((Collection) localObject2);
                } else if (entry.getKey().indexOf(MongoOperatorConst.ALL) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.ALL));
                    localObject2 = entry.getValue();
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).all((Collection) localObject2) : localCriteria.and(localObject1).all((Collection) localObject2);
                } else if (entry.getKey().indexOf(MongoOperatorConst.GTE) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.GTE));
                    localObject4 = StringUtil.appendStrs(localObject1, MongoOperatorConst.LTE);
                    localObject5 = StringUtil.appendStrs(localObject1, MongoOperatorConst.LT);
                    if (o.containsKey(localObject4)) {
                        localCriteria = localInteger == 0 ? Criteria.where(localObject1).gte(entry.getValue()).lte(o.get(localObject4)) : localCriteria.and(localObject1).gte(entry.getValue()).lte(o.get(localObject4));
                    } else if (o.containsKey(localObject5)) {
                        localCriteria = localInteger == 0 ? Criteria.where(localObject1).gte(entry.getValue()).lt(o.get(localObject5)) : localCriteria.and(localObject1).gte(entry.getValue()).lt(o.get(localObject5));
                    } else {
                        localCriteria = localInteger == 0 ? Criteria.where(localObject1).gte(entry.getValue()) : localCriteria.and(localObject1).gte(entry.getValue());
                    }
                } else if (entry.getKey().indexOf(MongoOperatorConst.LTE) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.LTE));
                    localObject4 = StringUtil.appendStrs(localObject1, MongoOperatorConst.GTE);
                    if (o.containsKey(localObject4)) {
                        continue;
                    }
                    localObject4 = StringUtil.appendStrs(localObject1, MongoOperatorConst.GT);
                    if (o.containsKey(localObject4)) {
                        continue;
                    }
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).lte(entry.getValue()) : localCriteria.and(localObject1).lte(entry.getValue());
                } else if (entry.getKey().indexOf(MongoOperatorConst.GT) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.GT));
                    localObject4 = StringUtil.appendStrs(localObject1, MongoOperatorConst.LTE);
                    localObject5 = StringUtil.appendStrs(localObject1, MongoOperatorConst.LT);
                    if (o.containsKey(localObject4)) {
                        localCriteria = localInteger == 0 ? Criteria.where(localObject1).gt(entry.getValue()).lte(o.get(localObject4)) : localCriteria.and(localObject1).gt(entry.getValue()).lte(o.get(localObject4));
                    } else if (o.containsKey(localObject5)) {
                        localCriteria = localInteger == 0 ? Criteria.where(localObject1).gt(entry.getValue()).lt(o.get(localObject5)) : localCriteria.and(localObject1).gt(entry.getValue()).lt(o.get(localObject5));
                    } else {
                        localCriteria = localInteger == 0 ? Criteria.where(localObject1).gt(entry.getValue()) : localCriteria.and(localObject1).gte(entry.getValue());
                    }
                } else if (entry.getKey().indexOf(MongoOperatorConst.LT) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.LT));
                    localObject4 = StringUtil.appendStrs(localObject1, MongoOperatorConst.GTE);
                    if (o.containsKey(localObject4)) {
                        continue;
                    }
                    localObject4 = StringUtil.appendStrs(localObject1, MongoOperatorConst.GT);
                    if (o.containsKey(localObject4)) {
                        continue;
                    }
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).lt(entry.getValue()) : localCriteria.and(localObject1).lt(entry.getValue());
                } else if (entry.getKey().indexOf(MongoOperatorConst.BET) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.BET));
                    localObject2 = entry.getValue();
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).gte(((List) localObject2).get(0)).lte(((List) localObject2).get(1)) : localCriteria.and(localObject1).gte(((List) localObject2).get(0)).lte(((List) localObject2).get(1));
                } else if (entry.getKey().indexOf(MongoOperatorConst.BLK) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.BLK));
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).regex(entry.getValue().toString()) : localCriteria.and(localObject1).regex(entry.getValue().toString());
                } else if (entry.getKey().indexOf(MongoOperatorConst.RLK) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.RLK));
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).regex(StringUtil.appendStrs("^", entry.getValue().toString())) : localCriteria.and(localObject1).regex(StringUtil.appendStrs("^", entry.getValue().toString()));
                } else if (entry.getKey().indexOf(MongoOperatorConst.LLK) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.LLK));
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).regex(StringUtil.appendStrs(entry.getValue().toString(), "$")) : localCriteria.and((String) localObject1).regex(StringUtil.appendStrs(entry.getValue().toString(), "^"));
                } else if (entry.getKey().indexOf(MongoOperatorConst.EXISTS) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.EXISTS));
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).exists(((Boolean) entry.getValue()).booleanValue()) : localCriteria.and(localObject1).exists(((Boolean) entry.getValue()).booleanValue());
                } else if (entry.getKey().indexOf(MongoOperatorConst.NE) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.NE));
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).ne(entry.getValue()) : localCriteria.and(localObject1).ne(entry.getValue());
                } else if (entry.getKey().indexOf(MongoOperatorConst.NOR) != -1) {
                    localObject2 = new ArrayList();
                    localObject3 = entry.getValue();
                    for (Object object : (List) localObject3) {
                        if (object instanceof ExampleStr) {
                            ((List) localObject2).add(buildCriteriaByExampleStr((ExampleStr) object));
                        }
                    }
                    localObject3 = new Criteria[((List) localObject2).size()];
                    localCriteria = localCriteria.norOperator((Criteria[]) ((List) localObject2).toArray((Object[]) localObject3));
                } else if (entry.getKey().indexOf(MongoOperatorConst.ELEMMATCH) != -1) {
                    localObject1 = entry.getKey().substring(0, entry.getKey().indexOf(MongoOperatorConst.ELEMMATCH));
                    localCriteria = localInteger == 0 ? Criteria.where(localObject1).elemMatch(buildCriteriaByExampleStr((ExampleStr) entry.getValue())) : localCriteria.and(localObject1).elemMatch((buildCriteriaByExampleStr((ExampleStr) entry.getValue())));
                } else {
                    localCriteria = localInteger == 0 ? Criteria.where(entry.getKey()).is(entry.getValue()) : localCriteria.and(entry.getKey()).is(entry.getValue());
                }
                localInteger += 1;
            }
            localObject2 = localObject3 = localObject1 = localObject4 = localObject5 = null;
        }
        return localCriteria;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy