The following code examples demonstrate thumbnail-related operations. For an overview of thumbnails, see Thumbnail Concepts.
The following Java™ and C# examples illustrate event-based thumbnail generation. The code creates a CmThumbnailGenerationSubscription instance. The subscription is run for new documents, checked-in versions of the Document class or subclass, with pdf or Microsoft Word mime types.
Java Example
// Create instance of CmThumbnailGenerationSubscription.
CmThumbnailGenerationSubscription sub = Factory.CmThumbnailGenerationSubscription.createInstance(os, "CmThumbnailGenerationSubscription");
sub.set_DisplayName("Event-based thumbnail subscription");
sub.set_SubscriptionTarget(Factory.ClassDefinition.getInstance(os, GuidConstants.Class_Document));
sub.set_IncludeSubclassesRequested(Boolean.TRUE);
sub.set_IsEnabled(Boolean.TRUE);
sub.set_IsSynchronous(Boolean.TRUE);
// CheckinEvent should be used.
SubscribedEventList sel = Factory.SubscribedEvent.createList();
SubscribedEvent se = Factory.SubscribedEvent.createInstance(os);
se.set_EventClass(Factory.EventClassDefinition.getInstance(os, GuidConstants.Class_CheckinEvent));
sel.add(se);
sub.set_SubscribedEvents(sel);
// Filter on documents with specified mime types.
sub.set_FilterExpression("MimeType IN ('application/pdf','application/msword')");
// The EventAction instance created by the Thumbnail Extension Add-on should be used.
EventActionSet eaSet = os.get_EventActions();
EventAction ea;
Iterator iter = eaSet.iterator();
// Get add-on created event action.
while (iter.hasNext())
{
ea = (EventAction) iter.next();
if (ea.get_ProgId().equalsIgnoreCase("com.filenet.engine.sweep.handler.ThumbnailGenerationHandler"))
{
sub.set_EventAction(ea);
sub.save(RefreshMode.NO_REFRESH);
}
}
C# Example
// Create instance of ICmThumbnailGenerationSubscription.
ICmThumbnailGenerationSubscription sub = Factory.CmThumbnailGenerationSubscription.CreateInstance(os, "CmThumbnailGenerationSubscription");
sub.DisplayName = "Event-based thumbnail subscription";
sub.SubscriptionTarget = Factory.ClassDefinition.GetInstance(os, GuidConstants.Class_Document);
sub.IncludeSubclassesRequested = true;
sub.IsEnabled = true;
sub.IsSynchronous = true;
// CheckinEvent should be used.
ISubscribedEventList sel = Factory.SubscribedEvent.CreateList();
ISubscribedEvent se = Factory.SubscribedEvent.CreateInstance(os);
se.EventClass = Factory.EventClassDefinition.GetInstance(os, GuidConstants.Class_CheckinEvent);
sel.Add(se);
sub.SubscribedEvents = sel;
// Filter on documents with specified mime types.
sub.FilterExpression = "MimeType IN ('application/pdf','application/msword')";
// The EventAction instance created by the Thumbnail Extension Add-on should be used.
IEventActionSet eaSet = os.EventActions;
foreach (IEventAction ea in eaSet)
{
if (ea.ProgId.Equals("com.filenet.engine.sweep.handler.ThumbnailGenerationHandler"))
{
sub.EventAction = ea;
sub.Save(RefreshMode.NO_REFRESH);
}
}
The following Java and C# examples illustrate sweep-based thumbnail generation, used to generate (or regenerate) thumbnails for some, or all, existing documents. The code creates a CmThumbnailGenerationJob instance, and configures the job to run every day during the hours between midnight and 6 AM, until it completes.
This example shows how sweep- and event-based thumbnail generation can be configured to complement each other. In the previous example for creating a subscription, thumbnails are generated for new documents with pdf or Microsoft Word mime types, when the new versions are checked in. In this example for creating a generation job, thumbnails will be generated for existing documents with pdf or Microsoft Word mime types, and which were persisted before April 1, 2012. The generation job will target only documents that existed before when the subscription was enabled (assuming the April 1 date).
Java Example
// Create instance of CmThumbnailGenerationJob.
CmThumbnailGenerationJob genJob = Factory.CmThumbnailGenerationJob.createInstance(os, "CmThumbnailGenerationJob");
genJob.set_DisplayName("Sweep-based thumbnail generation job");
genJob.set_SweepTarget(Factory.DocumentClassDefinition.getInstance(os, GuidConstants.Class_Document) );
// Filter on all released versions prior to April 1, with specified mime types.
genJob.set_FilterExpression("DateLastModified < 20120401T000000Z AND VersionStatus = 1 AND MimeType IN ('application/pdf','application/msword')");
// Create timeslots so that the job runs every day for 6 hours, starting at midnight.
CmTimeslotList tsList = Factory.CmTimeslot.createList();
for (int day = 0; day < 7; day++)
{
CmTimeslot ts = Factory.CmTimeslot.createInstance();
ts.set_Weekday(Weekday.getInstanceFromInt(day));
ts.set_Duration(Integer.valueOf(360));
ts.set_StartMinutesPastMidnight(Integer.valueOf(0));
tsList.add(ts);
}
// Set timeslots.
genJob.set_SweepTimeslots(tsList);
genJob.save(RefreshMode.NO_REFRESH);
C# Example
// Create instance of ICmThumbnailGenerationJob.
ICmThumbnailGenerationJob genJob = Factory.CmThumbnailGenerationJob.CreateInstance(os, "CmThumbnailGenerationJob");
genJob.DisplayName = "Thumbnail generation job";
genJob.SweepTarget = Factory.DocumentClassDefinition.GetInstance(os, GuidConstants.Class_Document);
// Filter on all released versions prior to April 1, with specified mime types.
genJob.FilterExpression = "DateLastModified < 20120401T000000Z AND VersionStatus = 1 AND MimeType IN ('application/pdf','application/msword')";
// Create timeslots so that the job runs every day for 6 hours, starting at midnight.
ICmTimeslotList tsList = Factory.CmTimeslot.CreateList();
for (int day = 0; day < 7; day++)
{
ICmTimeslot ts = Factory.CmTimeslot.CreateInstance();
ts.Weekday = (Weekday) day;
ts.Duration = 360;
ts.StartMinutesPastMidnight = 0;
tsList.Add(ts);
}
// Set timeslots.
genJob.SweepTimeslots = tsList;
genJob.Save(RefreshMode.NO_REFRESH);
The following Java and C# examples illustrate ad hoc thumbnail generation, where the client application creates the CmThumbnailRequest objects. The code creates a thumbnail request for each content element set on the document version.
Java Example
static PropertyFilter pf = new PropertyFilter();
...
// Get document for which thumbnails will be generated.
pf.addIncludeProperty(new FilterElement(null, null, null, "ContentElements ElementSequenceNumber", null));
Document doc = Factory.Document.fetchInstance(os, new Id("{B7E5D6C1-F7FD-48F2-AD9C-BAF652F0A00F}"), pf);
// Get document's content elements.
ContentElementList cel = doc.get_ContentElements();
Iterator iter = cel.iterator();
ContentElement ce;
CmThumbnailRequest request;
// Iterate content elements, and create a thumbnail request for each content element.
while (iter.hasNext())
{
ce = (ContentElement)iter.next();
request = Factory.CmThumbnailRequest.createInstance(os, ClassNames.CM_THUMBNAIL_REQUEST);
request.set_InputDocument(doc);
request.set_ElementSequenceNumber(ce.get_ElementSequenceNumber());
request.save(RefreshMode.REFRESH);
}
C# Example
static PropertyFilter pf = new PropertyFilter();
...
// Get document for which thumbnails will be generated.
pf.AddIncludeProperty (new FilterElement(null, null, null, "ContentElements ElementSequenceNumber", null) );
IDocument doc = Factory.Document.FetchInstance(os, new Id("{2D357CC2-2059-44BD-A846-170C4D0254D4}"), pf);
// Get document's content elements.
IContentElementList cel = doc.ContentElements;
ICmThumbnailRequest request;
// Iterate content elements, and create a thumbnail request for each content element.
foreach (IContentElement ce in cel)
{
request = Factory.CmThumbnailRequest.CreateInstance(os, ClassNames.CM_THUMBNAIL_REQUEST);
request.InputDocument = doc;
request.ElementSequenceNumber = ce.ElementSequenceNumber;
request.Save(RefreshMode.REFRESH);
}
The following Java and C# examples get all of the CmThumbnailRequest objects in the ThumbnailRequest queue table, and print the status of each request. For a failed request, thumbnail generation is tried again.
Java Example
// Build the SQL select statement.
String sqlStr = "Select * from CmThumbnailRequest";
SearchSQL sql = new SearchSQL(sqlStr);
SearchScope ss = new com.filenet.api.query.SearchScope(os);
// Get all requests in the ThumbnailRequest queue table.
IndependentObjectSet requestSet = (IndependentObjectSet)ss.fetchObjects(sql, null, null, Boolean.TRUE);
// Iterate request items and print status.
Iterator iter = requestSet.iterator();
CmThumbnailRequest request;
while (iter.hasNext())
{
request = (CmThumbnailRequest)iter.next();
System.out.println("Creator: " + request.get_Creator() +
"\nDate Created: " + request.get_DateCreated().toString() +
"\nFailure count: " + request.get_FailureCount() +
"\nQueue entry status: " + request.get_QueueEntryStatus() );
// For failed requests, reset FailureCount property to retry thumbnail generation.
if (request.get_QueueEntryStatus().equals(QueueEntryStatus.FAILED) )
{
System.out.println("Reason for failure: " + request.get_LastFailureReason());
request.set_FailureCount(0);
request.save(RefreshMode.REFRESH);
}
System.out.println("==================================");
}
C# Example
// Build the SQL select statement.
String sqlStr = "Select * from CmThumbnailRequest";
SearchSQL sql = new SearchSQL(sqlStr);
SearchScope ss = new SearchScope(os);
// Get all requests in the ThumbnailRequest queue table.
IIndependentObjectSet requestSet = (IIndependentObjectSet)ss.FetchObjects(sql, null, null, true);
// Iterate request items and print status.
foreach (ICmThumbnailRequest request in requestSet)
{
System.Console.WriteLine("Creator: " + request.Creator +
"\nDate Created: " + request.DateCreated.ToString() +
"\nFailure count: " + request.FailureCount +
"\nQueue entry status: " + request.QueueEntryStatus );
// For failed requests, reset FailureCount property to retry thumbnail generation.
if (request.QueueEntryStatus.Equals(QueueEntryStatus.FAILED) )
{
System.Console.WriteLine("Reason for failure: " + request.LastFailureReason);
request.FailureCount = 0;
request.Save(RefreshMode.REFRESH);
}
System.Console.WriteLine("==================================");
}
The following Java and C# examples show how to change the default format and size parameters that are used by the thumbnail generation service. These parameters are controlled by the CmThumbnailRequestSweep instance that is created by the Thumbnail Extension add-on.
Java Example
static PropertyFilter pf = new PropertyFilter();
...
pf.addIncludeProperty(new FilterElement(null, null, null, "IsEnabled ImageFormat ImageSize", null));
// Get instance created by the Thumbnail Extension Add-on.
CmThumbnailRequestSweep requestSweep = Factory.CmThumbnailRequestSweep.fetchInstance(os,
new Id("{8BBC6AE6-F1F8-4F8D-9086-48CD5F04628B}"), pf );
// Change the default image-related property settings.
requestSweep.set_ImageFormat(ThumbnailImageFormat.PNG);
requestSweep.set_ImageSize(ThumbnailImageSize.LARGE);
// Enable request sweep.
if (!requestSweep.get_IsEnabled()) requestSweep.set_IsEnabled(true);
requestSweep.save(RefreshMode.REFRESH);
C# Example
static PropertyFilter pf = new PropertyFilter();
...
pf.AddIncludeProperty(new FilterElement(null, null, null, "IsEnabled ImageFormat ImageSize", null));
// Get instance created by the Thumbnail Extension Add-on.
ICmThumbnailRequestSweep requestSweep = Factory.CmThumbnailRequestSweep.FetchInstance(os,
new Id("{8BBC6AE6-F1F8-4F8D-9086-48CD5F04628B}"), pf );
// Change the default image-related property settings.
requestSweep.ImageFormat = ThumbnailImageFormat.PNG;
requestSweep.ImageSize = ThumbnailImageSize.LARGE;
// Enable request sweep.
if (!(Boolean) requestSweep.IsEnabled) requestSweep.IsEnabled = true;
requestSweep.Save(RefreshMode.REFRESH);
The following Java and C# examples illustrate the user-generated option for creating a thumbnail, where the thumbnail image is generated by a third-party application and persisted on the Content Engine. In this scenario, the client application creates the CmThumbnail object directly. The thumbnail image is set on the CmThumbnail object, along with the document and element sequence number that is associated with the CmThumbnail object.
Java Example
static PropertyFilter pf = new PropertyFilter();
...
// Get document on which thumbnail will be set.
pf.addIncludeProperty(new FilterElement(1, null, null, "ContentElements ElementSequenceNumber", null));
Document doc = Factory.Document.fetchInstance(os, new Id("{BF90623C-BDC8-4A18-939C-EA556D90B623}"), pf);
// Create thumbnail object that will be associated with the document.
CmThumbnail tnObject = Factory.CmThumbnail.createInstance(os, ClassNames.CM_THUMBNAIL);
// Set content element sequence number and document on thumbnail object.
ContentElementList cel = doc.get_ContentElements();
ContentElement ce = (ContentElement)cel.get(0);
tnObject.set_ElementSequenceNumber(ce.get_ElementSequenceNumber());
tnObject.set_InputDocument(doc);
// Get user-generated thumbnail image rendered by third-party application.
// Set image on thumbnail object.
byte [] thumbImage = getThumbnailImage("C:\\thumbnail\\images\\HomePolicyTemplate.png");
tnObject.set_Image(thumbImage);
tnObject.set_MimeType("image/png");
tnObject.save(RefreshMode.REFRESH);
C# Example
static PropertyFilter pf = new PropertyFilter();
...
// Get document on which thumbnails will be set.
pf.AddIncludeProperty(new FilterElement(1, null, null, "ContentElements ElementSequenceNumber", null));
IDocument doc = Factory.Document.FetchInstance(os, new Id("{BF90623C-BDC8-4A18-939C-EA556D90B623}"), pf);
// Create thumbnail object that will be associated with the document.
ICmThumbnail tnObject = Factory.CmThumbnail.CreateInstance(os, ClassNames.CM_THUMBNAIL);
// Set content element sequence number and document on thumbnail object.
IContentElementList cel = doc.ContentElements;
IContentElement ce = (IContentElement)cel[0];
tnObject.ElementSequenceNumber = ce.ElementSequenceNumber;
tnObject.InputDocument = doc;
// Get user-generated thumbnail image rendered by third-party application.
// Set image on thumbnail object.
byte[] thumbImage = GetThumbnailImage("C:\\thumbnail\\images\\HomePolicyTemplate.png");
tnObject.Image = thumbImage;
tnObject.MimeType = "image/png";
tnObject.Save(RefreshMode.REFRESH);
The following Java and C# examples get all of the CmThumbnail objects associated with a document.
Java Example
static PropertyFilter pf = new PropertyFilter();
...
// Get document to check for thumbnails.
pf.addIncludeProperty(new FilterElement(null, null, null, "CmThumbnails", null));
Document doc = Factory.Document.fetchInstance(os, new Id("{792E0292-B36B-4B5D-8391-B2BFF990B3F1}"), pf);
// Iterate thumbnails and print information about each one.
CmThumbnailSet thumbnails = doc.get_CmThumbnails();
if (!doc.get_CmThumbnails().isEmpty())
{
Iterator iter = thumbnails.iterator();
while (iter.hasNext())
{
CmThumbnail tn = (CmThumbnail) iter.next();
System.out.println("Name of thumbnail: " + tn.get_Name() + "\n" +
"System generated: " + tn.get_IsSystemGenerated() + "\n" +
"Element sequence number: " + tn.get_ElementSequenceNumber());
}
}
else System.out.println("Document has no associated thumbnails");
C# Example
static PropertyFilter pf = new PropertyFilter();
...
// Get document to check for thumbnails.
pf.AddIncludeProperty(new FilterElement(null, null, null, "CmThumbnails", null));
IDocument doc = Factory.Document.FetchInstance(os, new Id("{792E0292-B36B-4B5D-8391-B2BFF990B3F1}"), pf);
// Iterate thumbnails and print information about each one.
ICmThumbnailSet thumbnails = doc.CmThumbnails;
if (!doc.CmThumbnails.IsEmpty())
{
foreach (ICmThumbnail tn in thumbnails)
{
System.Console.WriteLine("Name of thumbnail: " + tn.Name + "\n" +
"System generated: " + tn.IsSystemGenerated + "\n" +
"Element sequence number: " + tn.ElementSequenceNumber);
}
}
else System.Console.WriteLine("Document has no associated thumbnails");