Script - Compact documents with size equals to 0

Find all documents with size=0 and compact history of versions to latest.

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.openkm.api.OKMDocument;
import com.openkm.api.OKMRepository;
import com.openkm.db.bean.NodeDocumentVersion;
import com.openkm.util.ContextWrapper;

SessionFactory sessionFactory = ContextWrapper.getContext().getBean(SessionFactory.class);
OKMRepository okmRepository = ContextWrapper.getContext().getBean(OKMRepository.class);
OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);

String qs = "from NodeDocumentVersion ndv where ndv.size=0";
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Query q = session.createQuery(qs);
List docVersionList = q.list();
tx.commit();

print("Number of nodes: " + docVersionList.size() + "<br/>");

for (Object obj : docVersionList) {
    NodeDocumentVersion ndv = (NodeDocumentVersion) obj;
    okmDocument.purgeVersionHistory(null, ndv.getParent());
}

session.close();
print("done");
Table of contents [ Hide Show ]