2013-02-25 1800 views
0

有人可以發佈創建新文檔所需的OpenCMIS代碼,然後通過更新其內容流來更新該文檔嗎?我不想丟失原始文檔 - 我想在新文檔更新時保留版本歷史記錄。我正在使用Alfresco,但這應該適用於任何CMIS存儲庫。使用OpenCMIS創建和更新文檔,同時保持版本歷史記錄

+0

你是什麼意思與 「維護版本」?就像,你想要堅持或控制版本號? – skuro 2013-02-25 12:16:02

+0

當我使用document.setContentStream(..)更新Document時,它應該通過它自己更改版本並更改文檔的內容,並將這兩個文檔保留在版本History中。我想要這樣 – user2106213 2013-02-25 12:19:32

+0

你可以請發佈代碼創建文件?與版本控制? – user2106213 2013-02-25 12:31:04

回答

7

在露天,創建一個新的版本,只是讓被檢出後,返回給你的私人工作副本,更新PWC的內容流,然後檢查回。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 
+0

非常感謝你...我想知道更多的事情,我們可以將標籤更改爲2.0而不是1.1? – user2106213 2013-02-26 07:04:19

+0

是的,你只需要將主標誌從false更改爲true,所以你應該這樣做:ObjectId objectId = pwc.checkIn(true,null,contentStream,「這是一個重大改變」); – 2013-02-27 01:22:58

+0

謝謝你傑夫......你是天才 – user2106213 2013-02-27 05:29:25

相關問題