Example usage for com.mongodb.gridfs GridFSInputFile getMetaData

List of usage examples for com.mongodb.gridfs GridFSInputFile getMetaData

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFSInputFile getMetaData.

Prototype

public DBObject getMetaData() 

Source Link

Document

Gets the file metadata.

Usage

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

/**
 * On persist GFS./*  w w  w. ja  v a  2  s  .co m*/
 * 
 * @param entity
 *            the entity
 * @param entityId
 *            the entityId
 * @param entityMetadata
 *            the entity metadata
 * @param isUpdate
 *            the is update
 */
private void onPersistGFS(Object entity, Object entityId, EntityMetadata entityMetadata, boolean isUpdate) {
    KunderaGridFS gfs = new KunderaGridFS(mongoDb, entityMetadata.getTableName());
    if (!isUpdate) {
        GridFSInputFile gfsInputFile = handler.getGFSInputFileFromEntity(gfs, entityMetadata, entity,
                kunderaMetadata, isUpdate);
        saveGridFSFile(gfsInputFile, entityMetadata);
    } else {
        Object val = handler.getLobFromGFSEntity(gfs, entityMetadata, entity, kunderaMetadata);
        String md5 = MongoDBUtils.calculateMD5(val);
        GridFSDBFile outputFile = findGridFSDBFile(entityMetadata, entityId);

        // checking MD5 of the file to be updated with the file saved in DB
        if (md5.equals(outputFile.getMD5())) {
            DBObject metadata = handler.getMetadataFromGFSEntity(gfs, entityMetadata, entity, kunderaMetadata);
            outputFile.setMetaData(metadata);
            outputFile.save();
        } else {
            // GFSInput file is created corresponding to the entity to be
            // merged with a new ObjectID()
            GridFSInputFile gfsInputFile = handler.getGFSInputFileFromEntity(gfs, entityMetadata, entity,
                    kunderaMetadata, isUpdate);
            ObjectId updatedId = (ObjectId) gfsInputFile.getId();
            DBObject metadata = gfsInputFile.getMetaData();

            // updated file is saved in DB
            saveGridFSFile(gfsInputFile, entityMetadata);

            // last version of file is deleted
            DBObject query = new BasicDBObject("_id", outputFile.getId());
            gfs.remove(query);

            // newly added file is found using its _id
            outputFile = gfs.findOne(updatedId);

            // Id of entity (which is saved in metadata) is updated to its
            // actual Id
            metadata.put(((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName(), entityId);
            outputFile.setMetaData(metadata);

            // output file is updated
            outputFile.save();
        }
    }
}

From source file:com.linuxbox.enkive.docstore.mongogrid.MongoGridDocStoreService.java

License:Open Source License

void setFileMetaData(GridFSInputFile newFile, Document document, int shardKey) {
    newFile.setContentType(document.getMimeType());

    // store the encoding as meta-data for EncodedDocuments
    DBObject metaData = newFile.getMetaData();
    if (metaData == null) {
        metaData = new BasicDBObject();
    }// w  ww.  ja v a  2  s. com

    metaData.put(INDEX_STATUS_KEY, STATUS_UNINDEXED);
    metaData.put(FILE_EXTENSION_KEY, document.getFileExtension());
    metaData.put(BINARY_ENCODING_KEY, document.getBinaryEncoding());
    metaData.put(INDEX_SHARD_KEY, shardKey);

    newFile.setMetaData(metaData);
}