Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top