Example usage for com.mongodb BasicDBObject getObjectId

List of usage examples for com.mongodb BasicDBObject getObjectId

Introduction

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

Prototype

public ObjectId getObjectId(final String field) 

Source Link

Document

Returns the object id or null if not set.

Usage

From source file:net.cogz.friends.GearzFriends.java

License:Open Source License

public void denyFriendRequest(String player, String toDeny) {
    DBObject playerDocument = getPlayerDocument(player);
    Object friendsObj = playerDocument.get("friend_requests");
    if (friendsObj == null || !(friendsObj instanceof BasicDBList)) {
        friendsObj = new BasicDBList();
    }/*w  ww .  j ava 2  s .  co m*/
    BasicDBList friendsList = (BasicDBList) friendsObj;
    if (hasRequest(player, toDeny)) {
        for (Object object : friendsList) {
            if (!(object instanceof BasicDBObject))
                continue;
            BasicDBObject friend = (BasicDBObject) object;
            DBObject toRemoveDocument = getPlayerDocument(toDeny);
            ObjectId s = friend.getObjectId("name");
            if (s.equals(getObjectId(toRemoveDocument))) {
                friendsList.remove(friend);
                playerDocument.put("friend_requests", friendsList);
                getCollection().save(playerDocument);
                return;
            }
        }
    } else {
        throw new IllegalStateException("No friend request from that player");
    }
    playerDocument.put("friend_requests", friendsList);
    getCollection().save(playerDocument);
}

From source file:net.cogz.friends.GearzFriends.java

License:Open Source License

public boolean hasRequest(String player, String fromPlayer) {
    Object friendsObj = getPlayerDocument(player).get("friend_requests");
    if (friendsObj == null || !(friendsObj instanceof BasicDBList)) {
        return false;
    }/* w  ww .  j  a  v a 2  s  .  c  o  m*/
    BasicDBList friendsList = (BasicDBList) friendsObj;
    for (Object object : friendsList) {
        if (!(object instanceof BasicDBObject))
            continue;
        BasicDBObject friend = (BasicDBObject) object;
        DBObject toCheckDocument = getPlayerDocument(fromPlayer);
        ObjectId name = friend.getObjectId("name");
        if (name.equals(getObjectId(toCheckDocument))) {
            return true;
        }
    }
    return false;
}

From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java

/**
 * //from   w w w.  java2s  .  co  m
 * @return
 */
public ArrayList<Club> getClubsFromDb() {

    ArrayList<Club> clubs = new ArrayList<>();
    DBCollection dbCollectionClubs = db.getCollection("clubs");

    DBCursor cursor = dbCollectionClubs.find();

    BasicDBObject mongoObject = new BasicDBObject();

    if (cursor == null)
        System.out.println("Cursor er null");

    try {
        while (cursor.hasNext()) {
            mongoObject = (BasicDBObject) cursor.next();
            clubs.add(new Club(mongoObject.getObjectId("_id"), mongoObject.getInt("id"),
                    mongoObject.getString("melwinId"), mongoObject.getString("name"),
                    mongoObject.getString("chiefInstructorMelwinId")));

        }
    } finally {
        cursor.close();
    }
    return clubs;
}

From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java

/**
 * /*from  ww  w.  j  a v a 2  s  .  c  om*/
 * @return
 */
public ArrayList<License> getLicensesFromDb() {
    ArrayList<License> licenses = new ArrayList<>();

    DBCollection dbCollectionLicenses = db.getCollection("licenses");

    DBCursor cursor = dbCollectionLicenses.find();

    BasicDBObject mongoObject = new BasicDBObject();

    if (cursor == null)
        System.out.println("Cursor er null, licenses");

    try {
        while (cursor.hasNext()) {
            mongoObject = (BasicDBObject) cursor.next();
            licenses.add(new License(mongoObject.getObjectId("_id"), mongoObject.getInt("id"),
                    mongoObject.getString("melwinId"), mongoObject.getString("licenseName"), null, null));
        }
    } finally {
        cursor.close();
    }
    return licenses;
}

From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java

/**
 * // ww  w .ja  v  a 2 s .c  o m
 * @param parachutistsFromMelwin
 */
public ArrayList<Parachutist> getParachutistsFromDB() {
    // HjelpeLister
    ArrayList<Club> clubs = getClubsFromDb();
    ArrayList<License> licenses = getLicensesFromDb();

    ArrayList<Parachutist> parachutistsInMongoDB = new ArrayList<>();

    DBCollection dbCollectionParachutists = db.getCollection("jumpers");

    DBCursor cursor = dbCollectionParachutists.find();

    BasicDBObject mongoObject = new BasicDBObject();

    try {
        int hoppteller = 0;
        while (cursor.hasNext()) {
            mongoObject = (BasicDBObject) cursor.next();
            hoppteller++;
            ArrayList<Club> memberClubs = new ArrayList<>();
            ArrayList<License> memberLicenses = new ArrayList<>();

            BasicDBList referenceClubs = (BasicDBList) mongoObject.get("memberclubs");
            BasicDBList referenceLicenses = (BasicDBList) mongoObject.get("licenses");

            for (Object clubReference : referenceClubs) {
                for (Club club : clubs) {
                    if ((int) clubReference == club.getId()) {
                        memberClubs.add(club);
                    }
                }
            }

            for (Object licenseReference : referenceLicenses) {
                for (License license : licenses) {
                    if ((int) licenseReference == license.getId()) {
                        memberLicenses.add(license);
                    }
                }

            }

            Parachutist parachutist = new Parachutist(mongoObject.getObjectId("_id"), mongoObject.getInt("id"),
                    mongoObject.getString("melwinId"), null, // nakKey
                    new ArrayList<Club>(), // clubs
                    new ArrayList<License>(), // licenses
                    mongoObject.getString("firstname"), mongoObject.getString("lastname"),
                    mongoObject.getDate("birthdate"), mongoObject.getString("gender"),
                    mongoObject.getString("street"), mongoObject.getString("postnumber"),
                    mongoObject.getString("postplace"), mongoObject.getString("mail"),
                    mongoObject.getString("phone"), mongoObject.getString("password"));

            parachutist.setMemberclubs(memberClubs);
            parachutist.setLicenses(memberLicenses);

            parachutistsInMongoDB.add(parachutist);

        }
    } finally {
        cursor.close();
    }
    return parachutistsInMongoDB;
}

From source file:org.apache.flink.Job.java

License:Apache License

public static DataSet<Point> convertToPointSet(DataSet<Tuple2<BSONWritable, BSONWritable>> in) {
    return in.map(new MapFunction<Tuple2<BSONWritable, BSONWritable>, Point>() {
        @Override// w  ww. j a  v  a  2 s  . c  om
        public Point map(Tuple2<BSONWritable, BSONWritable> bsonWritableBSONWritableTuple2) throws Exception {
            BSONWritable bvalue = bsonWritableBSONWritableTuple2.getField(1);
            Object value = bvalue.getDoc();
            BasicDBObject point = (BasicDBObject) value;
            String id = point.getObjectId("_id").toString();
            Double x = (Double) point.get("x");
            Double y = (Double) point.get("y");
            //System.out.println("Point: " + id + " : " + x.toString() + " " + y.toString());
            return new Point(x, y);
        }
    });
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addTapPolicy(TapPolicy tapPolicy) throws DuplicateEntryException {

    // Check whether there is an existing policy with the same name
    if (tapPolicyExistsByName(tapPolicy.getName()))
        throw new DuplicateEntryException();

    // Get the tap policy table
    DBCollection table = database.getCollection(DatabaseNames.getTapPolicyTableName());

    // Create a document containing the new tap policy
    BasicDBObject document = tapPolicy.getAsDocument();

    // Add the new tap policy document to the tap policy table
    table.insert(document);/*from   ww  w  . j  a  v a 2 s  . c  om*/

    // Get the object ID from mongo and update it in the tap policy object
    ObjectId objectId = document.getObjectId("_id");
    tapPolicy.setObjectId(objectId.toString());

    logger.info("Added tap policy " + tapPolicy.getName() + " object ID " + tapPolicy.getObjectId());

    // Notify the application that the tap policy changed
    // tappingAppLogic.NotifyConfigChange(DBTableChangeEnum.TAP_POLICY, tapPolicy.getObjectId());
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addMatchCriteria(MatchCriteria matchCriteria) throws DuplicateEntryException {

    // Check whether there is an existing match criteria with the same name
    if (matchCriteriaExistsByName(matchCriteria.getName()))
        throw new DuplicateEntryException();

    // Get the match criteria table
    DBCollection table = database.getCollection(DatabaseNames.getMatchCriteriaTableName());

    // Create a document containing the new match criteria
    BasicDBObject document = matchCriteria.getAsDocument();

    // Add the new tap policy document to the MatchCriteria table
    table.insert(document);// www  .  j  a va2 s  . com

    // Get the object ID from mongo and update it in the MatchCriteria object
    ObjectId objectId = document.getObjectId("_id");
    matchCriteria.setObjectId(objectId.toString());

    logger.info(
            "Added match criteria " + matchCriteria.getName() + " object ID " + matchCriteria.getObjectId());

    // tappingAppLogic.NotifyConfigChange(DBTableChangeEnum.MATCH_CRITERIA, matchCriteria.getObjectId());
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addSwitchEntry(SwitchEntry switchEntry) throws DuplicateEntryException, NotSupportedException {

    // We aren't supporting this mode yet
    if (switchEntry.getTappingMode() == TappingModeEnum.NORMAL) {
        throw new NotSupportedException("Normal Mode is not suppored");
    }/*from ww w.  j a va 2s .c  o  m*/

    // Check whether there is an existing switch entry with the same name
    if (switchEntryExistsByName(switchEntry.getName()))
        throw new DuplicateEntryException();

    // Get the match criteria table
    DBCollection table = database.getCollection(DatabaseNames.getSwitchEntryTableName());

    // Create a document containing the new match criteria
    BasicDBObject document = switchEntry.getAsDocument();

    // Add the new tap policy document to the tap policy table
    table.insert(document);

    // Get the object ID from mongo and update it in the switch entry object
    ObjectId objectId = document.getObjectId("_id");
    switchEntry.setObjectId(objectId.toString());

    logger.info("Added Switch Entry " + switchEntry.getName() + " object ID " + switchEntry.getObjectId());
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addNextHopSwitch(NextHopSwitch nextHopSwitch) throws DuplicateEntryException {

    // Check whether there is an existing next hop switch with the same name
    if (nextHopSwitchExistsByName(nextHopSwitch.getName()))
        throw new DuplicateEntryException();

    // Get the match criteria table
    DBCollection table = database.getCollection(DatabaseNames.getNextHopSwitchTableName());

    // Create a document containing the new match criteria
    BasicDBObject document = nextHopSwitch.getAsDocument();

    // Add the new tap policy document to the tap policy table
    table.insert(document);/*from w  w w . j  a  v a 2  s  .co  m*/

    // Get the object ID from mongo and update it in the switch entry object
    ObjectId objectId = document.getObjectId("_id");
    nextHopSwitch.setObjectId(objectId.toString());

    logger.info(
            "Added Next Hop Switch " + nextHopSwitch.getName() + " object ID " + nextHopSwitch.getObjectId());
}