Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

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

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:com.fileoperations.FolderDownload.java

public Boolean copyFolder(String newPath) throws IOException {
    try {//ww w. jav a2s.  co m
        String mongoFolder = parentPath + pathMerger + folderName;

        BasicDBObject query = new BasicDBObject();
        query.put("_id", mongoFolder);
        DBCursor cursor = collection.find(query);
        if (cursor.hasNext()) {
            BasicDBObject newquery = new BasicDBObject();
            newquery.put("_id", newPath + pathMerger + folderName);
            if (collection.find(newquery).hasNext()) {
                return false;
            }

            getPathOfAllChildrenFolder(parentPath, folderName);
            BasicDBObject toFindAllFilesInFolder = new BasicDBObject();
            toFindAllFilesInFolder.put("$or", pathOfChildrenFolders);
            GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName);
            List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder);

            for (int i = 0; i < AllFiles.size(); i++) {
                GridFSDBFile indivFile = AllFiles.get(i);
                InputStream data = indivFile.getInputStream();
                String zipPath;
                zipPath = indivFile.get("path").toString();
                String tempFileName = indivFile.getFilename();

                zipPath = zipPath.replaceFirst(parentPath, newPath);
                BasicDBObject document = new BasicDBObject();
                document.append("_id", zipPath + pathMerger + tempFileName);
                document.append("folder", "0");
                document.append("parent", zipPath);
                document.append("name", tempFileName);
                int index = tempFileName.lastIndexOf(".");
                document.append("type", tempFileName.substring(index));
                collection.insert(document);
                GridFSInputFile inputFile = fileStore.createFile(data);
                inputFile.setId(zipPath + pathMerger + tempFileName);
                inputFile.put("path", zipPath);
                inputFile.setFilename(tempFileName);
                inputFile.save();

            }

            BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject();
            toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders);
            DBCursor allFolders = collection.find(toFindAllEmptyFilesInFolder);
            while (allFolders.hasNext()) {
                DBObject temp = allFolders.next();
                if (temp.get("folder").toString().equals("1")) {
                    String tempPath = temp.get("parent").toString().replaceFirst(parentPath, newPath);
                    BasicDBObject document = new BasicDBObject();
                    document.put("_id", tempPath + pathMerger + temp.get("name"));
                    document.put("folder", "1");
                    document.put("name", temp.get("name"));
                    document.put("parent", tempPath);
                    document.put("type", "1");
                    collection.insert(document);

                }
            }

            return true;
        } else {
            return false;
        }

    } finally {
        mymongo.closeConnection();
    }
}

From source file:com.fileoperations.FolderDownload.java

public Boolean renameFolder(String newName) throws IOException {
    try {//from   w ww .j  av  a2s  .  c om
        String mongoFolder = parentPath + pathMerger + folderName;
        BasicDBObject query = new BasicDBObject();
        query.put("_id", mongoFolder);
        DBCursor cursor = collection.find(query);
        if (cursor.hasNext()) {
            BasicDBObject newquery = new BasicDBObject();
            newquery.put("_id", parentPath + pathMerger + newName);
            if (collection.find(newquery).hasNext()) {
                return false;
            }
            BasicDBObject doc = new BasicDBObject();
            doc.put("_id", parentPath + pathMerger + newName);
            doc.put("folder", "1");
            doc.put("name", newName);
            doc.put("parent", parentPath);
            doc.put("type", "1");

            collection.insert(doc);

            getPathOfAllChildrenFolder(parentPath, folderName);
            BasicDBObject toFindAllFilesInFolder = new BasicDBObject();
            toFindAllFilesInFolder.put("$or", pathOfChildrenFolders);
            GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName);
            List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder);

            for (int i = 0; i < AllFiles.size(); i++) {
                GridFSDBFile indivFile = AllFiles.get(i);
                InputStream data = indivFile.getInputStream();
                String zipPath;
                zipPath = indivFile.get("path").toString();
                String tempFileName = indivFile.getFilename();
                zipPath = zipPath.replaceFirst(parentPath + pathMerger + folderName,
                        parentPath + pathMerger + newName);
                BasicDBObject document = new BasicDBObject();
                document.append("_id", zipPath + pathMerger + tempFileName);
                document.append("folder", "0");
                document.append("parent", zipPath);
                document.append("name", tempFileName);
                int index = tempFileName.lastIndexOf(".");
                document.append("type", tempFileName.substring(index));
                collection.insert(document);
                GridFSInputFile inputFile = fileStore.createFile(data);
                inputFile.setId(zipPath + pathMerger + tempFileName);
                inputFile.put("path", zipPath);
                inputFile.setFilename(tempFileName);
                inputFile.save();

            }

            BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject();
            toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders);
            DBCursor allFolders = collection.find(toFindAllEmptyFilesInFolder);
            while (allFolders.hasNext()) {
                DBObject temp = allFolders.next();
                if (temp.get("folder").toString().equals("1")) {
                    String tempPath = temp.get("parent").toString();
                    tempPath = tempPath.replaceFirst(parentPath + pathMerger + folderName,
                            parentPath + pathMerger + newName);
                    BasicDBObject updocument = new BasicDBObject();
                    updocument.put("_id", tempPath + pathMerger + temp.get("name"));
                    updocument.put("folder", "1");
                    updocument.put("name", temp.get("name"));
                    updocument.put("parent", tempPath);
                    updocument.put("type", "1");
                    collection.insert(updocument);

                }
            }

            return true;
        } else {
            return false;
        }

    } finally {
        mymongo.closeConnection();
    }
}

From source file:com.fileoperations.RenameFolder.java

public Boolean forSingleFile() {
    try {/*from w w w  . j a va 2s .  c o m*/
        if (oldName.contains(".")) {

            BasicDBObject query = new BasicDBObject();
            query.put("_id", parentPath + pathMerger + oldName);
            DBCursor cursor = collection.find(query);

            if (cursor.hasNext()) {
                DBObject renameFile = cursor.next();
                BasicDBObject checknewquery = new BasicDBObject();
                checknewquery.put("_id", parentPath + pathMerger + newName + renameFile.get("type").toString());
                DBCursor tempCursor = collection.find(checknewquery);
                if (tempCursor.hasNext()) {
                    return false;
                }

                GridFS file = new GridFS(mymongo.getDB(), userCollectionName);
                InputStream data = file.findOne(query).getInputStream();

                BasicDBObject document = new BasicDBObject();
                document.append("_id", parentPath + pathMerger + newName + renameFile.get("type").toString());
                document.append("folder", "0");
                document.append("parent", parentPath);
                document.append("name", newName + renameFile.get("type").toString());
                document.append("type", renameFile.get("type").toString());
                collection.insert(document);
                GridFSInputFile inputFile = file.createFile(data);
                inputFile.setId(parentPath + pathMerger + newName + renameFile.get("type").toString());
                inputFile.put("path", parentPath);
                inputFile.setFilename(newName + renameFile.get("type").toString());
                inputFile.save();
                file.remove(file.findOne(query));
                collection.remove(renameFile);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } finally {
        mymongo.closeConnection();
    }
}

From source file:com.fpt.xml.hth.db.lib.converter.MovieConverter.java

public BasicDBObject convertModelToBasicObject(MovieDB model) {
    String name = model.getName();
    String description = model.getDescription();
    String length = model.getLength();
    String actor = model.getActor();
    String director = model.getDirector();
    String poster = model.getPoster();
    String trailer = model.getTrailer();
    String show_date = model.getShow_date();
    String genre = model.getGenre();
    String age_restriction = model.getAge_restriction();
    String audio_type = model.getAudio_type();
    String video_type = model.getVideo_type();
    //set value for basicObj
    BasicDBObject basicObj = new BasicDBObject();
    basicObj.append("name", name);
    basicObj.append("description", description);
    basicObj.append("length", length);
    basicObj.append("actor", actor);
    basicObj.append("director", director);
    basicObj.append("poster", poster);
    basicObj.append("trailer", trailer);
    basicObj.append("show_date", show_date);
    basicObj.append("genre", genre);
    basicObj.append("age_restriction", age_restriction);
    basicObj.append("audio_type", audio_type);
    basicObj.append("video_type", video_type);
    return basicObj;

}

From source file:com.fpt.xml.hth.db.lib.converter.MovieTheaterSessionConverter.java

/**
 * To convert from MovieDBTheaterSessionDTO object to BasicDBObject object
 *
 * @param model//from  w  w w .ja  v a 2s  .  c o m
 * @return BasicDBObject
 */
public BasicDBObject convertModelToBasicObject(MovieTheaterSessionDTO model) {
    ObjectId id = model.getId();
    MovieDB movie = model.getMovie();
    List<TheaterSessionDTO> theaters = model.getTheaters();
    //convert movie to BasicDBObject
    BasicDBObject basicMovie = movieConverter.convertModelToBasicObject(movie);
    //convert lstTheaters to BasicDBList
    BasicDBList basicLst = new BasicDBList();
    for (int i = 0; i < theaters.size(); i++) {
        TheaterSessionDTO theaterSessionDTO = (TheaterSessionDTO) theaters.get(i);
        BasicDBObject basic = tsConverter.convertModelToBasicObject(theaterSessionDTO);
        basicLst.add(basic);
    }
    // set value for basicMovieTheaterSession
    BasicDBObject basicMovieTheaterSession = new BasicDBObject();
    basicMovieTheaterSession.append("_id", id);
    basicMovieTheaterSession.append("movie", basicMovie);
    basicMovieTheaterSession.append("theaters", basicLst);

    return basicMovieTheaterSession;
}

From source file:com.fpt.xml.hth.db.lib.converter.TheaterSessionConverter.java

/**
 * To convert from TheaterDBSessionDTO object to BasicDBObject object
 *
 * @param model/* w  w w . j a va 2  s .c om*/
 * @return BasicDBObject
 */
public BasicDBObject convertModelToBasicObject(TheaterSessionDTO model) {
    String cinemaName = model.getCinemaName();
    String id = model.getId();
    TheaterDB theater = model.getTheater();
    List<String> lstSessions = model.getLstSession();
    // convert theather to basicTheater
    BasicDBObject basicTheater = conveter.convertModelToBasicObject(theater);
    // convert lstSessions to 
    BasicDBList basicLst = new BasicDBList();
    for (int i = 0; i < lstSessions.size(); i++) {
        basicLst.add(lstSessions.get(i));
    }
    // convert object theater
    BasicDBObject basicTheaterSession = new BasicDBObject();
    basicTheaterSession.append("cinemaName", cinemaName);
    basicTheaterSession.append("theater", basicTheater);
    basicTheaterSession.append("sessions", basicLst);
    basicTheaterSession.append("id", id);
    return basicTheaterSession;
}

From source file:com.fpt.xml.hth.db.lib.DAO.MovieDAO.java

/**
 * get list movies by param city//from w w  w.ja v  a 2  s.co m
 *
 * @param city
 * @return List<MovieTheaterSessionDTO>
 */
public List<MovieTheaterSessionDTO> getAllByCity(String city) {
    connection();
    List<MovieTheaterSessionDTO> lst = new ArrayList<MovieTheaterSessionDTO>();
    DBCollection collection = movieCollection;

    BasicDBObject dbObject = new BasicDBObject();
    dbObject.append("theaters", new BasicDBObject("$elemMatch", new BasicDBObject("theater.city", city)));

    DBCursor cursor = collection.find(dbObject);

    while (cursor.hasNext()) {
        BasicDBObject basic = (BasicDBObject) cursor.next();
        MovieTheaterSessionDTO movieDto = converter.convertBasicObjectToModel(basic);
        //            List<TheaterSessionDTO> lstTheaterSession = new ArrayList<TheaterSessionDTO>();
        //            if (city != null && !city.isEmpty()) {
        //                for (TheaterSessionDTO theaterDTO : movieDto.getTheaters()) {
        //                    if (theaterDTO.getTheater().getCity().equals(city)) {                
        //                        lstTheaterSession.add(theaterDTO);
        //                    }
        //                }
        //            }
        //            movieDto.setTheaters(lstTheaterSession);
        lst.add(movieDto);
    }
    cursor.close();
    mongoClient.close();
    return lst;
}

From source file:com.gederin.blog.dao.SessionDAO.java

License:Apache License

public String startSession(String username) {

    // get 32 byte random number. that's a lot of bits.
    SecureRandom generator = new SecureRandom();
    byte randomBytes[] = new byte[32];
    generator.nextBytes(randomBytes);/*w ww.  j  a  va  2 s  .  c  om*/

    BASE64Encoder encoder = new BASE64Encoder();

    String sessionID = encoder.encode(randomBytes);

    // build the BSON object
    BasicDBObject session = new BasicDBObject("username", username);

    session.append("_id", sessionID);

    sessionsCollection.insert(session);

    return session.getString("_id");
}

From source file:com.gederin.blog.dao.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    BasicDBObject dbObject = new BasicDBObject("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        dbObject.append("email", email);
    }//from  ww  w .  ja  v  a2s.  c  o  m

    try {
        this.usersCollection.insert(dbObject);
        return true;
    } catch (MongoException.DuplicateKey e) {
        System.out.println("Username already in use: " + username);
        return false;
    }
}

From source file:com.geeksanon.UserDAO.java

License:Open Source License

/**
 * Add the user to the database./*  w w w .j a  va 2  s  .c  om*/
 * 
 * @param username
 *            username
 * @param password
 *            password
 * @param email
 *            email
 * @return boolean if added/ not
 */
public boolean addUser(String username, String password, String email) {
    String hashedPassword = createHashPassword(password, Integer.toString(new SecureRandom().nextInt()));
    BasicDBObject user = new BasicDBObject().append("_id", username).append("password", hashedPassword);
    if (email != null && !email.equals("")) {
        user.append("email", email);
    }
    try {
        userCollection.insert(user);
        return true;
    } catch (MongoException.DuplicateKey e) {
        LOGGER.error("Username exists!");
        return false;
    }
}