Example usage for com.mongodb DBCollection insert

List of usage examples for com.mongodb DBCollection insert

Introduction

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

Prototype

public WriteResult insert(final List<? extends DBObject> documents) 

Source Link

Document

Insert documents into a collection.

Usage

From source file:Server.Service.java

private void addClient() {
    try {/*from  w  w  w. j a v a 2s . com*/
        DBCollection clients = db.getCollection("clients");
        DBObject obj = clients.findOne(new BasicDBObject().append("ipAddr", this.ipAddr));
        String count = zeroFill(clients.find().count());
        if (obj == null) {
            BasicDBObject newObj = new BasicDBObject().append("ipAddr", this.ipAddr).append("SerialNum", count)
                    .append("money", encrypt("100000")).append("currentUser", "'null'");
            this.serialNum = count;
            clients.insert(newObj);
        } else {
            this.serialNum = obj.get("SerialNum").toString();
        }
    } catch (java.lang.NullPointerException e) {
        this.DBShutdown();
    }
}

From source file:Server.Service.java

private void check_register(register order) {
    String name = order.getName();
    String pwd = order.getPwd();//from ww w .j  a  v a 2 s  . c  o m
    BasicDBObject user = new BasicDBObject().append("name", name);
    DBCollection users = db.getCollection("users");
    DBObject obj = users.findOne(user);

    try {
        if (obj != null) {
            output.writeObject(new register(false, "USER_EXSIT"));
            output.flush();
        } else {
            BasicDBObject tmp = new BasicDBObject().append("name", name).append("pwd", pwd).append("credit", "")
                    .append("money", encrypt("0")).append("checked", false).append("locked", false)
                    .append("wrongTimes", 0);
            users.insert(tmp);
            output.writeObject(new register(true, ""));
        }
    } catch (IOException e) {
        this.DBShutdown();
    }
}

From source file:Server.Service.java

private void check_query(query order) {

    try {/*  ww  w . j  a v a2  s .com*/
        Date time = new Date();
        double money = 0;
        try {
            if ((order.getAmount() != null) && (!order.getAmount().equals("")))
                money = Double.parseDouble(new String(decrypty(order.getAmount())));
        } catch (NumberFormatException e) {
            output.writeObject(new query(false, "INVALID"));
            output.flush();
            return;
        }

        DBCollection users = db.getCollection("users");
        DBCollection clients = db.getCollection("clients");
        DBCollection records = db.getCollection("records");

        BasicDBObject userQ = new BasicDBObject().append("name", currentUser.getString("name"));
        DBObject user = users.findOne(userQ);
        BasicDBObject clientQ = new BasicDBObject().append("ipAddr", ipAddr);
        DBObject client = clients.findOne(clientQ);

        //DEAL THE QUERY PART
        if (order.type == method.WITHDRAW) {
            double user_money = Double.parseDouble(new String(decrypty(user.get("money").toString())));
            double client_money = Double.parseDouble(new String(decrypty(client.get("money").toString())));
            user_money -= money;
            client_money -= money;

            if (checkNegative(user_money, client_money, money)) {

                String user_money_tmp = encrypt(user_money + "");
                String client_money_tmp = encrypt(client_money + "");
                String user_tmp = "-" + money;

                users.update(userQ, new BasicDBObject().append("$set",
                        new BasicDBObject().append("money", user_money_tmp)));
                records.insert(new BasicDBObject().append("ipAddr", this.ipAddr)
                        .append("serial", this.serialNum).append("name", userQ.getString("name"))
                        .append("type", "WITHDRAW").append("delta", encrypt(user_tmp))
                        .append("remains", user_money_tmp).append("time", formatter.format(time)));
                clients.update(new BasicDBObject().append("ipAddr", ipAddr), new BasicDBObject().append("$set",
                        new BasicDBObject().append("money", client_money_tmp)));

                output.writeObject(new query(true, user_money_tmp));
                output.flush();
            }
        }

        else if (order.type == method.DEPOSIT) {
            double user_money = Double.parseDouble(new String(decrypty(user.get("money").toString())));
            double client_money = Double.parseDouble(new String(decrypty(client.get("money").toString())));
            user_money += money;
            client_money += money;
            if (checkNegative(user_money, client_money, money)) {
                String user_money_tmp = encrypt(user_money + "");
                String client_money_tmp = encrypt(client_money + "");
                String user_tmp = "+" + money;

                users.update(userQ, new BasicDBObject().append("$set",
                        new BasicDBObject().append("money", user_money_tmp)));
                records.insert(new BasicDBObject().append("ipAddr", this.ipAddr)
                        .append("serial", this.serialNum).append("name", userQ.getString("name"))
                        .append("type", "DEPOSIT").append("delta", encrypt(user_tmp))
                        .append("remains", user_money_tmp).append("time", formatter.format(time)));
                clients.update(new BasicDBObject().append("ipAddr", ipAddr), new BasicDBObject().append("$set",
                        new BasicDBObject().append("money", client_money_tmp)));

                output.writeObject(new query(true, user_money_tmp));
                output.flush();
            }
        }

        else if (order.type == method.TRANSFORM) {
            BasicDBObject targetQ = new BasicDBObject().append("name", order.getTarget());
            DBObject target = users.findOne(targetQ);
            if (target == null) {
                output.writeObject(new query(false, "NO_SUCH_USER"));
                output.flush();
            } else if (target.get("name").toString().equals(currentUser.getString("name"))) {
                output.writeObject(new query(false, "NOT_ALLOWED"));
                output.flush();
            } else {
                double user_money = Double.parseDouble(new String(decrypty(user.get("money").toString())));
                double target_money = Double.parseDouble(new String(decrypty(target.get("money").toString())));
                double client_money = Double.parseDouble(new String(decrypty(client.get("money").toString())));
                target_money += money;
                user_money -= money;
                if (checkNegative(user_money, target_money, money)) {
                    String target_money_tmp = encrypt(target_money + "");
                    String user_money_tmp = encrypt(user_money + "");
                    String client_money_tmp = encrypt(client_money + "");
                    String target_tmp = "+" + money;
                    String user_tmp = "-" + money;

                    users.update(userQ, new BasicDBObject().append("$set",
                            new BasicDBObject().append("money", user_money_tmp)));
                    users.update(targetQ, new BasicDBObject().append("$set",
                            new BasicDBObject().append("money", target_money_tmp)));
                    records.insert(new BasicDBObject().append("ipAddr", this.ipAddr)
                            .append("serial", this.serialNum).append("name", userQ.getString("name"))
                            .append("type", "TRANSFORM").append("delta", encrypt(user_tmp))
                            .append("remains", user_money_tmp).append("time", formatter.format(time)));
                    records.insert(new BasicDBObject().append("ipAddr", this.ipAddr)
                            .append("serial", this.serialNum).append("name", targetQ.getString("name"))
                            .append("type", "TRANSFORM").append("delta", encrypt(target_tmp))
                            .append("remains", target_money_tmp).append("time", formatter.format(time)));
                    clients.update(new BasicDBObject().append("ipAddr", ipAddr), new BasicDBObject()
                            .append("$set", new BasicDBObject().append("money", client_money_tmp)));

                    output.writeObject(new query(true, user_money_tmp));
                    output.flush();
                }
            }
        }

        else if (order.type == method.LOOKUP) {
            DBCursor cursor = records.find(userQ);
            ArrayList<tradeD> details = new ArrayList<>();
            while (cursor.hasNext()) {
                DBObject tmp = cursor.next();
                tradeD obj = new tradeD(tmp.get("serial").toString(), tmp.get("ipAddr").toString(),
                        tmp.get("name").toString(), tmp.get("type").toString(), tmp.get("delta").toString(),
                        tmp.get("remains").toString(), tmp.get("time").toString());
                details.add(obj);
            }
            output.writeObject(new query(true, "", details));
            output.flush();
        }

        else if (order.type == method.RESET) {
            String pwd = user.get("pwd").toString();
            if (order.getPwd0().equals(pwd) && (order.getPwd1() != null)) {
                users.update(currentUser,
                        new BasicDBObject().append("$set", new BasicDBObject().append("pwd", order.getPwd1())));
                output.writeObject(new query(true, "Success"));
                output.flush();
            } else {
                output.writeObject(new query(false, "wrongPwd"));
                output.flush();
            }
        }

    } catch (java.lang.NullPointerException ex) {
        Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
        this.DBShutdown();
    } catch (IOException ex) {
        this.DBShutdown();
        Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:serwlety.dodaj.OsobaServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*  w w w.j ava  2  s  . c  om*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    DBCollection db = baza.FabrykaPolaczenia.getInstance().getConnection().getCollection("Osoba");
    BasicDBObject doc = new BasicDBObject().append("imie", request.getParameter("imie"))
            .append("nazwisko", request.getParameter("nazwisko"))
            .append("partie", request.getParameterValues("partie"))
            .append("wyksztalcenie", request.getParameterValues("wyksztalcenie"));
    int i = 0;
    List<BasicDBObject> lista = new ArrayList<>();
    if (request.getParameterValues("dodatkowy") != null) {
        for (String dodatkowy : request.getParameterValues("dodatkowy")) {
            lista.add(new BasicDBObject(dodatkowy, request.getParameterValues("dodatkowy_wartosc")[i++]));
        }

    }
    doc.append("dodatkowe", lista);
    db.insert(doc);
    request.getRequestDispatcher("/WEB-INF/dodaj/osoba_dodana.jsp").forward(request, response);
}

From source file:simple.crawler.mongo.CrawlingDB.java

License:Open Source License

public boolean insert(CrawlingDBObject obj, Collection collection) {
    DB db = client.getDB(dbName);//from ww  w . j  a  v  a 2s  .c  om
    DBCollection con = db.getCollection(collection.toString());
    try {
        WriteResult result = con.insert(obj);
        if (result.getError() == null) {
            return true;
        } else {
            LOG.error(result.getError());
            return false;
        }
    } catch (MongoException.DuplicateKey e) {
        LOG.debug(e.getMessage());
        return false;
    }
}

From source file:spntoolsdata.crud.servispnCrud.java

public void insertCliente(MongoClient mongo, Cliente us) {
    DB db = mongo.getDB("dbservispntools");
    DBCollection DBCliente = db.getCollection("Clientes");
    DBCliente.insert(us.getDBObjectCliente());
}

From source file:spntoolsdata.crud.servispnCrud.java

public void insertUsuario(MongoClient mongo, Usuario us) {
    DB db = mongo.getDB("dbservispntools");
    DBCollection DBCliente = db.getCollection("Usuarios");
    DBCliente.insert(us.getDBObjectUsuario());
}

From source file:spntoolsdata.crud.servispnCrud.java

public void insertProvincia(MongoClient mongo, Provincia prv) {
    DB db = mongo.getDB("dbservispntools");
    DBCollection DBCliente = db.getCollection("Provincias");
    DBCliente.insert(prv.getDBObjectProvincia());
}

From source file:spntoolsdata.crud.servispnCrud.java

public void insertArtefacto(MongoClient mongo, Artefacto artf) {
    DB db = mongo.getDB("dbservispntools");
    DBCollection DBCliente = db.getCollection("Artefactos");
    DBCliente.insert(artf.getDBObjectArtefacto());
}

From source file:spntoolsdata.crud.servispnCrud.java

public void insertMarca(MongoClient mongo, Marca mra) {
    DB db = mongo.getDB("dbservispntools");
    DBCollection DBCliente = db.getCollection("Marcas");
    DBCliente.insert(mra.getDBObjectMarca());
}