List of usage examples for com.mongodb.gridfs GridFS GridFS
public GridFS(final DB db, final String bucket)
From source file:org.seadpdt.ROServices.java
License:Apache License
public ROServices() { db = MongoDB.getServicesDB();/*w w w . j ava 2 s .co m*/ metaDb = MongoDB.geMetaGenDB(); oreDb = MongoDB.geOreDB(); publicationsCollection = db.getCollection(MongoDB.researchObjects); repositoriesCollection = db.getCollection(MongoDB.repositories); oreMapCollection = metaDb.getCollection(MongoDB.oreMap); oreMapBucket = new GridFS(oreDb, MongoDB.oreMap); fgdcCollection = metaDb.getCollection(MongoDB.fgdc); control.setNoCache(true); }
From source file:org.teiid.translator.mongodb.MongoDBExecutionFactory.java
License:Open Source License
/** * @param field/*from w w w . ja v a2s. co m*/ * @param expectedClass * @return * @throws TranslatorException */ public Object retrieveValue(Object value, Class<?> expectedClass, DB mongoDB, String fqn, String colName) throws TranslatorException { if (value == null) { return null; } if (value.getClass().equals(expectedClass)) { return value; } if (value instanceof DBRef) { Object obj = ((DBRef) value).getId(); if (obj instanceof BasicDBObject) { BasicDBObject bdb = (BasicDBObject) obj; return bdb.get(colName); } return obj; } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Date.class)) { return new java.sql.Date(((java.util.Date) value).getTime()); } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Timestamp.class)) { return new java.sql.Timestamp(((java.util.Date) value).getTime()); } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Time.class)) { return new java.sql.Time(((java.util.Date) value).getTime()); } else if (value instanceof String && expectedClass.equals(BigDecimal.class)) { return new BigDecimal((String) value); } else if (value instanceof String && expectedClass.equals(BigInteger.class)) { return new BigInteger((String) value); } else if (value instanceof String && expectedClass.equals(Character.class)) { return new Character(((String) value).charAt(0)); } else if (value instanceof String && expectedClass.equals(BinaryType.class)) { return new BinaryType(((String) value).getBytes()); } else if (value instanceof String && expectedClass.equals(Blob.class)) { GridFS gfs = new GridFS(mongoDB, fqn); final GridFSDBFile resource = gfs.findOne((String) value); if (resource == null) { return null; } return new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return resource.getInputStream(); } }); } else if (value instanceof String && expectedClass.equals(Clob.class)) { GridFS gfs = new GridFS(mongoDB, fqn); final GridFSDBFile resource = gfs.findOne((String) value); if (resource == null) { return null; } return new ClobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return resource.getInputStream(); } }, -1); } else if (value instanceof String && expectedClass.equals(SQLXML.class)) { GridFS gfs = new GridFS(mongoDB, fqn); final GridFSDBFile resource = gfs.findOne((String) value); if (resource == null) { return null; } return new SQLXMLImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return resource.getInputStream(); } }); } else if (value instanceof BasicDBList) { BasicDBList arrayValues = (BasicDBList) value; //array if (expectedClass.isArray() && !(arrayValues.get(0) instanceof BasicDBObject)) { Class arrayType = expectedClass.getComponentType(); Object array = Array.newInstance(arrayType, arrayValues.size()); for (int i = 0; i < arrayValues.size(); i++) { Object arrayItem = retrieveValue(arrayValues.get(i), arrayType, mongoDB, fqn, colName); Array.set(array, i, arrayItem); } value = array; } } else if (value instanceof org.bson.types.ObjectId) { org.bson.types.ObjectId id = (org.bson.types.ObjectId) value; value = id.toStringBabble(); } else { Transform transform = DataTypeManager.getTransform(value.getClass(), expectedClass); if (transform != null) { try { value = transform.transform(value, expectedClass); } catch (TransformationException e) { throw new TranslatorException(e); } } } return value; }
From source file:org.teiid.translator.mongodb.MongoDBExecutionFactory.java
License:Open Source License
/** * Mongodb only supports certain data types, Teiid need to serialize them in other compatible * formats, and convert them back while reading them. * @param value/*from ww w .j a va 2 s .c o m*/ * @return */ public Object convertToMongoType(Object value, DB mongoDB, String fqn) throws TranslatorException { if (value == null) { return null; } try { if (value instanceof BigDecimal) { return ((BigDecimal) value).doubleValue(); } else if (value instanceof BigInteger) { return ((BigInteger) value).doubleValue(); } else if (value instanceof Character) { return ((Character) value).toString(); } else if (value instanceof java.sql.Date) { return new java.util.Date(((java.sql.Date) value).getTime()); } else if (value instanceof java.sql.Time) { return new java.util.Date(((java.sql.Time) value).getTime()); } else if (value instanceof java.sql.Timestamp) { return new java.util.Date(((java.sql.Timestamp) value).getTime()); } else if (value instanceof BinaryType) { return new Binary(((BinaryType) value).getBytes()); } else if (value instanceof byte[]) { return new Binary((byte[]) value); } else if (value instanceof Blob) { String uuid = UUID.randomUUID().toString(); GridFS gfs = new GridFS(mongoDB, fqn); GridFSInputFile gfsFile = gfs.createFile(((Blob) value).getBinaryStream()); gfsFile.setFilename(uuid); gfsFile.save(); return uuid; } else if (value instanceof Clob) { String uuid = UUID.randomUUID().toString(); GridFS gfs = new GridFS(mongoDB, fqn); GridFSInputFile gfsFile = gfs.createFile(((Clob) value).getAsciiStream()); gfsFile.setFilename(uuid); gfsFile.save(); return uuid; } else if (value instanceof SQLXML) { String uuid = UUID.randomUUID().toString(); GridFS gfs = new GridFS(mongoDB, fqn); GridFSInputFile gfsFile = gfs.createFile(((SQLXML) value).getBinaryStream()); gfsFile.setFilename(uuid); gfsFile.save(); return uuid; } else if (value instanceof Object[]) { BasicDBList list = new BasicDBList(); for (Object obj : (Object[]) value) { list.add(obj); } return list; } return value; } catch (SQLException e) { throw new TranslatorException(e); } }
From source file:org.waveprotocol.box.server.persistence.mongodb.MongoDbStore.java
License:Apache License
private GridFS getAttachmentGrid() { if (attachmentGrid == null) { attachmentGrid = new GridFS(database, "attachments"); }// w w w.j a v a2 s.c o m return attachmentGrid; }
From source file:rapture.blob.mongodb.GridFSBlobHandler.java
License:Open Source License
public GridFS getGridFS() { DB db = MongoDBFactory.getDB(instanceName); return new GridFS(db, bucket); }
From source file:rmi_video.VideoServer.java
@Override public boolean addVideo(VideoData d) throws RemoteException { try {//w ww . j a v a 2s . c om //Conexao com mongoDB MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("VideoDatabase"); try (FileOutputStream fos = new FileOutputStream("/Users/philcr/Documents/" + d.getFileName())) { fos.write(d.getData()); } File videoFile = new File("/Users/philcr/Documents/" + d.getFileName()); GridFS gfsVideo = new GridFS(db, "video"); //Cria e salva o arquivo no DB pelo GridFS GridFSInputFile gfsFile = gfsVideo.createFile(videoFile); gfsFile.setId(new ObjectId()); //Utiliza a criao de ID do mongo gfsFile.put("videoId", d.getId()); //Utiliza nosso metodo de ID gfsFile.setFilename(d.getFileName()); gfsFile.save(); //Exclui o arquivo local boolean deletedFlag = videoFile.delete(); if (!deletedFlag) { System.err.println("File could not be deleted!"); } mongoClient.close(); return true; } catch (UnknownHostException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (FileNotFoundException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (IOException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return false; } }
From source file:rmi_video.VideoServer.java
@Override public VideoData getVideo(String id) throws RemoteException { try {// ww w . j a v a 2s . c o m //Cria conexao com MongoDB MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("VideoDatabase"); //Recupera o video atraves do ID GridFS gfsVideo = new GridFS(db, "video"); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("videoId", id); GridFSDBFile videoForOutput = gfsVideo.findOne(whereQuery); String filename = videoForOutput.getFilename(); try (FileOutputStream fos = new FileOutputStream("/Users/philcr/Documents/" + filename)) { videoForOutput.writeTo(fos); } Path path = Paths.get("/Users/philcr/Documents/" + filename); byte[] data = Files.readAllBytes(path); File videoFile = new File("/Users/philcr/Documents/" + filename); //Exclui o arquivo local boolean deletedFlag = videoFile.delete(); if (!deletedFlag) { System.err.println("Video could not be deleted!"); } mongoClient.close(); return new VideoData(data, filename, id); } catch (UnknownHostException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (FileNotFoundException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (IOException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:tango.mongo.ImageManager.java
License:Open Source License
public ImageManager(MongoConnector mongo, DB project) { this.mongo = mongo; this.project = project; ArrayList<ObjectId> exps = mongo.getExperimentIds(); this.gfsField = new HashMap<ObjectId, GridFS>(exps.size()); this.gfsNucleus = new HashMap<ObjectId, GridFS>(exps.size()); gfsFieldThumbnail = new GridFS(project, "fieldThumbnail"); gfsNucleusThumbnail = new GridFS(project, "nucleusThumbnail"); DBCollection fieldsFilesT = project.getCollection("fieldThumbnail.files"); fieldsFilesT.createIndex(new BasicDBObject("field_id", 1)); DBCollection nucleiFilesT = project.getCollection("nucleusThumbnail.files"); nucleiFilesT.createIndex(new BasicDBObject("nucleus_id", 1).append("fileRank", 1)); for (ObjectId xp : exps) addExperiment(xp);//from w ww .ja v a 2 s.c o m }
From source file:tango.mongo.ImageManager.java
License:Open Source License
private void addExperiment(ObjectId id) { if (!gfsField.containsKey(id)) { String collectionName = "fieldImages_" + id.toHexString(); if (project.collectionExists(collectionName + ".files") || !project.collectionExists("field")) { gfsField.put(id, new GridFS(project, collectionName)); DBCollection fieldsFiles = project.getCollection(collectionName + ".files"); fieldsFiles.createIndex(new BasicDBObject("field_id", 1).append("fileRank", 1)); } else if (gfsFieldAll == null) {// retrocompatibilit gfsFieldAll = new GridFS(project, "field"); DBCollection fieldsFiles = project.getCollection("field.files"); fieldsFiles.createIndex(new BasicDBObject("field_id", 1).append("fileRank", 1)); gfsField.put(id, gfsFieldAll); }//w ww . j a va2 s .c o m } if (!gfsNucleus.containsKey(id)) { String collectionName = "nucleusImages_" + id.toHexString(); if (project.collectionExists(collectionName + ".files") || !project.collectionExists("nucleus")) { gfsNucleus.put(id, new GridFS(project, collectionName)); DBCollection nucleiFiles = project.getCollection(collectionName + ".files"); nucleiFiles .createIndex(new BasicDBObject("nucleus_id", 1).append("fileIdx", 1).append("fileType", 1)); } else if (gfsNucleusAll == null) {// retrocompatibilit gfsNucleusAll = new GridFS(project, "nucleus"); DBCollection nucleiFiles = project.getCollection("nucleus.files"); nucleiFiles .createIndex(new BasicDBObject("nucleus_id", 1).append("fileIdx", 1).append("fileType", 1)); gfsNucleus.put(id, gfsNucleusAll); } } }
From source file:tango.mongo.ImageManager.java
License:Open Source License
private void transferXP(ObjectId xpId, int counter) { GridFS gfsNuc = new GridFS(project, "fieldImages_" + xpId.toHexString()); GridFS gfsF = new GridFS(project, "nucleusImages_" + xpId.toHexString()); String label = "Update Experiment:" + counter + " "; ArrayList<ObjectId> fids = mongo.getFieldIds(xpId); Core.getProgressor().setAction(label + " Field Images"); Core.getProgressor().resetProgress(fids.size()); for (ObjectId fId : fids) { transferFiles("field_id", fId, this.gfsFieldAll, gfsF); Core.getProgressor().incrementStep(); }/*from ww w. ja v a2s . com*/ ArrayList<ObjectId> nids = mongo.getNucleusIds(xpId); Core.getProgressor().setAction(label + " Nucleus Images"); Core.getProgressor().resetProgress(nids.size()); for (ObjectId nId : nids) { transferFiles("nucleus_id", nId, this.gfsFieldAll, gfsF); Core.getProgressor().incrementStep(); } this.gfsField.put(xpId, gfsF); this.gfsNucleus.put(xpId, gfsNuc); }