Script - Recursive character renaming

Renames a character in all folders or documents from some initial path.

// Recursive renaming & to -
import com.openkm.api.OKMDocument;
import com.openkm.api.OKMFolder;
import com.openkm.api.OKMRepository;
import com.openkm.bean.Document;
import com.openkm.bean.Folder;
import com.openkm.util.ContextWrapper;
import com.openkm.util.PathUtils;
 
void findNodePathHelper(String uuid) throws Exception {
    OKMFolder okmFolder = ContextWrapper.getContext().getBean(OKMFolder.class);
    OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
    PathUtils pathUtils = ContextWrapper.getContext().getBean(PathUtils.class);

    for (Folder fld : okmFolder.getChildren(null, uuid)) {
        String fldName = pathUtils.getName(fld.getPath());

        if (fldName.contains("&")) {
            print(fld.getPath() + "<br>");
            String newName = fldName.replaceAll("&", "-");
            okmFolder.rename(null, fld.getUuid(), newName);
        }

        for (Document doc : okmDocument.getChildren(null, fld.getPath())) {
            String docName = pathUtils.getName(doc.getPath());

            if (docName.contains("&")) {
                print(doc.getPath() + "<br>");
                String newName = docName.replaceAll("&", "-");
                okmDocument.rename(null, doc.getUuid(), newName);
            }
        }

        findNodePathHelper(fld.getUuid());
    }
}
 
try {
    OKMRepository okmRepository = ContextWrapper.getContext().getBean(OKMRepository.class);
    String fldPath = "/okm:root";
    String fldUuid = okmRepository.getNodeUuid(null, fldPath);
    findNodePathHelper(fldUuid);
} catch (Exception e) {
    print(e.getMessage());
}
Table of contents [ Hide Show ]