Script - Folders deep

The script finds all folders with some deep.

The variable MAX_DEPTH=5 indicates the folder deep.

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openkm.api.OKMFolder;
import com.openkm.api.OKMRepository;
import com.openkm.core.Config;
import com.openkm.core.DatabaseException;
import com.openkm.db.bean.NodeBase;
import com.openkm.util.ContextWrapper;

Logger log = LoggerFactory.getLogger("com.openkm.folder.deep");
int MAX_DEPTH = 5;
int count = 0;
 
void findNodePathHelper(Session session, NodeBase parentNode, int depth) {
    OKMFolder okmFolder = ContextWrapper.getContext().getBean(OKMFolder.class);
    OKMRepository okmRepository = ContextWrapper.getContext().getBean(OKMRepository.class);

    String qs = "from NodeBase nb where nb.parent=:parent";
    Query q = session.createQuery(qs).setCacheable(true);
    q.setString("parent", parentNode.getUuid());

    for (Object obj : q.list()) {
        NodeBase nb = (NodeBase) obj;
        if (depth == MAX_DEPTH && okmFolder.isValid(null, nb.getUuid())) {
            print("uuid:" + nb.getUuid() + " path:" + okmRepository.getNodePath(null, nb.getUuid()) + "<br/>");
        } else if (depth < MAX_DEPTH) {
            findNodePathHelper(session, nb, depth + 1);
        }
    }
}
 
log.info("***** Process BEGIN *****");
String qs = "from NodeBase nb where nb.parent=:parent";
Session session = null;
 
log.info("***** Process BEGIN *****");
String qs = "from NodeBase nb where nb.parent=:parent";
Session session = null;
Transaction tx = null;

try {
    SessionFactory sessionFactory = ContextWrapper.getContext().getBean(SessionFactory.class);

    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    // First level nodes
    Query q = session.createQuery(qs);
    q.setString("parent", Config.ROOT_NODE_UUID);

    for (Object obj : q.list()) {
        NodeBase nb = (NodeBase) obj;
        // Process in depth
        findNodePathHelper(session, nb, 0);
    }
    tx.commit();
} catch (HibernateException e) {
    tx.rollback();
    throw new DatabaseException(e.getMessage(), e);
} finally {
    session.close();
}

log.info("***** Process END *****");

Images

Execution

Results

Table of contents [ Hide Show ]