Example usage for com.mongodb DBObject put

List of usage examples for com.mongodb DBObject put

Introduction

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

Prototype

Object put(String key, Object v);

Source Link

Document

Sets a name/value pair in this object.

Usage

From source file:com.bugull.mongo.BuguUpdater.java

License:Apache License

private T findOne(String id) {
    DBObject dbo = new BasicDBObject();
    dbo.put(Operator.ID, IdUtil.toDbId(clazz, id));
    DBObject result = coll.findOne(dbo);
    return MapperUtil.fromDBObject(clazz, result);
}

From source file:com.bugull.mongo.BuguUpdater.java

License:Apache License

/**
 * Remove one or more filed(column) of an entity
 * @param id the entity's id//  w w  w  .j  a v a2 s . c  o  m
 * @param keys the field's name
 * @return 
 */
public WriteResult unset(String id, String... keys) {
    DBObject query = new BasicDBObject();
    for (String key : keys) {
        query.put(key, 1);
    }
    DBObject unset = new BasicDBObject(Operator.UNSET, query);
    return updateOne(id, unset, keys);
}

From source file:com.bugull.mongo.BuguUpdater.java

License:Apache License

/**
 * Remove one or more filed(column).//from ww  w  .  jav a2s. co  m
 * @param query mathcing conditon
 * @param keys the field's name
 * @return 
 */
public WriteResult unset(BuguQuery query, String... keys) {
    DBObject dbo = new BasicDBObject();
    for (String key : keys) {
        dbo.put(key, 1);
    }
    DBObject unset = new BasicDBObject(Operator.UNSET, dbo);
    return updateMulti(query.getCondition(), unset, keys);
}

From source file:com.bugull.mongo.fs.BuguFS.java

License:Apache License

public void rename(String oldName, String newName) {
    DBObject query = new BasicDBObject(FILENAME, oldName);
    DBObject dbo = files.findOne(query);
    dbo.put(FILENAME, newName);
    files.save(dbo);//www  .  j  av  a 2s  . c  om
}

From source file:com.bugull.mongo.fs.BuguFS.java

License:Apache License

public void rename(GridFSDBFile file, String newName) {
    ObjectId id = (ObjectId) file.getId();
    DBObject query = new BasicDBObject(Operator.ID, id);
    DBObject dbo = files.findOne(query);
    dbo.put(FILENAME, newName);
    files.save(dbo);/*from  w ww  .  j a v a2 s.  c  o m*/
}

From source file:com.bugull.mongo.fs.ImageUploader.java

License:Apache License

private InputStream getOriginalInputStream() {
    DBObject query = new BasicDBObject(BuguFS.FILENAME, filename);
    query.put(DIMENSION, null);
    BuguFS fs = BuguFSFactory.getInstance().create(bucketName, chunkSize);
    GridFSDBFile f = fs.findOne(query);/*from   ww w. j a  v a  2  s.c  om*/
    return f.getInputStream();
}

From source file:com.bugull.mongo.fs.UploadedFileServlet.java

License:Apache License

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (!StringUtil.isEmpty(password)) {
        String p = request.getParameter("password");
        if (StringUtil.isEmpty(p) || !p.equals(password)) {
            return;
        }/*from  w  ww . j  av a2  s  .  c o  m*/
    }
    String uri = request.getRequestURI();
    int second = uri.indexOf(SLASH, 1);
    uri = uri.substring(second);
    int last = uri.lastIndexOf(SLASH);
    String filename = uri.substring(last + 1);
    DBObject query = new BasicDBObject(BuguFS.FILENAME, filename);
    query.put(ImageUploader.DIMENSION, null); //note: this is necessary!
    String bucketName = GridFS.DEFAULT_BUCKET;
    int first = uri.indexOf(SLASH);
    if (first != last) {
        String sub = uri.substring(first + 1, last);
        String[] arr = sub.split(SLASH);
        for (int i = 0; i < arr.length; i += 2) {
            if (arr[i].equals(BuguFS.BUCKET)) {
                bucketName = arr[i + 1];
            } else {
                query.put(arr[i], arr[i + 1]);
            }
        }
    }
    //check if the bucket is allowed to access by this servlet
    if (!StringUtil.isEmpty(allowBucket) && !allowBucket.equalsIgnoreCase(bucketName)) {
        return;
    }
    if (!StringUtil.isEmpty(forbidBucket) && forbidBucket.equalsIgnoreCase(bucketName)) {
        return;
    }
    BuguFS fs = BuguFSFactory.getInstance().create(bucketName);
    GridFSDBFile f = fs.findOne(query);
    if (f == null) {
        return;
    }
    OutputStream os = response.getOutputStream();
    int fileLength = (int) f.getLength();
    String ext = StringUtil.getExtention(filename);
    response.setContentType(getContentType(ext));
    String range = request.getHeader("Range");
    //normal http request, no "range" in header.
    if (StringUtil.isEmpty(range)) {
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentLength(fileLength);
        if (needCache(ext)) {
            String modifiedSince = request.getHeader("If-Modified-Since");
            DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
            df.setTimeZone(TimeZone.getTimeZone("GMT"));
            Date uploadDate = f.getUploadDate();
            String lastModified = df.format(uploadDate);
            if (modifiedSince != null) {
                Date modifiedDate = null;
                Date sinceDate = null;
                try {
                    modifiedDate = df.parse(lastModified);
                    sinceDate = df.parse(modifiedSince);
                } catch (ParseException ex) {
                    logger.error("Can not parse the Date", ex);
                }
                if (modifiedDate.compareTo(sinceDate) <= 0) {
                    response.setStatus(304); //Not Modified
                    return;
                }
            }
            long maxAge = 365L * 24L * 60L * 60L; //one year, in seconds
            response.setHeader("Cache-Control", "max-age=" + maxAge);
            response.setHeader("Last-Modified", lastModified);
            response.setDateHeader("Expires", uploadDate.getTime() + maxAge * 1000L);
        } else {
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
        }
        f.writeTo(os);
    }
    //has "range" in header
    else {
        range = range.substring("bytes=".length());
        if (StringUtil.isEmpty(range)) {
            return;
        }
        int begin = 0;
        int end = fileLength - 1;
        boolean onlyLast = range.startsWith("-");
        String[] rangeArray = range.split("-");
        if (rangeArray.length == 1) {
            if (onlyLast) {
                begin = fileLength - Integer.parseInt(rangeArray[0]);
            } else {
                begin = Integer.parseInt(rangeArray[0]);
            }
        } else if (rangeArray.length == 2) {
            begin = Integer.parseInt(rangeArray[0]);
            end = Integer.parseInt(rangeArray[1]);
        }
        response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
        int contentLength = end - begin + 1;
        response.setContentLength(contentLength);
        response.setHeader("Content-Range", "bytes " + begin + "-" + end + "/" + contentLength);
        InputStream is = f.getInputStream();
        is.skip(begin);
        int read = -1;
        int bufferSize = (int) f.getChunkSize();
        byte[] buffer = new byte[bufferSize];
        int remain = contentLength;
        int readSize = Math.min(bufferSize, remain);
        while ((read = is.read(buffer, 0, readSize)) != -1) {
            os.write(buffer, 0, read);
            remain -= read;
            if (remain <= 0) {
                break;
            }
            readSize = Math.min(bufferSize, remain);
        }
        StreamUtil.safeClose(is);
    }
    StreamUtil.safeClose(os);
}

From source file:com.bugull.mongo.misc.InternalDao.java

License:Apache License

/**
 * Get one entity without the lazy property.
 * @param id// w  ww . j  a v a2 s. c  om
 * @return 
 */
public T findOneLazily(String id) {
    DBObject dbo = new BasicDBObject();
    dbo.put(Operator.ID, IdUtil.toDbId(clazz, id));
    DBObject result = coll.findOne(dbo, keys);
    return MapperUtil.fromDBObject(clazz, result);
}

From source file:com.bugull.mongo.utils.MapperUtil.java

License:Apache License

/**
 * Convert an entity object to DBObject/*w  ww .  j a v a 2 s.  c om*/
 * @param obj
 * @return 
 */
public static DBObject toDBObject(Object obj) {
    if (obj == null) {
        return null;
    }
    Class<?> clazz = obj.getClass();
    Field[] fields = FieldsCache.getInstance().get(clazz);
    DBObject dbo = new BasicDBObject();
    for (Field field : fields) {
        Encoder encoder = EncoderFactory.create(obj, field);
        if (encoder != null && !encoder.isNullField()) {
            dbo.put(encoder.getFieldName(), encoder.encode());
        }
    }
    return dbo;
}

From source file:com.bugull.mongo.utils.MapperUtil.java

License:Apache License

/**
 * convert order string to DBObject.//from w w w.  j  a  v a2 s .  c o m
 * @param orderBy
 * @return 
 */
public static DBObject getSort(String orderBy) {
    DBObject sort = new BasicDBObject();
    orderBy = orderBy.replaceAll("[{}'']", "");
    String[] arr = orderBy.split(",");
    for (String s : arr) {
        String[] kv = s.split(":");
        String k = kv[0].trim();
        String v = kv[1].trim();
        if (k.equals("id")) { //it's not strict here, but can solve most cases.
            k = Operator.ID;
        }
        sort.put(k, Integer.parseInt(v));
    }
    return sort;
}