Search in sources :

Example 1 with MCRFSNODES

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

the class MCRIFS2Commands method repairUnicodeInDatabase.

@MCRCommand(syntax = "repair unicode in database {0}", help = "this fixes consequences of MCR-1423 in Database. If " + "{0} is false then nothing will be done (dry run).")
public static void repairUnicodeInDatabase(String execute) {
    boolean dry = execute.toLowerCase(Locale.ROOT).equals(Boolean.FALSE.toString().toLowerCase(Locale.ROOT));
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRFSNODES> getQuery = cb.createQuery(MCRFSNODES.class);
    List<MCRFSNODES> resultList = em.createQuery(getQuery.select(getQuery.from(MCRFSNODES.class))).getResultList();
    resultList.stream().forEach(node -> {
        String unnormalName = node.getName();
        String normalName = MCRXMLFunctions.normalizeUnicode(unnormalName);
        if (!unnormalName.equals(normalName)) {
            LOGGER.info("{} node {} with name {}", (dry) ? "Would Fix" : "Fixing", node.getId(), unnormalName);
            if (!dry) {
                node.setName(normalName);
            }
        }
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 2 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES 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 3 with MCRFSNODES

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

the class MCRIFSCommands method checkMCRFSNODESForDerivatesWithProjectID.

@MCRCommand(syntax = "check derivates of mcrfsnodes with project id {0}", help = "check the entries of MCRFSNODES for all derivates with project ID {0}")
public static void checkMCRFSNODESForDerivatesWithProjectID(String project_id) {
    LOGGER.info("Start check of MCRFSNODES for derivates with project ID {}", project_id);
    if (project_id == null || project_id.length() == 0) {
        LOGGER.error("Project ID missed for check MCRFSNODES entries of derivates with project ID {0}");
        return;
    }
    Map<String, MCRContentStore> availableStores = MCRContentStoreFactory.getAvailableStores();
    Session session = MCRHIBConnection.instance().getSession();
    MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
    List<String> id_list = mgr.listIDsForBase(project_id + "_derivate");
    int counter = 0;
    int maxresults = id_list.size();
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
    Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
    ParameterExpression<String> ownerID = cb.parameter(String.class);
    TypedQuery<MCRFSNODES> typedQuery = em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), ownerID), cb.equal(nodes.get(MCRFSNODES_.type), "F")).orderBy(cb.asc(nodes.get(MCRFSNODES_.storageid))));
    for (String derid : id_list) {
        counter++;
        LOGGER.info("Processing dataset {} from {} with ID: {}", counter, maxresults, derid);
        // check mcrfsnodes entries
        try {
            AtomicInteger nodeCount = new AtomicInteger();
            typedQuery.setParameter(ownerID, derid);
            typedQuery.getResultList().stream().forEach(fsNode -> {
                nodeCount.incrementAndGet();
                String store_name = fsNode.getStoreid();
                String storageid = fsNode.getStorageid();
                String name = fsNode.getName();
                long size = fsNode.getSize();
                Date date = fsNode.getDate();
                GregorianCalendar datecal = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
                datecal.setTime(date);
                String fctid = fsNode.getFctid();
                String md5 = fsNode.getMd5();
                session.evict(fsNode);
                LOGGER.debug("File for [owner] {} [name] {} [storeid] {} [storageid] {} [fctid] {} [size] {} [md5] {}", derid, name, store_name, storageid, fctid, size, md5);
                // get path of file
                MCRContentStore fs_store = availableStores.get(store_name);
                if (fs_store == null) {
                    LOGGER.error("Can't find content store {}", store_name);
                    return;
                }
                try {
                    File content_file = fs_store.getLocalFile(storageid);
                    if (content_file == null || !content_file.canRead()) {
                        LOGGER.error("   !!!! Can't access to file {} of store {}", storageid, store_name);
                    }
                } catch (Exception e) {
                    LOGGER.error("   !!!! Can't access to file {} of store {}", storageid, store_name);
                }
            });
            if (nodeCount.get() == 0) {
                LOGGER.error("   !!!! Can't find file entries in MCRFSNODES for {}", derid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.clear();
        }
    }
    LOGGER.info("Check done for {} entries", Integer.toString(counter));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) GregorianCalendar(java.util.GregorianCalendar) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) Date(java.util.Date) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SAXException(org.xml.sax.SAXException) MCRException(org.mycore.common.MCRException) NoSuchElementException(java.util.NoSuchElementException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NotDirectoryException(java.nio.file.NotDirectoryException) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) MCRFile(org.mycore.datamodel.ifs.MCRFile) File(java.io.File) Session(org.hibernate.Session) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 4 with MCRFSNODES

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

the class MCRIFSCommands method writeMD5SumFile.

@MCRCommand(syntax = "generate md5sum files in directory {0}", help = "writes md5sum files for every content store in directory {0}")
public static void writeMD5SumFile(String targetDirectory) throws IOException {
    File targetDir = getDirectory(targetDirectory);
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    MCRStreamQuery<MCRFSNODES> streamQuery = MCRStreamQuery.getInstance(em, "from MCRFSNODES where type='F' order by storeid, storageid", MCRFSNODES.class);
    Map<String, MCRContentStore> availableStores = MCRContentStoreFactory.getAvailableStores();
    String currentStoreId = null;
    MCRContentStore currentStore = null;
    File currentStoreBaseDir = null;
    BufferedWriter bw = null;
    String nameOfProject = MCRConfiguration.instance().getString("MCR.NameOfProject", "MyCoRe");
    try {
        Iterator<MCRFSNODES> fsnodes = streamQuery.getResultStream().iterator();
        while (fsnodes.hasNext()) {
            MCRFSNODES fsNode = fsnodes.next();
            String storeID = fsNode.getStoreid();
            String storageID = fsNode.getStorageid();
            String md5 = fsNode.getMd5();
            em.detach(fsNode);
            if (!storeID.equals(currentStoreId)) {
                // initialize current store
                currentStoreId = storeID;
                currentStore = availableStores.get(storeID);
                if (bw != null) {
                    bw.close();
                }
                File outputFile = new File(targetDir, MessageFormat.format("{0}-{1}.md5", nameOfProject, storeID));
                LOGGER.info("Writing to file: {}", outputFile.getAbsolutePath());
                bw = Files.newBufferedWriter(outputFile.toPath(), Charset.defaultCharset(), StandardOpenOption.CREATE);
                try {
                    currentStoreBaseDir = currentStore.getBaseDir();
                } catch (Exception e) {
                    LOGGER.warn("Could not get baseDir of store: {}", storeID, e);
                    currentStoreBaseDir = null;
                }
            }
            String path = currentStoreBaseDir != null ? currentStore.getLocalFile(storageID).getAbsolutePath() : storageID;
            // current store initialized
            String line = MessageFormat.format("{0}  {1}\n", md5, path);
            bw.write(line);
        }
    } finally {
        if (bw != null) {
            try {
                bw.close();
            } catch (IOException e1) {
                LOGGER.warn("Error while closing file.", e1);
            }
        }
        em.clear();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) IOException(java.io.IOException) MCRFile(org.mycore.datamodel.ifs.MCRFile) File(java.io.File) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SAXException(org.xml.sax.SAXException) MCRException(org.mycore.common.MCRException) NoSuchElementException(java.util.NoSuchElementException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NotDirectoryException(java.nio.file.NotDirectoryException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 5 with MCRFSNODES

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

the class MCRIFSCommands method checkDerivatesWithProjectIDInMCRFSNODES.

@MCRCommand(syntax = "check mcrfsnodes of derivates with project id {0}", help = "check the entries of MCRFSNODES with project ID {0} that the derivate exists")
public static void checkDerivatesWithProjectIDInMCRFSNODES(String project_id) {
    LOGGER.info("Start check of MCRFSNODES for derivates with project ID {}", project_id);
    if (project_id == null || project_id.length() == 0) {
        LOGGER.error("Project ID missed for check MCRFSNODES entries of derivates with project ID {0}");
        return;
    }
    MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<String> query = cb.createQuery(String.class);
    Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
    AtomicInteger counter = new AtomicInteger();
    em.createQuery(query.distinct(true).select(nodes.get(MCRFSNODES_.owner)).where(cb.like(nodes.get(MCRFSNODES_.owner), project_id + "\\_%"))).getResultList().stream().peek(ignore -> counter.incrementAndGet()).map(MCRObjectID::getInstance).filter(derID -> {
        try {
            return !mgr.exists(derID);
        } catch (IOException e) {
            LOGGER.error("Error while checking existence of {}", derID, e);
            return true;
        }
    }).forEach(missingDerivate -> LOGGER.error("   !!!! Can't find MCRFSNODES entry {} as existing derivate", missingDerivate));
    LOGGER.info("Check done for {} entries", counter.get());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Transformer(javax.xml.transform.Transformer) Arrays(java.util.Arrays) Date(java.util.Date) StreamResult(javax.xml.transform.stream.StreamResult) FileTime(java.nio.file.attribute.FileTime) MCRStreamQuery(org.mycore.backend.jpa.MCRStreamQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) URI(java.net.URI) Method(java.lang.reflect.Method) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ParameterExpression(javax.persistence.criteria.ParameterExpression) GregorianCalendar(java.util.GregorianCalendar) TimeZone(java.util.TimeZone) StandardOpenOption(java.nio.file.StandardOpenOption) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRUtils(org.mycore.common.MCRUtils) List(java.util.List) Attributes2Impl(org.xml.sax.ext.Attributes2Impl) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) SAXException(org.xml.sax.SAXException) MCRContentStoreFactory(org.mycore.datamodel.ifs.MCRContentStoreFactory) Session(org.hibernate.Session) MCRConfiguration(org.mycore.common.config.MCRConfiguration) TypedQuery(javax.persistence.TypedQuery) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) MCRFSNODES_(org.mycore.backend.hibernate.tables.MCRFSNODES_) Charset(java.nio.charset.Charset) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) Root(javax.persistence.criteria.Root) MCRFile(org.mycore.datamodel.ifs.MCRFile) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NotDirectoryException(java.nio.file.NotDirectoryException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EntityManager(javax.persistence.EntityManager) OutputKeys(javax.xml.transform.OutputKeys) Field(java.lang.reflect.Field) File(java.io.File) NameFileComparator(org.apache.commons.io.comparator.NameFileComparator) TransformerHandler(javax.xml.transform.sax.TransformerHandler) LogManager(org.apache.logging.log4j.LogManager) EntityManager(javax.persistence.EntityManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

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