@SuppressWarnings("serial") @Test public void testUploadRestletSaveWithAutoIncr() throws Exception { ((AbstractRuntimeService) runtime).setProperty("org.nuxeo.ecm.platform.liveedit.autoversioning", "minor"); DocumentModel doc = session.createDocumentModel("/", "myFile", "File"); doc = session.createDocument(doc); session.save(); doc = session.getDocument(doc.getRef()); assertNull( "At the begining the file document shouldn't have any content", doc.getProperty("file", "content")); Long major = (Long) doc.getPropertyValue("uid:major_version"); Long minor = (Long) doc.getPropertyValue("uid:minor_version"); UploadFileRestlet restlet = new UploadFileRestlet() { @Override protected CoreSession getDocumentManager() { return session; } }; String expectedFileName = "myfile.txt"; String blobPropertyName = DEFAULT_SCHEMA + ":" + DEFAULT_BLOB_FIELD; String filenamePropertyName = DEFAULT_SCHEMA + ":" + DEFAULT_FILENAME_FIELD; restlet.saveFileToDocument(expectedFileName, doc, blobPropertyName, filenamePropertyName, new ByteArrayInputStream( "Content of the file.".getBytes("UTF-8"))); doc = session.getDocument(doc.getRef()); assertEquals("The file name is", expectedFileName, doc.getProperty( DEFAULT_SCHEMA, DEFAULT_FILENAME_FIELD)); assertEquals( "The content of the file is", "Content of the file.", ((Blob) doc.getProperty(DEFAULT_SCHEMA, DEFAULT_BLOB_FIELD)).getString()); List<DocumentModel> versions = session.getVersions(doc.getRef()); assertTrue("Should have at least one version snapshotted", versions != null && versions.size() == 1); doc = session.getDocument(doc.getRef()); assertEquals("The major version shouldn't have been incremented", major, doc.getPropertyValue("uid:major_version")); assertEquals("The minor version should have been incremented", new Long(minor + 1), doc.getPropertyValue("uid:minor_version")); }
protected void saveFileToDocument(String filename, DocumentModel dm, String blobPropertyName, String filenamePropertyName, InputStream is) throws IOException, PropertyException, ClientException { Blob blob = StreamingBlob.createFromStream(is).persist(); blob.setFilename(filename); dm.setPropertyValue(blobPropertyName, (Serializable) blob); dm.setPropertyValue(filenamePropertyName, filename); getDocumentManager().saveDocument(dm); String versioningPolicy = Framework.getProperty(LIVED_AUTOVERSIONING_PROP); if (doAutoMinorIncrement(versioningPolicy, dm)) { if (dm.isCheckedOut()) { dm.checkIn(VersioningOption.MINOR, "Live edit (UploadFileRestlet) autoversioning"); } } getDocumentManager().save(); }
UploadFileRestlet extends BaseNuxeoRestlet implements
        LiveEditConstants, Serializable { protected void saveFileToDocument(String filename, DocumentModel dm, String blobPropertyName, String filenamePropertyName, InputStream is) throws IOException, PropertyException, ClientException { Blob blob = StreamingBlob.createFromStream(is).persist(); blob.setFilename(filename); dm.setPropertyValue(blobPropertyName, (Serializable) blob); dm.setPropertyValue(filenamePropertyName, filename); getDocumentManager().saveDocument(dm); String versioningPolicy = Framework.getProperty(LIVED_AUTOVERSIONING_PROP); if (doAutoMinorIncrement(versioningPolicy, dm)) { if (dm.isCheckedOut()) { dm.checkIn(VersioningOption.MINOR, "Live edit (UploadFileRestlet) autoversioning"); } } getDocumentManager().save(); } }
UploadFileRestlet extends BaseNuxeoRestlet implements
        LiveEditConstants, Serializable { protected void saveFileToDocument(String filename, DocumentModel dm, String blobPropertyName, String filenamePropertyName, InputStream is) throws IOException, PropertyException, ClientException { Blob blob = StreamingBlob.createFromStream(is).persist(); blob.setFilename(filename); dm.setPropertyValue(blobPropertyName, (Serializable) blob); dm.setPropertyValue(filenamePropertyName, filename); getDocumentManager().saveDocument(dm); String versioningPolicy = Framework.getProperty(LIVED_AUTOVERSIONING_PROP); if (doAutoMinorIncrement(versioningPolicy, dm)) { if (dm.isCheckedOut()) { dm.checkIn(VersioningOption.MINOR, "Live edit (UploadFileRestlet) autoversioning"); } } getDocumentManager().save(); }  }
UploadFileRestlet extends BaseNuxeoRestlet implements
        LiveEditConstants, Serializable { protected void saveFileToDocument(String filename, DocumentModel dm, String blobPropertyName, String filenamePropertyName, InputStream is) throws IOException, PropertyException, ClientException { Blob blob = StreamingBlob.createFromStream(is).persist(); blob.setFilename(filename); dm.setPropertyValue(blobPropertyName, (Serializable) blob); dm.setPropertyValue(filenamePropertyName, filename); getDocumentManager().saveDocument(dm); String versioningPolicy = Framework.getProperty(LIVED_AUTOVERSIONING_PROP); if (doAutoMinorIncrement(versioningPolicy, dm)) { if (dm.isCheckedOut()) { dm.checkIn(VersioningOption.MINOR, "Live edit (UploadFileRestlet) autoversioning"); } } getDocumentManager().save(); }  @SuppressWarnings("deprecation") @Override void handle(Request req, Response res); }
UploadFileRestlet extends BaseNuxeoRestlet implements
        LiveEditConstants, Serializable { protected void saveFileToDocument(String filename, DocumentModel dm, String blobPropertyName, String filenamePropertyName, InputStream is) throws IOException, PropertyException, ClientException { Blob blob = StreamingBlob.createFromStream(is).persist(); blob.setFilename(filename); dm.setPropertyValue(blobPropertyName, (Serializable) blob); dm.setPropertyValue(filenamePropertyName, filename); getDocumentManager().saveDocument(dm); String versioningPolicy = Framework.getProperty(LIVED_AUTOVERSIONING_PROP); if (doAutoMinorIncrement(versioningPolicy, dm)) { if (dm.isCheckedOut()) { dm.checkIn(VersioningOption.MINOR, "Live edit (UploadFileRestlet) autoversioning"); } } getDocumentManager().save(); }  @SuppressWarnings("deprecation") @Override void handle(Request req, Response res); static final String LIVED_AUTOVERSIONING_PROP; static final String POLICY_MINOR_INCR; }