Difference between revisions of "Folders deep"

From OpenKM Documentation
Jump to: navigation, search
Line 63: Line 63:
 
log.info("***** Process END *****");
 
log.info("***** Process END *****");
 
</source>
 
</source>
 +
 +
== Example ==

Revision as of 09:51, 6 February 2013

Find folders with some deep. Description:

  • The variable MAX_DEPTH=5 indicates the folder deep.
// com.openkm.dao.NodeBaseDAO.getInstance().fixNodePath();
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.openkm.core.DatabaseException;
import com.openkm.dao.bean.NodeBase;
import com.openkm.dao.HibernateUtil;
import com.openkm.core.Config;
import com.openkm.api.OKMRepository;
import com.openkm.api.OKMFolder;
 
Logger log = LoggerFactory.getLogger("com.openkm.folder.deep");
int MAX_DEPTH = 5;
int count = 0;
 
void findNodePathHelper(Session session, NodeBase parentNode, int depth) throws HibernateException {
    String qs = "from NodeBase nb where nb.parent=:parent";
    Query q = session.createQuery(qs).setCacheable(true);
    q.setString("parent", parentNode.getUuid());
 
    for (NodeBase nb : q.list()) { 	
        if (depth == MAX_DEPTH && OKMFolder.getInstance().isValid(null, OKMRepository.getInstance().getNodePath(null, nb.getUuid()))) {    
            print("uuid:"+ nb.getUuid() + " path:"+OKMRepository.getInstance().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;
 
try {
    session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
 
    // First level nodes
    Query q = session.createQuery(qs);
    q.setString("parent", Config.ROOT_NODE_UUID);
 
    for (NodeBase nb : q.list()) {
        // Process in depth
        findNodePathHelper(session, nb, 0);
    }
 
    HibernateUtil.commit(session.getTransaction());
} catch (HibernateException e) {
    HibernateUtil.rollback(session.getTransaction());
    throw new DatabaseException(e.getMessage(), e);
} finally {
    HibernateUtil.close(session);
}
 
log.info("***** Process END *****");

Example