If you need to add the filename of the content element of an object to a specific custom property of your object in a bulk operation you can use the following custom sweep action
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 hcc = HandlerCallContext.getInstance();
hcc.traceDetail("Entering PopulateFilenameHandler.onSweep");
hcc.traceDetail("sweepObject = " + sweepObject.getProperties().getIdValue(PropertyNames.ID) + " sweepItems.length = " + sweepItems.length);
// Iterate the sweepItems and populate the Dateiname property.
ii = 0;
for (ii = 0; ii < sweepItems.length; ii++){
// At the top of your loop, always check to make sure
// that the server is not shutting down.
// If it is, clean up and return control to the server.
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);
// Prüfe, ob das Property "Dateiname" bereits gefüllt ist
var currentDateiname = doc.getProperties().getStringValue("Dateiname");
if (currentDateiname == null || currentDateiname.trim() === "") {
hcc.traceDetail(" Dateiname ist leer, befülle es aus ContentElements");
var filename = getFilenameFromContent(doc, hcc);
if (filename != null && filename !== "") {
doc.getProperties().putValue("Dateiname", filename);
doc.save(com.filenet.api.constants.RefreshMode.NO_REFRESH);
hcc.traceDetail(" Dateiname gesetzt auf: " + filename);
sweepItems[ii].setOutcome(SweepItemOutcome.PROCESSED, "Dateiname gesetzt: " + filename);
} else {
hcc.traceDetail(" Kein Dateiname in ContentElements gefunden");
sweepItems[ii].setOutcome(SweepItemOutcome.PROCESSED, "Kein Content vorhanden - Dateiname nicht gesetzt");
}
} else {
hcc.traceDetail(" Dateiname bereits vorhanden: " + currentDateiname);
sweepItems[ii].setOutcome(SweepItemOutcome.PROCESSED, "Dateiname bereits vorhanden - übersprungen");
}
}catch (ioe){
// Set failure status on objects that fail to process.
sweepItems[ii].setOutcome(SweepItemOutcome.FAILED, "PopulateFilenameHandler: " + ioe.rhinoException.getMessage());
hcc.traceDetail("FAILED " + ioe.rhinoException.getMessage());
}
}
hcc.traceDetail("Exiting PopulateFilenameHandler.onSweep");
}
/*
* Called automatically when the handler is invoked by a custom sweep job
* or sweep policy. Specify properties required by the handler, if any.
* If you return an empty array, then all properties are fetched.
*/
function getRequiredProperties()
{
var pnames = ['Id', 'Dateiname', 'ContentElements'];
return pnames.toString();
}
/**
* Extrahiert den Dateinamen aus den ContentElements des Dokuments
*/
function getFilenameFromContent(doc, hcc) {
try {
var contentElements = doc.get_ContentElements();
if (contentElements == null || contentElements.isEmpty()) {
if(hcc) hcc.traceDetail(" Keine ContentElements vorhanden");
return null;
}
// Hole das erste ContentElement
var iterator = contentElements.iterator();
if (iterator.hasNext()) {
var contentElement = iterator.next();
// Prüfe den Typ des ContentElements
if (contentElement instanceof com.filenet.api.core.ContentTransfer) {
var contentTransfer = com.filenet.api.core.ContentTransfer(contentElement);
var retrievalName = contentTransfer.get_RetrievalName();
if(hcc) hcc.traceDetail(" RetrievalName gefunden: " + retrievalName);
return retrievalName;
} else if (contentElement instanceof com.filenet.api.core.ContentReference) {
var contentReference = com.filenet.api.core.ContentReference(contentElement);
var contentLocation = contentReference.get_ContentLocation();
if(hcc) hcc.traceDetail(" ContentLocation gefunden: " + contentLocation);
// Extrahiere Dateinamen aus dem Pfad (falls vorhanden)
if (contentLocation != null && contentLocation !== "") {
var lastSlash = Math.max(contentLocation.lastIndexOf('/'), contentLocation.lastIndexOf('\\'));
if (lastSlash >= 0) {
return contentLocation.substring(lastSlash + 1);
}
return contentLocation;
}
}
}
} catch (e) {
if(hcc) hcc.traceDetail(" Fehler beim Extrahieren des Dateinamens: " + e.message);
}
return null;
}