java - 使用 OpenCMIS 创建和更新文档,同时维护版本历史记录

标签 java alfresco opencmis

有人可以发布创建新文档所需的 OpenCMIS 代码,然后通过更新其内容流来更新该文档吗?我不想丢失原始文档——我想在新文档更新时保留版本历史记录。我正在使用 Alfresco,但这应该适用于任何 CMIS 存储库。

最佳答案

在 Alfresco 中,要创建新版本,只需获取结帐后返回给您的私有(private)工作副本,更新 PWC 的内容流,然后将其重新 checkin 。Alfresco 将为您管理版本。这是一个例子。

Folder folder = (Folder) getSession().getObjectByPath("/cmis-demo");

String timeStamp = new Long(System.currentTimeMillis()).toString();
String filename = "cmis-demo-doc (" + timeStamp + ")";

// Create a doc
Map <String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
properties.put(PropertyIds.NAME, filename);
String docText = "This is a sample document";
byte[] content = docText.getBytes();
InputStream stream = new ByteArrayInputStream(content);
ContentStream contentStream = getSession().getObjectFactory().createContentStream(filename, Long.valueOf(content.length), "text/plain", stream);

Document doc = folder.createDocument(
           properties,
           contentStream,
           VersioningState.MAJOR);

System.out.println("Created: " + doc.getId());
System.out.println("Content Length: " + doc.getContentStreamLength());
System.out.println("Version label:" + doc.getVersionLabel());

// Now update it with a new version
if (doc.getAllowableActions().getAllowableActions().contains(org.apache.chemistry.opencmis.commons.enums.Action.CAN_CHECK_OUT)) {
   doc.refresh();
   String testName = doc.getContentStream().getFileName();
   ObjectId idOfCheckedOutDocument = doc.checkOut();
   Document pwc = (Document) session.getObject(idOfCheckedOutDocument);
   docText = "This is a sample document with an UPDATE";
   content = docText.getBytes();
   stream = new ByteArrayInputStream(content);         
   contentStream = getSession().getObjectFactory().createContentStream(filename, Long.valueOf(content.length), "text/plain", stream);          
   ObjectId objectId = pwc.checkIn(false, null, contentStream, "just a minor change");
   doc = (Document) session.getObject(objectId);
   System.out.println("Version label is now:" + doc.getVersionLabel());
}

运行时,它输出:

Created: workspace://SpacesStore/d6f3fca2-bf9c-4a0e-8141-088d07d45359;1.0
Content Length: 25
Version label:1.0
Version label is now:1.1
Done

关于java - 使用 OpenCMIS 创建和更新文档,同时维护版本历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15066672/

相关文章:

java - Apache Chemistry OpenCMIS 服务器无法部署在 JBoss AS 7 上

java - 找不到具有 ID 的元素,即使它存在

java - 从 eclipse 导出后重命名 war 文件以命中 Web api

java - 如何通过自定义 Web 应用程序连接到 Alfresco 文档

java - 尝试检索特定路径下的文件夹/文件的 CMIS 查询不返回任何文档

java - 如何使用Alfresco OperationContext过滤掉文档?

java - 在Java中,当类B中发生事件而类B不知道类A的存在时,让类A执行某些操作的最佳方法是什么?

java - 如何确定数据库中保存的图像是否为 .gif、.jpg 或 .png 以在 servlet 上显示

javascript - 网页脚本不会因错误而重定向

javascript - 无法使用 Alfresco 的 Javascript API 将节点移动到其自己的树