6

One of the requirement is to update the document with new content also delete the old document. document Id and other properties of the previous document should be pointing to the new document with new content.

There any sample snippet to do the same thanks.

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
Shiv Gopal
  • 539
  • 2
  • 10
  • 21

1 Answers1

7

I did not quite get it whether you need to create a new document or a new version of the existing document. Properties can be copied automatically to the newly created version, thus using versioning seems more natural here. To accomplish this:

// check out the document 
Document currentVersion = .. // reference to existing document 
currentVersion.checkout(ReservationType.EXCLUSIVE, null, null, null);
currentVersion.save(RefreshMode.REFRESH);

// obtain the reservation object (new version in progress)
newVersion = (com.filenet.api.core.Document) documentObject.get_Reservation();

// set content
InputStream inputStream = .. // obtain input stream with content
ContentElementList contentElements = Factory.ContentElement.createList();
ContentTransfer contentTransfer = Factory.ContentTransfer.createInstance();
contentTransfer.setCaptureSource(inputStream);
contentTransfer.set_RetrievalName("content name");
contentTransfer.set_ContentType("proper MIME type");
contentElements.add(contentTransfer);
newVersion.set_ContentElements(contentElements);
newVersion.checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MINOR_VERSION);
newVersion.save(RefreshMode.NO_REFRESH);

// deleting obsolete version
currentVersion.delete();
currentVersion.save(RefreshMode.NO_REFRESH);

Properties that are designated for transfer to the reservation (default mode for all non-object properties) will make it into the new version, which effectively is the reservation object once it is persisted.

One thing to note is that the new version cannot have the same ID as the previous one, since each version is a distinct object. To use the same ID you would need to create a new document having this ID and copy properties manually (deleting the old document before persisting the new one).

UPDATE

Regarding atomic updates which must include several objects you have two options:

  1. Update objects in batch
  2. Use client-initiated JTA transaction (if you connect using EJB transport)

You can read about these in the documentation: Batch Concepts, Client-Initiated Transactions.

Using batches is more conventional way that you would normally use unless you have complex update scenario.

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
  • As extra information: Both versions of the same document, have the same VersionSeriesID property and a different ID property for each version. – icrovett Feb 05 '14 at 14:31
  • Thanks for the above code snippet. I would like to retain the old ID to point to point to new document/version. But the above note from you says to delete the old and create a new one..while after deletion and before inserting a new document/version..I want to make sure there is no failure..do I need to transaction bracket this? I do not want to get into a scenario where deletion of old document is successful and insertion of new doc/version failed for some reason. Please advice on how I can achieve this. Thanks. – Shiv Gopal Feb 05 '14 at 15:18
  • Thank you so much. From going by your code..seems like I can just update the content(Making it contentless/or simple message in file) of existing doc itself, it that possible? If that is possible then I dont need to creating a new one. That would solve my issue of making it contentless. I have different doc types(txt, pdf, excel etc) to deal with. I am also ok with having the same version of document if I go by this approach. – Shiv Gopal Feb 06 '14 at 05:02
  • 1
    @ShivGopal It is not possible to replace content of a document (document version) that is persisted in repository. This is by design. Changes to content always require creation of a new document/document version. – ᄂ ᄀ Feb 06 '14 at 06:09