I know this question has been asked on the forums before so I wanted
to contribute an example that I just completed.
I am using Spring to inject everything into my constructor and using
Mule to lookup to the DAO.
Now in my annotated object model:
Now when I look in MongoDB:
If anyone needs additional help, please feel free to ping me. I have
been doing this a lot on the RubyOnRails side and this is my first go
to contribute an example that I just completed.
I am using Spring to inject everything into my constructor and using
Mule to lookup to the DAO.
private Morphia morphia;
private Mongo mongo;
private String dbName;
protected GridFS gridStore;
protected NemsisLoggingDAO(Mongo mongo, Morphia morphia, String
dbName, String bucket) {
super(mongo, morphia, dbName);
gridStore = new GridFS(mongo.getDB(dbName), bucket);
}
public Object create(String fileName, String mimeType, InputStream
fileStream) throws IOException {
GridFSInputFile localFile = gridStore.createFile(fileStream,
fileName);
localFile.setContentType(mimeType);
localFile.save();
return localFile.getId();
}
Now in my annotated object model:
@Property("xml_attachment")
private Object xmlAttachment;
@Property("pdf_attachment")
private Object pdfAttachment;
Then when I call my DAO:
Object xmlFile = null;
Object pdfFile = null;
......
SimpleDateFormat sdf = new
SimpleDateFormat("MMddyyy_HH:mm:ss_zzz");
xmlFile = nlDAO.create("XML_" + xp.evaluate("//E06_02", d)
+ "_" + xp.evaluate("//E06_01", d) + "_" + sdf.format(new Date()),
"application/xml", convertXMLToInputStream(d));
pdfFile = nlDAO.create("PDF_" + xp.evaluate("//E06_02", d)
+ "_" + xp.evaluate("//E06_01", d) + "_" + sdf.format(new Date()),
"application/pdf", runsheet.getRunsheetPDF().getInputStream());
nl.setXmlAttachment(xmlFile);
nl.setPdfAttachment(pdfFile);
Now when I look in MongoDB:
attachments =
{ "_id" : ObjectId("4d96590b318d4fce110c4dec"), "chunkSize" :
NumberLong(262144), "length" : NumberLong(10146), "md5" :
"9d69465005bfdf2b9c356fecdfc6eda7", "filename" :
"XML_John_Doe_040111_19:00:27_EDT", "contentType" : "application/xml",
"uploadDate" : ISODate("2011-04-01T23:00:27.852Z"), "aliases" : null }
{ "_id" : ObjectId("4d96590b318d4fce110c4dee"), "chunkSize" :
NumberLong(262144), "length" : NumberLong(170760), "md5" :
"1851406f2dae5f2646307c47e2f54120", "filename" :
"PDF_John_Doe_040111_19:00:27_EDT", "contentType" : "application/pdf",
"uploadDate" : ISODate("2011-04-01T23:00:27.858Z"), "aliases" : null }
docment =
{ "_id" : ObjectId("4d96590b318d4fce110c4deb"), "className" :
"com.axialexchange.model.nemsis.NemsisLogging", "direction" :
"inbound", "source" : "ESO", "destination" : "allscripts-hmed",
"message_id" : "11223344", "date_created" :
ISODate("2011-04-01T23:00:27.830Z"), "date_of_birth" : "1967-08-13",
"sex" : "M", "last_name" : "Doe", "match" : false, "xml_attachment" :
ObjectId("4d96590b318d4fce110c4dec"), "pdf_attachment" :
ObjectId("4d96590b318d4fce110c4dee") }
If anyone needs additional help, please feel free to ping me. I have
been doing this a lot on the RubyOnRails side and this is my first go
using Morphia on the Java side.
原文:https://groups.google.com/d/topic/morphia/KldkcpWOOOw/discussion