With the following custim sweep action you can read all Retrieval Names of the content elemens that are attached to a FileNet object and write these names into a property of the specified class. In our case we had the ICCMail3 class and want to write the names of all attachments of a mail into a single property to be able to search for them.
importPackage(java.lang);
importClass(java.lang.System);
importPackage(Packages.com.filenet.api.sweep);
importPackage(Packages.com.filenet.api.property);
importPackage(Packages.com.filenet.api.security);
importPackage(Packages.com.filenet.api.core);
importPackage(Packages.com.filenet.api.constants);
importPackage(Packages.com.filenet.api.engine);
importPackage(Packages.com.filenet.api.collection);
function onPolicySweep(SweepObject, SweepPolicy, SweepItems){
}
function onSweep(sweepObject, sweepItems){
var MAX_PROPERTY_LENGTH = 4000;
var hcc = HandlerCallContext.getInstance();
hcc.traceDetail("Entering ICCMailAttachmentHandler.onSweep");
hcc.traceDetail("sweepObject = " + sweepObject.getProperties().getIdValue(PropertyNames.ID) + " sweepItems.length = " + sweepItems.length);
hcc.traceDetail("MAX_PROPERTY_LENGTH = " + MAX_PROPERTY_LENGTH);
ii = 0;
for (ii = 0; ii < sweepItems.length; ii++){
if (hcc != null && hcc.isShuttingDown()){
throw new EngineRuntimeException(
ExceptionCode.E_BACKGROUND_TASK_TERMINATED,
this.constructor.name + " is terminating prematurely because the server is shutting down"
);
}
var item = sweepItems[ii].getTarget();
var msg = "sweepItems[" + ii + "]= " + item.getProperties().getIdValue("ID");
hcc.traceDetail(msg);
try{
var doc = com.filenet.api.core.Document(item);
var className = doc.getClassName();
if(hcc) hcc.traceDetail(" Dokumentklasse: " + className);
var attachmentNames = extractAttachmentNames(doc, hcc);
if (attachmentNames != null && attachmentNames !== "") {
var originalLength = attachmentNames.length;
var truncatedValue = truncateToMaxLength(attachmentNames, MAX_PROPERTY_LENGTH, hcc);
var wasTruncated = (originalLength > truncatedValue.length);
doc.getProperties().putValue("ICCAttachmentNames", truncatedValue);
doc.save(com.filenet.api.constants.RefreshMode.REFRESH);
if (wasTruncated) {
if(hcc) hcc.traceDetail(" WARNUNG: Wert wurde truncated von " + originalLength + " auf " + truncatedValue.length + " Zeichen");
sweepItems[ii].setOutcome(SweepItemOutcome.PROCESSED,
"Attachment-Namen gesetzt (TRUNCATED: " + originalLength + " -> " + truncatedValue.length + " chars)");
} else {
if(hcc) hcc.traceDetail(" ICCAttachmentNames gesetzt auf: " + truncatedValue);
sweepItems[ii].setOutcome(SweepItemOutcome.PROCESSED,
"Attachment-Namen gesetzt (" + truncatedValue.length + " chars)");
}
} else {
if(hcc) hcc.traceDetail(" Keine ContentElements gefunden");
sweepItems[ii].setOutcome(SweepItemOutcome.PROCESSED,
"Keine Attachments vorhanden - Property nicht gesetzt");
}
}catch (ioe){
sweepItems[ii].setOutcome(SweepItemOutcome.FAILED,
"ICCMailAttachmentHandler: " + ioe.rhinoException.getMessage());
hcc.traceDetail("FAILED " + ioe.rhinoException.getMessage());
}
}
hcc.traceDetail("Exiting ICCMailAttachmentHandler.onSweep");
}
function getRequiredProperties(){
var pnames = ['Id', 'ContentElements', 'ICCAttachmentNames'];
return pnames.toString();
}
function truncateToMaxLength(value, maxLength, hcc){
if (value == null || value.length <= maxLength) {
return value;
}
var truncateIndicator = "...";
var effectiveMaxLength = maxLength - truncateIndicator.length;
if (effectiveMaxLength < 0) {
effectiveMaxLength = maxLength;
truncateIndicator = "";
}
var truncated = value.substring(0, effectiveMaxLength);
var lastComma = truncated.lastIndexOf(',');
if (lastComma > 0 && lastComma > (effectiveMaxLength * 0.8)) {
truncated = truncated.substring(0, lastComma);
if(hcc) hcc.traceDetail(" Intelligentes Truncate am Komma bei Position: " + lastComma);
} else {
if(hcc) hcc.traceDetail(" Hartes Truncate bei Position: " + effectiveMaxLength);
}
return truncated + truncateIndicator;
}
function extractAttachmentNames(doc, hcc){
try {
var contentElements = doc.get_ContentElements();
if (contentElements == null || contentElements.isEmpty()) {
if(hcc) hcc.traceDetail(" Keine ContentElements vorhanden");
return null;
}
if(hcc) hcc.traceDetail(" Anzahl ContentElements: " + contentElements.size());
var retrievalNames = [];
var iterator = contentElements.iterator();
var elementIndex = 0;
while (iterator.hasNext()) {
var contentElement = iterator.next();
elementIndex++;
if(hcc) hcc.traceDetail(" Verarbeite ContentElement #" + elementIndex);
var retrievalName = getRetrievalNameFromElement(contentElement, hcc);
if (retrievalName != null && retrievalName !== "") {
retrievalNames.push(retrievalName);
if(hcc) hcc.traceDetail(" -> RetrievalName: " + retrievalName);
} else {
if(hcc) hcc.traceDetail(" -> Kein RetrievalName gefunden");
}
}
if (retrievalNames.length > 0) {
var result = retrievalNames.join(", ");
if(hcc) hcc.traceDetail(" Gesamt-Result vor Truncate: " + result.length + " Zeichen, " + retrievalNames.length + " Attachments");
return result;
}
return null;
} catch (e) {
if(hcc) hcc.traceDetail(" Fehler beim Extrahieren der Attachment-Namen: " + e.message);
throw e;
}
}
function getRetrievalNameFromElement(contentElement, hcc){
try {
if (contentElement instanceof com.filenet.api.core.ContentTransfer) {
var contentTransfer = com.filenet.api.core.ContentTransfer(contentElement);
var retrievalName = contentTransfer.get_RetrievalName();
if (retrievalName != null && retrievalName !== "") {
return retrievalName;
}
}
else if (contentElement instanceof com.filenet.api.core.ContentReference) {
var contentReference = com.filenet.api.core.ContentReference(contentElement);
try {
var retrievalName = contentReference.get_RetrievalName();
if (retrievalName != null && retrievalName !== "") {
return retrievalName;
}
} catch(e) {
// RetrievalName nicht verfügbar
}
var contentLocation = contentReference.get_ContentLocation();
if (contentLocation != null && contentLocation !== "") {
var fileName = extractFileNameFromPath(contentLocation);
if (fileName != null) {
return fileName;
}
}
}
return null;
} catch(e) {
if(hcc) hcc.traceDetail(" Fehler beim Extrahieren von ContentElement: " + e.message);
return null;
}
}
function extractFileNameFromPath(path){
if (path == null || path === "") {
return null;
}
var lastSlash = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
if (lastSlash >= 0 && lastSlash < path.length - 1) {
return path.substring(lastSlash + 1);
}
return path;
}