Search in sources :

Example 1 with MCRFileMetadataManager

use of org.mycore.datamodel.ifs.MCRFileMetadataManager in project mycore by MyCoRe-Org.

the class MCRIFS2Commands method fixFileEntry.

private static void fixFileEntry(File node, String content_store, String derivate_id, String storage_base, boolean check_only) {
    LOGGER.debug("fixFileEntry : name = {}", node.getName());
    String storageid = node.getAbsolutePath().substring(storage_base.length()).replace("\\", "/");
    LOGGER.debug("fixFileEntry : storageid = {}", storageid);
    String id = "";
    String md5_old = "";
    long size_old = 0;
    boolean foundEntry = false;
    MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
    boolean transactionActive = mcrSession.isTransactionActive();
    if (!transactionActive) {
        mcrSession.beginTransaction();
    }
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
        Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
        try {
            MCRFSNODES fsNode = em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), derivate_id), cb.equal(nodes.get(MCRFSNODES_.storeid), content_store), cb.equal(nodes.get(MCRFSNODES_.storageid), storageid), cb.equal(nodes.get(MCRFSNODES_.type), "F"))).getSingleResult();
            LOGGER.debug("Found file entry for {}", storageid);
            foundEntry = true;
            id = fsNode.getId();
            md5_old = fsNode.getMd5();
            size_old = fsNode.getSize();
            em.detach(fsNode);
        } catch (NoResultException e) {
            LOGGER.error("Can't find file entry for {}", storageid);
            if (check_only)
                return;
        } catch (NonUniqueResultException e) {
            LOGGER.error("Non unique file entry for {}", storageid);
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // check fctid, size and MD5 of the file
    String fctid = "";
    String md5 = "";
    try (MCRContentInputStream cis = new MCRContentInputStream(new FileInputStream(node))) {
        byte[] header = cis.getHeader();
        fctid = MCRFileContentTypeFactory.detectType(node.getName(), header).getID();
        ByteStreams.copy(cis, ByteStreams.nullOutputStream());
        md5 = cis.getMD5String();
    } catch (MCRException | IOException e1) {
        e1.printStackTrace();
        return;
    }
    long size = node.length();
    LOGGER.debug("size old : {} <--> size : {}", Long.toString(size_old), Long.toString(size));
    LOGGER.debug("MD5 old : {} <--> MD5 : {}", md5_old, md5);
    if (size_old == size && md5_old.equals(md5)) {
        return;
    }
    if (foundEntry && size_old != size) {
        LOGGER.warn("Wrong file size for {} : {} <-> {}", storageid, size_old, size);
    }
    if (foundEntry && !md5.equals(md5_old)) {
        LOGGER.warn("Wrong file md5 for {} : {} <-> {}", storageid, md5_old, md5);
    }
    if (check_only)
        return;
    // fix entry
    LOGGER.info("Fix entry for file {}", storageid);
    if (!foundEntry) {
        MCRFileMetadataManager fmmgr = MCRFileMetadataManager.instance();
        id = fmmgr.createNodeID();
    }
    String pid = null;
    try {
        pid = getParentID(node, derivate_id);
    } catch (NoResultException e1) {
        LOGGER.error("Can't find parent id of directory for file {}", storageid);
    } 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("F");
        mcrfsnodes.setOwner(derivate_id);
        mcrfsnodes.setName(node.getName());
        mcrfsnodes.setSize(size);
        mcrfsnodes.setDate(new Date(node.lastModified()));
        mcrfsnodes.setStoreid(content_store);
        mcrfsnodes.setStorageid(storageid);
        mcrfsnodes.setFctid(fctid);
        mcrfsnodes.setMd5(md5);
        em.merge(mcrfsnodes);
        mcrSession.commitTransaction();
        LOGGER.debug("Entry {} fixed.", node.getName());
    } catch (PersistenceException pe) {
        mcrSession.rollbackTransaction();
        pe.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) NonUniqueResultException(javax.persistence.NonUniqueResultException) MCRException(org.mycore.common.MCRException) NoResultException(javax.persistence.NoResultException) IOException(java.io.IOException) 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) FileInputStream(java.io.FileInputStream) MCRFileMetadataManager(org.mycore.datamodel.ifs.MCRFileMetadataManager) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) MCRSession(org.mycore.common.MCRSession) MCRContentInputStream(org.mycore.datamodel.ifs.MCRContentInputStream) PersistenceException(javax.persistence.PersistenceException)

Example 2 with MCRFileMetadataManager

use of org.mycore.datamodel.ifs.MCRFileMetadataManager 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

IOException (java.io.IOException)2 Date (java.util.Date)2 EntityManager (javax.persistence.EntityManager)2 NoResultException (javax.persistence.NoResultException)2 NonUniqueResultException (javax.persistence.NonUniqueResultException)2 PersistenceException (javax.persistence.PersistenceException)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 HibernateException (org.hibernate.HibernateException)2 MCRFSNODES (org.mycore.backend.hibernate.tables.MCRFSNODES)2 MCRException (org.mycore.common.MCRException)2 MCRSession (org.mycore.common.MCRSession)2 MCRFileMetadataManager (org.mycore.datamodel.ifs.MCRFileMetadataManager)2 FileInputStream (java.io.FileInputStream)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 MCRContentInputStream (org.mycore.datamodel.ifs.MCRContentInputStream)1