Package com.rila.mongo

Source Code of com.rila.mongo.MessagesHistoryMongo

package com.rila.mongo;

import java.net.UnknownHostException;
import java.util.Date;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;

public class MessagesHistoryMongo {
  DB chatDatabase;

//  public static void main(String[] args) throws UnknownHostException {
//    MessagesHistoryMongo conect = new MessagesHistoryMongo("");
//    conect.saveMessage("toto va au ski");
//    conect.LastMessages();
//  }

  public MessagesHistoryMongo(String mongoURIString) throws UnknownHostException {
    String uri = "";
    if (mongoURIString == "") {
      uri = "mongodb://localhost";
    } else {
      uri = mongoURIString;
    }
    final MongoClient mongoClient = new MongoClient(new MongoClientURI(uri));
    chatDatabase = mongoClient.getDB("rilachatjava");

  }

  public void saveMessage(String message) {

    DBCollection messagesCollection = chatDatabase.getCollection("historymessages");

    BasicDBObject messageToRecode = new BasicDBObject();
    // d�finir les champs
    messageToRecode.append("message", message).append("date", new Date());

    messagesCollection.insert(messageToRecode);

    // besoin de retourner quelque chose?

  }

  public java.util.List<DBObject> LastMessages() {
    DBCollection messagesCollection = chatDatabase.getCollection("historymessages");
    java.util.List<DBObject> messages = messagesCollection.find().toArray();

    for (int i = 0; i < messages.size(); i++) {
      System.out.println(messages.get(i));
    }

    return messages;
  }

}
TOP

Related Classes of com.rila.mongo.MessagesHistoryMongo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.