Search in sources :

Example 11 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.

the class MCRHIBFileMetadataStore method retrieveChild.

@Override
public MCRFilesystemNode retrieveChild(String parentID, String name) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    TypedQuery<MCRFSNODES> childQuery = em.createNamedQuery("MCRFSNODES.getChild", MCRFSNODES.class);
    childQuery.setParameter("pid", parentID);
    childQuery.setParameter("name", name);
    try {
        MCRFSNODES node = childQuery.getSingleResult();
        return buildNode(node);
    } catch (NoResultException e) {
        return null;
    }
}
Also used : EntityManager(javax.persistence.EntityManager) NoResultException(javax.persistence.NoResultException) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES)

Example 12 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.

the class MCRHIBFileMetadataStore method retrieveChildren.

@Override
public List<MCRFilesystemNode> retrieveChildren(String parentID) throws MCRPersistenceException {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    TypedQuery<MCRFSNODES> childQuery = em.createNamedQuery("MCRFSNODES.getChildren", MCRFSNODES.class);
    childQuery.setParameter("pid", parentID);
    return childQuery.getResultList().stream().map(this::buildNode).collect(Collectors.toList());
}
Also used : EntityManager(javax.persistence.EntityManager) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES)

Example 13 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.

the class MCRIFS2Commands method getParentID.

private static String getParentID(File node, String derivate_id) throws NoResultException, NonUniqueResultException {
    File parent_node = node.getParentFile();
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
    Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
    MCRFSNODES fsNode = em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), derivate_id), cb.equal(nodes.get(MCRFSNODES_.name), parent_node.getName()), cb.equal(nodes.get(MCRFSNODES_.type), "D"))).getSingleResult();
    LOGGER.debug("Found directory entry for {}", parent_node.getName());
    em.detach(fsNode);
    return fsNode.getId();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) File(java.io.File) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES)

Example 14 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.

the class MCRIFS2Commands method fixDirectoryEntry.

private static void fixDirectoryEntry(File node, String derivate_id, String storage_base, boolean check_only) {
    String name = node.getName();
    LOGGER.debug("fixDirectoryEntry : name = {}", name);
    Session session = MCRHIBConnection.instance().getSession();
    Transaction tx = session.getTransaction();
    if (tx.getStatus().isNotOneOf(TransactionStatus.ACTIVE)) {
        tx.begin();
    }
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
    Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
    try {
        em.detach(em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), derivate_id), cb.equal(nodes.get(MCRFSNODES_.name), name), cb.equal(nodes.get(MCRFSNODES_.type), "D"))).getSingleResult());
        LOGGER.debug("Found directory entry for {}", name);
        return;
    } catch (NoResultException e) {
        LOGGER.error("Can't find directory entry for {}", name);
        if (check_only)
            return;
    } catch (NonUniqueResultException e) {
        LOGGER.error("Non unique directory entry for {}", name);
        return;
    } catch (Exception e) {
        e.printStackTrace();
    }
    // fix entry
    LOGGER.info("Fix entry for directory {}", name);
    MCRFileMetadataManager fmmgr = MCRFileMetadataManager.instance();
    String id = fmmgr.createNodeID();
    String pid = null;
    try {
        pid = getParentID(node, derivate_id);
    } catch (NoResultException e1) {
        if (!derivate_id.equals(name)) {
            LOGGER.error("Can't find parent id for directory {}", name);
            return;
        }
    } catch (NonUniqueResultException e1) {
        LOGGER.error("The directory entry for {} and {} is not unique!", derivate_id, node.getParentFile().getName());
        return;
    }
    try {
        MCRFSNODES mcrfsnodes = new MCRFSNODES();
        mcrfsnodes.setId(id);
        mcrfsnodes.setPid(pid);
        mcrfsnodes.setType("D");
        mcrfsnodes.setOwner(derivate_id);
        mcrfsnodes.setName(node.getName());
        mcrfsnodes.setDate(new Date(node.lastModified()));
        em.persist(mcrfsnodes);
        tx.commit();
        LOGGER.debug("Entry {} fixed.", name);
    } catch (HibernateException he) {
        if (tx != null) {
            tx.rollback();
        }
        he.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) NonUniqueResultException(javax.persistence.NonUniqueResultException) HibernateException(org.hibernate.HibernateException) NoResultException(javax.persistence.NoResultException) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) NoResultException(javax.persistence.NoResultException) MCRException(org.mycore.common.MCRException) NonUniqueResultException(javax.persistence.NonUniqueResultException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) HibernateException(org.hibernate.HibernateException) MCRFileMetadataManager(org.mycore.datamodel.ifs.MCRFileMetadataManager) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) MCRSession(org.mycore.common.MCRSession)

Aggregations

MCRFSNODES (org.mycore.backend.hibernate.tables.MCRFSNODES)14 EntityManager (javax.persistence.EntityManager)12 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 IOException (java.io.IOException)7 MCRException (org.mycore.common.MCRException)7 File (java.io.File)6 Session (org.hibernate.Session)6 MCRFile (org.mycore.datamodel.ifs.MCRFile)6 FileNotFoundException (java.io.FileNotFoundException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 NotDirectoryException (java.nio.file.NotDirectoryException)5 Date (java.util.Date)5 NoSuchElementException (java.util.NoSuchElementException)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 MCRContentStore (org.mycore.datamodel.ifs.MCRContentStore)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 SAXException (org.xml.sax.SAXException)5 GregorianCalendar (java.util.GregorianCalendar)4 BufferedWriter (java.io.BufferedWriter)3 FileInputStream (java.io.FileInputStream)3