Example usage for com.mongodb BasicDBList BasicDBList

List of usage examples for com.mongodb BasicDBList BasicDBList

Introduction

In this page you can find the example usage for com.mongodb BasicDBList BasicDBList.

Prototype

BasicDBList

Source Link

Usage

From source file:dao.SearchUserDao.java

public List<User> searchUser(String searchvalue) {
    BasicDBList or = new BasicDBList();

    BasicDBObject username = new BasicDBObject("username",
            new BasicDBObject("$regex", java.util.regex.Pattern.compile(searchvalue)).append("$options", "i"));
    BasicDBObject firstname = new BasicDBObject("firstname",
            new BasicDBObject("$regex", java.util.regex.Pattern.compile(searchvalue)).append("$options", "i"));
    BasicDBObject lastname = new BasicDBObject("lastname", java.util.regex.Pattern.compile(searchvalue));
    or.add(username);/*  w  w  w.  j  a  va  2  s  .  c  om*/
    or.add(firstname);
    or.add(lastname);
    DBObject query = new BasicDBObject("$or", or);

    DBCursor cursor = col.find(query);
    if (cursor.size() > 0) {
        System.out.println("user is exists");
    } else {
        System.out.println("user is not exists");
    }

    List<User> users = new ArrayList<>();
    try {
        while (cursor.hasNext()) {
            User tmpUser = UserConverter.toUsers(cursor.next());
            users.add(tmpUser);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return users;
}

From source file:dao.SearchUserDao.java

public List<Discussion> searchTopic(String searchvalue) {
    BasicDBList or1 = new BasicDBList();

    BasicDBObject topic = new BasicDBObject("Topic", java.util.regex.Pattern.compile(searchvalue));
    BasicDBObject content = new BasicDBObject("Content", java.util.regex.Pattern.compile(searchvalue));
    BasicDBObject tags = new BasicDBObject("Tags", java.util.regex.Pattern.compile(searchvalue));
    or1.add(topic);/*from   w  ww .  jav  a 2  s .c  o m*/
    or1.add(content);
    or1.add(tags);
    DBObject query1 = new BasicDBObject("$or", or1);
    DBCursor cursor1 = col1.find(query1);
    if (cursor1.size() > 0) {
        System.out.println("data is exist");
    } else {
        System.out.println("data is not exist");
    }

    while (cursor1.hasNext()) {

        System.out.println(cursor1.next());
    }

    return null;
}

From source file:DataAccess.DAO.OrderDAO.java

public String createOrder(Order order) {
    try {/*from w  w  w .j  av a  2s . c o  m*/
        if (connectDB()) {
            BasicDBObject doc = new BasicDBObject("id_order", order.getId()).append("date", order.getDate())
                    .append("total_price", order.getTotalPrice()).append("user_id", order.getUserId());
            BasicDBList solditemsBDL = new BasicDBList();
            Collection<Solditems> list = order.getSolditemsCollection();
            for (Solditems soleditem : list) {
                BasicDBObject soleditemBDO = new BasicDBObject("id_item", soleditem.getItems())
                        .append("quantity", soleditem.getQuantity());
                solditemsBDL.add(soleditemBDO);
            }
            doc.append("solditems", solditemsBDL);
            coll.insert(doc);
            System.out.println("Document (Order) inserted successfully");
            //return "Save Item " + order.getId() + " sucess";
            return "Tu orden fue procesada y recibirs tus productos dentro de poco";
        } else {
            return "Fail";
        }
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        return "Fail";
    }
}

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVBranch.java

License:Open Source License

private Map<String, Object> makeQueryObject() {
    BasicDBList l = new BasicDBList();
    String lba = MongoDBConstants.LIFETIME + "." + getRootCid();
    String iba = MongoDBConstants.LIFETIME + ".i" + getRootCid();

    //(1) check if there is information about the document's insertion
    //    available. if not just include this object.
    //    TODO this condition must be removed once we know the whole branching history
    l.add(new BasicDBObject(iba, new BasicDBObject("$exists", false)));

    if (!CompatibilityHelper.supportsAnd(getDB())) {
        //(2) check if the object has been deleted after this commit.
        //    we use a $not here, so the query will also return 'true' if
        //    the attribute is not set.
        l.add(new BasicDBObject(lba, new BasicDBObject("$not", new BasicDBObject("$lte", getHead()))));
    } else {// www.  ja va2 s  . c o m
        BasicDBList l2 = new BasicDBList();
        //(2) check if the object has been inserted in this commit or later
        l2.add(new BasicDBObject(iba, new BasicDBObject("$lte", getHead())));

        //(3) check if the object has been deleted after this commit.
        //    we use a $not here, so the query will also return 'true' if
        //    the attribute is not set.
        l2.add(new BasicDBObject(lba, new BasicDBObject("$not", new BasicDBObject("$lte", getHead()))));

        l.add(new BasicDBObject("$and", l2));
    }

    return Collections.unmodifiableMap(new BasicDBObject("$or", l));
}

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVFactory.java

License:Open Source License

@Override
public List<Object> createList() {
    return new BasicDBList();
}

From source file:de.ifgi.fmt.mongo.conv.LineStringConverter.java

License:Open Source License

/**
 * //from  w w  w  . jav  a  2 s  . c om
 * @param value
 * @param optionalExtraInfo
 * @return
 */
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null)
        return null;
    LineString ls = (LineString) value;
    BasicDBList line = new BasicDBList();
    for (Coordinate c : ls.getCoordinates()) {
        BasicDBList l = new BasicDBList();
        l.add(c.x);
        l.add(c.y);
        line.add(l);
    }
    return line;
}

From source file:de.ifgi.fmt.mongo.conv.PointConverter.java

License:Open Source License

/**
 * /*from w w  w.j  a  v a 2  s . co m*/
 * @param value
 * @param optionalExtraInfo
 * @return
 */
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null)
        return null;
    Point p = (Point) value;
    BasicDBList l = new BasicDBList();
    l.add(p.getCoordinate().x);
    l.add(p.getCoordinate().y);
    return l;
}

From source file:de.uni_koeln.spinfo.maalr.login.UserInfoDB.java

License:Apache License

List<MaalrUserInfo> getAllUsers(Role role, String text, String sortColumn, boolean sortAscending, int from,
        int length) {
    BasicDBObject query = new BasicDBObject();
    Pattern pattern = Pattern.compile(".*" + text + ".*", Pattern.CASE_INSENSITIVE);
    if (role != null) {
        query.put(Constants.Users.ROLE, role.toString());
    }/*from w  ww. ja  va2 s  .c  om*/
    if (text != null && text.trim().length() > 0) {
        BasicDBList attributes = new BasicDBList();
        DBObject firstName = new BasicDBObject();
        firstName.put(Constants.Users.FIRSTNAME, pattern);
        attributes.add(firstName);
        DBObject lastName = new BasicDBObject();
        lastName.put(Constants.Users.LASTNAME, pattern);
        attributes.add(lastName);
        DBObject login = new BasicDBObject();
        login.put(Constants.Users.LOGIN, pattern);
        attributes.add(login);
        query.append("$or", attributes);
    }
    DBCursor cursor = userCollection.find(query);
    if (sortColumn != null) {
        BasicDBObject sort = new BasicDBObject();
        sort.put(sortColumn, sortAscending ? 1 : -1);
        cursor.sort(sort);
    }
    cursor = cursor.skip(from).limit(length);
    List<MaalrUserInfo> all = new ArrayList<MaalrUserInfo>();
    while (cursor.hasNext()) {
        DBObject o = cursor.next();
        MaalrUserInfo user = new MaalrUserInfo(o);
        all.add(user);
    }
    cursor.close();
    return all;
}

From source file:de.uni_koeln.spinfo.maalr.mongo.core.Converter.java

License:Apache License

public static DBObject convertLexEntry(LexEntry entry) {
    DBObject object = new BasicDBObject();
    List<LemmaVersion> vHistory = entry.getVersionHistory();
    BasicDBList versions = new BasicDBList();
    for (LemmaVersion lemmaVersion : vHistory) {
        BasicDBObject obj = convertLemmaVersion(lemmaVersion);
        versions.add(obj);//w w  w  .  j ava 2  s.c o m
    }
    object.put(LexEntry.VERSIONS, versions);
    if (entry.getId() != null) {
        object.put(LexEntry.ID, new ObjectId(entry.getId()));
    }
    object.put(LexEntry.CURRENT, entry.getCurrentId());
    object.put(LexEntry.INTERNAL_ID, entry.getNextInternalId());
    object.put(LexEntry.CHANGE_STAMP, entry.getChangeStamp());
    return object;
}

From source file:de.uni_koeln.spinfo.maalr.mongo.core.Database.java

License:Apache License

private DBCursor query(String loginOrIP, Role role, Verification verification, String verifier, long startTime,
        long endTime, Status[] states, int limit, int offset, String orderField, boolean ascending) {
    // TODO: Add querying for 'current' state
    BasicDBObject query = new BasicDBObject();
    BasicDBObject attributes = new BasicDBObject();
    if (loginOrIP != null && loginOrIP.trim().length() > 0) {
        BasicDBList or = new BasicDBList();
        DBObject creator = new BasicDBObject();
        creator.put(QUERY_VERSION_CREATOR, loginOrIP);
        or.add(creator);//from  www.  j  a  va2s  .  c om
        DBObject ip = new BasicDBObject();
        ip.put(QUERY_VERSION_IP, loginOrIP);
        or.add(ip);
        attributes.put("$or", or);
    }
    if (verifier != null && verifier.trim().length() > 0) {
        attributes.put(QUERY_VERSION_VERIFIER, verifier);
    }
    if (role != null) {
        attributes.put(QUERY_VERSION_ROLE, role.toString());
    }
    if (verification != null) {
        attributes.put(QUERY_VERSION_VERIFICATION, verification.toString());
    }
    if (states != null && states.length > 0) {
        BasicDBList or = new BasicDBList();
        for (Status s : states) {
            or.add(s.toString());
        }
        DBObject nestedOr = new BasicDBObject();
        nestedOr.put("$in", or);
        attributes.put(QUERY_VERSION_STATE, nestedOr);
    }
    if (startTime > 0) {
        attributes.put(QUERY_VERSION_TIMESTAMP, new BasicDBObject("$gt", startTime));
    }
    if (endTime > 0) {
        attributes.put(QUERY_VERSION_TIMESTAMP, new BasicDBObject("$lt", endTime));
    }
    if (startTime > 0 && endTime > 0) {
        DBObject obj = new BasicDBObject("$lt", endTime);
        obj.put("$gt", startTime);
        attributes.put(QUERY_VERSION_TIMESTAMP, obj);
    }
    DBCursor found;
    if (attributes.size() > 0) {
        BasicDBObject elemMatch = new BasicDBObject("$elemMatch", attributes);
        query.append(LexEntry.VERSIONS, elemMatch);
        // query.append("$and", attributes);
        // query.append("$elemMatch", attributes);
        found = entryCollection.find(query);
    } else {
        found = entryCollection.find();
    }
    if (orderField != null) {
        DBObject order = new BasicDBObject();
        order.put(LexEntry.VERSIONS + "." + orderField, ascending ? 1 : -1);
        found.sort(order);
    }
    // TODO: This is inefficient! However, it should be ok for
    // small queries, which is the expected usecase.
    if (offset > 0) {
        found = found.skip(offset);
    }
    if (limit > 0) {
        found = found.limit(limit);
    }
    return found;
}