Search in sources :

Example 31 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRIFSCommands method moveFile.

@MCRCommand(syntax = "move ifs node {0} to store {1}", help = "Moves the MCRFile with IFSID {0} to a new MCRContentStore with ID {1}")
public static void moveFile(String ifsId, String storeID) throws IOException {
    MCRContentStore store = MCRContentStoreFactory.getStore(storeID);
    String storageID = moveFile(ifsId, store);
    LOGGER.debug("File id={} has storage ID {} in store {}.", ifsId, storageID, store);
}
Also used : MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 32 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRIFSCommands method writeMissingNodesReport.

@MCRCommand(syntax = "generate missing nodes report in directory {0}", help = "generates XML report over all content stores about missing ifs nodes and write it in directory {0}")
public static void writeMissingNodesReport(String targetDirectory) throws SAXException, TransformerConfigurationException, IOException {
    File targetDir = getDirectory(targetDirectory);
    Map<String, MCRContentStore> availableStores = MCRContentStoreFactory.getAvailableStores();
    final String nsURI = NS_URI;
    final String ATT_TYPE = CDATA;
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    Attributes2Impl atts = new Attributes2Impl();
    final String rootName = "missingnodes";
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    for (MCRContentStore currentStore : availableStores.values()) {
        File baseDir;
        try {
            baseDir = currentStore.getBaseDir();
            if (baseDir == null) {
                LOGGER.warn("Could not get baseDir of store: {}", currentStore.getID());
                continue;
            }
        } catch (Exception e) {
            LOGGER.warn("Could not get baseDir of store: {}", currentStore.getID(), e);
            continue;
        }
        MCRStreamQuery<String> streamQuery = MCRStreamQuery.getInstance(em, "select storeageid from MCRFSNODES where type='F' and storeid=:storeid order by storageid", String.class).setParameter("storeid", currentStore.getID());
        boolean endOfList = false;
        String nameOfProject = MCRConfiguration.instance().getString("MCR.NameOfProject", "MyCoRe");
        String storeID = currentStore.getID();
        File outputFile = new File(targetDir, MessageFormat.format("{0}-{1}-{2}.xml", nameOfProject, storeID, rootName));
        StreamResult streamResult;
        try {
            streamResult = new StreamResult(new FileOutputStream(outputFile));
        } catch (FileNotFoundException e) {
            // should not happen as we checked it before
            LOGGER.error(e);
            return;
        }
        try {
            TransformerHandler th = tf.newTransformerHandler();
            Transformer serializer = th.getTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            th.setResult(streamResult);
            LOGGER.info("Writing to file: {}", outputFile.getAbsolutePath());
            th.startDocument();
            atts.clear();
            atts.addAttribute(nsURI, "project", "project", ATT_TYPE, nameOfProject);
            atts.addAttribute(nsURI, "store", "store", ATT_TYPE, storeID);
            atts.addAttribute(nsURI, "baseDir", "baseDir", ATT_TYPE, baseDir.getAbsolutePath());
            th.startElement(nsURI, rootName, rootName, atts);
            URI baseURI = baseDir.toURI();
            try (Stream<String> resultStream = streamQuery.getResultStream()) {
                Iterator<String> storageIds = resultStream.iterator();
                int rows = -1;
                for (File currentFile : new FileStoreIterator(baseDir)) {
                    if (currentFile.isDirectory()) {
                        String relative = baseURI.relativize(currentFile.toURI()).getPath();
                        LOGGER.info("Checking segment: {}", relative);
                    } else {
                        int checkFile = endOfList ? -1 : checkFile(baseURI, currentFile, storageIds, rows);
                        rows++;
                        endOfList = checkFile == -1;
                        if (endOfList || checkFile == 1) {
                            LOGGER.warn("Found orphaned file: {}", currentFile);
                            atts.clear();
                            atts.addAttribute(NS_URI, ATT_FILE_NAME, ATT_FILE_NAME, CDATA, baseURI.relativize(currentFile.toURI()).getPath());
                            th.startElement(NS_URI, ELEMENT_FILE, ELEMENT_FILE, atts);
                            th.endElement(NS_URI, ELEMENT_FILE, ELEMENT_FILE);
                        }
                    }
                }
            }
            th.endElement(nsURI, rootName, rootName);
            th.endDocument();
        } finally {
            OutputStream stream = streamResult.getOutputStream();
            if (stream != null) {
                stream.close();
            }
        }
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) Attributes2Impl(org.xml.sax.ext.Attributes2Impl) StreamResult(javax.xml.transform.stream.StreamResult) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) FileNotFoundException(java.io.FileNotFoundException) URI(java.net.URI) 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) FileOutputStream(java.io.FileOutputStream) MCRFile(org.mycore.datamodel.ifs.MCRFile) File(java.io.File) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 33 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRIFSCommands method writeFileMD5Report.

@MCRCommand(syntax = "generate md5 file report in directory {0}", help = "generates XML a report over all content stores about failed md5 checks and write it in directory {0}")
public static void writeFileMD5Report(String targetDirectory) throws IOException, SAXException, TransformerConfigurationException {
    File targetDir = getDirectory(targetDirectory);
    FSNodeChecker checker = new MD5Checker();
    writeReport(targetDir, checker);
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 34 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand 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 35 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand 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)

Aggregations

MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)106 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)37 MCRException (org.mycore.common.MCRException)34 IOException (java.io.IOException)30 File (java.io.File)22 ArrayList (java.util.ArrayList)18 Document (org.jdom2.Document)17 JDOMException (org.jdom2.JDOMException)17 MCRObject (org.mycore.datamodel.metadata.MCRObject)17 Path (java.nio.file.Path)16 SAXException (org.xml.sax.SAXException)16 EntityManager (javax.persistence.EntityManager)15 MCRAccessException (org.mycore.access.MCRAccessException)15 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)15 MCRPath (org.mycore.datamodel.niofs.MCRPath)15 FileNotFoundException (java.io.FileNotFoundException)13 SAXParseException (org.xml.sax.SAXParseException)12 List (java.util.List)11 Element (org.jdom2.Element)11 MCRPersistenceException (org.mycore.common.MCRPersistenceException)11